11 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
12 "git.curoverse.com/arvados.git/sdk/go/auth"
13 "git.curoverse.com/arvados.git/sdk/go/httpserver"
16 var clientPool = arvadosclient.MakeClientPool()
18 type authHandler struct {
22 func (h *authHandler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
27 var validApiToken bool
29 w := httpserver.WrapResponseWriter(wOrig)
32 if w.WroteStatus() == 0 {
33 // Nobody has called WriteHeader yet: that
35 w.WriteHeader(statusCode)
36 w.Write([]byte(statusText))
39 // If the given password is a valid token, log the first 10 characters of the token.
40 // Otherwise: log the string <invalid> if a password is given, else an empty string.
43 if len(apiToken) > 0 {
44 passwordToLog = "<invalid>"
47 passwordToLog = apiToken[0:10]
50 httpserver.Log(r.RemoteAddr, passwordToLog, w.WroteStatus(), statusText, repoName, r.Method, r.URL.Path)
53 creds := auth.NewCredentialsFromHTTPRequest(r)
54 if len(creds.Tokens) == 0 {
55 statusCode, statusText = http.StatusUnauthorized, "no credentials provided"
56 w.Header().Add("WWW-Authenticate", "Basic realm=\"git\"")
59 apiToken = creds.Tokens[0]
61 // Access to paths "/foo/bar.git/*" and "/foo/bar/.git/*" are
62 // protected by the permissions on the repository named
64 pathParts := strings.SplitN(r.URL.Path[1:], ".git/", 2)
65 if len(pathParts) != 2 {
66 statusCode, statusText = http.StatusBadRequest, "bad request"
69 repoName = pathParts[0]
70 repoName = strings.TrimRight(repoName, "/")
72 arv := clientPool.Get()
74 statusCode, statusText = http.StatusInternalServerError, "connection pool failed: "+clientPool.Err().Error()
77 defer clientPool.Put(arv)
79 // Ask API server whether the repository is readable using
80 // this token (by trying to read it!)
81 arv.ApiToken = apiToken
82 reposFound := arvadosclient.Dict{}
83 if err := arv.List("repositories", arvadosclient.Dict{
84 "filters": [][]string{{"name", "=", repoName}},
85 }, &reposFound); err != nil {
86 statusCode, statusText = http.StatusInternalServerError, err.Error()
90 if avail, ok := reposFound["items_available"].(float64); !ok {
91 statusCode, statusText = http.StatusInternalServerError, "bad list response from API"
94 statusCode, statusText = http.StatusNotFound, "not found"
97 statusCode, statusText = http.StatusInternalServerError, "name collision"
101 repoUUID := reposFound["items"].([]interface{})[0].(map[string]interface{})["uuid"].(string)
103 isWrite := strings.HasSuffix(r.URL.Path, "/git-receive-pack")
107 err := arv.Update("repositories", repoUUID, arvadosclient.Dict{
108 "repository": arvadosclient.Dict{
109 "modified_at": time.Now().String(),
111 }, &arvadosclient.Dict{})
113 statusCode, statusText = http.StatusForbidden, err.Error()
119 // Regardless of whether the client asked for "/foo.git" or
120 // "/foo/.git", we choose whichever variant exists in our repo
121 // root, and we try {uuid}.git and {uuid}/.git first. If none
122 // of these exist, we 404 even though the API told us the repo
123 // _should_ exist (presumably this means the repo was just
124 // created, and gitolite sync hasn't run yet).
127 "/" + repoUUID + ".git",
128 "/" + repoUUID + "/.git",
129 "/" + repoName + ".git",
130 "/" + repoName + "/.git",
132 for _, dir := range tryDirs {
133 if fileInfo, err := os.Stat(theConfig.Root + dir); err != nil {
134 if !os.IsNotExist(err) {
135 statusCode, statusText = http.StatusInternalServerError, err.Error()
138 } else if fileInfo.IsDir() {
139 rewrittenPath = dir + "/" + pathParts[1]
143 if rewrittenPath == "" {
144 log.Println("WARNING:", repoUUID,
145 "git directory not found in", theConfig.Root, tryDirs)
146 // We say "content not found" to disambiguate from the
147 // earlier "API says that repo does not exist" error.
148 statusCode, statusText = http.StatusNotFound, "content not found"
151 r.URL.Path = rewrittenPath
153 handlerCopy := *h.handler
154 handlerCopy.Env = append(handlerCopy.Env, "REMOTE_USER="+r.RemoteAddr) // Should be username
155 handlerCopy.ServeHTTP(&w, r)