12 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
13 "git.curoverse.com/arvados.git/sdk/go/auth"
14 "git.curoverse.com/arvados.git/sdk/go/httpserver"
17 func newArvadosClient() interface{} {
18 arv, err := arvadosclient.MakeArvadosClient()
20 log.Println("MakeArvadosClient:", err)
26 var connectionPool = &sync.Pool{New: newArvadosClient}
28 type authHandler struct {
32 func (h *authHandler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
37 var validApiToken bool
39 w := httpserver.WrapResponseWriter(wOrig)
42 if w.WroteStatus() == 0 {
43 // Nobody has called WriteHeader yet: that
45 w.WriteHeader(statusCode)
46 w.Write([]byte(statusText))
49 // If the given password is a valid token, log the first 10 characters of the token.
50 // Otherwise: log the string <invalid> if a password is given, else an empty string.
53 if len(apiToken) > 0 {
54 passwordToLog = "<invalid>"
57 passwordToLog = apiToken[0:10]
60 log.Println(quoteStrings(r.RemoteAddr, passwordToLog, w.WroteStatus(), statusText, repoName, r.Method, r.URL.Path)...)
63 creds := auth.NewCredentialsFromHTTPRequest(r)
64 if len(creds.Tokens) == 0 {
65 statusCode, statusText = http.StatusUnauthorized, "no credentials provided"
66 w.Header().Add("WWW-Authenticate", "Basic realm=\"git\"")
69 apiToken = creds.Tokens[0]
71 // Access to paths "/foo/bar.git/*" and "/foo/bar/.git/*" are
72 // protected by the permissions on the repository named
74 pathParts := strings.SplitN(r.URL.Path[1:], ".git/", 2)
75 if len(pathParts) != 2 {
76 statusCode, statusText = http.StatusBadRequest, "bad request"
79 repoName = pathParts[0]
80 repoName = strings.TrimRight(repoName, "/")
82 arv, ok := connectionPool.Get().(*arvadosclient.ArvadosClient)
83 if !ok || arv == nil {
84 statusCode, statusText = http.StatusInternalServerError, "connection pool failed"
87 defer connectionPool.Put(arv)
89 // Ask API server whether the repository is readable using
90 // this token (by trying to read it!)
91 arv.ApiToken = apiToken
92 reposFound := arvadosclient.Dict{}
93 if err := arv.List("repositories", arvadosclient.Dict{
94 "filters": [][]string{{"name", "=", repoName}},
95 }, &reposFound); err != nil {
96 statusCode, statusText = http.StatusInternalServerError, err.Error()
100 if avail, ok := reposFound["items_available"].(float64); !ok {
101 statusCode, statusText = http.StatusInternalServerError, "bad list response from API"
103 } else if avail < 1 {
104 statusCode, statusText = http.StatusNotFound, "not found"
106 } else if avail > 1 {
107 statusCode, statusText = http.StatusInternalServerError, "name collision"
111 repoUUID := reposFound["items"].([]interface{})[0].(map[string]interface{})["uuid"].(string)
113 isWrite := strings.HasSuffix(r.URL.Path, "/git-receive-pack")
117 err := arv.Update("repositories", repoUUID, arvadosclient.Dict{
118 "repository": arvadosclient.Dict{
119 "modified_at": time.Now().String(),
121 }, &arvadosclient.Dict{})
123 statusCode, statusText = http.StatusForbidden, err.Error()
129 // Regardless of whether the client asked for "/foo.git" or
130 // "/foo/.git", we choose whichever variant exists in our repo
131 // root, and we try {uuid}.git and {uuid}/.git first. If none
132 // of these exist, we 404 even though the API told us the repo
133 // _should_ exist (presumably this means the repo was just
134 // created, and gitolite sync hasn't run yet).
137 "/" + repoUUID + ".git",
138 "/" + repoUUID + "/.git",
139 "/" + repoName + ".git",
140 "/" + repoName + "/.git",
142 for _, dir := range tryDirs {
143 if fileInfo, err := os.Stat(theConfig.Root + dir); err != nil {
144 if !os.IsNotExist(err) {
145 statusCode, statusText = http.StatusInternalServerError, err.Error()
148 } else if fileInfo.IsDir() {
149 rewrittenPath = dir + "/" + pathParts[1]
153 if rewrittenPath == "" {
154 log.Println("WARNING:", repoUUID,
155 "git directory not found in", theConfig.Root, tryDirs)
156 // We say "content not found" to disambiguate from the
157 // earlier "API says that repo does not exist" error.
158 statusCode, statusText = http.StatusNotFound, "content not found"
161 r.URL.Path = rewrittenPath
163 handlerCopy := *h.handler
164 handlerCopy.Env = append(handlerCopy.Env, "REMOTE_USER="+r.RemoteAddr) // Should be username
165 handlerCopy.ServeHTTP(&w, r)
168 var escaper = strings.NewReplacer("\"", "\\\"", "\\", "\\\\", "\n", "\\n")
170 // Transform strings so they are safer to write in logs (e.g.,
171 // 'foo"bar' becomes '"foo\"bar"'). Non-string args are left alone.
172 func quoteStrings(args ...interface{}) []interface{} {
173 for i, arg := range args {
174 if s, ok := arg.(string); ok {
175 args[i] = "\"" + escaper.Replace(s) + "\""