88 "strings"
99
1010 "github.com/Azure/azure-sdk-for-go/profiles/preview/preview/containerregistry/runtime/containerregistry"
11+ "github.com/Azure/azure-sdk-for-go/sdk/azcore"
12+ "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud"
1113 "github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
1214 "github.com/Azure/azure-sdk-for-go/sdk/azidentity"
1315 "golang.org/x/xerrors"
@@ -17,28 +19,41 @@ import (
1719
1820type Registry struct {
1921 domain string
22+ scope string
23+ cloud cloud.Configuration
2024}
2125
2226const (
23- azureURL = ".azurecr.io"
24- scope = "https://management.azure.com/.default"
25- scheme = "https"
27+ azureURL = ".azurecr.io"
28+ chinaAzureURL = ".azurecr.cn"
29+ scope = "https://management.azure.com/.default"
30+ chinaScope = "https://management.chinacloudapi.cn/.default"
31+ scheme = "https"
2632)
2733
2834func (r * Registry ) CheckOptions (domain string , _ types.RegistryOptions ) error {
29- if ! strings .HasSuffix (domain , azureURL ) {
30- return xerrors .Errorf ("Azure registry: %w" , types .InvalidURLPattern )
35+ if strings .HasSuffix (domain , azureURL ) {
36+ r .domain = domain
37+ r .scope = scope
38+ r .cloud = cloud .AzurePublic
39+ return nil
40+ } else if strings .HasSuffix (domain , chinaAzureURL ) {
41+ r .domain = domain
42+ r .scope = chinaScope
43+ r .cloud = cloud .AzureChina
44+ return nil
3145 }
32- r . domain = domain
33- return nil
46+
47+ return xerrors . Errorf ( "Azure registry: %w" , types . InvalidURLPattern )
3448}
3549
3650func (r * Registry ) GetCredential (ctx context.Context ) (string , string , error ) {
37- cred , err := azidentity .NewDefaultAzureCredential (nil )
51+ opts := azcore.ClientOptions {Cloud : r .cloud }
52+ cred , err := azidentity .NewDefaultAzureCredential (& azidentity.DefaultAzureCredentialOptions {ClientOptions : opts })
3853 if err != nil {
3954 return "" , "" , xerrors .Errorf ("unable to generate acr credential error: %w" , err )
4055 }
41- aadToken , err := cred .GetToken (ctx , policy.TokenRequestOptions {Scopes : []string {scope }})
56+ aadToken , err := cred .GetToken (ctx , policy.TokenRequestOptions {Scopes : []string {r . scope }})
4257 if err != nil {
4358 return "" , "" , xerrors .Errorf ("unable to get an access token: %w" , err )
4459 }
0 commit comments