Skip to content

Commit dc9ff60

Browse files
authored
Merge pull request #2012 from dougm/rest-header-auth
vapi: use header authentication in file Upload/Download
2 parents bf72e9f + be7742f commit dc9ff60

File tree

4 files changed

+37
-6
lines changed

4 files changed

+37
-6
lines changed

govc/library/export.go

+1-3
Original file line numberDiff line numberDiff line change
@@ -138,9 +138,7 @@ func (cmd *export) Run(ctx context.Context, f *flag.FlagSet) error {
138138

139139
download := func(src *url.URL, name string) error {
140140
p := soap.DefaultDownload
141-
p.Headers = map[string]string{
142-
"vmware-api-session-id": c.SessionID(),
143-
}
141+
144142
if isStdout {
145143
s, _, err := c.Download(ctx, src, &p)
146144
if err != nil {

govc/library/import.go

-3
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,6 @@ func (cmd *item) Run(ctx context.Context, f *flag.FlagSet) error {
202202
}
203203

204204
p := soap.DefaultUpload
205-
p.Headers = map[string]string{
206-
"vmware-api-session-id": session,
207-
}
208205
p.ContentLength = size
209206
u, err := url.Parse(update.UploadEndpoint.URI)
210207
if err != nil {

govc/test/library.bats

+1
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ load test_helper
107107

108108
name="$BATS_TMPDIR/govc-$id-export"
109109
run govc library.export "/my-content/$TTYLINUX_NAME/*.ovf" "$name"
110+
assert_success
110111
assert_equal "$(cat "$GOVC_IMAGES/$TTYLINUX_NAME.ovf")" "$(cat "$name")"
111112
rm "$name"
112113

vapi/rest/client.go

+35
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,41 @@ func (c *Client) Do(ctx context.Context, req *http.Request, resBody interface{})
184184
})
185185
}
186186

187+
// authHeaders ensures the given map contains a REST auth header
188+
func (c *Client) authHeaders(h map[string]string) map[string]string {
189+
if _, exists := h[internal.SessionCookieName]; exists {
190+
return h
191+
}
192+
if h == nil {
193+
h = make(map[string]string)
194+
}
195+
196+
h[internal.SessionCookieName] = c.SessionID()
197+
198+
return h
199+
}
200+
201+
// Download wraps soap.Client.Download, adding the REST authentication header
202+
func (c *Client) Download(ctx context.Context, u *url.URL, param *soap.Download) (io.ReadCloser, int64, error) {
203+
p := *param
204+
p.Headers = c.authHeaders(p.Headers)
205+
return c.Client.Download(ctx, u, &p)
206+
}
207+
208+
// DownloadFile wraps soap.Client.DownloadFile, adding the REST authentication header
209+
func (c *Client) DownloadFile(ctx context.Context, file string, u *url.URL, param *soap.Download) error {
210+
p := *param
211+
p.Headers = c.authHeaders(p.Headers)
212+
return c.Client.DownloadFile(ctx, file, u, &p)
213+
}
214+
215+
// Upload wraps soap.Client.Upload, adding the REST authentication header
216+
func (c *Client) Upload(ctx context.Context, f io.Reader, u *url.URL, param *soap.Upload) error {
217+
p := *param
218+
p.Headers = c.authHeaders(p.Headers)
219+
return c.Client.Upload(ctx, f, u, &p)
220+
}
221+
187222
// Login creates a new session via Basic Authentication with the given url.Userinfo.
188223
func (c *Client) Login(ctx context.Context, user *url.Userinfo) error {
189224
req := c.Resource(internal.SessionPath).Request(http.MethodPost)

0 commit comments

Comments
 (0)