Skip to content
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

(custom resources): Can not get public key for a KMS key #19065

Open
sashee opened this issue Feb 21, 2022 · 19 comments
Open

(custom resources): Can not get public key for a KMS key #19065

sashee opened this issue Feb 21, 2022 · 19 comments
Labels
@aws-cdk/aws-kms Related to AWS Key Management bug This issue is a bug. p1 service-api This issue is due to a problem in a service API

Comments

@sashee
Copy link

sashee commented Feb 21, 2022

What is the problem?

I tried to extract the public key for an asymmetric KMS key but I get a Response is not valid JSON error.

Reproduction Steps

const key = new aws_kms.Key(
  this,
  "key",
  {
    keySpec: aws_kms.KeySpec.ECC_NIST_P384,
    keyUsage: aws_kms.KeyUsage.SIGN_VERIFY,
  }
);
const publicKey = new custom_resources.AwsCustomResource(
  this,
  "publicKey",
  {
    onCreate: {
      service: "KMS",
      action: "getPublicKey",
      parameters: {
        KeyId: key.keyArn,
      },
      physicalResourceId: custom_resources.PhysicalResourceId.of(key.keyArn),
      outputPaths: ['PublicKey'],
    },
    policy: custom_resources.AwsCustomResourcePolicy.fromSdkCalls({
      resources: custom_resources.AwsCustomResourcePolicy.ANY_RESOURCE
    }),
  }
);

What did you expect to happen?

I expected that the stack deploys and I can extract the public key.

What actually happened?

Response is not valid JSON

In the CloudWatch Logs, I see that the PublicKey is retrieved, but it seems in a unusable format:

image

CDK CLI Version

2.12.0

Framework Version

No response

Node.js Version

v16.14.0

OS

Ubuntu

Language

Typescript

Language Version

No response

Other information

No response

@sashee sashee added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 21, 2022
@github-actions github-actions bot added the @aws-cdk/aws-kms Related to AWS Key Management label Feb 21, 2022
@ryparker ryparker added the p1 label Feb 21, 2022
@lacteolus
Copy link

The same issue with CDK 2.20.0.
Tried to create our own custom resource backed by custom lambda to get public key using boto3 but faced the problem described in aws-samples/aws-cdk-examples#641

@peterwoodworth
Copy link
Contributor

I wonder if this is because the response is a blob?

@peterwoodworth peterwoodworth removed the needs-triage This issue or PR still needs to be triaged. label Apr 20, 2022
@skinny85
Copy link
Contributor

skinny85 commented May 5, 2022

Unfortunately, I'm not sure CDK can do much here - we don't really control wha the response from the services is.

@skinny85 skinny85 added guidance Question that needs advice or information. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. service-api This issue is due to a problem in a service API and removed bug This issue is a bug. p1 labels May 5, 2022
@skinny85 skinny85 removed their assignment May 5, 2022
@github-actions
Copy link

github-actions bot commented May 7, 2022

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

@github-actions github-actions bot added the closing-soon This issue will automatically close in 4 days unless further comments are made. label May 7, 2022
@stefanopallicca-imagicle

Unfortunately, I'm not sure CDK can do much here - we don't really control wha the response from the services is.

Maybe it's a matter of reformatting (I'm sorry if the word is not the proper one) the command output to a sort of human readable?

After all, the aws kms get-public-key cli command output has the expected format:

image

@github-actions github-actions bot removed closing-soon This issue will automatically close in 4 days unless further comments are made. response-requested Waiting on additional info and feedback. Will move to "closing-soon" in 7 days. labels May 9, 2022
@skinny85
Copy link
Contributor

skinny85 commented May 9, 2022

The fact that you get that error message might also suggest that the response was an "Access Denied", which is not a JSON object.

Can you try temporarly giving the Custom Resource admin permissions, and see if that changes anything? (I guess also allow all principals from the account "kms:*" on the Key, just in case)

@stefanopallicca-imagicle

@skinny85 thanks for the answer. I've tried what you asked for, but still receiving Response is not valid JSON

@skinny85
Copy link
Contributor

Hmm, I'm kind of lost then.

Maybe I would try to do a call using the JavaScript AWS SDK, and see what response it gives me there perhaps...?

@johannes-idealayer
Copy link

The SDK returns this object. Of particular interest is PublicKey, which is a Uint8Array (i.e. it's not a base64-encoded string).

@skinny85
Copy link
Contributor

Interesting! That's probably the source of the Response is not valid JSON error.

@johannes-idealayer
Copy link

johannes-idealayer commented May 13, 2022

Edit: This comment is in response to a now-deleted comment pointing out this line as a potential cause for the error:

const childKey = Buffer.isBuffer(child[key]) ? child[key].toString('utf8') : child[key];

I don't think this is what's causing the "not valid JSON" error, but I'd like to point out that decoding an arbitrary buffer with .toString('utf8') is potentially a lossy conversion. The code will try to decode the buffer as UTF-8, and any part of it that is not valid UTF-8 will be replaced by the Unicode replacement character � (U+FFFD), making it impossible to get the original buffer.

@peterwoodworth peterwoodworth added bug This issue is a bug. p1 and removed guidance Question that needs advice or information. labels Jun 9, 2022
@dmitridr
Copy link

dmitridr commented Aug 11, 2022

Had the same issue as everyone here.

The line @johannes-sscrc linked does seem to be related to the error. Indeed, Publickey seems to be in a binary format: DER, and the proper way to encode it appears to be base64 and not utf8.

Thus, a quick fix for me was to replace the above line with:

const childKey = Buffer.isBuffer(child[key]) ? child[key].toString('base64') : child[key];

Then the PublicKey is returned in base64, and there's no error anymore. Generally, it would be good to have a hook/param here to allow the caller to specify how to encode the buffer for specific keys, in order to unblock such issues in the general case.

@l-irizarry
Copy link

@sashee did you end up finding a work-around for getting the public key of a KMS key using a custom resource? I'm getting the same invalid JSON error.

@sashee
Copy link
Author

sashee commented Nov 29, 2022

@synthetic-luis , yeah, a custom resource can fetch the public key and output it for other resources. Unfortunately, I can't provide code example as it was done for a client, but the implementation was straightforward.

@l-irizarry
Copy link

@sashee thanks for the response. Can you please elaborate on how you then overcame the invalid JSON issue (in general terms) as it relates to the code you posted above? For example, did you have to change your code above (e.g. add a new parameter to the getPublicKey call?)

@sashee
Copy link
Author

sashee commented Nov 29, 2022

@synthetic-luis , I just checked the code and I remember wrongly. So I needed a keypair for IVS playback key and I thought to use KMS for that. It did not work (partly because of the issue here) so I implemented a lambda function that generates the key. Not the ideal solution, but it works reliably so far.

@l-irizarry
Copy link

@sashee this helps a lot. Thank you!

@DanielLaberge
Copy link

Would love to have a fix or workaround for this.

@l-irizarry
Copy link

@DanielLaberge if it helps, what worked for me was creating a post deploy script that uses the KMS SDK to do what I needed to do

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-kms Related to AWS Key Management bug This issue is a bug. p1 service-api This issue is due to a problem in a service API
Projects
None yet
Development

Successfully merging a pull request may close this issue.