@@ -18,6 +18,7 @@ package session
18
18
19
19
import (
20
20
"context"
21
+ "crypto/tls"
21
22
"errors"
22
23
"fmt"
23
24
"io"
@@ -114,24 +115,48 @@ func (t *CustomTransport) RoundTrip(req *http.Request) (*http.Response, error) {
114
115
var soapResp SOAPResponse
115
116
if err := xml .Unmarshal (body , & soapResp ); err == nil {
116
117
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 ("================================" )
122
131
}
123
132
}
124
133
125
- // Check for privilege -related error messages in the response
134
+ // Check for authentication -related error messages in the response
126
135
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
127
151
privilegeKeywords := []string {
128
152
"privilege" , "permission" , "access denied" , "unauthorized" , "forbidden" ,
129
- "NoPermission" , "InvalidLogin " , "InvalidPrivilege " ,
153
+ "NoPermission" , "InvalidPrivilege " , "insufficient privileges " ,
130
154
}
131
155
for _ , keyword := range privilegeKeywords {
132
156
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" )
135
160
klog .Error ("==================================================" )
136
161
break
137
162
}
@@ -159,7 +184,7 @@ func newClientWithTimeout(ctx context.Context, u *url.URL, insecure bool, timeou
159
184
*/
160
185
161
186
customTransport := & CustomTransport {
162
- RoundTripper : http . DefaultTransport ,
187
+ RoundTripper : createTransport ( insecure ) ,
163
188
}
164
189
165
190
soapClient := soap .NewClient (u , insecure )
@@ -177,12 +202,8 @@ func newClientWithTimeout(ctx context.Context, u *url.URL, insecure bool, timeou
177
202
SessionManager : session .NewManager (vimClient ),
178
203
}
179
204
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
186
207
187
208
// Create SOAP client with custom transport
188
209
//client.Transport = customTransport
@@ -210,7 +231,7 @@ func GetOrCreate(
210
231
return & session , nil
211
232
}
212
233
}
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 )
214
235
215
236
soapURL , err := soap .ParseURL (server )
216
237
if err != nil {
@@ -230,6 +251,12 @@ func GetOrCreate(
230
251
// Set up user agent before login for being able to track mapi component in vcenter sessions list
231
252
client .UserAgent = "machineAPIvSphereProvider"
232
253
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
+ }
233
260
return nil , fmt .Errorf ("unable to login to vCenter: %w" , err )
234
261
}
235
262
@@ -363,3 +390,18 @@ func (s *Session) WithCachingTagsManager(ctx context.Context, f func(m *CachingT
363
390
364
391
return f (m )
365
392
}
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