-
Notifications
You must be signed in to change notification settings - Fork 361
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: generate SSH private key (#1060)
Instead of hardcoded value generate SSH Private key in tests Fixes: #823 Signed-off-by: Abhijeet Kasurde <[email protected]>
- Loading branch information
Showing
4 changed files
with
45 additions
and
9 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 |
---|---|---|
|
@@ -5,3 +5,4 @@ pytest-timeout | |
pytest-xdist | ||
flake8==4.0.1 | ||
yamllint | ||
cryptography |
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 |
---|---|---|
@@ -1,11 +1,40 @@ | ||
import time | ||
from cryptography.hazmat.backends import default_backend | ||
from cryptography.hazmat.primitives.asymmetric.rsa import generate_private_key | ||
from cryptography.hazmat.primitives.serialization import ( | ||
Encoding, | ||
NoEncryption, | ||
PrivateFormat, | ||
) | ||
|
||
|
||
def iterate_timeout(max_seconds, purpose, interval=2): | ||
start = time.time() | ||
count = 0 | ||
while (time.time() < start + max_seconds): | ||
while time.time() < start + max_seconds: | ||
count += 1 | ||
yield count | ||
time.sleep(interval) | ||
raise Exception("Timeout waiting for %s" % purpose) | ||
|
||
|
||
class RSAKey: | ||
"""In-memory RSA key generation and management utils.""" | ||
|
||
def __init__(self): | ||
_rsa_key_obj = generate_private_key( | ||
public_exponent=65537, | ||
key_size=1024, | ||
backend=default_backend(), | ||
) | ||
|
||
_private_rsa_key_repr = _rsa_key_obj.private_bytes( | ||
encoding=Encoding.PEM, | ||
format=PrivateFormat.TraditionalOpenSSL, # A.K.A. PKCS#1 | ||
encryption_algorithm=NoEncryption(), | ||
) | ||
self._private_rsa_key_repr = _private_rsa_key_repr.decode() | ||
|
||
@property | ||
def private(self) -> str: | ||
return self._private_rsa_key_repr |