8
8
"strings"
9
9
10
10
"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"
11
13
"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
12
14
"github.com/Azure/azure-sdk-for-go/sdk/azidentity"
13
15
"golang.org/x/xerrors"
@@ -17,28 +19,41 @@ import (
17
19
18
20
type Registry struct {
19
21
domain string
22
+ scope string
23
+ cloud cloud.Configuration
20
24
}
21
25
22
26
const (
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"
26
32
)
27
33
28
34
func (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
31
45
}
32
- r . domain = domain
33
- return nil
46
+
47
+ return xerrors . Errorf ( "Azure registry: %w" , types . InvalidURLPattern )
34
48
}
35
49
36
50
func (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 })
38
53
if err != nil {
39
54
return "" , "" , xerrors .Errorf ("unable to generate acr credential error: %w" , err )
40
55
}
41
- aadToken , err := cred .GetToken (ctx , policy.TokenRequestOptions {Scopes : []string {scope }})
56
+ aadToken , err := cred .GetToken (ctx , policy.TokenRequestOptions {Scopes : []string {r . scope }})
42
57
if err != nil {
43
58
return "" , "" , xerrors .Errorf ("unable to get an access token: %w" , err )
44
59
}
0 commit comments