Skip to content

Commit

Permalink
Update to use registry instead of pattern and add a few more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
callumcrossley12 committed Nov 5, 2024
1 parent 43719b0 commit b273f4c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ The general format for the file is highlighted below.

```yaml
registryConfigs:
- pattern: <registry_matching_pattern>
- registry: <registry_matching_pattern>
config:
profile: "<AWS_PROFILE_NAME>"
```
Expand All @@ -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"
```
Expand Down
12 changes: 6 additions & 6 deletions ecr-login/config/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type RegistryConfig struct {
}

type RegistryConfigEntry struct {
Pattern string `yaml:"pattern"`
Registry string `yaml:"registry"`
Config RegistryConfig `yaml:"config"`
}

Expand All @@ -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
Expand All @@ -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 != "" {
Expand Down Expand Up @@ -80,16 +80,16 @@ 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
}
}

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 {
Expand Down
4 changes: 2 additions & 2 deletions ecr-login/config/registry/registry_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 18 additions & 10 deletions ecr-login/config/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -233,7 +244,6 @@ func TestGetRegistryProfileWildcards(t *testing.T) {
AddRegistryConfigWithProfile("123456789000.*", "production").
Build(),
expectedProfile: "production",
expectedError: false,
},
{
name: "Wildcard suffix first match",
Expand All @@ -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",
Expand All @@ -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 {
Expand Down

0 comments on commit b273f4c

Please sign in to comment.