Skip to content

Commit 0215a6c

Browse files
committed
oc login: Respect insecure flag from kubeconfig
When running oc login, the insecure flag from kubeconfig is not consulted properly when calling getClientConfig(). This is now fixed. Assisted-by: Claude Code
1 parent 1c5f490 commit 0215a6c

File tree

2 files changed

+76
-5
lines changed

2 files changed

+76
-5
lines changed

pkg/cli/login/loginoptions.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ func (o *LoginOptions) getClientConfig() (*restclient.Config, error) {
160160
}
161161
o.Server = serverNormalized
162162
clientConfig.Host = o.Server
163-
clientConfig.Insecure = o.InsecureTLS
163+
clientConfig.Insecure = o.InsecureTLS || hasExistingInsecureCluster(*clientConfig, *o.StartingKubeConfig)
164164

165-
if !o.InsecureTLS {
165+
if !clientConfig.Insecure {
166166
// use specified CA or find existing CA
167167
if len(o.CAFile) > 0 {
168168
clientConfig.CAFile = o.CAFile
@@ -188,9 +188,7 @@ func (o *LoginOptions) getClientConfig() (*restclient.Config, error) {
188188
// connection or if we already have a cluster stanza that tells us to
189189
// connect to this particular server insecurely
190190
case x509.UnknownAuthorityError, x509.HostnameError, x509.CertificateInvalidError:
191-
if o.InsecureTLS ||
192-
hasExistingInsecureCluster(*clientConfig, *o.StartingKubeConfig) ||
193-
promptForInsecureTLS(o.In, o.Out, err) {
191+
if clientConfig.Insecure || promptForInsecureTLS(o.In, o.Out, err) {
194192
clientConfig.Insecure = true
195193
clientConfig.CAFile = ""
196194
clientConfig.CAData = nil

pkg/cli/login/loginoptions_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,79 @@ func TestDialToHTTPSServer(t *testing.T) {
377377
}
378378
}
379379

380+
func TestGetClientConfig_InsecureSkipTLSVerify(t *testing.T) {
381+
// Test that insecure-skip-tls-verify setting from kubeconfig is respected
382+
// when logging in without the --insecure-skip-tls-verify flag.
383+
384+
server := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
385+
w.WriteHeader(http.StatusOK)
386+
}))
387+
defer server.Close()
388+
389+
testCases := map[string]struct {
390+
insecureFlag bool
391+
insecureKubeconfig bool
392+
expectedInsecureClientConfig bool
393+
}{
394+
"command flag set": {
395+
insecureFlag: true,
396+
expectedInsecureClientConfig: true,
397+
},
398+
"kubeconfig flag set": {
399+
insecureKubeconfig: true,
400+
expectedInsecureClientConfig: true,
401+
},
402+
"no flag set": {
403+
insecureFlag: false,
404+
insecureKubeconfig: false,
405+
expectedInsecureClientConfig: false,
406+
},
407+
"both command and kubeconfig flag set": {
408+
insecureFlag: true,
409+
insecureKubeconfig: true,
410+
expectedInsecureClientConfig: true,
411+
},
412+
}
413+
414+
for name, test := range testCases {
415+
t.Run(name, func(t *testing.T) {
416+
startingConfig := &kclientcmdapi.Config{
417+
Clusters: map[string]*kclientcmdapi.Cluster{},
418+
}
419+
if test.insecureKubeconfig {
420+
startingConfig.Clusters["test-cluster"] = &kclientcmdapi.Cluster{
421+
Server: server.URL,
422+
InsecureSkipTLSVerify: true,
423+
}
424+
}
425+
426+
options := &LoginOptions{
427+
Server: server.URL,
428+
InsecureTLS: test.insecureFlag,
429+
StartingKubeConfig: startingConfig,
430+
}
431+
432+
clientConfig, err := options.getClientConfig()
433+
if err != nil {
434+
if test.expectedInsecureClientConfig {
435+
t.Fatalf("Expected to succeed with insecure connection, but got error: %v", err)
436+
} else {
437+
// If we expect secure connection and get a TLS error, that's expected
438+
// since we're using a test server with a self-signed cert.
439+
if err.Error() != certificateAuthorityUnknownMsg {
440+
t.Fatalf("Expected to fail with insecure connection, but got another error: %v", err)
441+
}
442+
return
443+
}
444+
}
445+
446+
if clientConfig.Insecure != test.expectedInsecureClientConfig {
447+
t.Errorf("expected Insecure=%v, got %v", test.expectedInsecureClientConfig, clientConfig.Insecure)
448+
}
449+
})
450+
}
451+
}
452+
380453
func TestPreserveExecProviderOnUsernameLogin(t *testing.T) {
381454
// Test that when using -u flag with existing OIDC credentials,
382455
// the ExecProvider configuration is preserved

0 commit comments

Comments
 (0)