-
Notifications
You must be signed in to change notification settings - Fork 284
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #327 from keel-hq/feature/301_better_registry_matc…
…hing Feature/301 better registry matching
- Loading branch information
Showing
4 changed files
with
130 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package secrets | ||
|
||
import ( | ||
"net/url" | ||
"strings" | ||
) | ||
|
||
func registryMatches(imageRegistry, secretRegistry string) bool { | ||
|
||
if imageRegistry == secretRegistry { | ||
return true | ||
} | ||
|
||
imageRegistry = stripScheme(imageRegistry) | ||
secretRegistry = stripScheme(secretRegistry) | ||
|
||
if imageRegistry == secretRegistry { | ||
return true | ||
} | ||
|
||
// checking domains only | ||
if domainOnly(imageRegistry) == domainOnly(secretRegistry) { | ||
return true | ||
} | ||
|
||
// stripping any paths | ||
irh, err := url.Parse("https://" + imageRegistry) | ||
if err != nil { | ||
return false | ||
} | ||
srh, err := url.Parse("https://" + secretRegistry) | ||
if err != nil { | ||
return false | ||
} | ||
|
||
if irh.Hostname() == srh.Hostname() { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
func stripScheme(url string) string { | ||
|
||
if strings.HasPrefix(url, "http://") { | ||
return strings.TrimPrefix(url, "http://") | ||
} | ||
if strings.HasPrefix(url, "https://") { | ||
return strings.TrimPrefix(url, "https://") | ||
} | ||
return url | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package secrets | ||
|
||
import "testing" | ||
|
||
func Test_registryMatches(t *testing.T) { | ||
type args struct { | ||
imageRegistry string | ||
secretRegistry string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want bool | ||
}{ | ||
{ | ||
name: "matches", | ||
args: args{imageRegistry: "docker.io", secretRegistry: "docker.io"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "doesnt match", | ||
args: args{imageRegistry: "docker.io", secretRegistry: "index.docker.io"}, | ||
want: false, | ||
}, | ||
{ | ||
name: "matches, secret with port", | ||
args: args{imageRegistry: "docker.io", secretRegistry: "docker.io:443"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "matches, image with port", | ||
args: args{imageRegistry: "docker.io:443", secretRegistry: "docker.io"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "matches, image with scheme", | ||
args: args{imageRegistry: "https://docker.io", secretRegistry: "docker.io"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "matches, secret with scheme", | ||
args: args{imageRegistry: "docker.io", secretRegistry: "https://docker.io"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "matches, both with scheme", | ||
args: args{imageRegistry: "https://docker.io", secretRegistry: "https://docker.io"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "matches, both with scheme and port", | ||
args: args{imageRegistry: "https://docker.io:443", secretRegistry: "https://docker.io:443"}, | ||
want: true, | ||
}, | ||
{ | ||
name: "matches, both with scheme and port and a URL path in the secret", | ||
args: args{imageRegistry: "https://docker.io:443", secretRegistry: "https://docker.io:443/v1"}, | ||
want: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := registryMatches(tt.args.imageRegistry, tt.args.secretRegistry); got != tt.want { | ||
t.Errorf("registryMatches() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters