X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/703179225b04309485c0a1cefb794df6c919e84f..e9bfb6900e3340bfcdcea691466cd849ca7d9ffc:/lib/controller/fed_containers.go diff --git a/lib/controller/fed_containers.go b/lib/controller/fed_containers.go index a3c292583f..7fd5b25ad2 100644 --- a/lib/controller/fed_containers.go +++ b/lib/controller/fed_containers.go @@ -9,8 +9,8 @@ import ( "encoding/json" "fmt" "io/ioutil" - "log" "net/http" + "strings" "git.curoverse.com/arvados.git/sdk/go/auth" "git.curoverse.com/arvados.git/sdk/go/httpserver" @@ -25,8 +25,27 @@ func remoteContainerRequestCreate( w http.ResponseWriter, req *http.Request) bool { - if effectiveMethod != "POST" || uuid != "" || remainder != "" || - *clusterId == "" || *clusterId == h.handler.Cluster.ClusterID { + if effectiveMethod != "POST" || uuid != "" || remainder != "" { + return false + } + + // First make sure supplied token is valid. + creds := auth.NewCredentials() + creds.LoadTokensFromHTTPRequest(req) + + currentUser, err := h.handler.validateAPItoken(req, creds.Tokens[0]) + if err != nil { + httpserver.Error(w, err.Error(), http.StatusForbidden) + return true + } + + if *clusterId == "" { + *clusterId = h.handler.Cluster.ClusterID + } + + if strings.HasPrefix(currentUser.Authorization.UUID, h.handler.Cluster.ClusterID) && + *clusterId == h.handler.Cluster.ClusterID { + // local user submitting container request to local cluster return false } @@ -38,7 +57,7 @@ func remoteContainerRequestCreate( originalBody := req.Body defer originalBody.Close() var request map[string]interface{} - err := json.NewDecoder(req.Body).Decode(&request) + err = json.NewDecoder(req.Body).Decode(&request) if err != nil { httpserver.Error(w, err.Error(), http.StatusBadRequest) return true @@ -64,29 +83,31 @@ func remoteContainerRequestCreate( // If runtime_token is not set, create a new token if _, ok := containerRequest["runtime_token"]; !ok { - log.Printf("ok %v", ok) - - // First make sure supplied token is valid. - creds := auth.NewCredentials() - creds.LoadTokensFromHTTPRequest(req) - - currentUser, err := h.handler.validateAPItoken(req, creds.Tokens[0]) - if err != nil { - httpserver.Error(w, err.Error(), http.StatusForbidden) - return true - } - if len(currentUser.Authorization.Scopes) != 1 || currentUser.Authorization.Scopes[0] != "all" { httpserver.Error(w, "Token scope is not [all]", http.StatusForbidden) return true } - newtok, err := h.handler.createAPItoken(req, currentUser.UUID, nil) - if err != nil { - httpserver.Error(w, err.Error(), http.StatusForbidden) - return true + if strings.HasPrefix(currentUser.Authorization.UUID, h.handler.Cluster.ClusterID) { + // Local user, submitting to a remote cluster. + // Create a new time-limited token. + newtok, err := h.handler.createAPItoken(req, currentUser.UUID, nil) + if err != nil { + httpserver.Error(w, err.Error(), http.StatusForbidden) + return true + } + containerRequest["runtime_token"] = newtok.TokenV2() + } else { + // Remote user. Container request will use the + // current token, minus the trailing portion + // (optional container uuid). + sp := strings.Split(creds.Tokens[0], "/") + if len(sp) >= 3 { + containerRequest["runtime_token"] = strings.Join(sp[0:3], "/") + } else { + containerRequest["runtime_token"] = creds.Tokens[0] + } } - containerRequest["runtime_token"] = newtok.TokenV2() } newbody, err := json.Marshal(request) @@ -95,10 +116,7 @@ func remoteContainerRequestCreate( req.ContentLength = int64(buf.Len()) req.Header.Set("Content-Length", fmt.Sprintf("%v", buf.Len())) - resp, cancel, err := h.handler.remoteClusterRequest(*clusterId, req) - if cancel != nil { - defer cancel() - } + resp, err := h.handler.remoteClusterRequest(*clusterId, req) h.handler.proxy.ForwardResponse(w, resp, err) return true }