1
- using System . Collections . Generic ;
1
+ using System ;
2
+ using System . Collections . Generic ;
2
3
using Amazon . Runtime ;
4
+ using Amazon . Runtime . CredentialManagement ;
3
5
4
6
namespace Confluent . SchemaRegistry . Encryption . Aws
5
7
{
@@ -13,20 +15,73 @@ public static void Register()
13
15
public static readonly string Prefix = "aws-kms://" ;
14
16
public static readonly string AccessKeyId = "access.key.id" ;
15
17
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
+
17
23
public string GetKeyUrlPrefix ( )
18
24
{
19
25
return Prefix ;
20
26
}
21
27
22
28
public IKmsClient NewKmsClient ( IDictionary < string , string > config , string keyUrl )
23
29
{
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
+ }
24
45
AWSCredentials credentials = null ;
25
46
if ( config . TryGetValue ( AccessKeyId , out string accessKeyId )
26
47
&& config . TryGetValue ( SecretAccessKey , out string secretAccessKey ) )
27
48
{
28
49
credentials = new BasicAWSCredentials ( accessKeyId , secretAccessKey ) ;
29
50
}
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
+ }
30
85
return new AwsKmsClient ( keyUrl , credentials ) ;
31
86
}
32
87
}
0 commit comments