Skip to content

Commit 1ab0518

Browse files
authored
DGS-22404 Add AppRole auth for HC Vault (#378)
* DGS-22404 Add AppRole auth for HC Vault * Minor cleanup
1 parent e56315c commit 1ab0518

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

schemaregistry/rules/encryption/hcvault/hcvault-client.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ export class HcVaultClient implements KmsClient {
88
private keyUri: string
99
private keyId: string
1010
private keyName: string
11+
private authPromise?: Promise<void>
1112

12-
constructor(keyUri: string, namespace?: string, token?: string) {
13+
constructor(keyUri: string, namespace?: string, token?: string,
14+
roleId?: string, secretId?: string) {
1315
if (!keyUri.startsWith(HcVaultDriver.PREFIX)) {
1416
throw new Error(`key uri must start with ${HcVaultDriver.PREFIX}`)
1517
}
@@ -27,19 +29,34 @@ export class HcVaultClient implements KmsClient {
2729
...token && { token },
2830
apiVersion: 'v1',
2931
})
32+
if (roleId != null && secretId != null) {
33+
this.authPromise = this.kmsClient.approleLogin({role_id: roleId, secret_id: secretId})
34+
.then((result) => {
35+
this.kmsClient.token = result.auth.client_token
36+
})
37+
}
3038
}
3139

3240
supported(keyUri: string): boolean {
3341
return this.keyUri === keyUri
3442
}
3543

44+
private async ensureAuthenticated(): Promise<void> {
45+
if (this.authPromise) {
46+
await this.authPromise
47+
this.authPromise = undefined // Clear after first use
48+
}
49+
}
50+
3651
async encrypt(plaintext: Buffer): Promise<Buffer> {
52+
await this.ensureAuthenticated()
3753
const response = await this.kmsClient.encryptData({name: this.keyName, plaintext: plaintext.toString('base64') })
3854
let data = response.data.ciphertext
3955
return Buffer.from(data, 'utf8')
4056
}
4157

4258
async decrypt(ciphertext: Buffer): Promise<Buffer> {
59+
await this.ensureAuthenticated()
4360
const response = await this.kmsClient.decryptData({name: this.keyName, ciphertext: ciphertext.toString('utf8') })
4461
let data = response.data.plaintext
4562
return Buffer.from(data, 'base64');

schemaregistry/rules/encryption/hcvault/hcvault-driver.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export class HcVaultDriver implements KmsDriver {
66
static PREFIX = 'hcvault://'
77
static TOKEN_ID = 'token.id'
88
static NAMESPACE = 'namespace'
9+
static APPROLE_ROLE_ID = 'approle.role.id'
10+
static APPROLE_SECRET_ID = 'approle.secret.id'
911

1012
/**
1113
* Register the HashiCorp Vault driver with the KMS registry.
@@ -21,12 +23,25 @@ export class HcVaultDriver implements KmsDriver {
2123
newKmsClient(config: Map<string, string>, keyUrl?: string): KmsClient {
2224
const uriPrefix = keyUrl != null ? keyUrl : HcVaultDriver.PREFIX
2325
let tokenId = config.get(HcVaultDriver.TOKEN_ID)
24-
let ns = config.get(HcVaultDriver.NAMESPACE)
2526
if (tokenId == null)
2627
{
2728
tokenId = process.env["VAULT_TOKEN"]
29+
}
30+
let ns = config.get(HcVaultDriver.NAMESPACE)
31+
if (ns == null)
32+
{
2833
ns = process.env["VAULT_NAMESPACE"]
2934
}
30-
return new HcVaultClient(uriPrefix, ns, tokenId)
35+
let roleId = config.get(HcVaultDriver.APPROLE_ROLE_ID)
36+
if (roleId == null)
37+
{
38+
roleId = process.env["VAULT_APPROLE_ROLE_ID"]
39+
}
40+
let secretId = config.get(HcVaultDriver.APPROLE_SECRET_ID)
41+
if (secretId == null)
42+
{
43+
secretId = process.env["VAULT_APPROLE_SECRET_ID"]
44+
}
45+
return new HcVaultClient(uriPrefix, ns, tokenId, roleId, secretId)
3146
}
3247
}

0 commit comments

Comments
 (0)