Skip to content

Commit d1ec89d

Browse files
authored
feat(misconf): enabled China configuration for ACRs (#7156)
1 parent 2a577a7 commit d1ec89d

File tree

2 files changed

+28
-9
lines changed

2 files changed

+28
-9
lines changed

pkg/fanal/image/registry/azure/azure.go

+24-9
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ import (
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

1820
type Registry struct {
1921
domain string
22+
scope string
23+
cloud cloud.Configuration
2024
}
2125

2226
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"
2632
)
2733

2834
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
3145
}
32-
r.domain = domain
33-
return nil
46+
47+
return xerrors.Errorf("Azure registry: %w", types.InvalidURLPattern)
3448
}
3549

3650
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})
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
}

pkg/fanal/image/registry/azure/azure_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ func TestRegistry_CheckOptions(t *testing.T) {
2020
name: "happy path",
2121
domain: "test.azurecr.io",
2222
},
23+
{
24+
name: "china happy path",
25+
domain: "test.azurecr.cn",
26+
},
2327
{
2428
name: "invalidURL",
2529
domain: "not-azurecr.io",

0 commit comments

Comments
 (0)