Commit d528e25c authored by Kirill Smelkov's avatar Kirill Smelkov

X on auth info refresh ...

parent b8c0fedd
Pipeline #111 failed with stage
...@@ -33,25 +33,40 @@ func blobPreAuthorizeHandler(handleFunc serviceHandleFunc) serviceHandleFunc { ...@@ -33,25 +33,40 @@ func blobPreAuthorizeHandler(handleFunc serviceHandleFunc) serviceHandleFunc {
type authInfo struct { type authInfo struct {
authResponse authorizationResponse authResponse authorizationResponse
timestamp int64 // in seconds Tauth int64 // in seconds
Naccess int64
} }
// project -> authInfo // project -> authInfo
// FIXME it have to be not only project (privateToken etc...) // FIXME it have to be not only project (privateToken etc...)
var authCache = make(map[string]authInfo) var authCache = make(map[string]authInfo)
// verify that download access is authorized by auth backend const authCacheRefresh = 30 // in seconds
func verifyDownloadAccess(w http.ResponseWriter, r *gitRequest, project string) bool {
// XXX do we need mutex to lock authCache ? // refresh cache entry periodically while it is used
auth, ok := authCache[project] // if the entry is detected to be not used - remove it from cache and stop rereshing
if ok { func authRefresh(u *upstream, project string) {
log.Printf("downloadOk cached %v ago: %v", for ;; {
time.Since(time.Unix(auth.timestamp, 0)), time.Sleep(authCacheRefresh)
auth.authResponse)
r.authorizationResponse = auth.authResponse // XXX lock?
return (auth.authResponse.RepoPath != "") // XXX ok? auth, ok := authCache[project]
if !ok { // someone removed the entry from cache - no
break // need to further refresh XXX ok?
}
if auth.Naccess == 0 { // not used - we can remove and stop refreshing
delete(authCache, project)
break
}
askAuthBackend(u, project)
} }
}
// ask auth backend whether download is ok for project
func askAuthBackend(u *upstream, project string) authorizationResponse {
// request to verify whether download is possible via asking as git fetch would do // request to verify whether download is possible via asking as git fetch would do
// XXX privateToken not propagated, etc ... // XXX privateToken not propagated, etc ...
reqDownloadAccess, err := http.NewRequest("GET", project + ".git/info/refs?service=git-upload-pack", nil) reqDownloadAccess, err := http.NewRequest("GET", project + ".git/info/refs?service=git-upload-pack", nil)
...@@ -62,16 +77,37 @@ func verifyDownloadAccess(w http.ResponseWriter, r *gitRequest, project string) ...@@ -62,16 +77,37 @@ func verifyDownloadAccess(w http.ResponseWriter, r *gitRequest, project string)
// swap original request to 'verify-download' one // swap original request to 'verify-download' one
//requestBlob := r.Request //requestBlob := r.Request
r.Request = reqDownloadAccess r := &gitRequest{
Request: reqDownloadAccess,
u: u,
}
downloadOk := false // downloadOk := false
preAuthorizeHandler( preAuthorizeHandler(
func(w http.ResponseWriter, r *gitRequest) { func(w http.ResponseWriter, r *gitRequest) {
// if we ever get to this point - auth handler approved // if we ever get to this point - auth handler approved
// access and thus it is ok to download // access and thus it is ok to download
downloadOk = true // downloadOk = true
}, "") (w, r) }, "") (w, r)
return r.authorizationResponse
}
// verify that download access is authorized by auth backend
func verifyDownloadAccess(w http.ResponseWriter, r *gitRequest, project string) bool {
// XXX do we need mutex to lock authCache ?
auth, ok := authCache[project]
if ok {
log.Printf("downloadOk cached %v ago: %v",
time.Since(time.Unix(auth.Tauth, 0)),
auth.authResponse)
r.authorizationResponse = auth.authResponse
return (auth.authResponse.RepoPath != "") // XXX ok?
}
r.authorizationResponse = askAuthBackend(r.u, project)
// XXX do we need to lock authCache ? // XXX do we need to lock authCache ?
authCache[project] = authInfo{r.authorizationResponse, time.Now().Unix()} authCache[project] = authInfo{r.authorizationResponse, time.Now().Unix()}
return downloadOk return downloadOk
......
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