Skip to content

feat: add azure oidc PKI auth instead of client secret #9054

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 15 commits into from
Aug 14, 2023

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Aug 11, 2023

Closes #7266

What this does

This implements Azure's Certificate Credentials for OIDC providers. This uses a client certificate and key instead of client_secret for oauth2 auth.

To use

--oidc-issuer-url="<issuer>" --oidc-client-id="<client_id>" --oidc-client-cert-file=<file.crt> --oidc-client-key-file=<file.key>

There implementation is a super-set of the standard. So this might work for other providers, however I am sure they probably all have their own unique quirks. So I am not going to try and make this compatible with other providers until we get those requests.

Implementation

I just wrote a wrapper for the existing oauth2 config. So the oauth flow remains the same, I just add to the oauth2 context on the Exchange call:

opts = append(opts,
oauth2.SetAuthURLParam("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"),
oauth2.SetAuthURLParam("client_assertion", signed),
)

For the TokenSource, I had to write my own. I used the internal one in the oauth2 package as a guide:

func (src *jwtTokenSource) Token() (*oauth2.Token, error) {

Testing

The unit tests only focus on asserting the client requests to the IDP. I do not attempt to actually implement some fake IDP.

The first is TestAzureADPKIOIDC which tests the Exchange method adds the correct client_assertion for the PKI auth.

The second is a more e2e test for the oauth2 flow, TestSavedAzureADPKIOIDC. It mocks out all the responses by making a fake http.Client with a transport. I did this because I wanted to use a real oauth2.Config to actually invoke the right code paths. The oauth2.Config is what sets the auth payload in the header or the params. The existing fake oidc code does not use the oauth2 package, so it didn't allow me to assert the proper request payloads.

I used actual response from a real Azure AD instance, and stubbed out some of the JWTs that do not do anything. This means I can test both the initial Exchange and the token refresh and assert the actual http.Requests sent have the right authoriazation fields.

Errors

If you provide a key + cert pair that is invalid:

{"message":"Internal error exchanging Oauth code.","detail":"oauth2: \"invalid_client\" \"AADSTS700027: The certificate with identifier used to sign the client assertion is not registered on application. [Reason - The key was not found., Thumbprint of key used by client: 'B847C7861E8C8F123E3176C95EBD71CA0AD0AB71', Please visit the Azure Portal, Graph Explorer or directly use MS Graph to see configured keys for app Id '<app_id>'. Review the documentation at https://docs.microsoft.com/en-us/graph/deployments to determine the corresponding service endpoint and https://docs.microsoft.com/en-us/graph/api/application-get?view=graph-rest-1.0\u0026tabs=http to build a query request URL, such as 'https://graph.microsoft.com/beta/applications/<app_id>'].\\r\\nTrace ID: 2960d3ed-7bc1-4c78-8590-39bf0d5b6300\\r\\nCorrelation ID: <id>\\r\\nTimestamp: 2023-08-11 17:53:01Z\" \"https://login.microsoftonline.com/error?code=700027\""}

@Emyrk Emyrk marked this pull request as draft August 11, 2023 18:52
@Emyrk Emyrk marked this pull request as ready for review August 11, 2023 23:08
@Emyrk Emyrk requested a review from kylecarbs August 11, 2023 23:09
@Emyrk Emyrk merged commit 25ce30d into main Aug 14, 2023
@Emyrk Emyrk deleted the stevenmasley/pki_auth_assert branch August 14, 2023 22:33
@github-actions github-actions bot locked and limited conversation to collaborators Aug 14, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Feat: Enable OIDC authentication with PKI
2 participants