-
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 #3 from jasonrig/dummy-key
add dummy key; code cleanup; update gh actions
- Loading branch information
Showing
10 changed files
with
120 additions
and
31 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,32 @@ | ||
from typing import Optional | ||
|
||
from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePrivateKey, ECDSA | ||
from cryptography.hazmat.primitives.serialization import load_pem_private_key | ||
|
||
from paramiko_cloud.base import BaseKeyECDSA, CloudSigningKey | ||
|
||
|
||
class _LocalSigningKey(CloudSigningKey): | ||
""" | ||
A dummy signing key | ||
""" | ||
def __init__(self, key: EllipticCurvePrivateKey): | ||
super().__init__(key.curve) | ||
self.key = key | ||
|
||
def sign(self, data: bytes, signature_algorithm: ECDSA) -> bytes: | ||
return self.key.sign(data, signature_algorithm) | ||
|
||
|
||
class ECDSAKey(BaseKeyECDSA): | ||
""" | ||
A dummy key that demonstrates the abstraction, but just loads they key from file. | ||
Args: | ||
pem_private_key: A PEM-formatted private key | ||
password: An optional password to decrypt the private key | ||
""" | ||
def __init__(self, pem_private_key: bytes, password: Optional[bytes] = None): | ||
private_key: EllipticCurvePrivateKey = load_pem_private_key(pem_private_key, password) | ||
public_key = private_key.public_key() | ||
super().__init__((_LocalSigningKey(private_key), public_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,45 @@ | ||
from unittest import TestCase | ||
|
||
from cryptography.hazmat.primitives import serialization | ||
from cryptography.hazmat.primitives.asymmetric import ec | ||
from paramiko.rsakey import RSAKey | ||
|
||
from paramiko_cloud.dummy.keys import ECDSAKey | ||
from paramiko_cloud.test_helpers import parse_certificate, sha256_fingerprint | ||
|
||
private_key = ec.generate_private_key(ec.SECP256R1()).private_bytes( | ||
encoding=serialization.Encoding.PEM, | ||
format=serialization.PrivateFormat.PKCS8, | ||
encryption_algorithm=serialization.NoEncryption() | ||
) | ||
|
||
|
||
class TestECDSAKey(TestCase): | ||
def test_key_from_cloud_can_sign(self): | ||
key = ECDSAKey(private_key) | ||
signature = key.sign_ssh_data(b"hello world") | ||
signature.rewind() | ||
self.assertTrue(key.verify_ssh_sig(b"hello world", signature), "Signature is invalid") | ||
|
||
def test_key_from_cloud_can_produce_valid_certificate(self): | ||
ca_key = ECDSAKey(private_key) | ||
client_key = RSAKey.generate(1024) | ||
cert_string = ca_key.sign_certificate(client_key, ["test.user"]).cert_string() | ||
exit_code, cert_details = parse_certificate(cert_string) | ||
self.assertEqual( | ||
cert_details.public_key, | ||
"RSA-CERT SHA256:{}".format(sha256_fingerprint(client_key)) | ||
) | ||
self.assertEqual( | ||
cert_details.signing_ca, | ||
"ECDSA SHA256:{} (using ecdsa-sha2-nistp{})".format( | ||
sha256_fingerprint(ca_key), | ||
ca_key.ecdsa_curve.key_length | ||
) | ||
) | ||
self.assertEqual( | ||
exit_code, 0, | ||
"Could not parse generated certificate with ssh-keygen, exit code {}".format( | ||
exit_code | ||
) | ||
) |
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