Skip to content

Commit ea9ccf9

Browse files
amasolovcursoragent
andcommitted
fix(cpo): use KubeAPIServerDNSName for OAuth LoginURL when set
When KubeAPIServerDNSName is configured on a HostedControlPlane, the OAuth token display page shows the ControlPlaneEndpoint host (typically a LoadBalancer IP) in the oc login command instead of the custom FQDN. The LoginURL in the OAuth server config determines what users see as the --server value on the token display page. Previously it always used ControlPlaneEndpoint.Host, which does not reflect a custom DNS name set via spec.kubeAPIServerDNSName. This change makes the OAuth config prefer KubeAPIServerDNSName for the LoginURL when set, while preserving the existing IBMCloud login URL override annotation as the highest-priority override. Signed-off-by: Alexey Masolov <amasolov@redhat.com> Co-authored-by: Cursor <cursoragent@cursor.com>
1 parent 7ac2953 commit ea9ccf9

2 files changed

Lines changed: 114 additions & 1 deletion

File tree

control-plane-operator/controllers/hostedcontrolplane/v2/oauth/config.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ func adaptOAuthConfig(cpContext component.WorkloadContext, cfg *osinv1.OsinServe
7272
controlPlaneEndpoint := cpContext.HCP.Status.ControlPlaneEndpoint
7373
cfg.OAuthConfig.MasterURL = masterUrl
7474
cfg.OAuthConfig.MasterPublicURL = masterUrl
75-
cfg.OAuthConfig.LoginURL = fmt.Sprintf("https://%s:%d", controlPlaneEndpoint.Host, controlPlaneEndpoint.Port)
75+
76+
loginHost := controlPlaneEndpoint.Host
77+
if customDNS := cpContext.HCP.Spec.KubeAPIServerDNSName; len(customDNS) > 0 {
78+
loginHost = customDNS
79+
}
80+
cfg.OAuthConfig.LoginURL = fmt.Sprintf("https://%s:%d", loginHost, controlPlaneEndpoint.Port)
81+
7682
// loginURLOverride can be used to specify an override for the oauth config login url. The need for this arises
7783
// when the login a provider uses doesn't conform to the standard login url in hypershift. The only supported use case
7884
// for this is IBMCloud Red Hat Openshift
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
package oauth
2+
3+
import (
4+
"testing"
5+
6+
. "github.com/onsi/gomega"
7+
8+
hyperv1 "github.com/openshift/hypershift/api/hypershift/v1beta1"
9+
"github.com/openshift/hypershift/control-plane-operator/controllers/hostedcontrolplane/infra"
10+
component "github.com/openshift/hypershift/support/controlplane-component"
11+
12+
osinv1 "github.com/openshift/api/osin/v1"
13+
)
14+
15+
func TestAdaptOAuthConfig(t *testing.T) {
16+
testCases := []struct {
17+
name string
18+
oauthHost string
19+
oauthPort int32
20+
cpEndpointHost string
21+
cpEndpointPort int32
22+
kasDNSName string
23+
loginURLOverride string
24+
expectedLoginURL string
25+
expectedMasterURL string
26+
}{
27+
{
28+
name: "When no custom DNS is set, it should use the control plane endpoint for LoginURL",
29+
oauthHost: "oauth.example.com",
30+
oauthPort: 443,
31+
cpEndpointHost: "api.example.com",
32+
cpEndpointPort: 6443,
33+
expectedLoginURL: "https://api.example.com:6443",
34+
expectedMasterURL: "https://oauth.example.com:443",
35+
},
36+
{
37+
name: "When KubeAPIServerDNSName is set, it should use the custom DNS name for LoginURL",
38+
oauthHost: "oauth.example.com",
39+
oauthPort: 443,
40+
cpEndpointHost: "10.0.0.1",
41+
cpEndpointPort: 6443,
42+
kasDNSName: "api.custom.example.com",
43+
expectedLoginURL: "https://api.custom.example.com:6443",
44+
expectedMasterURL: "https://oauth.example.com:443",
45+
},
46+
{
47+
name: "When control plane endpoint is an IP and no custom DNS is set, it should use the IP for LoginURL",
48+
oauthHost: "10.0.0.2",
49+
oauthPort: 443,
50+
cpEndpointHost: "10.0.0.1",
51+
cpEndpointPort: 6443,
52+
expectedLoginURL: "https://10.0.0.1:6443",
53+
expectedMasterURL: "https://10.0.0.2:443",
54+
},
55+
{
56+
name: "When login URL override annotation is set, it should take precedence over KubeAPIServerDNSName",
57+
oauthHost: "oauth.example.com",
58+
oauthPort: 443,
59+
cpEndpointHost: "10.0.0.1",
60+
cpEndpointPort: 6443,
61+
kasDNSName: "api.custom.example.com",
62+
loginURLOverride: "https://ibm.override.example.com:6443",
63+
expectedLoginURL: "https://ibm.override.example.com:6443",
64+
expectedMasterURL: "https://oauth.example.com:443",
65+
},
66+
}
67+
68+
for _, tc := range testCases {
69+
t.Run(tc.name, func(t *testing.T) {
70+
g := NewWithT(t)
71+
72+
hcp := &hyperv1.HostedControlPlane{
73+
Spec: hyperv1.HostedControlPlaneSpec{
74+
KubeAPIServerDNSName: tc.kasDNSName,
75+
},
76+
Status: hyperv1.HostedControlPlaneStatus{
77+
ControlPlaneEndpoint: hyperv1.APIEndpoint{
78+
Host: tc.cpEndpointHost,
79+
Port: tc.cpEndpointPort,
80+
},
81+
},
82+
}
83+
if tc.loginURLOverride != "" {
84+
hcp.Annotations = map[string]string{
85+
hyperv1.OauthLoginURLOverrideAnnotation: tc.loginURLOverride,
86+
}
87+
}
88+
89+
cpContext := component.WorkloadContext{
90+
HCP: hcp,
91+
InfraStatus: infra.InfrastructureStatus{
92+
OAuthHost: tc.oauthHost,
93+
OAuthPort: tc.oauthPort,
94+
},
95+
}
96+
97+
cfg := &osinv1.OsinServerConfig{}
98+
cfg.OAuthConfig = osinv1.OAuthConfig{}
99+
100+
adaptOAuthConfig(cpContext, cfg)
101+
102+
g.Expect(cfg.OAuthConfig.LoginURL).To(Equal(tc.expectedLoginURL))
103+
g.Expect(cfg.OAuthConfig.MasterURL).To(Equal(tc.expectedMasterURL))
104+
g.Expect(cfg.OAuthConfig.MasterPublicURL).To(Equal(tc.expectedMasterURL))
105+
})
106+
}
107+
}

0 commit comments

Comments
 (0)