Skip to content

Commit 7f93e5b

Browse files
committed
DGS-22404 Add AppRole auth for HC Vault
1 parent 7c258c6 commit 7f93e5b

File tree

2 files changed

+44
-8
lines changed

2 files changed

+44
-8
lines changed

src/Confluent.SchemaRegistry.Encryption.HcVault/HcVaultKmsClient.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,22 @@ public class HcVaultKmsClient : IKmsClient
1717

1818
public string KekId { get; }
1919
public string Namespace { get; }
20-
public string TokenId { get; }
21-
20+
2221
public HcVaultKmsClient(string kekId, string ns, string tokenId)
22+
: this(kekId, ns, new TokenAuthMethodInfo(tokenId))
23+
{
24+
}
25+
26+
public HcVaultKmsClient(string kekId, string ns, IAuthMethodInfo authMethod)
2327
{
2428
KekId = kekId;
2529
Namespace = ns;
26-
TokenId = tokenId;
27-
30+
2831
if (!kekId.StartsWith(HcVaultKmsDriver.Prefix))
2932
{
3033
throw new ArgumentException(string.Format($"key URI must start with {HcVaultKmsDriver.Prefix}"));
3134
}
3235
keyId = KekId.Substring(HcVaultKmsDriver.Prefix.Length);
33-
IAuthMethodInfo authMethod = new TokenAuthMethodInfo(tokenId);
3436
Uri uri = new Uri(keyId);
3537
if (uri.Segments.Length == 0)
3638
{

src/Confluent.SchemaRegistry.Encryption.HcVault/HcVaultKmsDriver.cs

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
using System;
22
using System.Collections.Generic;
3+
using VaultSharp.V1.AuthMethods;
4+
using VaultSharp.V1.AuthMethods.AppRole;
5+
using VaultSharp.V1.AuthMethods.Token;
36

47
namespace Confluent.SchemaRegistry.Encryption.HcVault
58
{
@@ -13,7 +16,9 @@ public static void Register()
1316
public static readonly string Prefix = "hcvault://";
1417
public static readonly string TokenId = "token.id";
1518
public static readonly string Namespace = "namespace";
16-
19+
public static readonly string AppRoleId = "app.role.id";
20+
public static readonly string AppRoleSecretId = "app.role.secret.id";
21+
1722
public string GetKeyUrlPrefix()
1823
{
1924
return Prefix;
@@ -22,13 +27,42 @@ public string GetKeyUrlPrefix()
2227
public IKmsClient NewKmsClient(IDictionary<string, string> config, string keyUrl)
2328
{
2429
config.TryGetValue(TokenId, out string tokenId);
25-
config.TryGetValue(Namespace, out string ns);
2630
if (tokenId == null)
2731
{
2832
tokenId = Environment.GetEnvironmentVariable("VAULT_TOKEN");
33+
}
34+
config.TryGetValue(Namespace, out string ns);
35+
if (ns == null)
36+
{
2937
ns = Environment.GetEnvironmentVariable("VAULT_NAMESPACE");
3038
}
31-
return new HcVaultKmsClient(keyUrl, ns, tokenId);
39+
config.TryGetValue(AppRoleId, out string appRoleId);
40+
if (appRoleId == null)
41+
{
42+
appRoleId = Environment.GetEnvironmentVariable("VAULT_APP_ROLE_ID");
43+
}
44+
config.TryGetValue(AppRoleSecretId, out string appRoleSecretId);
45+
if (appRoleSecretId == null)
46+
{
47+
appRoleSecretId = Environment.GetEnvironmentVariable("VAULT_APP_ROLE_SECRET_ID");
48+
}
49+
50+
IAuthMethodInfo authMethod;
51+
if (appRoleId != null && appRoleSecretId != null)
52+
{
53+
authMethod = new AppRoleAuthMethodInfo(appRoleId, appRoleSecretId);
54+
}
55+
else if (tokenId != null)
56+
{
57+
authMethod = new TokenAuthMethodInfo(tokenId);
58+
}
59+
else
60+
{
61+
throw new ArgumentException($"Either {TokenId} or both {AppRoleId} and {AppRoleSecretId} " +
62+
$"must be provided in config or environment variables.");
63+
}
64+
65+
return new HcVaultKmsClient(keyUrl, ns, authMethod);
3266
}
3367
}
3468
}

0 commit comments

Comments
 (0)