1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
22 "git.arvados.org/arvados.git/sdk/go/arvados"
23 "git.arvados.org/arvados.git/sdk/go/auth"
26 type TokenProvider func(context.Context) ([]string, error)
28 func PassthroughTokenProvider(ctx context.Context) ([]string, error) {
29 if incoming, ok := auth.FromContext(ctx); !ok {
30 return nil, errors.New("no token provided")
32 return incoming.Tokens, nil
37 SendHeader http.Header
39 httpClient http.Client
41 tokenProvider TokenProvider
44 func NewConn(clusterID string, url *url.URL, insecure bool, tp TokenProvider) *Conn {
45 transport := http.DefaultTransport
47 // It's not safe to copy *http.DefaultTransport
48 // because it has a mutex (which might be locked)
49 // protecting a private map (which might not be nil).
50 // So we build our own, using the Go 1.12 default
51 // values, ignoring any changes the application has
52 // made to http.DefaultTransport.
53 transport = &http.Transport{
54 DialContext: (&net.Dialer{
55 Timeout: 30 * time.Second,
56 KeepAlive: 30 * time.Second,
60 IdleConnTimeout: 90 * time.Second,
61 TLSHandshakeTimeout: 10 * time.Second,
62 ExpectContinueTimeout: 1 * time.Second,
63 TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
68 httpClient: http.Client{
69 CheckRedirect: func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse },
77 func (conn *Conn) requestAndDecode(ctx context.Context, dst interface{}, ep arvados.APIEndpoint, body io.Reader, opts interface{}) error {
78 aClient := arvados.Client{
79 Client: &conn.httpClient,
80 Scheme: conn.baseURL.Scheme,
81 APIHost: conn.baseURL.Host,
82 SendHeader: conn.SendHeader,
84 tokens, err := conn.tokenProvider(ctx)
87 } else if len(tokens) > 0 {
88 ctx = arvados.ContextWithAuthorization(ctx, "Bearer "+tokens[0])
90 // Use a non-empty auth string to ensure we override
91 // any default token set on aClient -- and to avoid
92 // having the remote prompt us to send a token by
94 ctx = arvados.ContextWithAuthorization(ctx, "Bearer -")
97 // Encode opts to JSON and decode from there to a
98 // map[string]interface{}, so we can munge the query params
99 // using the JSON key names specified by opts' struct tags.
100 j, err := json.Marshal(opts)
102 return fmt.Errorf("%T: requestAndDecode: Marshal opts: %s", conn, err)
104 var params map[string]interface{}
105 dec := json.NewDecoder(bytes.NewBuffer(j))
107 err = dec.Decode(¶ms)
109 return fmt.Errorf("%T: requestAndDecode: Decode opts: %s", conn, err)
111 if attrs, ok := params["attrs"]; ok && ep.AttrsKey != "" {
112 params[ep.AttrsKey] = attrs
113 delete(params, "attrs")
115 if limitStr, ok := params["limit"]; ok {
116 if limit, err := strconv.ParseInt(string(limitStr.(json.Number)), 10, 64); err == nil && limit < 0 {
117 // Negative limit means "not specified" here, but some
118 // servers/versions do not accept that, so we need to
119 // remove it entirely.
120 delete(params, "limit")
124 params["reader_tokens"] = tokens[1:]
127 if strings.Contains(ep.Path, "/{uuid}") {
128 uuid, _ := params["uuid"].(string)
129 path = strings.Replace(path, "/{uuid}", "/"+uuid, 1)
130 delete(params, "uuid")
132 return aClient.RequestAndDecodeContext(ctx, dst, ep.Method, path, body, params)
135 func (conn *Conn) BaseURL() url.URL {
139 func (conn *Conn) ConfigGet(ctx context.Context) (json.RawMessage, error) {
140 ep := arvados.EndpointConfigGet
141 var resp json.RawMessage
142 err := conn.requestAndDecode(ctx, &resp, ep, nil, nil)
146 func (conn *Conn) Login(ctx context.Context, options arvados.LoginOptions) (arvados.LoginResponse, error) {
147 ep := arvados.EndpointLogin
148 var resp arvados.LoginResponse
149 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
150 resp.RedirectLocation = conn.relativeToBaseURL(resp.RedirectLocation)
154 func (conn *Conn) Logout(ctx context.Context, options arvados.LogoutOptions) (arvados.LogoutResponse, error) {
155 ep := arvados.EndpointLogout
156 var resp arvados.LogoutResponse
157 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
158 resp.RedirectLocation = conn.relativeToBaseURL(resp.RedirectLocation)
162 // If the given location is a valid URL and its origin is the same as
163 // conn.baseURL, return it as a relative URL. Otherwise, return it
165 func (conn *Conn) relativeToBaseURL(location string) string {
166 u, err := url.Parse(location)
167 if err == nil && u.Scheme == conn.baseURL.Scheme && strings.ToLower(u.Host) == strings.ToLower(conn.baseURL.Host) {
178 func (conn *Conn) CollectionCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Collection, error) {
179 ep := arvados.EndpointCollectionCreate
180 var resp arvados.Collection
181 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
185 func (conn *Conn) CollectionUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Collection, error) {
186 ep := arvados.EndpointCollectionUpdate
187 var resp arvados.Collection
188 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
192 func (conn *Conn) CollectionGet(ctx context.Context, options arvados.GetOptions) (arvados.Collection, error) {
193 ep := arvados.EndpointCollectionGet
194 var resp arvados.Collection
195 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
199 func (conn *Conn) CollectionList(ctx context.Context, options arvados.ListOptions) (arvados.CollectionList, error) {
200 ep := arvados.EndpointCollectionList
201 var resp arvados.CollectionList
202 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
206 func (conn *Conn) CollectionProvenance(ctx context.Context, options arvados.GetOptions) (map[string]interface{}, error) {
207 ep := arvados.EndpointCollectionProvenance
208 var resp map[string]interface{}
209 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
213 func (conn *Conn) CollectionUsedBy(ctx context.Context, options arvados.GetOptions) (map[string]interface{}, error) {
214 ep := arvados.EndpointCollectionUsedBy
215 var resp map[string]interface{}
216 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
220 func (conn *Conn) CollectionDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Collection, error) {
221 ep := arvados.EndpointCollectionDelete
222 var resp arvados.Collection
223 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
227 func (conn *Conn) CollectionTrash(ctx context.Context, options arvados.DeleteOptions) (arvados.Collection, error) {
228 ep := arvados.EndpointCollectionTrash
229 var resp arvados.Collection
230 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
234 func (conn *Conn) CollectionUntrash(ctx context.Context, options arvados.UntrashOptions) (arvados.Collection, error) {
235 ep := arvados.EndpointCollectionUntrash
236 var resp arvados.Collection
237 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
241 func (conn *Conn) ContainerCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Container, error) {
242 ep := arvados.EndpointContainerCreate
243 var resp arvados.Container
244 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
248 func (conn *Conn) ContainerUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Container, error) {
249 ep := arvados.EndpointContainerUpdate
250 var resp arvados.Container
251 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
255 func (conn *Conn) ContainerGet(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
256 ep := arvados.EndpointContainerGet
257 var resp arvados.Container
258 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
262 func (conn *Conn) ContainerList(ctx context.Context, options arvados.ListOptions) (arvados.ContainerList, error) {
263 ep := arvados.EndpointContainerList
264 var resp arvados.ContainerList
265 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
269 func (conn *Conn) ContainerDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Container, error) {
270 ep := arvados.EndpointContainerDelete
271 var resp arvados.Container
272 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
276 func (conn *Conn) ContainerLock(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
277 ep := arvados.EndpointContainerLock
278 var resp arvados.Container
279 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
283 func (conn *Conn) ContainerUnlock(ctx context.Context, options arvados.GetOptions) (arvados.Container, error) {
284 ep := arvados.EndpointContainerUnlock
285 var resp arvados.Container
286 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
290 func (conn *Conn) SpecimenCreate(ctx context.Context, options arvados.CreateOptions) (arvados.Specimen, error) {
291 ep := arvados.EndpointSpecimenCreate
292 var resp arvados.Specimen
293 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
297 func (conn *Conn) SpecimenUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.Specimen, error) {
298 ep := arvados.EndpointSpecimenUpdate
299 var resp arvados.Specimen
300 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
304 func (conn *Conn) SpecimenGet(ctx context.Context, options arvados.GetOptions) (arvados.Specimen, error) {
305 ep := arvados.EndpointSpecimenGet
306 var resp arvados.Specimen
307 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
311 func (conn *Conn) SpecimenList(ctx context.Context, options arvados.ListOptions) (arvados.SpecimenList, error) {
312 ep := arvados.EndpointSpecimenList
313 var resp arvados.SpecimenList
314 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
318 func (conn *Conn) SpecimenDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.Specimen, error) {
319 ep := arvados.EndpointSpecimenDelete
320 var resp arvados.Specimen
321 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
325 func (conn *Conn) UserCreate(ctx context.Context, options arvados.CreateOptions) (arvados.User, error) {
326 ep := arvados.EndpointUserCreate
327 var resp arvados.User
328 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
331 func (conn *Conn) UserUpdate(ctx context.Context, options arvados.UpdateOptions) (arvados.User, error) {
332 ep := arvados.EndpointUserUpdate
333 var resp arvados.User
334 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
337 func (conn *Conn) UserUpdateUUID(ctx context.Context, options arvados.UpdateUUIDOptions) (arvados.User, error) {
338 ep := arvados.EndpointUserUpdateUUID
339 var resp arvados.User
340 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
343 func (conn *Conn) UserMerge(ctx context.Context, options arvados.UserMergeOptions) (arvados.User, error) {
344 ep := arvados.EndpointUserMerge
345 var resp arvados.User
346 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
349 func (conn *Conn) UserActivate(ctx context.Context, options arvados.UserActivateOptions) (arvados.User, error) {
350 ep := arvados.EndpointUserActivate
351 var resp arvados.User
352 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
355 func (conn *Conn) UserSetup(ctx context.Context, options arvados.UserSetupOptions) (map[string]interface{}, error) {
356 ep := arvados.EndpointUserSetup
357 var resp map[string]interface{}
358 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
361 func (conn *Conn) UserUnsetup(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
362 ep := arvados.EndpointUserUnsetup
363 var resp arvados.User
364 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
367 func (conn *Conn) UserGet(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
368 ep := arvados.EndpointUserGet
369 var resp arvados.User
370 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
373 func (conn *Conn) UserGetCurrent(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
374 ep := arvados.EndpointUserGetCurrent
375 var resp arvados.User
376 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
379 func (conn *Conn) UserGetSystem(ctx context.Context, options arvados.GetOptions) (arvados.User, error) {
380 ep := arvados.EndpointUserGetSystem
381 var resp arvados.User
382 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
385 func (conn *Conn) UserList(ctx context.Context, options arvados.ListOptions) (arvados.UserList, error) {
386 ep := arvados.EndpointUserList
387 var resp arvados.UserList
388 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
391 func (conn *Conn) UserDelete(ctx context.Context, options arvados.DeleteOptions) (arvados.User, error) {
392 ep := arvados.EndpointUserDelete
393 var resp arvados.User
394 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
398 func (conn *Conn) APIClientAuthorizationCurrent(ctx context.Context, options arvados.GetOptions) (arvados.APIClientAuthorization, error) {
399 ep := arvados.EndpointAPIClientAuthorizationCurrent
400 var resp arvados.APIClientAuthorization
401 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
405 type UserSessionAuthInfo struct {
406 Email string `json:"email"`
407 AlternateEmails []string `json:"alternate_emails"`
408 FirstName string `json:"first_name"`
409 LastName string `json:"last_name"`
410 Username string `json:"username"`
413 type UserSessionCreateOptions struct {
414 AuthInfo UserSessionAuthInfo `json:"auth_info"`
415 ReturnTo string `json:"return_to"`
418 func (conn *Conn) UserSessionCreate(ctx context.Context, options UserSessionCreateOptions) (arvados.LoginResponse, error) {
419 ep := arvados.APIEndpoint{Method: "POST", Path: "auth/controller/callback"}
420 var resp arvados.LoginResponse
421 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)
425 func (conn *Conn) UserBatchUpdate(ctx context.Context, options arvados.UserBatchUpdateOptions) (arvados.UserList, error) {
426 ep := arvados.APIEndpoint{Method: "PATCH", Path: "arvados/v1/users/batch_update"}
427 var resp arvados.UserList
428 err := conn.requestAndDecode(ctx, &resp, ep, nil, options)