Commit 921cdfef authored by Kirill Smelkov's avatar Kirill Smelkov

X Factor out auth verification to separate function

parent ad3c668c
Pipeline #110 failed with stage
...@@ -31,43 +31,63 @@ func blobPreAuthorizeHandler(handleFunc serviceHandleFunc) serviceHandleFunc { ...@@ -31,43 +31,63 @@ func blobPreAuthorizeHandler(handleFunc serviceHandleFunc) serviceHandleFunc {
} }
} }
// verify that download access is authorized by auth backend
func verifyDownloadAccess(w http.ResponseWriter, r *gitRequest, project string) (downloadOk bool) {
downloadOk = false
// request to verify whether download is possible via asking as git fetch would do
// XXX privateToken not propagated, etc ...
reqDownloadAccess, err := http.NewRequest("GET", project + ".git/info/refs?service=git-upload-pack", nil)
if err != nil {
fail500(w, "GET git-upload-pack", err)
return
}
// swap original request to 'verify-download' one
//requestBlob := r.Request
r.Request = reqDownloadAccess
preAuthorizeHandler(
func(w http.ResponseWriter, r *gitRequest) {
// if we ever get to this point - auth handler approved
// access and thus it is ok to download
downloadOk = true
}, "") (w, r)
return
}
var projectRe = regexp.MustCompile(`^/[\w\.-]+/[\w\.-]+/`)
func handleGetBlobRaw(w http.ResponseWriter, r *gitRequest) { func handleGetBlobRaw(w http.ResponseWriter, r *gitRequest) {
Tstart := time.Now() Tstart := time.Now()
// extract project & refpath // extract project & refpath
// /namespace/project/raw/branch/file -> /namespace/project, branch/file // /namespace/project/raw/branch/file -> /namespace/project, branch/file
projectRe := regexp.MustCompile(`^/[\w\.-]+/[\w\.-]+/`)
project := projectRe.FindString(r.Request.URL.Path) project := projectRe.FindString(r.Request.URL.Path)
refpath := r.Request.URL.Path[len(project):] refpath := r.Request.URL.Path[len(project):]
if project == "" { if project == "" {
fail500(w, "extract project name", nil) fail500(w, "extract project name", nil)
return return
} }
//assert project[-1] == "/" // assert project[-1] == "/"
project = project[:len(project)-1] project = project[:len(project)-1]
// assert refpath[:4] == "raw/"
if refpath[:4] != "raw/" { if refpath[:4] != "raw/" {
fail500(w, "refpath != raw/...", nil) fail500(w, "refpath != raw/...", nil)
return return
} }
refpath = refpath[4:] refpath = refpath[4:]
//log.Printf("BLOB1 %v %v", project, refpath)
// request to verify whether download is possible via asking as git fetch would do if !verifyDownloadAccess(w, r, project) {
// XXX privateToken not propagated, etc ... // XXX verifyDownloadAccess already emitted 403 headers etc ...
reqDownloadAccess, err := http.NewRequest("GET", project + ".git/info/refs?service=git-upload-pack", nil)
if err != nil {
fail500(w, "GET git-upload-pack", err)
return return
} }
// swap original request to 'verify-download' one handleGetBlobRaw2(w, r, refpath)
//requestBlob := r.Request
r.Request = reqDownloadAccess
preAuthorizeHandler(func(w http.ResponseWriter, r *gitRequest) {
handleGetBlobRaw2(w, r, refpath)
}, "") (w, r)
Tend := time.Now() Tend := time.Now()
...@@ -105,9 +125,9 @@ func handleGetBlobRaw2(w http.ResponseWriter, r *gitRequest, refpath string) { ...@@ -105,9 +125,9 @@ func handleGetBlobRaw2(w http.ResponseWriter, r *gitRequest, refpath string) {
//setRawHeaders(...) //setRawHeaders(...)
w.WriteHeader(200) // XXX too early w.WriteHeader(200) // XXX too early
//_, err = io.Copy(os.Stdout, blobStdout) //_, err = io.Copy(os.Stdout, blobStdout)
if err != nil { //if err != nil {
panic(err) // panic(err)
} //}
if _, err := io.Copy(w, blobStdout); err != nil { if _, err := io.Copy(w, blobStdout); err != nil {
logContext("io.Copy", err) logContext("io.Copy", err)
return return
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment