mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-26 05:03:09 +00:00
Fix ACR MSI cross-subscription authentication error
This commit is contained in:
parent
16193d83a4
commit
b5cdb78190
@ -22,6 +22,7 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2017-10-01/containerregistry"
|
"github.com/Azure/azure-sdk-for-go/services/containerregistry/mgmt/2017-10-01/containerregistry"
|
||||||
@ -44,7 +45,10 @@ const (
|
|||||||
maxReadLength = 10 * 1 << 20 // 10MB
|
maxReadLength = 10 * 1 << 20 // 10MB
|
||||||
)
|
)
|
||||||
|
|
||||||
var containerRegistryUrls = []string{"*.azurecr.io", "*.azurecr.cn", "*.azurecr.de", "*.azurecr.us"}
|
var (
|
||||||
|
containerRegistryUrls = []string{"*.azurecr.io", "*.azurecr.cn", "*.azurecr.de", "*.azurecr.us"}
|
||||||
|
acrRE = regexp.MustCompile(`.*\.azurecr\.io|.*\.azurecr\.cn|.*\.azurecr\.de|.*\.azurecr\.us`)
|
||||||
|
)
|
||||||
|
|
||||||
// init registers the various means by which credentials may
|
// init registers the various means by which credentials may
|
||||||
// be resolved on Azure.
|
// be resolved on Azure.
|
||||||
@ -182,26 +186,16 @@ func (a *acrProvider) Enabled() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (a *acrProvider) Provide(image string) credentialprovider.DockerConfig {
|
func (a *acrProvider) Provide(image string) credentialprovider.DockerConfig {
|
||||||
|
klog.V(4).Infof("try to provide secret for image %s", image)
|
||||||
cfg := credentialprovider.DockerConfig{}
|
cfg := credentialprovider.DockerConfig{}
|
||||||
ctx, cancel := getContextWithCancel()
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
if a.config.UseManagedIdentityExtension {
|
if a.config.UseManagedIdentityExtension {
|
||||||
klog.V(4).Infof("listing registries")
|
if loginServer := parseACRLoginServerFromImage(image); loginServer == "" {
|
||||||
result, err := a.registryClient.List(ctx)
|
klog.V(4).Infof("image(%s) is not from ACR, skip MSI authentication", image)
|
||||||
if err != nil {
|
} else {
|
||||||
klog.Errorf("Failed to list registries: %v", err)
|
if cred, err := getACRDockerEntryFromARMToken(a, loginServer); err == nil {
|
||||||
return cfg
|
cfg[loginServer] = *cred
|
||||||
}
|
|
||||||
|
|
||||||
for ix := range result {
|
|
||||||
loginServer := getLoginServer(result[ix])
|
|
||||||
klog.V(2).Infof("loginServer: %s", loginServer)
|
|
||||||
cred, err := getACRDockerEntryFromARMToken(a, loginServer)
|
|
||||||
if err != nil {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
cfg[loginServer] = *cred
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Add our entry for each of the supported container registry URLs
|
// Add our entry for each of the supported container registry URLs
|
||||||
@ -229,6 +223,11 @@ func getLoginServer(registry containerregistry.Registry) string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func getACRDockerEntryFromARMToken(a *acrProvider, loginServer string) (*credentialprovider.DockerConfigEntry, error) {
|
func getACRDockerEntryFromARMToken(a *acrProvider, loginServer string) (*credentialprovider.DockerConfigEntry, error) {
|
||||||
|
// Run EnsureFresh to make sure the token is valid and does not expire
|
||||||
|
if err := a.servicePrincipalToken.EnsureFresh(); err != nil {
|
||||||
|
klog.Errorf("Failed to ensure fresh service principal token: %v", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
armAccessToken := a.servicePrincipalToken.OAuthToken()
|
armAccessToken := a.servicePrincipalToken.OAuthToken()
|
||||||
|
|
||||||
klog.V(4).Infof("discovering auth redirects for: %s", loginServer)
|
klog.V(4).Infof("discovering auth redirects for: %s", loginServer)
|
||||||
@ -254,6 +253,16 @@ func getACRDockerEntryFromARMToken(a *acrProvider, loginServer string) (*credent
|
|||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// parseACRLoginServerFromImage takes image as parameter and returns login server of it.
|
||||||
|
// Parameter `image` is expected in following format: foo.azurecr.io/bar/imageName:version
|
||||||
|
// If the provided image is not an acr image, this function will return an empty string.
|
||||||
|
func parseACRLoginServerFromImage(image string) string {
|
||||||
|
match := acrRE.FindAllString(image, -1)
|
||||||
|
if len(match) == 1 {
|
||||||
|
return match[0]
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
func (a *acrProvider) LazyProvide(image string) *credentialprovider.DockerConfigEntry {
|
func (a *acrProvider) LazyProvide(image string) *credentialprovider.DockerConfigEntry {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -94,3 +94,28 @@ func Test(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestParseACRLoginServerFromImage(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
image string
|
||||||
|
expected string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
image: "invalidImage",
|
||||||
|
expected: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
image: "docker.io/library/busybox:latest",
|
||||||
|
expected: "",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
image: "foo.azurecr.io/bar/image:version",
|
||||||
|
expected: "foo.azurecr.io",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, test := range tests {
|
||||||
|
if loginServer := parseACRLoginServerFromImage(test.image); loginServer != test.expected {
|
||||||
|
t.Errorf("function parseACRLoginServerFromImage returns \"%s\" for image %s, expected \"%s\"", loginServer, test.image, test.expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user