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

Use azidentity instead of ADAL #2748

Merged
merged 1 commit into from
Dec 15, 2022

Conversation

r4f4
Copy link
Contributor

@r4f4 r4f4 commented Oct 21, 2022

ADAL is being deprecated, so use azidentity with an adapter so the new authentication can work with v1 SDK clients. The only impact for users is to update the environment variables used for certificate authentication, from AZURE_CERTIFICATE_ to AZURE_CLIENT_CERTIFICATE_.

Signed-off-by: Rafael Fonseca [email protected]

What type of PR is this?
/kind other

What this PR does / why we need it: ADAL is being deprecated, so use azidentity with an adapter so the new authentication can work with v1 SDK clients.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

Please confirm that if this PR changes any image versions, then that's the sole change this PR makes.

TODOs:

  • squashed commits
  • includes documentation
  • adds unit tests

Release note:

Replace deprecated ADAL authentication with MSAL.
action required: for users relying on certificate authentication, the environment variables used by the libraries changed from AZURE_CERTIFICATE_ to AZURE_CLIENT_CERTIFICATE_

@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Oct 21, 2022
@k8s-ci-robot
Copy link
Contributor

@r4f4: The label(s) kind/other cannot be applied, because the repository doesn't have them.

In response to this:

ADAL is being deprecated, so use azidentity with an adapter so the new authentication can work with v1 SDK clients.

Signed-off-by: Rafael Fonseca [email protected]

What type of PR is this?
/kind other

What this PR does / why we need it: ADAL is being deprecated, so use azidentity with an adapter so the new authentication can work with v1 SDK clients.

