forked from ansible-collections/community.aws
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request ansible-collections#38 from phospi/master
Added explicit encoding for key_data string.
- Loading branch information
Showing
3 changed files
with
89 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
-----BEGIN RSA PRIVATE KEY----- | ||
MIICXQIBAAKBgQDAt4WXUohebyTXAxEBkfCjuaKBgv5VgGwHeSWomB0IoKszlNHL | ||
itadHg/vDi1gHSeRANw4KccpFAEIy4Oq3bMpI/rFrDdj/otp4wDcZKuIxq8OtU4b | ||
KBXsSJD9vxAMZktaJ28gpv+mSjnmz+uC0QiuticKaO62pWPGdd6RjuylkwIDAQAB | ||
AoGAUNSo069qQzGa4hQHLgFoTUOvRWMMChCzPu8xPGWQx+2b4SaqWBUDryLMzBfG | ||
MGoKDmet9mCPiEs7o9S4hRI38m2dKBPHRjpFJDPrJmsKNyjk9yBrcJf6EysNEPbd | ||
mYt7DxyUHVNQJpLOPXuMFSi/iloXTBRZ0dEzvhCp2nmX9wECQQD8+s89dwIm41QK | ||
laqELxSVDtSkfLkBIYtw4xPEfuXufna7LHXnR6b9CELAD8L5ht5CiXHzVPpiuwz4 | ||
AaIvK44tAkEAwwSHaT6AOeXKNnNLTM+UzFW4rKixsSMQVD/7OjU0/IabFOkE+uY/ | ||
WTgLrp1OsqhhDRS/F/eN9uj0dXHXgBEavwJAImW77gCTg1QfpjzJbaW1J7tXgHIQ | ||
+a1k91l445vZib8aR8L42RSuCPOpl9HM0f7bk7J6kvp3/Rqv3bzjH4TNlQJBAId1 | ||
k+FEqqiMtsLPntRBs+ei+13i51pVMrhyoLyzzJRDo2EI4o6sdAAy79pgJhPu5UrC | ||
yGGLcK667WLOqpOoTd0CQQC/4Bq12KCwk9VEWOzNV+kPFzTb85RuzwH5Tis+Fbp2 | ||
CNc26WPeNwOvNxXgzAve4G4CaUNLnmATatr5BKjU8Xkr | ||
-----END RSA PRIVATE KEY----- |
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 __future__ import (absolute_import, division, print_function) | ||
|
||
__metaclass__ = type | ||
|
||
''' | ||
Commands to encrypt a message that can be decrypted: | ||
from cryptography.hazmat.backends import default_backend | ||
from cryptography.hazmat.primitives.serialization import load_pem_private_key | ||
from cryptography.hazmat.primitives.asymmetric.padding import PKCS1v15 | ||
import base64 | ||
path = '/path/to/rsa_public_key.pem' | ||
with open(path, 'r') as f: | ||
rsa_public_key_pem = to_text(f.read()) | ||
load_pem_public_key(rsa_public_key_pem = , default_backend()) | ||
base64_cipher = public_key.encrypt('Ansible_AWS_EC2_Win_Password', PKCS1v15()) | ||
string_cipher = base64.b64encode(base64_cipher) | ||
''' | ||
|
||
from ansible.module_utils._text import to_bytes, to_text | ||
from ansible_collections.community.aws.plugins.modules.ec2_win_password import setup_module_object, ec2_win_password | ||
from ansible_collections.community.aws.tests.unit.compat.mock import patch | ||
from ansible_collections.community.aws.tests.unit.modules.utils import AnsibleExitJson, ModuleTestCase, set_module_args | ||
|
||
fixture_prefix = 'tests/unit/modules/fixtures/certs' | ||
|
||
|
||
class TestEc2WinPasswordModule(ModuleTestCase): | ||
@patch('ansible_collections.community.aws.plugins.modules.ec2_win_password.ec2_connect') | ||
def test_decryption(self, mock_connect): | ||
|
||
path = fixture_prefix + '/ec2_win_password.pem' | ||
with open(path, 'r') as f: | ||
pem = to_text(f.read()) | ||
|
||
with self.assertRaises(AnsibleExitJson) as exec_info: | ||
set_module_args({'instance_id': 'i-12345', | ||
'key_data': pem | ||
}) | ||
module = setup_module_object() | ||
mock_connect().get_password_data.return_value = 'L2k1iFiu/TRrjGr6Rwco/T3C7xkWxUw4+YPYpGGOmP3KDdy3hT1' \ | ||
'8RvdDJ2i0e+y7wUcH43DwbRYSlkSyALY/nzjSV9R5NChUyVs3W5' \ | ||
'5oiVuyTKsk0lor8dFJ9z9unq14tScZHvyQ3Nx1ggOtS18S9Pk55q' \ | ||
'IaCXfx26ucH76VRho=' | ||
ec2_win_password(module) | ||
|
||
self.assertEqual( | ||
exec_info.exception.args[0]['win_password'], | ||
to_bytes('Ansible_AWS_EC2_Win_Password'), | ||
) |