11using System ;
22using System . Collections . Generic ;
3+ using VaultSharp . V1 . AuthMethods ;
4+ using VaultSharp . V1 . AuthMethods . AppRole ;
5+ using VaultSharp . V1 . AuthMethods . Token ;
36
47namespace 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 ApproleRoleId = "approle.role.id" ;
20+ public static readonly string ApproleSecretId = "approle.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 ( 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 ) ;
3266 }
3367 }
3468}
0 commit comments