1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using VaultSharp . V1 . AuthMethods ;
4
+ using VaultSharp . V1 . AuthMethods . AppRole ;
5
+ using VaultSharp . V1 . AuthMethods . Token ;
3
6
4
7
namespace Confluent . SchemaRegistry . Encryption . HcVault
5
8
{
@@ -13,7 +16,9 @@ public static void Register()
13
16
public static readonly string Prefix = "hcvault://" ;
14
17
public static readonly string TokenId = "token.id" ;
15
18
public static readonly string Namespace = "namespace" ;
16
-
19
+ public static readonly string ApproleRoleId = "approle.role.id" ;
20
+ public static readonly string ApproleSecretId = "approle.secret.id" ;
21
+
17
22
public string GetKeyUrlPrefix ( )
18
23
{
19
24
return Prefix ;
@@ -22,13 +27,42 @@ public string GetKeyUrlPrefix()
22
27
public IKmsClient NewKmsClient ( IDictionary < string , string > config , string keyUrl )
23
28
{
24
29
config . TryGetValue ( TokenId , out string tokenId ) ;
25
- config . TryGetValue ( Namespace , out string ns ) ;
26
30
if ( tokenId == null )
27
31
{
28
32
tokenId = Environment . GetEnvironmentVariable ( "VAULT_TOKEN" ) ;
33
+ }
34
+ config . TryGetValue ( Namespace , out string ns ) ;
35
+ if ( ns == null )
36
+ {
29
37
ns = Environment . GetEnvironmentVariable ( "VAULT_NAMESPACE" ) ;
30
38
}
31
- return new HcVaultKmsClient ( keyUrl , ns , tokenId ) ;
39
+ config . TryGetValue ( ApproleRoleId , out string roleId ) ;
40
+ if ( roleId == null )
41
+ {
42
+ roleId = Environment . GetEnvironmentVariable ( "VAULT_APPROLE_ROLE_ID" ) ;
43
+ }
44
+ config . TryGetValue ( ApproleSecretId , out string secretId ) ;
45
+ if ( secretId == null )
46
+ {
47
+ secretId = Environment . GetEnvironmentVariable ( "VAULT_APPROLE_SECRET_ID" ) ;
48
+ }
49
+
50
+ IAuthMethodInfo authMethod ;
51
+ if ( roleId != null && secretId != null )
52
+ {
53
+ authMethod = new AppRoleAuthMethodInfo ( roleId , secretId ) ;
54
+ }
55
+ else if ( tokenId != null )
56
+ {
57
+ authMethod = new TokenAuthMethodInfo ( tokenId ) ;
58
+ }
59
+ else
60
+ {
61
+ throw new ArgumentException ( $ "Either { TokenId } or both { ApproleRoleId } and { ApproleSecretId } " +
62
+ $ "must be provided in config or environment variables.") ;
63
+ }
64
+
65
+ return new HcVaultKmsClient ( keyUrl , ns , authMethod ) ;
32
66
}
33
67
}
34
68
}
0 commit comments