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

Add JWKS data source for fetching public keys for JWT validation #447

Merged
merged 7 commits into from
Apr 24, 2024

Conversation

clf-cklf
Copy link
Contributor

This adds a data provider for the JWKS public keys used for verifying workload identity JWTs. These are exposed at the /.well-known/jwks.json HTTP API endpoint.

One use for this would be to provide these keys to Vault via https://registry.terraform.io/providers/hashicorp/vault/latest/docs/resources/jwt_auth_backend#jwt_validation_pubkeys. This would be useful, because Vault cannot access the HTTPS API with mTLS enabled unless one turns off client verification: https://developer.hashicorp.com/nomad/docs/integrations/vault/acl#mutual-tls-in-nomad

The keys are returned as both a list of objects with human-readable field names, and as a list of PEM-encoded X.509 public keys; the latter is, I believe, the preferred way to pass PKI information in Terraform, and is the format that the Terraform Vault provider expects.

@hashicorp-cla
Copy link

hashicorp-cla commented Mar 28, 2024

CLA assistant check
All committers have signed the CLA.


func keyToPem(key Key) (string, error) {

// FIXME does Nomad always use RSA keys for JWKS?
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure about this, but I think Nomad only uses RSA for this?

Copy link
Contributor

Choose a reason for hiding this comment

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

In theory the root key can also use EdDSA, but I think for OIDC RSA is required.
https://github.com/hashicorp/nomad/blob/78f9f178670097eac9036c4bf83f7e0ceb55e6f3/nomad/encrypter.go#L499-L505

But we can probably remove this check altogether? It would also help future-proof this data source in case new algorithms are added later on.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I did a bit of investigation on this: to support EdDSA it looks like we would need to add crv and x fields for Keys, and then construct a ed25519 public key from them. We would still need the KeyType check to see which type of key to construct ("OKP" for EdDSA).

Also, as you pointed out, OIDC doesn't support non-RSA keys, so Nomad doesn't have code to make JWKs out of them. We would therefore need to construct a mocked-up key to test with.

I'm happy to do this, just want to make sure this sounds like worthwhile effort at this point. An alternative approach for future-proofing might be to ask for Nomad's ListKeys RPC to be make available via the HTTP API and fetch the keyset directly.

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, I see. I think we can just leave RSA support like it is then, maybe just update the comment to explain why only RSA is supported?

Supporting new key types seem like will require code changes here as well, so not a bit deal.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like you and @wraithm already added a comment - thank you! I pushed a quick fix to change the indentation to match the surrounding code, I think it looks good to go now?

nomad/data_source_jwks.go Outdated Show resolved Hide resolved
@wraithm
Copy link

wraithm commented Apr 1, 2024

What do you think about this @lgfa29 ?

Copy link
Contributor

@lgfa29 lgfa29 left a comment

Choose a reason for hiding this comment

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

Thank you for the very comprehensive PR @clintonford-btnl!

I left some small suggestions, but things are looking great 🙂


func keyToPem(key Key) (string, error) {

// FIXME does Nomad always use RSA keys for JWKS?
Copy link
Contributor

Choose a reason for hiding this comment

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

In theory the root key can also use EdDSA, but I think for OIDC RSA is required.
https://github.com/hashicorp/nomad/blob/78f9f178670097eac9036c4bf83f7e0ceb55e6f3/nomad/encrypter.go#L499-L505

But we can probably remove this check altogether? It would also help future-proof this data source in case new algorithms are added later on.

nomad/data_source_jwks.go Outdated Show resolved Hide resolved
@@ -163,6 +163,7 @@ func Provider() *schema.Provider {
"nomad_regions": dataSourceRegions(),
"nomad_volumes": dataSourceVolumes(),
"nomad_variable": dataSourceVariable(),
"nomad_jwks": dataSourceJWKS(),
Copy link
Contributor

Choose a reason for hiding this comment

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

We should keep the keys sorted alphabetically so it's easier to read them.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed - thank you!

website/docs/d/jwks.html.markdown Outdated Show resolved Hide resolved
@clf-cklf
Copy link
Contributor Author

clf-cklf commented Apr 4, 2024

Thanks @lgfa29 ! I'm on vacation this week but will be able to respond and implement your suggestions this coming Monday. Your comments all look good, no concerns from me at this point.

nomad/data_source_jwks.go Outdated Show resolved Hide resolved
Copy link
Contributor

@lgfa29 lgfa29 left a comment

Choose a reason for hiding this comment

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

Thank you for the updates @clf-cklf.

I no longer work at HashiCorp, so I can't get this PR merged, but I'm leaving my approval here to signal to the rest of the team that these changes look good 😄

@wraithm
Copy link

wraithm commented Apr 23, 2024

Thanks @lgfa29! Do you know who's taken over maintainership of this project?

@jrasell jrasell self-assigned this Apr 24, 2024
@jrasell jrasell self-requested a review April 24, 2024 07:03
Copy link
Member

@jrasell jrasell left a comment

Choose a reason for hiding this comment

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

LGTM, thanks @clf-cklf.

@jrasell jrasell merged commit b167023 into hashicorp:main Apr 24, 2024
1 check passed
@clf-cklf
Copy link
Contributor Author

Thank you for the updates @clf-cklf.

I no longer work at HashiCorp, so I can't get this PR merged, but I'm leaving my approval here to signal to the rest of the team that these changes look good 😄

Thanks @lgfa29 ! Best wishes for your next role!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants