Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 1834222: Check for custom console route hostname not to be the same as the default route #426

Merged
merged 1 commit into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 20 additions & 6 deletions pkg/console/controllers/route/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,21 @@ func (c *RouteSyncController) sync() error {

statusHandler := status.NewStatusHandler(c.operatorClient)

defaultRoute, defaultRouteErrReason, defaultRouteErr := c.SyncDefaultRoute(updatedOperatorConfig)
statusHandler.AddConditions(status.HandleProgressingOrDegraded("DefaultRouteSync", defaultRouteErrReason, defaultRouteErr))
if defaultRouteErr != nil {
return statusHandler.FlushAndReturn(defaultRouteErr)
}

// try to sync the custom route first. If the sync fails for any reason, error
// out the sync loop and inform about this fact instead of putting default
// route into inaccessible state.
customRoute, customRouteErrReason, customRouteErr := c.SyncCustomRoute(updatedOperatorConfig)
statusHandler.AddConditions(status.HandleProgressingOrDegraded("CustomRouteSync", customRouteErrReason, customRouteErr))
if customRouteErr != nil {
return statusHandler.FlushAndReturn(customRouteErr)
}

defaultRoute, defaultRouteErrReason, defaultRouteErr := c.SyncDefaultRoute(updatedOperatorConfig)
statusHandler.AddConditions(status.HandleProgressingOrDegraded("DefaultRouteSync", defaultRouteErrReason, defaultRouteErr))
if defaultRouteErr != nil {
return statusHandler.FlushAndReturn(defaultRouteErr)
}

activeRoute := defaultRoute
if routesub.IsCustomRouteSet(updatedOperatorConfig) {
activeRoute = customRoute
Expand Down Expand Up @@ -228,6 +231,13 @@ func (c *RouteSyncController) ValidateCustomRouteConfig(operatorConfig *operator
if err != nil {
return nil, err
}

// Check if the custom route hostname is not same as the default one
defaultRouteHostname := GetDefaultRouteHost(ingress.Spec.Domain)
if operatorConfig.Spec.Route.Hostname == defaultRouteHostname {
return nil, fmt.Errorf("custom route hostname is duplicate of the default route hostname")
}

// Check if the custom hostname has cluster domain suffix, which indicates
// if a secret that contains TLS certificate and key needs to exist in the
// `openshift-config` namespace and referenced in the operator config.
Expand Down Expand Up @@ -286,6 +296,10 @@ func ValidateCustomCertSecret(customCertSecret *corev1.Secret) (*routesub.Custom
return customTLS, nil
}

func GetDefaultRouteHost(ingressDomain string) string {
return fmt.Sprintf("%s-%s.%s", api.OpenShiftConsoleRouteName, api.OpenShiftConsoleNamespace, ingressDomain)
}

func certificateVerifier(customCert []byte) error {
block, _ := pem.Decode([]byte(customCert))
if block == nil {
Expand Down
26 changes: 26 additions & 0 deletions pkg/console/controllers/route/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,3 +200,29 @@ func TestValidateCustomCertSecret(t *testing.T) {
})
}
}

func TestGetDefaultRouteHost(t *testing.T) {
type args struct {
ingressDomain string
}
tests := []struct {
name string
args args
want string
}{
{
name: "Test assembling linux amd64 specific URL",
args: args{
ingressDomain: "apps.devcluster.openshift.com",
},
want: "console-openshift-console.apps.devcluster.openshift.com",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if diff := deep.Equal(GetDefaultRouteHost(tt.args.ingressDomain), tt.want); diff != nil {
t.Error(diff)
}
})
}
}
17 changes: 0 additions & 17 deletions pkg/console/subresource/route/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,23 +59,6 @@ func ApplyRoute(client routeclient.RoutesGetter, recorder events.Recorder, requi
return actual, true, err
}

// ensures route exists.
// handles 404 with a create
// returns any other error
func GetOrCreate(ctx context.Context, client routeclient.RoutesGetter, required *routev1.Route) (*routev1.Route, bool, error) {
isNew := false
route, err := client.Routes(required.Namespace).Get(ctx, required.Name, metav1.GetOptions{})
if apierrors.IsNotFound(err) {
isNew = true
route, err = client.Routes(required.Namespace).Create(ctx, required, metav1.CreateOptions{})
}

if err != nil {
return nil, isNew, err
}
return route, isNew, nil
}

// Default `console` route points by default to the `console` service.
// If custom hostname for the console is set, then the default route
// should point to the redirect `console-redirect` service and the
Expand Down