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

fix: Add session lookup fallback for soap header auth #3672

Merged
merged 1 commit into from
Jan 7, 2025
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions vim25/soap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,16 +214,31 @@ func (c *Client) NewServiceClient(path string, namespace string) *Client {
return c.newServiceClientWithTransport(path, namespace, c.t)
}

// SessionCookie returns a SessionCookie with value of the vmware_soap_session http.Cookie.
func (c *Client) SessionCookie() *HeaderElement {
for _, cookie := range c.Jar.Cookies(c.URL()) {
func sessionCookie(jar http.CookieJar, u *url.URL) *HeaderElement {
for _, cookie := range jar.Cookies(u) {
if cookie.Name == SessionCookieName {
return &HeaderElement{Value: cookie.Value}
}
}
return nil
}

// SessionCookie returns a SessionCookie with value of the vmware_soap_session http.Cookie.
func (c *Client) SessionCookie() *HeaderElement {
u := c.URL()

if cookie := sessionCookie(c.Jar, u); cookie != nil {
return cookie
}

// Default "/sdk" Path would match above,
// but saw a case of Path == "sdk", where above returns nil.
// The jar entry Path is normally "/", so fallback to that.
u.Path = "/"

return sessionCookie(c.Jar, u)
}

func (c *Client) newServiceClientWithTransport(path string, namespace string, t *http.Transport) *Client {
vc := c.URL()
u, err := url.Parse(path)
Expand Down
26 changes: 26 additions & 0 deletions vim25/soap/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,29 @@ func TestParseURL(t *testing.T) {
})
}
}

func TestSessionCookie(t *testing.T) {
u := &url.URL{
Scheme: "http",
Host: "localhost:1080",
Path: "sdk", // see comment in Client.SessionCookie
}

c := NewClient(u, true)

cookie := &http.Cookie{
Name: SessionCookieName,
Value: "ANY",
Path: "/",
Domain: "localhost",
HttpOnly: true,
Secure: false,
}

c.Jar.SetCookies(u, []*http.Cookie{cookie})

val := c.SessionCookie()
if val == nil {
t.Fatal("no session cookie")
}
}
Loading