Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull public ssh key from file. #249

Merged
merged 2 commits into from
Jun 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,8 @@ Requirements
- azure-mgmt-compute
- azure-mgmt-network
- azure-mgmt-resource
- certifi
- Click
- cryptography
- paramiko
- pycryptodome
- pytest
- PyYaml
- testinfra
Expand Down
3 changes: 0 additions & 3 deletions docs/source/start.rst
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,8 @@ Requirements
- azure-mgmt-compute
- azure-mgmt-network
- azure-mgmt-resource
- certifi
- Click
- cryptography
- paramiko
- pycryptodome
- pytest
- PyYaml
- testinfra
Expand Down
2 changes: 1 addition & 1 deletion img_proof/ipa_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ def _get_ssh_public_key(self):
"""
Generate SSH public key from private key.
"""
key = ipa_utils.generate_public_ssh_key(self.ssh_private_key_file)
key = ipa_utils.get_public_ssh_key(self.ssh_private_key_file)
return key.decode()

def _is_instance_running(self):
Expand Down
2 changes: 1 addition & 1 deletion img_proof/ipa_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ def _get_user_data(self):
to the instance. The public key is generated from the
private key file.
"""
key = ipa_utils.generate_public_ssh_key(
key = ipa_utils.get_public_ssh_key(
self.ssh_private_key_file
).decode()

Expand Down
2 changes: 1 addition & 1 deletion img_proof/ipa_gce.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def _get_instance(self):

def _get_ssh_public_key(self):
"""Generate SSH public key from private key."""
key = ipa_utils.generate_public_ssh_key(self.ssh_private_key_file)
key = ipa_utils.get_public_ssh_key(self.ssh_private_key_file)
return '{user}:{key} {user}'.format(
user=self.ssh_user,
key=key.decode()
Expand Down
2 changes: 1 addition & 1 deletion img_proof/ipa_oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def _is_instance_running(self):

def _get_ssh_public_key(self):
"""Generate SSH public key from private key."""
key = ipa_utils.generate_public_ssh_key(self.ssh_private_key_file)
key = ipa_utils.get_public_ssh_key(self.ssh_private_key_file)
return key.decode()

def get_console_log(self):
Expand Down
32 changes: 9 additions & 23 deletions img_proof/ipa_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@
from img_proof.ipa_constants import SYNC_POINTS
from img_proof.ipa_exceptions import IpaSSHException, IpaUtilsException

from cryptography.hazmat.backends import default_backend
from cryptography.hazmat.primitives import serialization

CLIENT_CACHE = {}


Expand Down Expand Up @@ -193,32 +190,21 @@ def generate_instance_name(name):
return '%s-%s' % (name, get_random_string(length=5))


def generate_public_ssh_key(ssh_private_key_file):
"""Generate SSH public key from private key file."""
def get_public_ssh_key(ssh_private_key_file):
"""Get SSH public key from private key file."""
pub_key = ssh_private_key_file + '.pub'

try:
with open(ssh_private_key_file, "rb") as key_file:
with open(pub_key, "rb") as key_file:
key = key_file.read()
except FileNotFoundError:
raise IpaUtilsException(
'SSH private key file: %s cannot be found.' % ssh_private_key_file
)

try:
private_key = serialization.load_pem_private_key(
key,
password=None,
backend=default_backend()
)
except ValueError:
raise IpaUtilsException(
'SSH private key file: %s is not a valid key file.'
% ssh_private_key_file
'SSH public key file: {key_path} cannot be found.'.format(
key_path=pub_key
)
)

return private_key.public_key().public_bytes(
serialization.Encoding.OpenSSH,
serialization.PublicFormat.OpenSSH
)
return key


def get_config_values(config_path, section, default='default'):
Expand Down
6 changes: 0 additions & 6 deletions package/python3-img-proof.spec
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,11 @@ BuildRequires: python3-azure-mgmt-compute
BuildRequires: python3-azure-mgmt-network
BuildRequires: python3-azure-mgmt-resource
BuildRequires: python3-boto3
BuildRequires: python3-certifi
BuildRequires: python3-click
BuildRequires: python3-cryptography
BuildRequires: python3-google-api-python-client
BuildRequires: python3-google-auth
BuildRequires: python3-oci-sdk
BuildRequires: python3-paramiko
BuildRequires: python3-pycryptodome
BuildRequires: python3-pytest
BuildRequires: python3-testinfra
%if %{with test}
Expand All @@ -54,14 +51,11 @@ Requires: python3-azure-mgmt-compute
Requires: python3-azure-mgmt-network
Requires: python3-azure-mgmt-resource
Requires: python3-boto3
Requires: python3-certifi
Requires: python3-click
Requires: python3-cryptography
Requires: python3-google-api-python-client
Requires: python3-google-auth
Requires: python3-oci-sdk
Requires: python3-paramiko
Requires: python3-pycryptodome
Requires: python3-pytest
Requires: python3-testinfra
BuildArch: noarch
Expand Down
3 changes: 0 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@ azure-common
azure-mgmt-compute
azure-mgmt-network
azure-mgmt-resource
certifi
Click
cryptography
paramiko
pycryptodome
pytest
PyYAML
testinfra
Expand Down
1 change: 1 addition & 0 deletions tests/data/ida_test.pub
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDjo9ZkUp/dKKVButD+yaDkcWt4goSysXqibMoMedCp1GiKBXA6aeCY8eaenGTxo0fhIVyDX6QyNpE6OP5TYb7emrf7ze7OW2TvtxNZJOoGeT6e7rE/GzvM7UKxUVOl/h1TQ6oASD8nFQgVqjxpaALfouk0lPyBDS0WgvIGUYffYYHQaxzwebhs4ZpcBU3VYJTRFdecw0QzAakSOsUZyMWZUS7GzGR9NfoBf7H3AYp5rcbVOncFnFDZGAZxMJ65I18yU7RsLzaSzBgpUGmhEGaUHhRwGsLT+y+0oO2ftiIGgnYWa1cMDsrWypTm1gSI8+ivHZkmHI9PUDlDzrAQ4FmP
6 changes: 3 additions & 3 deletions tests/test_ipa_azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ def test_get_management_client_exception(self, mock_get_client):
assert str(error.value) == 'Unable to create resource management ' \
'client: Not valid.'

@patch('img_proof.ipa_azure.ipa_utils.generate_public_ssh_key')
def test_get_ssh_public_key(self, mock_generate_pub_key):
mock_generate_pub_key.return_value = b'pub-key'
@patch('img_proof.ipa_azure.ipa_utils.get_public_ssh_key')
def test_get_ssh_public_key(self, mock_get_pub_key):
mock_get_pub_key.return_value = b'pub-key'
provider = self.helper_get_provider()
key = provider._get_ssh_public_key()

Expand Down
6 changes: 3 additions & 3 deletions tests/test_ipa_cloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ def test_cloud_get_ssh_client(self, mock_get_ssh_client):

ipa_utils.clear_cache()

@patch('img_proof.ipa_cloud.ipa_utils.generate_public_ssh_key')
def test_cloud_get_user_data(self, mock_generate_ssh_key):
mock_generate_ssh_key.return_value = b'testkey12345'
@patch('img_proof.ipa_cloud.ipa_utils.get_public_ssh_key')
def test_cloud_get_user_data(self, mock_get_ssh_key):
mock_get_ssh_key.return_value = b'testkey12345'

provider = IpaCloud(*args, **self.kwargs)

Expand Down
6 changes: 3 additions & 3 deletions tests/test_ipa_oci.py
Original file line number Diff line number Diff line change
Expand Up @@ -437,12 +437,12 @@ def test_oci_get_vnic_attachments(self, mock_init, mock_oci):

cloud._get_vnic_attachments(compartment_id, instance_id)

@patch('img_proof.ipa_utils.generate_public_ssh_key')
@patch('img_proof.ipa_utils.get_public_ssh_key')
@patch.object(OCICloud, '__init__')
def test_get_ssh_public_key(self, mock_init, mock_generate_public_ssh_key):
def test_get_ssh_public_key(self, mock_init, mock_get_public_ssh_key):
"""Test oci get ssh public key method."""
mock_init.return_value = None
mock_generate_public_ssh_key.return_value = b'key123'
mock_get_public_ssh_key.return_value = b'key123'

cloud = OCICloud(**self.kwargs)
cloud.ssh_private_key_file = 'tests/oci/api_key.pem'
Expand Down