1- using System . Collections . Generic ;
1+ using System ;
2+ using System . Collections . Generic ;
23using Amazon . Runtime ;
4+ using Amazon . Runtime . CredentialManagement ;
35
46namespace Confluent . SchemaRegistry . Encryption . Aws
57{
@@ -13,20 +15,73 @@ public static void Register()
1315 public static readonly string Prefix = "aws-kms://" ;
1416 public static readonly string AccessKeyId = "access.key.id" ;
1517 public static readonly string SecretAccessKey = "secret.access.key" ;
16-
18+ public static readonly string Profile = "profile" ;
19+ public static readonly string RoleArn = "role.arn" ;
20+ public static readonly string RoleSessionName = "role.session.name" ;
21+ public static readonly string RoleExternalId = "role.external.id" ;
22+
1723 public string GetKeyUrlPrefix ( )
1824 {
1925 return Prefix ;
2026 }
2127
2228 public IKmsClient NewKmsClient ( IDictionary < string , string > config , string keyUrl )
2329 {
30+ config . TryGetValue ( RoleArn , out string roleArn ) ;
31+ if ( roleArn == null )
32+ {
33+ roleArn = Environment . GetEnvironmentVariable ( "AWS_ROLE_ARN" ) ;
34+ }
35+ config . TryGetValue ( RoleSessionName , out string roleSessionName ) ;
36+ if ( roleSessionName == null )
37+ {
38+ roleSessionName = Environment . GetEnvironmentVariable ( "AWS_ROLE_SESSION_NAME" ) ;
39+ }
40+ config . TryGetValue ( RoleExternalId , out string roleExternalId ) ;
41+ if ( roleExternalId == null )
42+ {
43+ roleExternalId = Environment . GetEnvironmentVariable ( "AWS_ROLE_EXTERNAL_ID" ) ;
44+ }
2445 AWSCredentials credentials = null ;
2546 if ( config . TryGetValue ( AccessKeyId , out string accessKeyId )
2647 && config . TryGetValue ( SecretAccessKey , out string secretAccessKey ) )
2748 {
2849 credentials = new BasicAWSCredentials ( accessKeyId , secretAccessKey ) ;
2950 }
51+ else if ( config . TryGetValue ( Profile , out string profile ) )
52+ {
53+ var credentialProfileStoreChain = new CredentialProfileStoreChain ( ) ;
54+ if ( credentialProfileStoreChain . TryGetAWSCredentials (
55+ profile , out AWSCredentials creds ) )
56+ credentials = creds ;
57+ }
58+ if ( credentials == null )
59+ {
60+ credentials = FallbackCredentialsFactory . GetCredentials ( ) ;
61+ }
62+ if ( roleArn != null )
63+ {
64+ if ( string . IsNullOrEmpty ( roleExternalId ) )
65+ {
66+ credentials = new AssumeRoleAWSCredentials (
67+ credentials ,
68+ roleArn ,
69+ roleSessionName ?? "confluent-encrypt" ) ;
70+ }
71+ else
72+ {
73+ var options = new AssumeRoleAWSCredentialsOptions
74+ {
75+ ExternalId = roleExternalId
76+ } ;
77+
78+ credentials = new AssumeRoleAWSCredentials (
79+ credentials ,
80+ roleArn ,
81+ roleSessionName ?? "confluent-encrypt" ,
82+ options ) ;
83+ }
84+ }
3085 return new AwsKmsClient ( keyUrl , credentials ) ;
3186 }
3287 }
0 commit comments