-
Notifications
You must be signed in to change notification settings - Fork 2.3k
added changes to consumer grants and source details in crypto-kms plugin. #20114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -12,7 +12,11 @@ | |||||||||||||||||||||||||||||||||||||||||
| import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider; | ||||||||||||||||||||||||||||||||||||||||||
| import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration; | ||||||||||||||||||||||||||||||||||||||||||
| import software.amazon.awssdk.core.exception.SdkException; | ||||||||||||||||||||||||||||||||||||||||||
| import software.amazon.awssdk.core.interceptor.Context; | ||||||||||||||||||||||||||||||||||||||||||
| import software.amazon.awssdk.core.interceptor.ExecutionAttributes; | ||||||||||||||||||||||||||||||||||||||||||
| import software.amazon.awssdk.core.interceptor.ExecutionInterceptor; | ||||||||||||||||||||||||||||||||||||||||||
| import software.amazon.awssdk.core.retry.RetryPolicy; | ||||||||||||||||||||||||||||||||||||||||||
| import software.amazon.awssdk.http.SdkHttpRequest; | ||||||||||||||||||||||||||||||||||||||||||
| import software.amazon.awssdk.http.apache.ApacheHttpClient; | ||||||||||||||||||||||||||||||||||||||||||
| import software.amazon.awssdk.http.apache.ProxyConfiguration; | ||||||||||||||||||||||||||||||||||||||||||
| import software.amazon.awssdk.profiles.ProfileFileSystemSetting; | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -36,7 +40,9 @@ | |||||||||||||||||||||||||||||||||||||||||
| import java.net.URISyntaxException; | ||||||||||||||||||||||||||||||||||||||||||
| import java.time.Duration; | ||||||||||||||||||||||||||||||||||||||||||
| import java.util.Arrays; | ||||||||||||||||||||||||||||||||||||||||||
| import java.util.Collections; | ||||||||||||||||||||||||||||||||||||||||||
| import java.util.HashMap; | ||||||||||||||||||||||||||||||||||||||||||
| import java.util.List; | ||||||||||||||||||||||||||||||||||||||||||
| import java.util.Map; | ||||||||||||||||||||||||||||||||||||||||||
| import java.util.stream.Collectors; | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -54,6 +60,8 @@ public class KmsService implements Closeable { | |||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| static final Setting<String> KEY_ARN_SETTING = Setting.simpleString("kms.key_arn", Setting.Property.NodeScope); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| static final Setting<String> GRANT_TOKENS_SETTING = Setting.simpleString("kms.grant_tokens", Setting.Property.NodeScope); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| private volatile Map<KmsClientSettings, AmazonKmsClientReference> clientsCache = emptyMap(); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -73,7 +81,7 @@ public KmsService() { | |||||||||||||||||||||||||||||||||||||||||
| private KmsClient buildClient(KmsClientSettings clientSettings) { | ||||||||||||||||||||||||||||||||||||||||||
| AccessController.doPrivileged(KmsService::setDefaultAwsProfilePath); | ||||||||||||||||||||||||||||||||||||||||||
| final AwsCredentialsProvider awsCredentialsProvider = buildCredentials(clientSettings); | ||||||||||||||||||||||||||||||||||||||||||
| final ClientOverrideConfiguration overrideConfiguration = buildOverrideConfiguration(); | ||||||||||||||||||||||||||||||||||||||||||
| final ClientOverrideConfiguration overrideConfiguration = buildOverrideConfiguration(clientSettings); | ||||||||||||||||||||||||||||||||||||||||||
| final ProxyConfiguration proxyConfiguration = AccessController.doPrivileged(() -> buildProxyConfiguration(clientSettings)); | ||||||||||||||||||||||||||||||||||||||||||
| return buildClient( | ||||||||||||||||||||||||||||||||||||||||||
| awsCredentialsProvider, | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -133,6 +141,24 @@ ProxyConfiguration buildProxyConfiguration(KmsClientSettings clientSettings) { | |||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ClientOverrideConfiguration buildOverrideConfiguration(KmsClientSettings clientSettings) { | ||||||||||||||||||||||||||||||||||||||||||
| ClientOverrideConfiguration.Builder builder = ClientOverrideConfiguration.builder().retryPolicy(buildRetryPolicy()); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| if (Strings.hasText(clientSettings.sourceContext)) { | ||||||||||||||||||||||||||||||||||||||||||
| Map<String, String> sourceHeaders = parseKeyValuePairs(clientSettings.sourceContext, "source_context"); | ||||||||||||||||||||||||||||||||||||||||||
| builder.addExecutionInterceptor(new ExecutionInterceptor() { | ||||||||||||||||||||||||||||||||||||||||||
| @Override | ||||||||||||||||||||||||||||||||||||||||||
| public SdkHttpRequest modifyHttpRequest(Context.ModifyHttpRequest context, ExecutionAttributes executionAttributes) { | ||||||||||||||||||||||||||||||||||||||||||
| SdkHttpRequest.Builder requestBuilder = context.httpRequest().toBuilder(); | ||||||||||||||||||||||||||||||||||||||||||
| sourceHeaders.forEach((key, value) -> { requestBuilder.putHeader(key, value); }); | ||||||||||||||||||||||||||||||||||||||||||
| return requestBuilder.build(); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return builder.build(); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| ClientOverrideConfiguration buildOverrideConfiguration() { | ||||||||||||||||||||||||||||||||||||||||||
| return ClientOverrideConfiguration.builder().retryPolicy(buildRetryPolicy()).build(); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -252,22 +278,28 @@ public MasterKeyProvider createMasterKeyProvider(CryptoMetadata cryptoMetadata) | |||||||||||||||||||||||||||||||||||||||||
| String kmsEncCtx = ENC_CTX_SETTING.get(cryptoSettings); | ||||||||||||||||||||||||||||||||||||||||||
| Map<String, String> encCtx; | ||||||||||||||||||||||||||||||||||||||||||
| if (Strings.hasText(kmsEncCtx)) { | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| encCtx = Arrays.stream(kmsEncCtx.split(",")) | ||||||||||||||||||||||||||||||||||||||||||
| .map(s -> s.split("=")) | ||||||||||||||||||||||||||||||||||||||||||
| .collect(Collectors.toMap(e -> e[0].trim(), e -> e[1].trim())); | ||||||||||||||||||||||||||||||||||||||||||
| } catch (Exception ex) { | ||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalArgumentException(ENC_CTX_SETTING.getKey() + " Format should be: Name1=Value1, Name2=Value2"); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| encCtx = parseKeyValuePairs(kmsEncCtx, ENC_CTX_SETTING.getKey()); | ||||||||||||||||||||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||||||||||||||||||||
| encCtx = new HashMap<>(); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Extract grant details | ||||||||||||||||||||||||||||||||||||||||||
| String grantTokensStr = GRANT_TOKENS_SETTING.get(cryptoSettings); | ||||||||||||||||||||||||||||||||||||||||||
| List<String> grantTokens = Strings.hasText(grantTokensStr) ? Arrays.asList(grantTokensStr.split(",")) : Collections.emptyList(); | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| // Verify client creation is successful to early detect any failure. | ||||||||||||||||||||||||||||||||||||||||||
| try (AmazonKmsClientReference clientReference = client(cryptoMetadata)) { | ||||||||||||||||||||||||||||||||||||||||||
| clientReference.get(); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| return new KmsMasterKeyProvider(encCtx, keyArn, () -> client(cryptoMetadata)); | ||||||||||||||||||||||||||||||||||||||||||
| return new KmsMasterKeyProvider(encCtx, keyArn, grantTokens, () -> client(cryptoMetadata)); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||
| private static Map<String, String> parseKeyValuePairs(String input, String settingName) { | ||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||
| return Arrays.stream(input.split(",")).map(s -> s.split("=")).collect(Collectors.toMap(e -> e[0].trim(), e -> e[1].trim())); | ||||||||||||||||||||||||||||||||||||||||||
| } catch (Exception ex) { | ||||||||||||||||||||||||||||||||||||||||||
| throw new IllegalArgumentException(settingName + " Format should be: Name1=Value1, Name2=Value2"); | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+298
to
304
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
If the input contains an entry without an private static Map<String, String> parseKeyValuePairs(String input, String settingName) {
- try {
- return Arrays.stream(input.split(",")).map(s -> s.split("=")).collect(Collectors.toMap(e -> e[0].trim(), e -> e[1].trim()));
- } catch (Exception ex) {
- throw new IllegalArgumentException(settingName + " Format should be: Name1=Value1, Name2=Value2");
- }
+ Map<String, String> result = new HashMap<>();
+ for (String pair : input.split(",")) {
+ String[] keyValue = pair.split("=", 2);
+ if (keyValue.length != 2) {
+ throw new IllegalArgumentException(
+ settingName + " contains invalid entry '" + pair.trim() + "'. Format should be: Name1=Value1,Name2=Value2"
+ );
+ }
+ result.put(keyValue[0].trim(), keyValue[1].trim());
+ }
+ return result;
}Using 📝 Committable suggestion
Suggested change
🤖 Prompt for AI Agents |
||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Grant tokens are not trimmed after splitting.
Unlike
parseKeyValuePairswhich trims whitespace, splitting grant tokens by comma retains any surrounding whitespace (e.g.,"grant1, grant2"yields[" grant2"]). The testing logs show spaces:[grant1, grant2, grant3].📝 Committable suggestion
🤖 Prompt for AI Agents