Skip to content

Commit adc1b81

Browse files
authored
DGS-22404 Add AppRole auth for HC Vault (#2084)
1 parent a3b43aa commit adc1b81

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/confluent_kafka/schema_registry/rules/encryption/hcvault/hcvault_client.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ class HcVaultKmsClient(tink.KmsClient):
2929
"""Basic HashiCorp Vault client for AEAD."""
3030

3131
def __init__(
32-
self, key_uri: Optional[str], token: str, ns: Optional[str] = None
32+
self, key_uri: Optional[str], token: Optional[str], ns: Optional[str] = None,
33+
role_id: Optional[str] = None, secret_id: Optional[str] = None
3334
) -> None:
34-
"""Creates a new GcpKmsClient that is bound to the key specified in 'key_uri'.
35+
"""Creates a new HcVaultKmsClient that is bound to the key specified in 'key_uri'.
3536
3637
Uses the specified credentials when communicating with the KMS.
3738
@@ -59,6 +60,8 @@ def __init__(
5960
namespace=ns,
6061
verify=False
6162
)
63+
if role_id and secret_id:
64+
self._client.auth.approle.login(role_id=role_id, secret_id=secret_id)
6265

6366
def does_support(self, key_uri: str) -> bool:
6467
"""Returns true iff this client supports KMS key specified in 'key_uri'.

src/confluent_kafka/schema_registry/rules/encryption/hcvault/hcvault_driver.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
_PREFIX = "hcvault://"
2424
_TOKEN_ID = "token.id"
2525
_NAMESPACE = "namespace"
26+
_APPROLE_ROLE_ID = "approle.role.id"
27+
_APPROLE_SECRET_ID = "approle.secret.id"
2628

2729

2830
class HcVaultKmsDriver(KmsDriver):
@@ -37,11 +39,18 @@ def new_kms_client(self, conf: dict, key_url: str) -> KmsClient:
3739
if key_url is not None:
3840
uri_prefix = key_url
3941
token = conf.get(_TOKEN_ID)
40-
namespace = conf.get(_NAMESPACE)
4142
if token is None:
4243
token = os.getenv("VAULT_TOKEN")
44+
namespace = conf.get(_NAMESPACE)
45+
if namespace is None:
4346
namespace = os.getenv("VAULT_NAMESPACE")
44-
return HcVaultKmsClient(uri_prefix, token, namespace)
47+
role_id = conf.get(_APPROLE_ROLE_ID)
48+
if role_id is None:
49+
role_id = os.getenv("VAULT_APPROLE_ROLE_ID")
50+
secret_id = conf.get(_APPROLE_SECRET_ID)
51+
if secret_id is None:
52+
secret_id = os.getenv("VAULT_APPROLE_SECRET_ID")
53+
return HcVaultKmsClient(uri_prefix, token, namespace, role_id, secret_id)
4554

4655
@classmethod
4756
def register(cls):

0 commit comments

Comments
 (0)