-
Notifications
You must be signed in to change notification settings - Fork 177
ACR Login: Use Azure SDK library to get the credential and token #3398
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,12 +1,12 @@ | ||
| package k8s | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "path" | ||
| "strings" | ||
|
|
||
| "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy" | ||
| "github.com/Azure/azure-sdk-for-go/sdk/azidentity" | ||
| "github.com/google/go-containerregistry/pkg/name" | ||
| "github.com/google/go-containerregistry/pkg/v1/google" | ||
|
|
||
|
|
@@ -54,32 +54,22 @@ func GetACRCredentialLoader() []creds.CredentialsCallback { | |
| if !strings.HasSuffix(registry, ".azurecr.io") { | ||
| return oci.Credentials{}, creds.ErrCredentialsNotFound | ||
| } | ||
|
|
||
| f, err := os.Open(path.Join(os.Getenv("HOME"), ".azure", "accessTokens.json")) | ||
| // Use Azure SDK to get access token | ||
| azCredentials, err := azidentity.NewDefaultAzureCredential(nil) | ||
| if err != nil { | ||
| return oci.Credentials{}, fmt.Errorf("open Azure access tokens: %w", err) | ||
| } | ||
| defer f.Close() | ||
|
|
||
| var tokens []struct { | ||
| AccessToken string `json:"accessToken"` | ||
| Resource string `json:"resource"` | ||
| } | ||
|
|
||
| if err := json.NewDecoder(f).Decode(&tokens); err != nil { | ||
| return oci.Credentials{}, fmt.Errorf("decode Azure access tokens: %w", err) | ||
| return oci.Credentials{}, fmt.Errorf("failed to create default azure credentials: %w", err) | ||
| } | ||
|
|
||
| target := "https://" + registry | ||
| for _, t := range tokens { | ||
| if t.Resource == target { | ||
| return oci.Credentials{ | ||
| Username: "00000000-0000-0000-0000-000000000000", | ||
| Password: t.AccessToken, | ||
| }, nil | ||
| } | ||
| scope := "https://containerregistry.azure.net/.default" | ||
| token, err := azCredentials.GetToken(context.Background(), policy.TokenRequestOptions{ | ||
| Scopes: []string{scope}, | ||
| }) | ||
| if err != nil { | ||
| return oci.Credentials{}, fmt.Errorf("failed to get azure access token: %w", err) | ||
| } | ||
| return oci.Credentials{}, creds.ErrCredentialsNotFound | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I notice that |
||
| return oci.Credentials{ | ||
| Username: "00000000-0000-0000-0000-000000000000", | ||
| Password: token.Token, | ||
| }, nil | ||
| }, | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We might want to plumb through a context object to GetACRCredentialLoader in case this thing can lock up or need canceling.