Skip to content

Commit

Permalink
Ops: Update azure cloud token auth
Browse files Browse the repository at this point in the history
Signed-off-by: mihaiandreiratoiu <mihai.ratoiu@uipath.com>
  • Loading branch information
mihaiandreiratoiu committed Aug 21, 2023
1 parent 0518e84 commit 8611884
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
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) {
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

0 comments on commit 8611884

Please sign in to comment.