aws-sdk-java icon indicating copy to clipboard operation
aws-sdk-java copied to clipboard

KMS: Creating GrantRequest wit GrantConstrains fails

Open neoecos opened this issue 11 years ago • 9 comments

When creating a grantRequest with a grant subset constraint fails.

The error:

More than one constraint supplied (Service: AWSKMS; Status Code: 400; Error Code: ValidationException; Request ID: b2b4d06d-84a2-11e4-9a58-5523b2dae570)

The bug: When creating a new GrantConstraints() object creates two constraints lists, one for equals constraints and other for subset constraints, even though I'm just using subset constraints.

request.toString() example:

{
  KeyId: 531bb400-fd88-44fd-9999-6d2fc2a1000,
  GranteePrincipal: arn:aws:iam::337568200000:user/development,
  Operations: [Encrypt, Decrypt],
  Constraints: {
    EncryptionContextSubset: {contextSubset1=p9nfeie5askl3laeb6j208mt3b0rrl2v, environment=development},
    EncryptionContextEquals: {}  //Added by default by GrantConstraints constuctor.
  },
  GrantTokens: []
}

@Transactional(propagation=Propagation.MANDATORY)
    def getEncryptDecryptGrantTokenForContext(def context){
        def request = new CreateGrantRequest().withKeyId(awsKmsKeyId).withGranteePrincipal(userArn)
        request.setConstraints(getGrantConstrainsSubsetForAdditionalContext(context))
        request.setOperations([GrantOperation.Encrypt.toString(), GrantOperation.Decrypt.toString()])
        kmsClient.createGrant(request).getGrantToken()
    }

    @Transactional(propagation=Propagation.MANDATORY)
    def GrantConstraints getGrantConstrainsSubsetForAdditionalContext(def additionalContext){
        def constraints = new GrantConstraints()
        for (Map.Entry context: (awsKmsDefaultContext + additionalContext).entrySet()) {
            constraints.addEncryptionContextSubsetEntry(context.getKey().toString(), context.getValue().toString())
        }
    }

neoecos avatar Dec 15 '14 21:12 neoecos

I reproduced the bug using the aws cli, y removed the EncryptionContextEquals param from the request and worked.

neoecos avatar Dec 15 '14 22:12 neoecos

I was digging in the problem, and in the CreateGrantRequestMarshaller.java:93 the validation is only on null and not the size of the Map, also the GrantConstraints.getEncryptionContext{Subset,Equals} will initialize a new Empty HashMap if given constraints maps are null.

One simple way to fix this bug is to add a length validation in the marshaller lines 93 and 105.

neoecos avatar Dec 15 '14 22:12 neoecos

According to the documentation, it should support both entries, so maybe is service implementation issue and not a issue of the client libraries.

{
"Constraints": {
"EncryptionContextEquals":
{
"string" :
"string"
},
"EncryptionContextSubset":
{
"string" :
"string"
}
},
"GrantTokens": [
"string"
],
"GranteePrincipal": "string",
"KeyId": "string",
"Operations": [
"string"
],
"RetiringPrincipal": "string"
}

neoecos avatar Dec 15 '14 23:12 neoecos

According to the AWS KMS service team, "You are only allowed to specify one constraint type. The other must be null."

Hope this helps.

hansonchar avatar Dec 16 '14 22:12 hansonchar

Hi @hansonchar , so the library is wrong in the implementation because the marshalling adds an empty equals and subset constraints always, using {}, not a null value.

neoecos avatar Dec 17 '14 15:12 neoecos

For what it's worth, here's the workaround I've used:

GrantConstraints constraints = new GrantConstraints() {
    @Override
    public Map<String, String> getEncryptionContextEquals() {
        return null;
    }
}.addEncryptionContextSubsetEntry("test-key", "test-value");

ajfitzpatrick avatar Dec 17 '14 22:12 ajfitzpatrick

Clever :) We are looking into modifying the code generator so the Marshaller will do the right thing.

hansonchar avatar Dec 17 '14 22:12 hansonchar

@hansonchar Thanks for help. Rigth now i'm working with the modified version in my repo for the kms artifact.

neoecos avatar Dec 17 '14 23:12 neoecos

The fix to exclude empty map in the marshalled request (to AWS KMS) will be available in the upcoming release. Resolving this for now.

hansonchar avatar Dec 18 '14 17:12 hansonchar