Merge branch '6859-fix-invalid-manifests' refs #6859
[arvados.git] / services / arv-git-httpd / auth_handler.go
index ff1a1d5e9b13bc61d318596d24ad092a193712f9..fccb0c9576864634481a2e69b7237def54b6f0ec 100644 (file)
@@ -3,10 +3,8 @@ package main
 import (
        "log"
        "net/http"
-       "net/http/cgi"
        "os"
        "strings"
-       "sync"
        "time"
 
        "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
@@ -14,19 +12,10 @@ import (
        "git.curoverse.com/arvados.git/sdk/go/httpserver"
 )
 
-func newArvadosClient() interface{} {
-       arv, err := arvadosclient.MakeArvadosClient()
-       if err != nil {
-               log.Println("MakeArvadosClient:", err)
-               return nil
-       }
-       return &arv
-}
-
-var connectionPool = &sync.Pool{New: newArvadosClient}
+var clientPool = arvadosclient.MakeClientPool()
 
 type authHandler struct {
-       handler *cgi.Handler
+       handler http.Handler
 }
 
 func (h *authHandler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
@@ -57,7 +46,7 @@ func (h *authHandler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
                        passwordToLog = apiToken[0:10]
                }
 
-               log.Println(quoteStrings(r.RemoteAddr, passwordToLog, w.WroteStatus(), statusText, repoName, r.Method, r.URL.Path)...)
+               httpserver.Log(r.RemoteAddr, passwordToLog, w.WroteStatus(), statusText, repoName, r.Method, r.URL.Path)
        }()
 
        creds := auth.NewCredentialsFromHTTPRequest(r)
@@ -79,12 +68,12 @@ func (h *authHandler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
        repoName = pathParts[0]
        repoName = strings.TrimRight(repoName, "/")
 
-       arv, ok := connectionPool.Get().(*arvadosclient.ArvadosClient)
-       if !ok || arv == nil {
-               statusCode, statusText = http.StatusInternalServerError, "connection pool failed"
+       arv := clientPool.Get()
+       if arv == nil {
+               statusCode, statusText = http.StatusInternalServerError, "connection pool failed: "+clientPool.Err().Error()
                return
        }
-       defer connectionPool.Put(arv)
+       defer clientPool.Put(arv)
 
        // Ask API server whether the repository is readable using
        // this token (by trying to read it!)
@@ -160,20 +149,5 @@ func (h *authHandler) ServeHTTP(wOrig http.ResponseWriter, r *http.Request) {
        }
        r.URL.Path = rewrittenPath
 
-       handlerCopy := *h.handler
-       handlerCopy.Env = append(handlerCopy.Env, "REMOTE_USER="+r.RemoteAddr) // Should be username
-       handlerCopy.ServeHTTP(&w, r)
-}
-
-var escaper = strings.NewReplacer("\"", "\\\"", "\\", "\\\\", "\n", "\\n")
-
-// Transform strings so they are safer to write in logs (e.g.,
-// 'foo"bar' becomes '"foo\"bar"'). Non-string args are left alone.
-func quoteStrings(args ...interface{}) []interface{} {
-       for i, arg := range args {
-               if s, ok := arg.(string); ok {
-                       args[i] = "\"" + escaper.Replace(s) + "\""
-               }
-       }
-       return args
+       h.handler.ServeHTTP(&w, r)
 }