|
| 1 | +import base64 |
| 2 | +import os |
| 3 | + |
| 4 | +import urllib3 |
| 5 | +from azure.core.pipeline.transport._requests_basic import RequestsTransport |
| 6 | +from azure.identity import DefaultAzureCredential |
| 7 | +from azure.keyvault.certificates import CertificateClient, CertificatePolicy |
| 8 | +from azure.keyvault.keys import KeyClient, KeyOperation |
| 9 | +from azure.keyvault.keys.crypto import CryptographyClient, EncryptionAlgorithm, EncryptResult, DecryptResult |
| 10 | +from azure.keyvault.secrets import SecretClient |
| 11 | +from cryptography import x509 |
| 12 | +from cryptography.hazmat.primitives.asymmetric.ec import EllipticCurvePrivateKey |
| 13 | +from cryptography.hazmat.primitives.serialization.pkcs12 import load_key_and_certificates |
| 14 | + |
| 15 | +API_VERSION = "7.6" |
| 16 | + |
| 17 | + |
| 18 | +def hello_secrets_from_an_external_container(): |
| 19 | + """ |
| 20 | + Entry point function for a custom Docker container to test connectivity |
| 21 | + and "secrets" functionality with Lowkey Vault. |
| 22 | +
|
| 23 | + This function is designed to run inside a separate container within the |
| 24 | + same Docker network as a Lowkey Vault instance. |
| 25 | + """ |
| 26 | + vault_url = os.environ["CONNECTION_URL"] |
| 27 | + secret_message = os.environ["SECRET_VALUE"] |
| 28 | + secret_name = os.environ["SECRET_NAME"] |
| 29 | + # test secrets API to see that the container works |
| 30 | + secret_client = None |
| 31 | + try: |
| 32 | + # ignore SSL errors because we are using a self-signed certificate |
| 33 | + transport_secrets = RequestsTransport(connection_verify=False) |
| 34 | + # create the client we need |
| 35 | + secret_client = SecretClient( |
| 36 | + vault_url=vault_url, |
| 37 | + credential=DefaultAzureCredential(), |
| 38 | + verify_challenge_resource=False, |
| 39 | + transport=transport_secrets, |
| 40 | + api_version=API_VERSION, |
| 41 | + ) |
| 42 | + # set the result as a secret |
| 43 | + secret_client.set_secret(name=secret_name, value=secret_message) |
| 44 | + # get back the value |
| 45 | + actual = secret_client.get_secret(name=secret_name).value |
| 46 | + |
| 47 | + # verify the result |
| 48 | + assert actual == secret_message |
| 49 | + print("Lowkey Vault Container created.") |
| 50 | + except Exception as e: |
| 51 | + print(f"Something went wrong : {e}") |
| 52 | + finally: |
| 53 | + # close client |
| 54 | + if secret_client is not None: |
| 55 | + secret_client.close() |
| 56 | + |
| 57 | + |
| 58 | +def hello_keys_from_an_external_container(): |
| 59 | + """ |
| 60 | + Entry point function for a custom Docker container to test connectivity |
| 61 | + and "keys" functionality with Lowkey Vault. |
| 62 | +
|
| 63 | + This function is designed to run inside a separate container within the |
| 64 | + same Docker network as a Lowkey Vault instance. |
| 65 | + """ |
| 66 | + vault_url = os.environ["CONNECTION_URL"] |
| 67 | + secret_message = os.environ["SECRET_VALUE"] |
| 68 | + key_name = os.environ["KEY_NAME"] |
| 69 | + # test key API to see that the container works |
| 70 | + key_client = None |
| 71 | + crypto_client = None |
| 72 | + try: |
| 73 | + # ignore SSL errors because we are using a self-signed certificate |
| 74 | + transport_keys = RequestsTransport(connection_verify=False) |
| 75 | + transport_crypto = RequestsTransport(connection_verify=False) |
| 76 | + # create the clients we need |
| 77 | + key_client = KeyClient( |
| 78 | + vault_url=vault_url, |
| 79 | + credential=DefaultAzureCredential(), |
| 80 | + verify_challenge_resource=False, |
| 81 | + transport=transport_keys, |
| 82 | + api_version=API_VERSION, |
| 83 | + ) |
| 84 | + # create a new key |
| 85 | + key_client.create_rsa_key( |
| 86 | + name=key_name, |
| 87 | + size=2048, |
| 88 | + key_operations=[KeyOperation.encrypt, KeyOperation.decrypt, KeyOperation.wrap_key, KeyOperation.unwrap_key], |
| 89 | + ) |
| 90 | + |
| 91 | + crypto_client = CryptographyClient( |
| 92 | + key=key_client.get_key(name=key_name).id, |
| 93 | + credential=DefaultAzureCredential(), |
| 94 | + verify_challenge_resource=False, |
| 95 | + transport=transport_crypto, |
| 96 | + api_version=API_VERSION, |
| 97 | + ) |
| 98 | + |
| 99 | + # encode the text |
| 100 | + text_as_bytes: bytes = bytes(secret_message.encode("utf-8")) |
| 101 | + encrypted: EncryptResult = crypto_client.encrypt( |
| 102 | + algorithm=EncryptionAlgorithm.rsa_oaep_256, plaintext=text_as_bytes |
| 103 | + ) |
| 104 | + cipher_text: bytes = encrypted.ciphertext |
| 105 | + |
| 106 | + # decode the cipher text |
| 107 | + decrypted: DecryptResult = crypto_client.decrypt( |
| 108 | + algorithm=EncryptionAlgorithm.rsa_oaep_256, ciphertext=cipher_text |
| 109 | + ) |
| 110 | + decrypted_text: str = decrypted.plaintext.decode("utf-8") |
| 111 | + |
| 112 | + # verify the result |
| 113 | + assert decrypted_text == secret_message |
| 114 | + print("Lowkey Vault Container created.") |
| 115 | + except Exception as e: |
| 116 | + print(f"Something went wrong : {e}") |
| 117 | + finally: |
| 118 | + # close clients |
| 119 | + if key_client is not None: |
| 120 | + key_client.close() |
| 121 | + if crypto_client is not None: |
| 122 | + crypto_client.close() |
| 123 | + |
| 124 | + |
| 125 | +def hello_certificates_from_an_external_container(): |
| 126 | + """ |
| 127 | + Entry point function for a custom Docker container to test connectivity |
| 128 | + and "certificates" functionality with Lowkey Vault. |
| 129 | +
|
| 130 | + This function is designed to run inside a separate container within the |
| 131 | + same Docker network as a Lowkey Vault instance. |
| 132 | + """ |
| 133 | + vault_url = os.environ["CONNECTION_URL"] |
| 134 | + cert_name = os.environ["CERT_NAME"] |
| 135 | + # test certificates API to see that the container works |
| 136 | + certificate_client = None |
| 137 | + secret_client = None |
| 138 | + try: |
| 139 | + # ignore SSL errors because we are using a self-signed certificate |
| 140 | + transport_certs = RequestsTransport(connection_verify=False) |
| 141 | + transport_secrets = RequestsTransport(connection_verify=False) |
| 142 | + # create the clients we need |
| 143 | + certificate_client = CertificateClient( |
| 144 | + vault_url=vault_url, |
| 145 | + credential=DefaultAzureCredential(), |
| 146 | + verify_challenge_resource=False, |
| 147 | + transport=transport_certs, |
| 148 | + api_version=API_VERSION, |
| 149 | + ) |
| 150 | + secret_client = SecretClient( |
| 151 | + vault_url=vault_url, |
| 152 | + credential=DefaultAzureCredential(), |
| 153 | + verify_challenge_resource=False, |
| 154 | + transport=transport_secrets, |
| 155 | + api_version=API_VERSION, |
| 156 | + ) |
| 157 | + |
| 158 | + subject_name: str = "CN=example.com" |
| 159 | + policy: CertificatePolicy = CertificatePolicy( |
| 160 | + issuer_name="Self", |
| 161 | + subject=subject_name, |
| 162 | + key_curve_name="P-256", |
| 163 | + key_type="EC", |
| 164 | + validity_in_months=12, |
| 165 | + content_type="application/x-pkcs12", |
| 166 | + ) |
| 167 | + certificate_client.begin_create_certificate(certificate_name=cert_name, policy=policy).wait() |
| 168 | + |
| 169 | + cert_value = secret_client.get_secret(name=cert_name).value |
| 170 | + |
| 171 | + # decode base64 secret |
| 172 | + decoded = base64.b64decode(cert_value) |
| 173 | + # open decoded secret as PKCS12 file |
| 174 | + pkcs12 = load_key_and_certificates(decoded, b"") |
| 175 | + |
| 176 | + # get the components |
| 177 | + ec_key: EllipticCurvePrivateKey = pkcs12[0] |
| 178 | + x509_cert: x509.Certificate = pkcs12[1] |
| 179 | + |
| 180 | + # verify the result |
| 181 | + assert subject_name == x509_cert.subject.rdns[0].rfc4514_string() |
| 182 | + assert "secp256r1" == ec_key.curve.name |
| 183 | + |
| 184 | + print("Lowkey Vault Container created.") |
| 185 | + except Exception as e: |
| 186 | + print(f"Something went wrong : {e}") |
| 187 | + finally: |
| 188 | + # close clients |
| 189 | + if certificate_client is not None: |
| 190 | + certificate_client.close() |
| 191 | + if secret_client is not None: |
| 192 | + secret_client.close() |
| 193 | + |
| 194 | + |
| 195 | +if __name__ == "__main__": |
| 196 | + mode = os.getenv("TEST") |
| 197 | + # ignore cert errors |
| 198 | + urllib3.disable_warnings() |
| 199 | + if mode == "secrets": |
| 200 | + hello_secrets_from_an_external_container() |
| 201 | + elif mode == "keys": |
| 202 | + hello_keys_from_an_external_container() |
| 203 | + elif mode == "certificates": |
| 204 | + hello_certificates_from_an_external_container() |
| 205 | + else: |
| 206 | + print("The TEST env variable must be 'secrets', 'keys', or 'certificates'.") |
0 commit comments