Which issue(s) this PR fixes (optional, in fixes #<issue number>(, fixes #<issue_number>, ...) format, will close the issue(s) when PR gets merged):
Fixes #

Special notes for your reviewer:

Please confirm that if this PR changes any image versions, then that's the sole change this PR makes.

TODOs:

  • squashed commits
  • includes documentation
  • adds unit tests

Release note:


Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@k8s-ci-robot k8s-ci-robot added do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. labels Oct 21, 2022
@k8s-ci-robot
Copy link
Contributor

Welcome @r4f4!

It looks like this is your first PR to kubernetes-sigs/cluster-api-provider-azure 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/cluster-api-provider-azure has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 21, 2022
@k8s-ci-robot
Copy link
Contributor

Hi @r4f4. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository.

@r4f4
Copy link
Contributor Author

r4f4 commented Oct 24, 2022

/release-note-none

@k8s-ci-robot k8s-ci-robot added release-note-none Denotes a PR that doesn't merit a release note. and removed do-not-merge/release-note-label-needed Indicates that a PR should not merge because it's missing one of the release note labels. labels Oct 24, 2022
@mboersma
Copy link
Contributor

/ok-to-test

@k8s-ci-robot k8s-ci-robot added ok-to-test Indicates a non-member PR verified by an org member that is safe to test. and removed needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. labels Oct 28, 2022
"github.com/Azure/go-autorest/autorest"
"github.com/Azure/go-autorest/autorest/adal"
"github.com/jongio/azidext/go/azidext"
Copy link
Contributor

Choose a reason for hiding this comment

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

what is this repo? it seems that it's in someone's personal GitHub org?

Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's what I did. See the section https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/azidentity/MIGRATION.md#use-azidentity-credentials-with-older-packages where they recommend jongio's repo for getting an adapter so the new authorizer can work with older SDK clients.

Copy link
Contributor

Choose a reason for hiding this comment

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

Got it, thanks for clarifying

@sonasingh46
Copy link
Contributor

This would be nice to get in. I have mentioned about this in workload identity proposal as a future work -- but prior work is fine.
#2814

With azure workload identity, it seem we have to have use azidentity.

@@ -136,44 +139,50 @@ func (p *ManagedControlPlaneCredentialsProvider) GetAuthorizer(ctx context.Conte

// GetAuthorizer returns an Azure authorizer based on the provided azure identity and cluster metadata.
func (p *AzureCredentialsProvider) GetAuthorizer(ctx context.Context, resourceManagerEndpoint, activeDirectoryEndpoint string, clusterMeta metav1.ObjectMeta) (autorest.Authorizer, error) {
var spt *adal.ServicePrincipalToken
var authErr error
var cred azcore.TokenCredential
Copy link
Contributor

Choose a reason for hiding this comment

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

Any particular reason for declaring these variables explicitly vs using shorthand in the switch case?

e.g

cred, err := azidentity.NewManagedIdentityCredential(&options)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. If I don't do this then I can't use them on lines https://github.com/kubernetes-sigs/cluster-api-provider-azure/pull/2748/files#diff-b6ab207506064afcbd1d777c1c7296e2953d2653a8843bfb39107af5cf5b0a87R174-R177.

The alternative would be to duplicate those lines in each case branch, which I tried to avoid.

Comment on lines 182 to 189
if len(endpoint) > 0 && endpoint[len(endpoint)-1] != '/' {
endpoint += "/"
}
Copy link
Member

Choose a reason for hiding this comment

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

Why is this additional / added here? resourceManagerEndpoint in the GetAuthorizer call should already have the exact url that the token needs to be requested for? FYI, adding an extra slash or removing an existing one can render the token unusable because the resource identifier has to be an exact match.

xref: https://github.com/Azure/azure-workload-identity/blob/main/pkg/proxy/proxy.go#L194-L203

Copy link
Member

Choose a reason for hiding this comment

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

I was just looking at #2814 and found a ref to this, so figured i'll review it. I didn't mean to lurk 😆

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for the review. I've changed the code to be similar to the one you linked.

@k8s-ci-robot k8s-ci-robot added size/M Denotes a PR that changes 30-99 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Nov 17, 2022
@sonasingh46
Copy link
Contributor

/retest

@sonasingh46
Copy link
Contributor

@r4f4 -- The PR is still titled WIP. Do you see any more work that needs to be done? Also, let me know if you need some help on getting this to a merge state.
Thanks for doing this. It will be really helpful in getting the azwi integration.

@r4f4
Copy link
Contributor Author

r4f4 commented Nov 23, 2022

@r4f4 -- The PR is still titled WIP. Do you see any more work that needs to be done? Also, let me know if you need some help on getting this to a merge state. Thanks for doing this. It will be really helpful in getting the azwi integration.

I just noticed that this code https://github.com/kubernetes-sigs/cluster-api-provider-azure/blob/main/azure/scope/clients.go#L96-L98 needs to be changed as well.

auth.EnvironmentSettings.GetAuthorizer docs says it creates an authorizer in the order "Client credentials, Client certificate, Username password, MSI". That's very similar to the DefaultAzureCredential [1] which tries even more auth options: "Environment (client secret, certificate, user/pwd), ManagedIdentity, AzureCli". Here we can already notice a change in the username and MSI orders, not sure if that's a problem.

Now what is a problem is that autorest uses different env vars from MSAL for client certificate authentication. autorest uses AZURE_CERTIFICATE_ [2] whereas MSAL uses AZURE_CLIENT_CERTIFICATE_ [3]. I'm still not sure what to do about that.

[1] https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#DefaultAzureCredential
[2] https://github.com/azure/go-autorest/blob/v12.0.0/autorest/azure/auth/auth.go#L45-L46
[3] https://pkg.go.dev/github.com/Azure/azure-sdk-for-go/sdk/azidentity#hdr-Service_principal_with_certificate

@sonasingh46
Copy link
Contributor

[1] Re the env variables -- if MSAL uses a different env var name that will mean to communicate this to users to set up a env variable with the new name if a capz version containing this change is used or upgraded to.
I don't think this is something that can be or should be controlled from within capz.

[2] I do not see ordering as a problem too.

But it is worth to put these in release note and as well as in PR description.

@aramase @CecileRobertMichon Any thoughts?

@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Nov 23, 2022
@k8s-ci-robot k8s-ci-robot removed lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. labels Dec 14, 2022
@r4f4
Copy link
Contributor Author

r4f4 commented Dec 14, 2022

Rebased and added the comment as requested.

@sonasingh46
Copy link
Contributor

/retest

1 similar comment
@sonasingh46
Copy link
Contributor

/retest

@sonasingh46
Copy link
Contributor

/lgtm
/approve

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 14, 2022
@CecileRobertMichon
Copy link
Contributor

Are we keeping the 3 distinct commits intentionally or would it make sense to squash those?

@sonasingh46
Copy link
Contributor

I think that should be squashed and @r4f4 kept it for easy reviews. @CecileRobertMichon

@r4f4 -- Please squash the commits.

@k8s-ci-robot k8s-ci-robot removed the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 14, 2022
@r4f4
Copy link
Contributor Author

r4f4 commented Dec 14, 2022

Squashed commits.

@sonasingh46
Copy link
Contributor

/retest

Copy link
Contributor

@CecileRobertMichon CecileRobertMichon left a comment

Choose a reason for hiding this comment

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

just a minor code cleanup comment from me, otherwise LGTM

ADAL is being deprecated, so use azidentity with an adapter so the new
authentication can work with v1 SDK clients.

Note that for certificate authentication, autorest uses the
`AZURE_CERTIFICATE_` prefix for environment variables whereas azidentity
uses `AZURE_CLIENT_CERTIFICATE_`. We make sure the latter is set to the
value of the former so as not to break upgrades.

Signed-off-by: Rafael Fonseca <[email protected]>
@CecileRobertMichon
Copy link
Contributor

/test pull-cluster-api-provider-azure-e2e-optional

@CecileRobertMichon
Copy link
Contributor

/lgtm

@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Dec 14, 2022
@sonasingh46
Copy link
Contributor

/lgtm
/approve

Copy link
Contributor

@mboersma mboersma left a comment

Choose a reason for hiding this comment

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

/lgtm
/approve

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mboersma, sonasingh46

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Dec 15, 2022
@jackfrancis jackfrancis added this to the v1.7 milestone Dec 15, 2022
@CecileRobertMichon
Copy link
Contributor

/retest

@CecileRobertMichon
Copy link
Contributor

another flake?

/retest

@k8s-ci-robot k8s-ci-robot merged commit d20fe73 into kubernetes-sigs:main Dec 15, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test. release-note-action-required Denotes a PR that introduces potentially breaking changes that require user action. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants