diff --git a/README.md b/README.md index 9e56111b..fcfe705d 100644 --- a/README.md +++ b/README.md @@ -313,7 +313,7 @@ The general format for the file is highlighted below. ```yaml registryConfigs: - - pattern: + - registry: config: profile: "" ``` @@ -322,13 +322,13 @@ For example: ```yaml registryConfigs: - - pattern: "123456789000.dkr.ecr.ap-southeast-2.amazonaws.com" + - registry: "123456789000.dkr.ecr.ap-southeast-2.amazonaws.com" config: profile: "some_profile" - - pattern: "987654321000.us-east-1.amazonaws.com" + - registry: "987654321000.us-east-1.amazonaws.com" config: profile: "another_profile" - - pattern: "*.us-east-1.amazonaws.com" + - registry: "*.us-east-1.amazonaws.com" config: profile: "fallback_profile" ``` diff --git a/ecr-login/config/registry/registry.go b/ecr-login/config/registry/registry.go index 2dfb6533..d7f16822 100644 --- a/ecr-login/config/registry/registry.go +++ b/ecr-login/config/registry/registry.go @@ -15,7 +15,7 @@ type RegistryConfig struct { } type RegistryConfigEntry struct { - Pattern string `yaml:"pattern"` + Registry string `yaml:"registry"` Config RegistryConfig `yaml:"config"` } @@ -31,7 +31,7 @@ var ( GetRegistryProfile = getRegistryProfile // Provide override for mocking ) -// Helper to match registry with wildard patterns +// Helper to match registry with wildcard prefix and suffix patterns func matchesPattern(pattern, registry string) bool { if pattern == "*" { return true @@ -45,7 +45,7 @@ func matchesPattern(pattern, registry string) bool { return pattern == registry // Exact match } -// Function to determine the RegistryConfigPath +// Function to get the RegistryConfigPath func getRegistryConfigPath() string { // Get the path from the environment variable if configPath := os.Getenv(ENV_AWS_ECR_REGISTRY_CONFIG_PATH); configPath != "" { @@ -80,7 +80,7 @@ func getRegistryConfig(registry string) (*RegistryConfig, error) { // Look for the registry configuration with wildcards support in file order for _, entry := range configs.RegistryConfigs { - if matchesPattern(entry.Pattern, registry) { + if matchesPattern(entry.Registry, registry) { return &entry.Config, nil } } @@ -88,8 +88,8 @@ func getRegistryConfig(registry string) (*RegistryConfig, error) { return nil, nil // Return nil if registry is not found } -// GetRegistryProfile attempts to retrieve a profile from the RegistryConfig for the specified registry. -// Returns a the profile string if found, or nil if not found. +// GetRegistryProfile attempts to retrieve a profile from the RegistryConfig for the specified registry pattern. +// Returns the profile string if found, or empty string if not found. func getRegistryProfile(registry string) (string, error) { config, err := getRegistryConfig(registry) if err != nil { diff --git a/ecr-login/config/registry/registry_builder.go b/ecr-login/config/registry/registry_builder.go index ef362e69..aa883cb0 100644 --- a/ecr-login/config/registry/registry_builder.go +++ b/ecr-login/config/registry/registry_builder.go @@ -15,9 +15,9 @@ func NewRegistryConfigBuilder() *RegistryConfigBuilder { } // Adds a new registry configuration with the given name and credential. -func (b *RegistryConfigBuilder) AddRegistryConfigWithProfile(pattern string, profile string) *RegistryConfigBuilder { +func (b *RegistryConfigBuilder) AddRegistryConfigWithProfile(registry string, profile string) *RegistryConfigBuilder { entry := RegistryConfigEntry{ - Pattern: pattern, + Registry: registry, Config: RegistryConfig{Profile: profile}, } b.config.RegistryConfigs = append(b.config.RegistryConfigs, entry) // Append to the slice diff --git a/ecr-login/config/registry/registry_test.go b/ecr-login/config/registry/registry_test.go index e7ddf9d4..622070fd 100644 --- a/ecr-login/config/registry/registry_test.go +++ b/ecr-login/config/registry/registry_test.go @@ -180,14 +180,12 @@ func TestGetRegistryProfileWildcards(t *testing.T) { registryPattern string registryConfigs *RegistryConfigs expectedProfile string - expectedError bool }{ { name: "No config", registryPattern: "123456789000.dkr.ecr.ap-southeast-2.amazonaws.com", registryConfigs: nil, expectedProfile: "", - expectedError: false, }, { name: "Exact match", @@ -196,7 +194,6 @@ func TestGetRegistryProfileWildcards(t *testing.T) { AddRegistryConfigWithProfile("123456789000.dkr.ecr.ap-southeast-2.amazonaws.com", "production"). Build(), expectedProfile: "production", - expectedError: false, }, { name: "No match", @@ -205,7 +202,23 @@ func TestGetRegistryProfileWildcards(t *testing.T) { AddRegistryConfigWithProfile("987654321000.dkr.ecr.ap-southeast-2.amazonaws.com", "production"). Build(), expectedProfile: "", - expectedError: false, + }, + { + name: "Duplicate match use first match", + registryPattern: "987654321000.us-east-1.amazonaws.com", + registryConfigs: NewRegistryConfigBuilder(). + AddRegistryConfigWithProfile("987654321000.us-east-1.amazonaws.com", "production"). + AddRegistryConfigWithProfile("987654321000.us-east-1.amazonaws.com", "other_profile"). + Build(), + expectedProfile: "production", + }, + { + name: "Unsupported complex wildcard, no match", + registryPattern: "123456789000.dkr.ecr.ap-southeast-2.amazonaws.com", + registryConfigs: NewRegistryConfigBuilder(). + AddRegistryConfigWithProfile("*.dkr.ecr.*.amazonaws.com", "production"). + Build(), + expectedProfile: "", }, { name: "Wildcard prefix single match", @@ -214,7 +227,6 @@ func TestGetRegistryProfileWildcards(t *testing.T) { AddRegistryConfigWithProfile("*.dkr.ecr.ap-southeast-2.amazonaws.com", "production"). Build(), expectedProfile: "production", - expectedError: false, }, { name: "Wildcard prefix first match", @@ -224,7 +236,6 @@ func TestGetRegistryProfileWildcards(t *testing.T) { AddRegistryConfigWithProfile("123456789000.dkr.ecr.ap-southeast-2.amazonaws.com", "some_other_profile"). Build(), expectedProfile: "production", - expectedError: false, }, { name: "Wildcard suffix single match", @@ -233,7 +244,6 @@ func TestGetRegistryProfileWildcards(t *testing.T) { AddRegistryConfigWithProfile("123456789000.*", "production"). Build(), expectedProfile: "production", - expectedError: false, }, { name: "Wildcard suffix first match", @@ -244,7 +254,6 @@ func TestGetRegistryProfileWildcards(t *testing.T) { AddRegistryConfigWithProfile("*.dkr.ecr.ap-southeast-2.amazonaws.com", "yet_another_profile"). Build(), expectedProfile: "production", - expectedError: false, }, { name: "Wildcard fallback match", @@ -254,8 +263,7 @@ func TestGetRegistryProfileWildcards(t *testing.T) { AddRegistryConfigWithProfile("*.us-east-1.amazonaws.com", "fallback_profile"). Build(), expectedProfile: "fallback_profile", - expectedError: false, - }, + }, } for _, tc := range testCases {