-
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.
Merge branch 'feature/add-ssh-key-command' into develop
- Loading branch information
Showing
13 changed files
with
192 additions
and
27 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
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
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
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
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,72 @@ | ||
# -*- coding: utf-8 -*- | ||
"""Module with ssh key commands.""" | ||
import os.path | ||
from ..core.exceptions import ArgumentRequiredException | ||
from ..core.commands import DetailCommand, ListCommand | ||
from ..core.models.terminal import SshKey | ||
|
||
|
||
# pylint: disable=too-few-public-methods | ||
class SshKeyGeneratorMixin(object): | ||
"""Mixin for create new ssh key from file.""" | ||
|
||
# pylint: disable=no-self-use | ||
def generate_ssh_key_instance(self, path): | ||
"""Generate ssh key from file.""" | ||
with open(path, 'r') as _file: | ||
content = _file.read() | ||
label = os.path.basename(path) | ||
return SshKey(private_key=content, label=label) | ||
|
||
|
||
class SshKeyCommand(SshKeyGeneratorMixin, DetailCommand): | ||
"""Operate with Host object.""" | ||
|
||
allowed_operations = DetailCommand.all_operations | ||
model_class = SshKey | ||
|
||
def get_parser(self, prog_name): | ||
"""Create command line argument parser. | ||
Use it to add extra options to argument parser. | ||
""" | ||
parser = super(SshKeyCommand, self).get_parser(prog_name) | ||
parser.add_argument( | ||
'-i', '--identity-file', | ||
metavar='PRIVATE_KEY', help='Private key.' | ||
) | ||
return parser | ||
|
||
def create(self, parsed_args): | ||
"""Handle create new instance command.""" | ||
if not parsed_args.identity_file: | ||
raise ArgumentRequiredException('Identity file is required.') | ||
|
||
self.create_instance(parsed_args) | ||
|
||
# pylint: disable=no-self-use | ||
def serialize_args(self, args, instance=None): | ||
"""Convert args to instance.""" | ||
if instance: | ||
ssh_key = instance | ||
if args.identity_file: | ||
with open(args.identity_file, 'r') as _file: | ||
ssh_key.private_key = _file.read() | ||
else: | ||
ssh_key = self.generate_ssh_key_instance(args.identity_file) | ||
|
||
if args.label: | ||
ssh_key.label = args.label | ||
return ssh_key | ||
|
||
|
||
class SshKeysCommand(ListCommand): | ||
"""Manage ssh key objects.""" | ||
|
||
model_class = SshKey | ||
|
||
# pylint: disable=unused-argument | ||
def take_action(self, parsed_args): | ||
"""Process CLI call.""" | ||
instances = self.storage.get_all(self.model_class) | ||
return self.prepare_result(instances) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#!/usr/bin/env bats | ||
|
||
setup() { | ||
rm ~/.serverauditor.storage || true | ||
touch key | ||
} | ||
|
||
teardown() { | ||
rm key | ||
} | ||
|
||
@test "key help by arg" { | ||
run serverauditor key --help | ||
[ "$status" -eq 0 ] | ||
} | ||
|
||
@test "key help command" { | ||
run serverauditor help key | ||
[ "$status" -eq 0 ] | ||
} | ||
|
||
@test "Add general key" { | ||
run serverauditor key -L test -i key | ||
[ "$status" -eq 0 ] | ||
! [ -z $(cat ~/.serverauditor.storage) ] | ||
} | ||
|
||
@test "Add many keys" { | ||
run serverauditor key -L test_1 -i key | ||
run serverauditor key -L test_2 -i key | ||
run serverauditor key -L test_3 -i key | ||
[ "$status" -eq 0 ] | ||
! [ -z $(cat ~/.serverauditor.storage) ] | ||
} | ||
|
||
@test "Update key" { | ||
key=$(serverauditor key -L test -i key) | ||
run serverauditor key -i key $key | ||
[ "$status" -eq 0 ] | ||
! [ -z $(cat ~/.serverauditor.storage) ] | ||
} | ||
|
||
@test "Update many keys" { | ||
key1=$(serverauditor key -L test_1 -i key) | ||
key2=$(serverauditor key -L test_2 -i key) | ||
key3=$(serverauditor key -L test_3 -i key) | ||
run serverauditor key -i key $key1 $key2 $key3 | ||
[ "$status" -eq 0 ] | ||
! [ -z $(cat ~/.serverauditor.storage) ] | ||
} | ||
|
||
@test "Delete key" { | ||
key=$(serverauditor key -L test_1 -i key) | ||
run serverauditor key --delete $key | ||
[ "$status" -eq 0 ] | ||
! [ -z $(cat ~/.serverauditor.storage) ] | ||
} | ||
|
||
@test "Delete many keys" { | ||
key1=$(serverauditor key -L test_1 -i key) | ||
key2=$(serverauditor key -L test_2 -i key) | ||
key3=$(serverauditor key -L test_3 -i key) | ||
run serverauditor key --delete $key1 $key2 $key3 | ||
[ "$status" -eq 0 ] | ||
! [ -z $(cat ~/.serverauditor.storage) ] | ||
} |
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,27 @@ | ||
#!/usr/bin/env bats | ||
|
||
setup() { | ||
rm ~/.serverauditor.storage || true | ||
touch key | ||
} | ||
|
||
teardown() { | ||
rm key | ||
} | ||
|
||
@test "keys help by arg" { | ||
run serverauditor keys --help | ||
[ "$status" -eq 0 ] | ||
} | ||
|
||
@test "keys help command" { | ||
run serverauditor help keys | ||
[ "$status" -eq 0 ] | ||
} | ||
|
||
@test "List snippets in table format" { | ||
serverauditor key -L test -i key | ||
run serverauditor keys | ||
[ "$status" -eq 0 ] | ||
! [ -z $(cat ~/.serverauditor.storage) ] | ||
} |
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