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

oci/auth: Add support for Azure China and US Gov regions #631

Merged
merged 1 commit into from
Aug 21, 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
17 changes: 15 additions & 2 deletions oci/auth/azure/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ func (c *Client) getLoginAuth(ctx context.Context, registryURL string) (authn.Au
c.credential = cred
}

configurationEnvironment := getCloudConfiguration(registryURL)
// Obtain access token using the token credential.
// TODO: Add support for other azure endpoints as well.
armToken, err := c.credential.GetToken(ctx, policy.TokenRequestOptions{
Scopes: []string{cloud.AzurePublic.Services[cloud.ResourceManager].Endpoint + "/" + ".default"},
Scopes: []string{configurationEnvironment.Services[cloud.ResourceManager].Endpoint + "/" + ".default"},
})
if err != nil {
return authConfig, err
Expand All @@ -98,6 +98,19 @@ func (c *Client) getLoginAuth(ctx context.Context, registryURL string) (authn.Au
}, nil
}

// getCloudConfiguration returns the cloud configuration based on the registry URL.
// List from https://github.com/Azure/azure-sdk-for-go/blob/main/sdk/containers/azcontainerregistry/cloud_config.go#L16
func getCloudConfiguration(url string) cloud.Configuration {
switch {
case strings.HasSuffix(url, ".azurecr.cn"):
return cloud.AzureChina
case strings.HasSuffix(url, ".azurecr.us"):
return cloud.AzureGovernment
default:
return cloud.AzurePublic
}
}

// ValidHost returns if a given host is a Azure container registry.
// List from https://github.com/kubernetes/kubernetes/blob/v1.23.1/pkg/credentialprovider/azure/azure_credentials.go#L55
func ValidHost(host string) bool {
Expand Down
20 changes: 20 additions & 0 deletions oci/auth/azure/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"testing"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -113,6 +114,25 @@ func TestValidHost(t *testing.T) {
}
}

func TestGetCloudConfiguration(t *testing.T) {
mihaiandreiratoiu marked this conversation as resolved.
Show resolved Hide resolved
tests := []struct {
host string
result cloud.Configuration
}{
{"foo.azurecr.io", cloud.AzurePublic},
{"foo.azurecr.cn", cloud.AzureChina},
{"foo.azurecr.de", cloud.AzurePublic},
{"foo.azurecr.us", cloud.AzureGovernment},
}

for _, tt := range tests {
t.Run(tt.host, func(t *testing.T) {
g := NewWithT(t)
g.Expect(getCloudConfiguration(tt.host)).To(Equal(tt.result))
})
}
}

func TestLogin(t *testing.T) {
tests := []struct {
name string
Expand Down