Merge branch 'master' into 6827-no-passwords-in-logs
[arvados.git] / services / arv-git-httpd / auth_handler.go
1 package main
2
3 import (
4         "log"
5         "net/http"
6         "net/http/cgi"
7         "os"
8         "strings"
9         "sync"
10         "time"
11
12         "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
13 )
14
15 func newArvadosClient() interface{} {
16         arv, err := arvadosclient.MakeArvadosClient()
17         if err != nil {
18                 log.Println("MakeArvadosClient:", err)
19                 return nil
20         }
21         return &arv
22 }
23
24 var connectionPool = &sync.Pool{New: newArvadosClient}
25
26 type spyingResponseWriter struct {
27         http.ResponseWriter
28         wroteStatus *int
29 }
30
31 func (w spyingResponseWriter) WriteHeader(s int) {
32         *w.wroteStatus = s
33         w.ResponseWriter.WriteHeader(s)
34 }
35
36 type authHandler struct {
37         handler *cgi.Handler
38 }
39
40 func (h *authHandler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
41         var statusCode int
42         var statusText string
43         var username, password string
44         var repoName string
45         var wroteStatus int
46
47         w := spyingResponseWriter{wOrig, &wroteStatus}
48
49         defer func() {
50                 if wroteStatus == 0 {
51                         // Nobody has called WriteHeader yet: that must be our job.
52                         w.WriteHeader(statusCode)
53                         w.Write([]byte(statusText))
54                 }
55
56                 passwordToLog := ""
57                 if statusCode == 401 || strings.Contains(statusText, "Unauthorized") {
58                         if len(password) > 0 {
59                                 passwordToLog = "<invalid>"
60                         }
61                 } else {
62                         passwordToLog = password[0:10]
63                 }
64
65                 log.Println(quoteStrings(r.RemoteAddr, username, passwordToLog, wroteStatus, statusText, repoName, r.Method, r.URL.Path)...)
66         }()
67
68         // HTTP request username is logged, but unused. Password is an
69         // Arvados API token.
70         username, password, ok := BasicAuth(r)
71         if !ok || username == "" || password == "" {
72                 statusCode, statusText = http.StatusUnauthorized, "no credentials provided"
73                 w.Header().Add("WWW-Authenticate", "Basic realm=\"git\"")
74                 return
75         }
76
77         // Access to paths "/foo/bar.git/*" and "/foo/bar/.git/*" are
78         // protected by the permissions on the repository named
79         // "foo/bar".
80         pathParts := strings.SplitN(r.URL.Path[1:], ".git/", 2)
81         if len(pathParts) != 2 {
82                 statusCode, statusText = http.StatusBadRequest, "bad request"
83                 return
84         }
85         repoName = pathParts[0]
86         repoName = strings.TrimRight(repoName, "/")
87
88         arv, ok := connectionPool.Get().(*arvadosclient.ArvadosClient)
89         if !ok || arv == nil {
90                 statusCode, statusText = http.StatusInternalServerError, "connection pool failed"
91                 return
92         }
93         defer connectionPool.Put(arv)
94
95         // Ask API server whether the repository is readable using
96         // this token (by trying to read it!)
97         arv.ApiToken = password
98         reposFound := arvadosclient.Dict{}
99         if err := arv.List("repositories", arvadosclient.Dict{
100                 "filters": [][]string{{"name", "=", repoName}},
101         }, &reposFound); err != nil {
102                 statusCode, statusText = http.StatusInternalServerError, err.Error()
103                 return
104         }
105         if avail, ok := reposFound["items_available"].(float64); !ok {
106                 statusCode, statusText = http.StatusInternalServerError, "bad list response from API"
107                 return
108         } else if avail < 1 {
109                 statusCode, statusText = http.StatusNotFound, "not found"
110                 return
111         } else if avail > 1 {
112                 statusCode, statusText = http.StatusInternalServerError, "name collision"
113                 return
114         }
115
116         repoUUID := reposFound["items"].([]interface{})[0].(map[string]interface{})["uuid"].(string)
117
118         isWrite := strings.HasSuffix(r.URL.Path, "/git-receive-pack")
119         if !isWrite {
120                 statusText = "read"
121         } else {
122                 err := arv.Update("repositories", repoUUID, arvadosclient.Dict{
123                         "repository": arvadosclient.Dict{
124                                 "modified_at": time.Now().String(),
125                         },
126                 }, &arvadosclient.Dict{})
127                 if err != nil {
128                         statusCode, statusText = http.StatusForbidden, err.Error()
129                         return
130                 }
131                 statusText = "write"
132         }
133
134         // Regardless of whether the client asked for "/foo.git" or
135         // "/foo/.git", we choose whichever variant exists in our repo
136         // root, and we try {uuid}.git and {uuid}/.git first. If none
137         // of these exist, we 404 even though the API told us the repo
138         // _should_ exist (presumably this means the repo was just
139         // created, and gitolite sync hasn't run yet).
140         rewrittenPath := ""
141         tryDirs := []string{
142                 "/" + repoUUID + ".git",
143                 "/" + repoUUID + "/.git",
144                 "/" + repoName + ".git",
145                 "/" + repoName + "/.git",
146         }
147         for _, dir := range tryDirs {
148                 if fileInfo, err := os.Stat(theConfig.Root + dir); err != nil {
149                         if !os.IsNotExist(err) {
150                                 statusCode, statusText = http.StatusInternalServerError, err.Error()
151                                 return
152                         }
153                 } else if fileInfo.IsDir() {
154                         rewrittenPath = dir + "/" + pathParts[1]
155                         break
156                 }
157         }
158         if rewrittenPath == "" {
159                 log.Println("WARNING:", repoUUID,
160                         "git directory not found in", theConfig.Root, tryDirs)
161                 // We say "content not found" to disambiguate from the
162                 // earlier "API says that repo does not exist" error.
163                 statusCode, statusText = http.StatusNotFound, "content not found"
164                 return
165         }
166         r.URL.Path = rewrittenPath
167
168         handlerCopy := *h.handler
169         handlerCopy.Env = append(handlerCopy.Env, "REMOTE_USER="+r.RemoteAddr) // Should be username
170         handlerCopy.ServeHTTP(&w, r)
171 }
172
173 var escaper = strings.NewReplacer("\"", "\\\"", "\\", "\\\\", "\n", "\\n")
174
175 // Transform strings so they are safer to write in logs (e.g.,
176 // 'foo"bar' becomes '"foo\"bar"'). Non-string args are left alone.
177 func quoteStrings(args ...interface{}) []interface{} {
178         for i, arg := range args {
179                 if s, ok := arg.(string); ok {
180                         args[i] = "\"" + escaper.Replace(s) + "\""
181                 }
182         }
183         return args
184 }