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

fix(iam): oidc provider fetches leaf certificate thumbprint instead of root #22802

Merged
merged 40 commits into from
Nov 12, 2022

Conversation

iliapolo
Copy link
Contributor

@iliapolo iliapolo commented Nov 6, 2022

Currently, the implementation of the OIDC provider custom resource fetches the certificate chain using:

let cert = socket.getPeerCertificate(true);

It then extracts the root certificate by detecting a circular reference in the cert.issuerCertificate property.

} while ( cert && typeof cert === 'object' && !unqiueCerts.has(cert));

As it turns out, this results in the wrong certificate being extracted. I observed this while running an EKS integration test.

The current certificate thumbprint that is extracted is: AD7E1C28B064EF8F6003402014C3D0E3370EB58A.
While the expected thumbprint is: 9E99A48A9960B14926BB7F3B02E22DA2B0AB7280. (this is the value we used to hardcode)

The recommended way for extracting the correct thumbprint according to AWS is using openssl s_client -showcerts. When I tried this, I did in fact see that the last certificate returned by this command has the correct thumbprint.

During investigation, I noticed that socket.getPeerCertificate(true) returns another additional certificate, acting as the root one. This is aligned with what the comment made in the original issue. This additional certificate is not the correct one, and we should be using the one just before it in the chain. There is however no way to detect that "second to last" certificate in this way, because it doesn't resolve to a circular reference.

After some digging, I switched the implementation to use socket.getPeerX509Certificate(), a new method that only exists in Node16. This method skips over the incorrect certificate, and results in the correct thumbprint.

Screen Shot 2022-11-06 at 10 04 51 PM

Fixes #8607


All Submissions:

Adding new Unconventional Dependencies:

  • This PR adds new unconventional dependencies following the process described here

New Features

  • Have you added the new feature to an integration test?
    • Did you use yarn integ to deploy the infrastructure and generate the snapshot (i.e. yarn integ without --dry-run)?

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license

@gitpod-io
Copy link

gitpod-io bot commented Nov 6, 2022

@github-actions github-actions bot added the p2 label Nov 6, 2022
@aws-cdk-automation aws-cdk-automation requested a review from a team November 6, 2022 21:18
@mergify mergify bot added the contribution/core This is a PR that came from AWS. label Nov 6, 2022
@github-actions github-actions bot added bug This issue is a bug. effort/medium Medium work item – several days of effort labels Nov 6, 2022
Copy link
Collaborator

@aws-cdk-automation aws-cdk-automation left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pull request linter has failed. See the aws-cdk-automation comment below for failure reasons. If you believe this pull request should receive an exemption, please comment and provide a justification.

@iliapolo iliapolo added p0 pr-linter/exempt-integ-test The PR linter will not require integ test changes and removed p2 labels Nov 6, 2022
@aws-cdk-automation aws-cdk-automation dismissed their stale review November 6, 2022 23:11

✅ Updated pull request passes all PRLinter validations. Dissmissing previous PRLinter review.

mergify bot pushed a commit that referenced this pull request Nov 7, 2022
@iliapolo iliapolo marked this pull request as ready for review November 10, 2022 11:33
@vinayak-kukreja vinayak-kukreja added the pr/do-not-merge This PR should not be merged at this time. label Nov 10, 2022
@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 5f6a5d6
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@iliapolo iliapolo changed the title fix(iam): oidc provider fetches wrong thumbprint fix(iam): oidc provider fetches leaf certificate thumbprint instead of root Nov 11, 2022
@iliapolo iliapolo removed the pr/do-not-merge This PR should not be merged at this time. label Nov 12, 2022
@mergify
Copy link
Contributor

mergify bot commented Nov 12, 2022

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@mergify mergify bot merged commit 280b876 into main Nov 12, 2022
@mergify mergify bot deleted the epolon/oidc-x509 branch November 12, 2022 22:49
iliapolo added a commit that referenced this pull request Nov 14, 2022
…f root (#22802)

Currently, the implementation of the OIDC provider custom resource fetches the certificate chain using:

https://github.com/aws/aws-cdk/blob/9bde9f3149cbfa6e7b97204f54e7cef5c9127971/packages/%40aws-cdk/aws-iam/lib/oidc-provider/external.ts#L40

It then extracts the root certificate by detecting a circular reference in the `cert.issuerCertificate` property.

https://github.com/aws/aws-cdk/blob/9bde9f3149cbfa6e7b97204f54e7cef5c9127971/packages/%40aws-cdk/aws-iam/lib/oidc-provider/external.ts#L46

As it turns out, this results in the wrong certificate being extracted. I observed this while running an [EKS integration test](https://github.com/aws/aws-cdk/blob/main/packages/%40aws-cdk/aws-eks/test/integ.eks-service-account-sdk-call.ts).

The current certificate thumbprint that is extracted is: `AD7E1C28B064EF8F6003402014C3D0E3370EB58A`.
While the expected thumbprint is: `9E99A48A9960B14926BB7F3B02E22DA2B0AB7280`. (this is the value we used to [hardcode](https://github.com/aws/aws-cdk/blob/v2.50.0/packages/%40aws-cdk/aws-eks/lib/oidc-provider.ts#L49))

The [recommended way](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_create_oidc_verify-thumbprint.html) for extracting the correct thumbprint according to AWS is using `openssl s_client -showcerts`. When I tried this, I did in fact see that the last certificate returned by this command has the correct thumbprint.

During investigation, I noticed that `socket.getPeerCertificate(true)` returns another additional certificate, acting as the root one. This is aligned with what the [comment](#8607 (comment)) made in the original issue. This additional certificate is not the correct one, and we should be using the one just before it in the chain. There is however no way to detect that "second to last" certificate in this way, because it doesn't resolve to a circular reference.

After some digging, I switched the implementation to use `socket.getPeerX509Certificate()`, a new method that only exists in Node16. This method skips over the incorrect certificate, and results in the correct thumbprint.

<img width="539" alt="Screen Shot 2022-11-06 at 10 04 51 PM" src="https://user-images.githubusercontent.com/1428812/200195623-6735377b-a82f-472f-884d-7bec450c32c6.png">

Fixes #8607

----

* [X] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
mergify bot pushed a commit that referenced this pull request Nov 16, 2022
…f root (#22924)

Backports #22802 and #22608 to v1.

----

### All Submissions:

* [x] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Unconventional Dependencies:

* [ ] This PR adds new unconventional dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-new-unconventional-dependencies)

### New Features

* [x] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [x] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@PeterBaker0
Copy link

Thanks @iliapolo - working well in our deployment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue is a bug. contribution/core This is a PR that came from AWS. effort/medium Medium work item – several days of effort p0 pr-linter/exempt-integ-test The PR linter will not require integ test changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

‼️ (iam): OpenIdConnectProvider defaults to first thumbprint instead of root CA thumbprint
8 participants