Skip to content

Commit

Permalink
fix: address comments
Browse files Browse the repository at this point in the history
Signed-off-by: Binbin Li <[email protected]>
  • Loading branch information
binbin-li committed Jan 26, 2025
1 parent 39af35f commit 81a8cab
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
3 changes: 2 additions & 1 deletion pkg/common/oras/authprovider/azure/azureworkloadidentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,8 @@ func validateHost(host string, endpoints []string) error {
return nil
}
case 1:
if strings.HasSuffix(host, strings.TrimPrefix(endpoint, "*")) {
index := strings.Index(host, ".")
if index > -1 && host[index:] == strings.TrimPrefix(endpoint, "*") {
return nil
}
default:
Expand Down
90 changes: 90 additions & 0 deletions pkg/common/oras/authprovider/azure/azureworkloadidentity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -441,3 +441,93 @@ func TestAzureWIValidation_EnvironmentVariables_ExpectedResults(t *testing.T) {
t.Fatalf("create auth provider should have failed: expected err %s, but got err %s", expectedErr, err)
}
}

func TestValidateEndpoints(t *testing.T) {
tests := []struct {
name string
endpoint string
expectedErr bool
}{
{
name: "global wildcard",
endpoint: "*",
expectedErr: true,
},
{
name: "multiple wildcard",
endpoint: "*.example.*",
expectedErr: true,
},
{
name: "no subdomain",
endpoint: "*.",
expectedErr: true,
},
{
name: "full qualified domain",
endpoint: "example.com",
expectedErr: false,
},
{
name: "valid wildcard domain",
endpoint: "*.example.com",
expectedErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateEndpoints([]string{tt.endpoint})
if tt.expectedErr != (err != nil) {
t.Fatalf("expected error: %v, got error: %v", tt.expectedErr, err)
}
})
}
}

func TestValidateHost(t *testing.T) {
endpoints := []string{
"*.azurecr.io",
"example.azurecr.io",
}
tests := []struct {
name string
host string
expectedErr bool
}{
{
name: "empty host",
host: "",
expectedErr: true,
},
{
name: "valid host",
host: "example.azurecr.io",
expectedErr: false,
},
{
name: "no subdomain",
host: "azurecr.io",
expectedErr: true,
},
{
name: "multiple subdomains",
host: "example.test.azurecr.io",
expectedErr: true,
},
{
name: "matched host",
host: "test.azurecr.io",
expectedErr: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := validateHost(tt.host, endpoints)
if tt.expectedErr != (err != nil) {
t.Fatalf("expected error: %v, got error: %v", tt.expectedErr, err)
}
})
}
}
2 changes: 1 addition & 1 deletion pkg/common/oras/authprovider/azure/const.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
dockerTokenLoginUsernameGUID = "00000000-0000-0000-0000-000000000000"
AADResource = "https://containerregistry.azure.net/.default"
defaultACRExpiryDuration time.Duration = 3 * time.Hour
defaultACREndpoint = ".*.azurecr.io"
defaultACREndpoint = "*.azurecr.io"
)

var logOpt = logger.Option{
Expand Down

0 comments on commit 81a8cab

Please sign in to comment.