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 type authHandler struct {
18 clientPool *arvadosclient.ClientPool
22 func (h *authHandler) setup() {
23 os.Setenv("ARVADOS_API_HOST", theConfig.Client.APIHost)
24 h.clientPool = arvadosclient.MakeClientPool()
27 func (h *authHandler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
28 h.setupOnce.Do(h.setup)
34 var validApiToken bool
36 w := httpserver.WrapResponseWriter(wOrig)
39 if w.WroteStatus() == 0 {
40 // Nobody has called WriteHeader yet: that
42 w.WriteHeader(statusCode)
43 w.Write([]byte(statusText))
46 // If the given password is a valid token, log the first 10 characters of the token.
47 // Otherwise: log the string <invalid> if a password is given, else an empty string.
50 if len(apiToken) > 0 {
51 passwordToLog = "<invalid>"
54 passwordToLog = apiToken[0:10]
57 httpserver.Log(r.RemoteAddr, passwordToLog, w.WroteStatus(), statusText, repoName, r.Method, r.URL.Path)
60 creds := auth.NewCredentialsFromHTTPRequest(r)
61 if len(creds.Tokens) == 0 {
62 statusCode, statusText = http.StatusUnauthorized, "no credentials provided"
63 w.Header().Add("WWW-Authenticate", "Basic realm=\"git\"")
66 apiToken = creds.Tokens[0]
68 // Access to paths "/foo/bar.git/*" and "/foo/bar/.git/*" are
69 // protected by the permissions on the repository named
71 pathParts := strings.SplitN(r.URL.Path[1:], ".git/", 2)
72 if len(pathParts) != 2 {
73 statusCode, statusText = http.StatusBadRequest, "bad request"
76 repoName = pathParts[0]
77 repoName = strings.TrimRight(repoName, "/")
79 arv := h.clientPool.Get()
81 statusCode, statusText = http.StatusInternalServerError, "connection pool failed: "+h.clientPool.Err().Error()
84 defer h.clientPool.Put(arv)
86 // Ask API server whether the repository is readable using
87 // this token (by trying to read it!)
88 arv.ApiToken = apiToken
89 reposFound := arvadosclient.Dict{}
90 if err := arv.List("repositories", arvadosclient.Dict{
91 "filters": [][]string{{"name", "=", repoName}},
92 }, &reposFound); err != nil {
93 statusCode, statusText = http.StatusInternalServerError, err.Error()
97 if avail, ok := reposFound["items_available"].(float64); !ok {
98 statusCode, statusText = http.StatusInternalServerError, "bad list response from API"
100 } else if avail < 1 {
101 statusCode, statusText = http.StatusNotFound, "not found"
103 } else if avail > 1 {
104 statusCode, statusText = http.StatusInternalServerError, "name collision"
108 repoUUID := reposFound["items"].([]interface{})[0].(map[string]interface{})["uuid"].(string)
110 isWrite := strings.HasSuffix(r.URL.Path, "/git-receive-pack")
114 err := arv.Update("repositories", repoUUID, arvadosclient.Dict{
115 "repository": arvadosclient.Dict{
116 "modified_at": time.Now().String(),
118 }, &arvadosclient.Dict{})
120 statusCode, statusText = http.StatusForbidden, err.Error()
126 // Regardless of whether the client asked for "/foo.git" or
127 // "/foo/.git", we choose whichever variant exists in our repo
128 // root, and we try {uuid}.git and {uuid}/.git first. If none
129 // of these exist, we 404 even though the API told us the repo
130 // _should_ exist (presumably this means the repo was just
131 // created, and gitolite sync hasn't run yet).
134 "/" + repoUUID + ".git",
135 "/" + repoUUID + "/.git",
136 "/" + repoName + ".git",
137 "/" + repoName + "/.git",
139 for _, dir := range tryDirs {
140 if fileInfo, err := os.Stat(theConfig.Root + dir); err != nil {
141 if !os.IsNotExist(err) {
142 statusCode, statusText = http.StatusInternalServerError, err.Error()
145 } else if fileInfo.IsDir() {
146 rewrittenPath = dir + "/" + pathParts[1]
150 if rewrittenPath == "" {
151 log.Println("WARNING:", repoUUID,
152 "git directory not found in", theConfig.Root, tryDirs)
153 // We say "content not found" to disambiguate from the
154 // earlier "API says that repo does not exist" error.
155 statusCode, statusText = http.StatusNotFound, "content not found"
158 r.URL.Path = rewrittenPath
160 h.handler.ServeHTTP(&w, r)