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 azure managed identity to azure package in blockstorage #1615

Merged

Conversation

leuyentran
Copy link
Contributor

Change Overview

This PR add :

  1. Support for Azure Manage Identity by adding MSI authorizer
  2. Add an Authenticate() interface to check validity of Azure creds. Azure Managed Identity is the first type to be supported for this interface . Similar to other Azure interfaces, this interface is not united tested because creating a new Azure provider involves calling Instance Azure Metadata which will results in context timeout
  3. Add utilities functions to provide appropriate abstractions levels for point 1 and 2 + unit tests

Pull request type

Please check the type of change your PR introduces:

  • 🚧 Work in Progress
  • 🌈 Refactoring (no functional changes, no api changes)
  • 🐹 Trivial/Minor
  • 🐛 Bugfix
  • 🌻 Feature
  • 🗺️ Documentation
  • 🤖 Test

Test Plan

  • 💪 Manual
  • ⚡ Unit test
  • 💚 E2E

@github-actions
Copy link
Contributor

Thanks for submitting this pull request 🎉. The team will review it soon and get back to you.

If you haven't already, please take a moment to review our project contributing guideline and Code of Conduct document.

@@ -44,6 +44,22 @@ func (s *AdStorage) Type() blockstorage.Type {
return blockstorage.TypeAD
}

// Authenticate check Azure creds if the credType is supported
func (s *AdStorage) Authenticate(ctx context.Context, credType string, creds map[string]string) (CredsValidity, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This interface is not united tested because creating a new Azure provider involves calling Azure Instance Metadata which will results in context timeout

// authenticate with MSI creds
type msiAuthenticator struct{}

func (m *msiAuthenticator) authenticate(creds map[string]string) (CredsValidity, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This interface is not united tested because it involves network calls which will results in context timeout

@leuyentran leuyentran requested a review from bathina2 August 31, 2022 19:20
@leuyentran leuyentran force-pushed the Add-Azure-Managed-Identity-to-azure-package-in-blockstorage branch from cc9e986 to e39e7ad Compare August 31, 2022 20:16
@leuyentran leuyentran force-pushed the Add-Azure-Managed-Identity-to-azure-package-in-blockstorage branch from e39e7ad to 8daf339 Compare August 31, 2022 20:24
pkg/blockstorage/azure/client.go Outdated Show resolved Hide resolved
pkg/blockstorage/azure/auth.go Outdated Show resolved Hide resolved
pkg/blockstorage/azure/auth.go Outdated Show resolved Hide resolved
pkg/blockstorage/azure/auth.go Show resolved Hide resolved
pkg/blockstorage/azure/auth.go Outdated Show resolved Hide resolved
pkg/blockstorage/azure/client.go Outdated Show resolved Hide resolved
pkg/blockstorage/azure/auth.go Outdated Show resolved Hide resolved
pkg/blockstorage/azure/auth.go Outdated Show resolved Hide resolved
pkg/blockstorage/azure/auth.go Outdated Show resolved Hide resolved
pkg/blockstorage/azure/auth.go Outdated Show resolved Hide resolved
@leuyentran leuyentran force-pushed the Add-Azure-Managed-Identity-to-azure-package-in-blockstorage branch from a4dbac2 to 1d425f4 Compare September 1, 2022 20:33
@leuyentran leuyentran mentioned this pull request Sep 1, 2022
10 tasks
@leuyentran leuyentran marked this pull request as ready for review September 1, 2022 20:34
import (
"context"

"github.com/Azure/go-autorest/autorest/adal"
Copy link
Contributor

Choose a reason for hiding this comment

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

Looks like this package will be deprecated next year. Can we look into using azidentity instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The purpose of interface Authenticate() is to validate Azure creds as a one-of action. These creds are then passed on to an Authorizer() that later authenticates and return a token for every Azure API calls that interact with Azure resources.
And for Authorizer(), we are still using adal indirectly as part of github.com/Azure/go-autorest/autorest/azure/auth

Furthermore, azidentity and adal differs slightly in their credential definitions and workflows. So I'd prefer not to use azidentity for Authenticate() and adal for Authorizer() in case we don't completely understand the nuances in expectations.

Instead, I had abstracted out the logics as much as I can, which should make APIs swapping easier during migration :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Can you create a GH issue to track the migration task for both the adal and auth packages? Thanks.

Copy link
Contributor Author

@leuyentran leuyentran Sep 2, 2022

Choose a reason for hiding this comment

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

Copy link
Contributor

@ihcsim ihcsim left a comment

Choose a reason for hiding this comment

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

Code change LGTM. I will leave it to @onkarbhat and @bathina2 to approve.

Comment on lines +61 to +67
// network call to check for token
err = spt.Refresh()
if err != nil {
return errors.Wrap(err, "Failed to refresh token")
}
// creds passed authentication
return nil
Copy link
Contributor

Choose a reason for hiding this comment

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

TIOLI: Consider just return spt.Refresh(), since Refresh() already returns comprehensive error messages. Same with clientSecretAuthenticator.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The caller, i.e. Authenticate() directly return results of these methods. Therefore, the explicitly here is meant for code readability for future developers to understand: err==nil is considered passing authentication.

pkg/blockstorage/azure/auth_tests.go Outdated Show resolved Hide resolved
pkg/blockstorage/azure/azuredisk.go Outdated Show resolved Hide resolved
import (
"context"

"github.com/Azure/go-autorest/autorest/adal"
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you create a GH issue to track the migration task for both the adal and auth packages? Thanks.

pkg/blockstorage/azure/azuredisk.go Outdated Show resolved Hide resolved
@bathina2
Copy link
Contributor

bathina2 commented Sep 1, 2022

The changes look good. However, I do have some design opinions. From the caller side I would expect something like -

authenticator, err := GetAuthenticator
if err != nil {
    //spit out err
}
err = authenticator.Authenticate()

Let me know if you have time to discuss tomorrow

Copy link
Contributor

@bathina2 bathina2 left a comment

Choose a reason for hiding this comment

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

Looks good, Minor nits about naming. Might be good to be more explicit, but not a big deal

}

// authenticate with MSI creds
type msiAuthenticator struct{}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: consider renaming to msiAzureAuthenticator

}

// authenticate with client secret creds
type clientSecretAuthenticator struct{}
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: consider renaming to clientSecretAzureAuthenitcator.

Copy link
Contributor Author

@leuyentran leuyentran Sep 5, 2022

Choose a reason for hiding this comment

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

Since the package name is azure , I think it is okay to leave the naming as is. If they need to be public for some reason in the future, it would still be azure.ClientSecretAuthenticator

@mergify mergify bot merged commit 09018c8 into master Sep 5, 2022
@mergify mergify bot deleted the Add-Azure-Managed-Identity-to-azure-package-in-blockstorage branch September 5, 2022 21:18
Copy link
Contributor

@onkarbhat onkarbhat left a comment

Choose a reason for hiding this comment

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

LGTM . Posting it after merge.

Authenticate(creds map[string]string) error
}

func NewAzureAutheticator(config map[string]string) (AzureAuthenticator, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Please add comments for exported methods/structs in this PR.

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

Successfully merging this pull request may close these issues.

4 participants