Skip to content

Commit 50b5201

Browse files
committed
fix insecure
1 parent f1be565 commit 50b5201

File tree

1 file changed

+59
-17
lines changed

1 file changed

+59
-17
lines changed

pkg/controller/vsphere/session/session.go

Lines changed: 59 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package session
1818

1919
import (
2020
"context"
21+
"crypto/tls"
2122
"errors"
2223
"fmt"
2324
"io"
@@ -114,24 +115,48 @@ func (t *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
114115
var soapResp SOAPResponse
115116
if err := xml.Unmarshal(body, &soapResp); err == nil {
116117
if soapResp.Body.Fault != nil {
117-
klog.Error("=== PRIVILEGE ERROR DETECTED ===")
118-
klog.Errorf("Fault Code: %s\n", soapResp.Body.Fault.Code.Value)
119-
klog.Errorf("Fault Reason: %s\n", soapResp.Body.Fault.Reason.Value)
120-
klog.Errorf("Fault Detail: %s\n", soapResp.Body.Fault.Detail.Content)
121-
klog.Error("================================\n")
118+
klog.Error("=== SOAP FAULT DETECTED ===")
119+
klog.Errorf("Fault Code: %s", soapResp.Body.Fault.Code.Value)
120+
klog.Errorf("Fault Reason: %s", soapResp.Body.Fault.Reason.Value)
121+
klog.Errorf("Fault Detail: %s", soapResp.Body.Fault.Detail.Content)
122+
123+
// Check if this is an authentication error
124+
if strings.Contains(strings.ToLower(soapResp.Body.Fault.Reason.Value), "incorrect user name or password") ||
125+
strings.Contains(strings.ToLower(soapResp.Body.Fault.Reason.Value), "cannot complete login") {
126+
klog.Error("=== AUTHENTICATION ERROR DETECTED ===")
127+
klog.Error("Please verify your vSphere username and password credentials")
128+
klog.Error("================================================")
129+
}
130+
klog.Error("================================")
122131
}
123132
}
124133

125-
// Check for privilege-related error messages in the response
134+
// Check for authentication-related error messages in the response
126135
bodyStr := string(body)
136+
authKeywords := []string{
137+
"incorrect user name or password", "cannot complete login", "invalidlogin",
138+
"authentication failed", "login failed", "invalid credentials",
139+
}
140+
for _, keyword := range authKeywords {
141+
if strings.Contains(strings.ToLower(bodyStr), strings.ToLower(keyword)) {
142+
klog.Errorf("=== AUTHENTICATION ISSUE DETECTED (keyword: %s) ===", keyword)
143+
klog.Error("Response contains authentication-related content")
144+
klog.Error("Please verify your vSphere username and password")
145+
klog.Error("================================================")
146+
break
147+
}
148+
}
149+
150+
// Check for privilege-related error messages in the response
127151
privilegeKeywords := []string{
128152
"privilege", "permission", "access denied", "unauthorized", "forbidden",
129-
"NoPermission", "InvalidLogin", "InvalidPrivilege",
153+
"NoPermission", "InvalidPrivilege", "insufficient privileges",
130154
}
131155
for _, keyword := range privilegeKeywords {
132156
if strings.Contains(strings.ToLower(bodyStr), strings.ToLower(keyword)) {
133-
klog.Errorf("=== POTENTIAL PRIVILEGE ISSUE DETECTED (keyword: %s) ===\n", keyword)
134-
klog.Error("Response contains privilege-related content\n")
157+
klog.Errorf("=== POTENTIAL PRIVILEGE ISSUE DETECTED (keyword: %s) ===", keyword)
158+
klog.Error("Response contains privilege-related content")
159+
klog.Error("Please verify user has sufficient vSphere permissions")
135160
klog.Error("==================================================")
136161
break
137162
}
@@ -159,7 +184,7 @@ func newClientWithTimeout(ctx context.Context, u *url.URL, insecure bool, timeou
159184
*/
160185

161186
customTransport := &CustomTransport{
162-
RoundTripper: http.DefaultTransport,
187+
RoundTripper: createTransport(insecure),
163188
}
164189

165190
soapClient := soap.NewClient(u, insecure)
@@ -177,12 +202,8 @@ func newClientWithTimeout(ctx context.Context, u *url.URL, insecure bool, timeou
177202
SessionManager: session.NewManager(vimClient),
178203
}
179204

180-
// Login to vSphere
181-
err = client.Login(ctx, u.User)
182-
if err != nil {
183-
log.Fatalf("Failed to login to vSphere: %v", err)
184-
}
185-
defer client.Logout(clientCreateCtx)
205+
// Note: We don't login here because u.User is nil
206+
// The actual login happens later in GetOrCreate with proper credentials
186207

187208
// Create SOAP client with custom transport
188209
//client.Transport = customTransport
@@ -210,7 +231,7 @@ func GetOrCreate(
210231
return &session, nil
211232
}
212233
}
213-
klog.Infof("No existing vCenter session found, creating new session")
234+
klog.Infof("No existing vCenter session found, creating new session for server: %s, datacenter: %s, username: %s", server, datacenter, username)
214235

215236
soapURL, err := soap.ParseURL(server)
216237
if err != nil {
@@ -230,6 +251,12 @@ func GetOrCreate(
230251
// Set up user agent before login for being able to track mapi component in vcenter sessions list
231252
client.UserAgent = "machineAPIvSphereProvider"
232253
if err := client.Login(ctx, url.UserPassword(username, password)); err != nil {
254+
// Check if it's a credential-related error
255+
if strings.Contains(err.Error(), "incorrect user name or password") ||
256+
strings.Contains(err.Error(), "Cannot complete login") ||
257+
strings.Contains(err.Error(), "InvalidLogin") {
258+
return nil, fmt.Errorf("vSphere authentication failed - please verify username and password: %w", err)
259+
}
233260
return nil, fmt.Errorf("unable to login to vCenter: %w", err)
234261
}
235262

@@ -363,3 +390,18 @@ func (s *Session) WithCachingTagsManager(ctx context.Context, f func(m *CachingT
363390

364391
return f(m)
365392
}
393+
394+
// createTransport creates a transport that respects the insecure flag
395+
func createTransport(insecure bool) http.RoundTripper {
396+
if insecure {
397+
// Create a transport that skips TLS verification
398+
transport := &http.Transport{
399+
TLSClientConfig: &tls.Config{
400+
InsecureSkipVerify: true,
401+
},
402+
}
403+
return transport
404+
}
405+
// Use default transport for secure connections
406+
return http.DefaultTransport
407+
}

0 commit comments

Comments
 (0)