An example of a KmsClient implementation with master keys managed by Hashicorp Vault KMS. See Vault documentation: https://www.vaultproject.io/api/secret/transit Not for production use!
| 28 | |
| 29 | |
| 30 | class VaultClient(pe.KmsClient): |
| 31 | """An example of a KmsClient implementation with master keys |
| 32 | managed by Hashicorp Vault KMS. |
| 33 | See Vault documentation: https://www.vaultproject.io/api/secret/transit |
| 34 | Not for production use! |
| 35 | """ |
| 36 | JSON_MEDIA_TYPE = "application/json; charset=utf-8" |
| 37 | DEFAULT_TRANSIT_ENGINE = "/v1/transit/" |
| 38 | WRAP_ENDPOINT = "encrypt/" |
| 39 | UNWRAP_ENDPOINT = "decrypt/" |
| 40 | TOKEN_HEADER = "X-Vault-Token" |
| 41 | |
| 42 | def __init__(self, kms_connection_config): |
| 43 | """Create a VaultClient instance. |
| 44 | |
| 45 | Parameters |
| 46 | ---------- |
| 47 | kms_connection_config : KmsConnectionConfig |
| 48 | configuration parameters to connect to vault, |
| 49 | e.g. URL and access token |
| 50 | """ |
| 51 | pe.KmsClient.__init__(self) |
| 52 | self.kms_url = kms_connection_config.kms_instance_url + \ |
| 53 | VaultClient.DEFAULT_TRANSIT_ENGINE |
| 54 | self.kms_connection_config = kms_connection_config |
| 55 | |
| 56 | def wrap_key(self, key_bytes, master_key_identifier): |
| 57 | """Call Vault to wrap key key_bytes with key |
| 58 | identified by master_key_identifier.""" |
| 59 | endpoint = self.kms_url + VaultClient.WRAP_ENDPOINT |
| 60 | headers = {VaultClient.TOKEN_HEADER: |
| 61 | self.kms_connection_config.key_access_token} |
| 62 | r = requests.post(endpoint + master_key_identifier, |
| 63 | headers=headers, |
| 64 | data={'plaintext': base64.b64encode(key_bytes)}) |
| 65 | r.raise_for_status() |
| 66 | r_dict = r.json() |
| 67 | wrapped_key = r_dict['data']['ciphertext'] |
| 68 | return wrapped_key |
| 69 | |
| 70 | def unwrap_key(self, wrapped_key, master_key_identifier): |
| 71 | """Call Vault to unwrap wrapped_key with key |
| 72 | identified by master_key_identifier""" |
| 73 | endpoint = self.kms_url + VaultClient.UNWRAP_ENDPOINT |
| 74 | headers = {VaultClient.TOKEN_HEADER: |
| 75 | self.kms_connection_config.key_access_token} |
| 76 | r = requests.post(endpoint + master_key_identifier, |
| 77 | headers=headers, |
| 78 | data={'ciphertext': wrapped_key}) |
| 79 | r.raise_for_status() |
| 80 | r_dict = r.json() |
| 81 | plaintext = r_dict['data']['plaintext'] |
| 82 | key_bytes = base64.b64decode(plaintext) |
| 83 | return key_bytes |
| 84 | |
| 85 | |
| 86 | def parquet_write_read_with_vault(parquet_filename): |