-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sync with Serverauditior cloud module.
Improve serializers and add cryptor usage. Also, add tests for saving models. Fix storing data and remote_instances fields. Add context manager for storage. Fix storage logic. Now terminal entries stored in lists not i dictionaries. Move password prompt to mixin. Add taghosts model.
- Loading branch information
EvgeneOskin
committed
Jul 25, 2015
1 parent
db6bfd6
commit e7e5173
Showing
34 changed files
with
599 additions
and
540 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[run] | ||
branch = True | ||
source = serverauditor_sshconfig | ||
include = *.py | ||
|
||
[report] | ||
precision = 2 | ||
show_missing = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
.DS_Store | ||
|
||
.idea | ||
.tox | ||
.coverage | ||
|
||
*.pyc | ||
*.pyo | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[nosetests] | ||
with-coverage=1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
inherits: | ||
- strictness_veryhigh | ||
- full_pep8 | ||
- doc_warnings | ||
|
||
ignore-paths: | ||
- .git | ||
|
||
pylint: | ||
options: | ||
max-parents: 12 | ||
disable: | ||
# - broad-except | ||
# - pointless-except | ||
# - bad-super-call | ||
# - nonstandard-exception |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
from base64 import b64decode | ||
from ..core.commands import AbstractCommand | ||
from .controllers import ApiController | ||
from .cryptor import RNCryptor | ||
|
||
|
||
class PushCommand(AbstractCommand): | ||
|
||
"""Push data to Serverauditor cloud.""" | ||
|
||
def get_parser(self, prog_name): | ||
parser = super(PushCommand, self).get_parser(prog_name) | ||
parser.add_argument( | ||
'-s', '--silent', action='store_true', | ||
help='Do not produce any interactions.' | ||
) | ||
parser.add_argument( | ||
'-S', '--strategy', metavar='STRATEGY_NAME', | ||
help='Force to use specific strategy to merge data.' | ||
) | ||
return parser | ||
|
||
def take_action(self, parsed_args): | ||
self.log.info('Push data to Serverauditor cloud.') | ||
|
||
|
||
class PullCommand(AbstractCommand): | ||
|
||
"""Pull data from Serverauditor cloud.""" | ||
|
||
def get_parser(self, prog_name): | ||
parser = super(PullCommand, self).get_parser(prog_name) | ||
parser.add_argument( | ||
'-s', '--strategy', metavar='STRATEGY_NAME', | ||
help='Force to use specific strategy to merge data.' | ||
) | ||
return parser | ||
|
||
def take_action(self, parsed_args): | ||
encryption_salt = b64decode(self.config.get('User', 'salt')) | ||
hmac_salt = b64decode(self.config.get('User', 'hmac_salt')) | ||
password = self.prompt_password() | ||
cryptor = RNCryptor() | ||
cryptor.password = password | ||
cryptor.encryption_salt = encryption_salt | ||
cryptor.hmac_salt = hmac_salt | ||
controller = ApiController(self.storage, self.config, cryptor) | ||
with self.storage: | ||
controller.get_bulk() | ||
self.log.info('Pull data from Serverauditor cloud.') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from .serializers import BulkSerializer | ||
from ..core.api import API | ||
|
||
|
||
class CryptoController(object): | ||
|
||
def __init__(self, cryptor): | ||
self.cryptor = cryptor | ||
|
||
def _mutate_fields(self, model, mutator): | ||
for i in model.crypto_fields: | ||
crypto_field = getattr(model, i) | ||
if crypto_field: | ||
setattr(model, i, mutator(crypto_field)) | ||
return model | ||
|
||
def encrypt(self, model): | ||
return self._mutate_fields(model, self.cryptor.encrypt) | ||
|
||
def decrypt(self, model): | ||
return self._mutate_fields(model, self.cryptor.decrypt) | ||
|
||
|
||
class ApiController(object): | ||
|
||
mapping = dict( | ||
bulk=dict(url='v2/terminal/bulk/', serializer=BulkSerializer) | ||
) | ||
|
||
def __init__(self, storage, config, cryptor): | ||
self.config = config | ||
username = self.config.get('User', 'username') | ||
apikey = self.config.get('User', 'apikey') | ||
assert username | ||
assert apikey | ||
self.api = API(username, apikey) | ||
self.storage = storage | ||
self.crypto_controller = CryptoController(cryptor) | ||
|
||
def _get(self, mapped): | ||
serializer = mapped['serializer']( | ||
storage=self.storage, crypto_controller=self.crypto_controller | ||
) | ||
response = self.api.get(mapped['url']) | ||
|
||
model = serializer.to_model(response) | ||
return model | ||
|
||
def get_bulk(self): | ||
mapped = self.mapping['bulk'] | ||
model = self._get(mapped) | ||
self.config.set('CloudSynchronization', 'last_synced', | ||
model['last_synced']) | ||
self.config.write() | ||
|
||
def _post(self, mapped, request_model): | ||
request_model = request_model | ||
serializer = mapped['serializer']( | ||
storage=self.storage, crypto_controller=self.crypto_controller | ||
) | ||
|
||
payload = serializer.to_payload(request_model) | ||
response = self.api.post(mapped['url'], payload) | ||
|
||
response_model = serializer.to_models(response) | ||
return response_model | ||
|
||
def post_bulk(self): | ||
mapped = self.mapping['bulk'] | ||
model = {} | ||
model['last_synced'] = self.config.get( | ||
'CloudSynchronization', 'last_synced' | ||
) | ||
assert model['last_synced'] | ||
out_model = self._post(mapped, model) | ||
self.config.set('CloudSynchronization', 'last_synced', | ||
out_model['last_synced']) | ||
self.config.write() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Oops, something went wrong.