generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 18
feat: add KMS Discovery Keyring #324
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
175 changes: 175 additions & 0 deletions
175
src/main/java/software/amazon/encryption/s3/materials/KmsDiscoveryKeyring.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
package software.amazon.encryption.s3.materials; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration; | ||
import software.amazon.awssdk.core.ApiName; | ||
import software.amazon.awssdk.core.SdkBytes; | ||
import software.amazon.awssdk.services.kms.KmsClient; | ||
import software.amazon.awssdk.services.kms.model.DecryptRequest; | ||
import software.amazon.awssdk.services.kms.model.DecryptResponse; | ||
import software.amazon.awssdk.services.s3.model.GetObjectRequest; | ||
import software.amazon.encryption.s3.S3EncryptionClient; | ||
import software.amazon.encryption.s3.S3EncryptionClientException; | ||
import software.amazon.encryption.s3.internal.ApiNameVersion; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
public class KmsDiscoveryKeyring extends S3Keyring { | ||
private static final ApiName API_NAME = ApiNameVersion.apiNameWithVersion(); | ||
private static final String KEY_ID_CONTEXT_KEY = "kms_cmk_id"; | ||
|
||
private final KmsClient _kmsClient; | ||
|
||
private final Map<String, DecryptDataKeyStrategy> decryptDataKeyStrategies = new HashMap<>(); | ||
|
||
/** | ||
* This keyring will decrypt the object without specifying the KMS key | ||
* in its configuration. This is similar to the Encryption SDK's Discovery Keyring. | ||
* NOTE: There is no Discovery Filter, as kms+context mode used in v2/v3 does not persist the KMS key ID | ||
* to the object metadata, so it is not possible to safely filter on this attribute. | ||
* @param builder | ||
*/ | ||
public KmsDiscoveryKeyring(Builder builder) { | ||
super(builder); | ||
|
||
_kmsClient = builder._kmsClient; | ||
decryptDataKeyStrategies.put(_kmsDiscoveryStrategy.keyProviderInfo(), _kmsDiscoveryStrategy); | ||
decryptDataKeyStrategies.put(_kmsContextDiscoveryStrategy.keyProviderInfo(), _kmsContextDiscoveryStrategy); | ||
} | ||
|
||
/** | ||
* This DecryptDataKeyStrategy decrypts objects encrypted using the legacy kms v1 mode | ||
* using whichever key the object was encrypted with. | ||
*/ | ||
private final DecryptDataKeyStrategy _kmsDiscoveryStrategy = new DecryptDataKeyStrategy() { | ||
|
||
private static final String KEY_PROVIDER_INFO = "kms"; | ||
|
||
@Override | ||
public boolean isLegacy() { | ||
return true; | ||
} | ||
|
||
@Override | ||
public String keyProviderInfo() { | ||
return KEY_PROVIDER_INFO; | ||
} | ||
|
||
@Override | ||
public byte[] decryptDataKey(DecryptionMaterials materials, byte[] encryptedDataKey) { | ||
DecryptRequest request = DecryptRequest.builder() | ||
.encryptionContext(materials.encryptionContext()) | ||
.ciphertextBlob(SdkBytes.fromByteArray(encryptedDataKey)) | ||
.overrideConfiguration(builder -> builder.addApiName(API_NAME)) | ||
.build(); | ||
|
||
DecryptResponse response = _kmsClient.decrypt(request); | ||
return response.plaintext().asByteArray(); | ||
} | ||
}; | ||
|
||
/** | ||
* This DecryptDataKeyStrategy decrypts objects encrypted using the kms+context v2 mode | ||
* using whichever key the object was encrypted with. | ||
*/ | ||
private final DecryptDataKeyStrategy _kmsContextDiscoveryStrategy = new DecryptDataKeyStrategy() { | ||
|
||
private static final String KEY_PROVIDER_INFO = "kms+context"; | ||
private static final String ENCRYPTION_CONTEXT_ALGORITHM_KEY = "aws:x-amz-cek-alg"; | ||
|
||
@Override | ||
public boolean isLegacy() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public String keyProviderInfo() { | ||
return KEY_PROVIDER_INFO; | ||
} | ||
|
||
@Override | ||
public byte[] decryptDataKey(DecryptionMaterials materials, byte[] encryptedDataKey) { | ||
Map<String, String> requestEncryptionContext = new HashMap<>(); | ||
GetObjectRequest s3Request = materials.s3Request(); | ||
if (s3Request.overrideConfiguration().isPresent()) { | ||
AwsRequestOverrideConfiguration overrideConfig = s3Request.overrideConfiguration().get(); | ||
Optional<Map<String, String>> optEncryptionContext = overrideConfig | ||
.executionAttributes() | ||
.getOptionalAttribute(S3EncryptionClient.ENCRYPTION_CONTEXT); | ||
if (optEncryptionContext.isPresent()) { | ||
requestEncryptionContext = new HashMap<>(optEncryptionContext.get()); | ||
} | ||
} | ||
|
||
// We are validating the encryption context to match S3EC V2 behavior | ||
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. nit: Consider linking to said behavior 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. the code is in another repo, |
||
// Refer to KMSMaterialsHandler in the V2 client for details | ||
Map<String, String> materialsEncryptionContextCopy = new HashMap<>(materials.encryptionContext()); | ||
materialsEncryptionContextCopy.remove(KEY_ID_CONTEXT_KEY); | ||
materialsEncryptionContextCopy.remove(ENCRYPTION_CONTEXT_ALGORITHM_KEY); | ||
if (!materialsEncryptionContextCopy.equals(requestEncryptionContext)) { | ||
throw new S3EncryptionClientException("Provided encryption context does not match information retrieved from S3"); | ||
} | ||
|
||
DecryptRequest request = DecryptRequest.builder() | ||
.encryptionContext(materials.encryptionContext()) | ||
.ciphertextBlob(SdkBytes.fromByteArray(encryptedDataKey)) | ||
.overrideConfiguration(builder -> builder.addApiName(API_NAME)) | ||
.build(); | ||
|
||
DecryptResponse response = _kmsClient.decrypt(request); | ||
return response.plaintext().asByteArray(); | ||
} | ||
}; | ||
|
||
public static Builder builder() { | ||
return new Builder(); | ||
} | ||
|
||
@Override | ||
protected GenerateDataKeyStrategy generateDataKeyStrategy() { | ||
throw new S3EncryptionClientException("KmsDiscoveryKeyring does not support GenerateDataKey"); | ||
} | ||
|
||
@Override | ||
protected EncryptDataKeyStrategy encryptDataKeyStrategy() { | ||
throw new S3EncryptionClientException("KmsDiscoveryKeyring does not support EncryptDataKey"); | ||
} | ||
|
||
@Override | ||
protected Map<String, DecryptDataKeyStrategy> decryptDataKeyStrategies() { | ||
return decryptDataKeyStrategies; | ||
} | ||
|
||
public static class Builder extends S3Keyring.Builder<KmsDiscoveryKeyring, Builder> { | ||
private KmsClient _kmsClient; | ||
|
||
private Builder() { | ||
super(); | ||
} | ||
|
||
@Override | ||
protected Builder builder() { | ||
return this; | ||
} | ||
|
||
/** | ||
* Note that this does NOT create a defensive clone of KmsClient. Any modifications made to the wrapped | ||
* client will be reflected in this Builder. | ||
*/ | ||
@SuppressFBWarnings(value = "EI_EXPOSE_REP2", justification = "Pass mutability into wrapping client") | ||
public Builder kmsClient(KmsClient kmsClient) { | ||
_kmsClient = kmsClient; | ||
return this; | ||
} | ||
|
||
public KmsDiscoveryKeyring build() { | ||
if (_kmsClient == null) { | ||
_kmsClient = KmsClient.create(); | ||
} | ||
|
||
return new KmsDiscoveryKeyring(this); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
nit: Consider adding some words explaining each strategy; how are they different? Why is this one legacy?
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.
sure,
it maps to kms v1 and kms+context (v2/v3)