1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
15 "git.curoverse.com/arvados.git/sdk/go/auth"
16 "git.curoverse.com/arvados.git/sdk/go/httpserver"
19 func remoteContainerRequestCreate(
20 h *genericFederatedRequestHandler,
21 effectiveMethod string,
25 w http.ResponseWriter,
26 req *http.Request) bool {
28 if effectiveMethod != "POST" || uuid != "" || remainder != "" ||
29 *clusterId == "" || *clusterId == h.handler.Cluster.ClusterID {
33 if req.Header.Get("Content-Type") != "application/json" {
34 httpserver.Error(w, "Expected Content-Type: application/json, got "+req.Header.Get("Content-Type"), http.StatusBadRequest)
38 originalBody := req.Body
39 defer originalBody.Close()
40 var request map[string]interface{}
41 err := json.NewDecoder(req.Body).Decode(&request)
43 httpserver.Error(w, err.Error(), http.StatusBadRequest)
47 crString, ok := request["container_request"].(string)
49 var crJson map[string]interface{}
50 err := json.Unmarshal([]byte(crString), &crJson)
52 httpserver.Error(w, err.Error(), http.StatusBadRequest)
56 request["container_request"] = crJson
59 containerRequest, ok := request["container_request"].(map[string]interface{})
61 // Use toplevel object as the container_request object
62 containerRequest = request
65 // If runtime_token is not set, create a new token
66 if _, ok := containerRequest["runtime_token"]; !ok {
67 log.Printf("ok %v", ok)
69 // First make sure supplied token is valid.
70 creds := auth.NewCredentials()
71 creds.LoadTokensFromHTTPRequest(req)
73 currentUser, err := h.handler.validateAPItoken(req, creds.Tokens[0])
75 httpserver.Error(w, err.Error(), http.StatusForbidden)
79 if len(currentUser.Authorization.Scopes) != 1 || currentUser.Authorization.Scopes[0] != "all" {
80 httpserver.Error(w, "Token scope is not [all]", http.StatusForbidden)
84 newtok, err := h.handler.createAPItoken(req, currentUser.UUID, nil)
86 httpserver.Error(w, err.Error(), http.StatusForbidden)
89 containerRequest["runtime_token"] = newtok.TokenV2()
92 newbody, err := json.Marshal(request)
93 buf := bytes.NewBuffer(newbody)
94 req.Body = ioutil.NopCloser(buf)
95 req.ContentLength = int64(buf.Len())
96 req.Header.Set("Content-Length", fmt.Sprintf("%v", buf.Len()))
98 resp, cancel, err := h.handler.remoteClusterRequest(*clusterId, req)
102 h.handler.proxy.ForwardResponse(w, resp, err)