This is a mock class implementation of KmsClient, built for testing only. During tests that involve CryptoFactory.rotate_master_keys, separate instances of this client will be created when writing, rotating keys, and reading back parquet data. To help unit tests verify that external
| 71 | |
| 72 | |
| 73 | class MockVersioningKmsClient(pe.KmsClient): |
| 74 | """This is a mock class implementation of KmsClient, built for testing |
| 75 | only. |
| 76 | |
| 77 | During tests that involve CryptoFactory.rotate_master_keys, separate |
| 78 | instances of this client will be created when writing, rotating keys, and |
| 79 | reading back parquet data. To help unit tests verify that external key |
| 80 | material was stored correctly at each step, this client wraps keys with a |
| 81 | master_key_identifier and a version number. To ensure each client wraps |
| 82 | with the correct version, the current version is persisted in the |
| 83 | key_access_token attribute of the KmsConnectionConfig shared by all clients |
| 84 | """ |
| 85 | |
| 86 | def __init__(self, connection_config) -> None: |
| 87 | pe.KmsClient.__init__(self) |
| 88 | self.connection_config = connection_config |
| 89 | |
| 90 | @property |
| 91 | def master_key_version(self) -> int: |
| 92 | return int(self.connection_config.key_access_token) |
| 93 | |
| 94 | def wrap_key(self, key_bytes: bytes, master_key_identifier: str) -> str: |
| 95 | b64key = base64.b64encode(key_bytes).decode('utf-8') |
| 96 | return f"{master_key_identifier}:v{self.master_key_version}:{b64key}" |
| 97 | |
| 98 | def unwrap_key( |
| 99 | self, |
| 100 | wrapped_key: str, |
| 101 | master_key_identifier: str) -> bytes: |
| 102 | key_id, _, key = parse_wrapped_key(wrapped_key) |
| 103 | if key_id != master_key_identifier: |
| 104 | raise ValueError("Mismatched master key identifiers:", |
| 105 | key_id, master_key_identifier) |
| 106 | return key |
| 107 | |
| 108 | |
| 109 | def verify_file_encrypted(path): |