|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""Key management client interface for table encryption.""" |
| 18 | + |
| 19 | +from __future__ import annotations |
| 20 | + |
| 21 | +from abc import ABC, abstractmethod |
| 22 | +from dataclasses import dataclass, field |
| 23 | + |
| 24 | + |
| 25 | +@dataclass(frozen=True) |
| 26 | +class GeneratedKey: |
| 27 | + """A newly generated key, both in the clear and wrapped by the key management service.""" |
| 28 | + |
| 29 | + key: bytes = field(repr=False) |
| 30 | + wrapped_key: bytes |
| 31 | + |
| 32 | + |
| 33 | +class KeyManagementClient(ABC): |
| 34 | + """A base class for key management service implementations. |
| 35 | +
|
| 36 | + Wraps and unwraps table encryption keys using master keys that the service holds. |
| 37 | + """ |
| 38 | + |
| 39 | + @abstractmethod |
| 40 | + def wrap_key(self, key: bytes, wrapping_key_id: str) -> bytes: |
| 41 | + """Wrap a key using the master key identified by `wrapping_key_id`. |
| 42 | +
|
| 43 | + Args: |
| 44 | + key (bytes): The key to wrap. |
| 45 | + wrapping_key_id (str): Identifies the master key held by the service. |
| 46 | + """ |
| 47 | + |
| 48 | + @abstractmethod |
| 49 | + def unwrap_key(self, wrapped_key: bytes, wrapping_key_id: str) -> bytes: |
| 50 | + """Unwrap a key using the master key identified by `wrapping_key_id`. |
| 51 | +
|
| 52 | + Args: |
| 53 | + wrapped_key (bytes): The wrapped key, as returned by `wrap_key`. |
| 54 | + wrapping_key_id (str): Identifies the master key held by the service. |
| 55 | + """ |
| 56 | + |
| 57 | + def supports_key_generation(self) -> bool: |
| 58 | + """Whether the service generates keys itself, rather than only wrapping them.""" |
| 59 | + return False |
| 60 | + |
| 61 | + def generate_key(self, wrapping_key_id: str) -> GeneratedKey: |
| 62 | + """Generate a new key, wrapped by the master key identified by `wrapping_key_id`. |
| 63 | +
|
| 64 | + Args: |
| 65 | + wrapping_key_id (str): Identifies the master key held by the service. |
| 66 | + """ |
| 67 | + raise NotImplementedError(f"{type(self).__name__} does not support key generation") |
0 commit comments