Skip to content

Commit 2915785

Browse files
authored
Add AWS AssumeRole support to AWS KMS (confluentinc#2379)
1 parent 87979d8 commit 2915785

File tree

2 files changed

+59
-3
lines changed

2 files changed

+59
-3
lines changed

src/Confluent.SchemaRegistry.Encryption.Aws/AwsKmsDriver.cs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using Amazon.Runtime;
4+
using Amazon.Runtime.CredentialManagement;
35

46
namespace 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
}

src/Confluent.SchemaRegistry.Encryption.Aws/Confluent.SchemaRegistry.Encryption.Aws.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
</ItemGroup>
3030

3131
<ItemGroup>
32-
<PackageReference Include="AWSSDK.KeyManagementService" Version="3.7.302.19" />
32+
<PackageReference Include="AWSSDK.KeyManagementService" Version="3.7.400.61" />
33+
<PackageReference Include="AWSSDK.SecurityToken" Version="3.7.401.10" />
3334
</ItemGroup>
3435

3536
<ItemGroup>

0 commit comments

Comments
 (0)