Skip to content

Commit

Permalink
fix: registry check strips port (#4185)
Browse files Browse the repository at this point in the history
  • Loading branch information
emosbaugh authored Nov 30, 2023
1 parent 2c823f6 commit fa8e0ac
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 15 deletions.
10 changes: 5 additions & 5 deletions cmd/kots/cli/admin-console-push-images.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ func AdminPushImagesCmd() *cobra.Command {
}

func genAndCheckPushOptions(endpoint string, namespace string, log *logger.CLILogger, v *viper.Viper) (*kotsadmtypes.PushImagesOptions, error) {
hostname, err := getHostnameFromEndpoint(endpoint)
host, err := getHostFromEndpoint(endpoint)
if err != nil {
return nil, errors.Wrap(err, "failed get hostname from endpoint")
return nil, errors.Wrap(err, "failed get host from endpoint")
}

username := v.GetString("registry-username")
password := v.GetString("registry-password")
if username == "" && password == "" {
u, p, err := getRegistryCredentialsFromSecret(hostname, namespace)
u, p, err := getRegistryCredentialsFromSecret(host, namespace)
if err != nil {
if !kuberneteserrors.IsNotFound(err) {
log.Info("Failed to find registry credentials, will try to push anonymously: %v", err)
Expand All @@ -107,9 +107,9 @@ func genAndCheckPushOptions(endpoint string, namespace string, log *logger.CLILo
if !v.GetBool("skip-registry-check") {
log.ActionWithSpinner("Validating registry information")

if err := dockerregistry.CheckAccess(hostname, username, password); err != nil {
if err := dockerregistry.CheckAccess(host, username, password); err != nil {
log.FinishSpinnerWithError()
return nil, fmt.Errorf("Failed to test access to %q with user %q: %v", hostname, username, err)
return nil, fmt.Errorf("Failed to test access to %q with user %q: %v", host, username, err)
}
log.FinishSpinner()
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/kots/cli/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ func InstallCmd() *cobra.Command {
if registryConfig.OverrideRegistry != "" && !v.GetBool("skip-registry-check") {
log.ActionWithSpinner("Validating registry information")

hostname, err := getHostnameFromEndpoint(registryConfig.OverrideRegistry)
host, err := getHostFromEndpoint(registryConfig.OverrideRegistry)
if err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "failed get hostname from endpoint")
return errors.Wrap(err, "failed get host from endpoint")
}

if err := dockerregistry.CheckAccess(hostname, registryConfig.Username, registryConfig.Password); err != nil {
if err := dockerregistry.CheckAccess(host, registryConfig.Username, registryConfig.Password); err != nil {
log.FinishSpinnerWithError()
return fmt.Errorf("Failed to test access to %q with user %q: %v", hostname, registryConfig.Username, err)
return fmt.Errorf("Failed to test access to %q with user %q: %v", host, registryConfig.Username, err)
}

log.FinishSpinner()
Expand Down
8 changes: 4 additions & 4 deletions cmd/kots/cli/upstream-upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,15 @@ func UpstreamUpgradeCmd() *cobra.Command {
if registryConfig.OverrideRegistry != "" && !v.GetBool("skip-registry-check") {
log.ActionWithSpinner("Validating registry information")

hostname, err := getHostnameFromEndpoint(registryConfig.OverrideRegistry)
host, err := getHostFromEndpoint(registryConfig.OverrideRegistry)
if err != nil {
log.FinishSpinnerWithError()
return errors.Wrap(err, "failed get hostname from endpoint")
return errors.Wrap(err, "failed get host from endpoint")
}

if err := dockerregistry.CheckAccess(hostname, registryConfig.Username, registryConfig.Password); err != nil {
if err := dockerregistry.CheckAccess(host, registryConfig.Username, registryConfig.Password); err != nil {
log.FinishSpinnerWithError()
return fmt.Errorf("Failed to test access to %q with user %q: %v", hostname, registryConfig.Username, err)
return fmt.Errorf("Failed to test access to %q with user %q: %v", host, registryConfig.Username, err)
}

log.FinishSpinner()
Expand Down
4 changes: 2 additions & 2 deletions cmd/kots/cli/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func ExpandDir(input string) string {
return uploadPath
}

func getHostnameFromEndpoint(endpoint string) (string, error) {
func getHostFromEndpoint(endpoint string) (string, error) {
if !strings.HasPrefix(endpoint, "http") {
// url.Parse doesn't work without scheme
endpoint = fmt.Sprintf("https://%s", endpoint)
Expand All @@ -38,5 +38,5 @@ func getHostnameFromEndpoint(endpoint string) (string, error) {
return "", errors.Wrap(err, "failed to parse endpoint")
}

return parsed.Hostname(), nil
return parsed.Host, nil
}
49 changes: 49 additions & 0 deletions cmd/kots/cli/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,52 @@ func TestExpandDir(t *testing.T) {
})
}
}

func Test_getHostFromEndpoint(t *testing.T) {
type args struct {
endpoint string
}
tests := []struct {
name string
args args
want string
wantErr bool
}{
{
"endpoint without scheme",
args{
endpoint: "localhost",
},
"localhost",
false,
},
{
"endpoint with scheme",
args{
endpoint: "https://localhost",
},
"localhost",
false,
},
{
"endpoint with port",
args{
endpoint: "localhost:3000",
},
"localhost:3000",
false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := getHostFromEndpoint(tt.args.endpoint)
if (err != nil) != tt.wantErr {
t.Errorf("getHostFromEndpoint() error = %v, wantErr %v", err, tt.wantErr)
return
}
if got != tt.want {
t.Errorf("getHostFromEndpoint() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit fa8e0ac

Please sign in to comment.