Skip to content

Add MSI Support for Azure plugin. #6938

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 1 commit into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelogs/unreleased/6938-yanggangtony
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add MSI Support for Azure plugin.
11 changes: 10 additions & 1 deletion pkg/util/azure/credential.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
"github.com/pkg/errors"
)

// NewCredential chains the config credential and workload identity credential
// NewCredential chains the config credential , workload identity credential , managed identity credential
func NewCredential(creds map[string]string, options policy.ClientOptions) (azcore.TokenCredential, error) {
var (
credential []azcore.TokenCredential
Expand Down Expand Up @@ -60,6 +60,15 @@ func NewCredential(creds map[string]string, options policy.ClientOptions) (azcor
errMsgs = append(errMsgs, err.Error())
}

//managed identity credential
o := &azidentity.ManagedIdentityCredentialOptions{ClientOptions: options, ID: azidentity.ClientID(creds[CredentialKeyClientID])}
msi, err := azidentity.NewManagedIdentityCredential(o)
if err == nil {
credential = append(credential, msi)
} else {
errMsgs = append(errMsgs, err.Error())
}

if len(credential) == 0 {
return nil, errors.Errorf("failed to create Azure credential: %s", strings.Join(errMsgs, "\n\t"))
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/util/azure/credential_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ limitations under the License.
package azure

import (
"context"
"testing"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
Expand All @@ -26,10 +28,15 @@ import (

func TestNewCredential(t *testing.T) {
options := policy.ClientOptions{}

// no credentials
creds := map[string]string{}
_, err := NewCredential(creds, options)
tokenCredential, _ := NewCredential(creds, options)

var scopes []string
scopes = append(scopes, "https://management.core.windows.net//.default")

ctx, _ := context.WithTimeout(context.Background(), time.Second*2)
_, err := tokenCredential.GetToken(ctx, policy.TokenRequestOptions{Scopes: scopes})
require.NotNil(t, err)

// config credential
Expand All @@ -40,6 +47,7 @@ func TestNewCredential(t *testing.T) {
}
_, err = NewCredential(creds, options)
require.Nil(t, err)

}

func Test_newConfigCredential(t *testing.T) {
Expand Down