MCPcopy Create free account
hub / github.com/apache/arrow / InMemoryKmsClient

Class InMemoryKmsClient

python/pyarrow/tests/parquet/encryption.py:23–54  ·  view source on GitHub ↗

This is a mock class implementation of KmsClient, built for testing only.

Source from the content-addressed store, hash-verified

21
22
23class InMemoryKmsClient(pe.KmsClient):
24 """This is a mock class implementation of KmsClient, built for testing
25 only.
26 """
27
28 def __init__(self, config):
29 """Create an InMemoryKmsClient instance."""
30 pe.KmsClient.__init__(self)
31 self.master_keys_map = config.custom_kms_conf
32
33 def wrap_key(self, key_bytes, master_key_identifier):
34 """Not a secure cipher - the wrapped key
35 is just the master key concatenated with key bytes"""
36 master_key_bytes = self.master_keys_map[master_key_identifier].encode(
37 'utf-8')
38 wrapped_key = b"".join([master_key_bytes, key_bytes])
39 result = base64.b64encode(wrapped_key)
40 return result
41
42 def unwrap_key(self, wrapped_key, master_key_identifier):
43 """Not a secure cipher - just extract the key from
44 the wrapped key"""
45 if master_key_identifier not in self.master_keys_map:
46 raise ValueError("Unknown master key", master_key_identifier)
47 expected_master_key = self.master_keys_map[master_key_identifier]
48 decoded_wrapped_key = base64.b64decode(wrapped_key)
49 master_key_bytes = decoded_wrapped_key[:16]
50 decrypted_key = decoded_wrapped_key[16:]
51 if (expected_master_key == master_key_bytes.decode('utf-8')):
52 return decrypted_key
53 raise ValueError("Incorrect master key used",
54 master_key_bytes, decrypted_key)
55
56
57def parse_wrapped_key(wrapped_key: str) -> tuple[str, int, bytes]:

Callers 3

kms_factoryFunction · 0.90
kms_factoryFunction · 0.90
kms_factoryFunction · 0.90

Calls

no outgoing calls

Tested by 2

kms_factoryFunction · 0.72
kms_factoryFunction · 0.72