| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strings" |
| 6 | |
| 7 | "gitlab.com/Timfa/bluegit/internal/access" |
| 8 | "gitlab.com/Timfa/bluegit/internal/gitrepo" |
| 9 | "gitlab.com/Timfa/bluegit/internal/store" |
| 10 | ) |
| 11 | |
| 12 | func (s *Server) gitHTTP(w http.ResponseWriter, r *http.Request) { |
| 13 | if s.StoragePath() == "" { |
| 14 | http.Error(w, s.noStorageText(r), http.StatusServiceUnavailable) |
| 15 | return |
| 16 | } |
| 17 | owner := r.PathValue("owner") |
| 18 | repoSeg := r.PathValue("repo") |
| 19 | if !strings.HasSuffix(repoSeg, ".git") { |
| 20 | http.NotFound(w, r) |
| 21 | return |
| 22 | } |
| 23 | name := strings.TrimSuffix(repoSeg, ".git") |
| 24 | repo, err := s.Store.RepoByPath(r.Context(), owner, name) |
| 25 | if err != nil { |
| 26 | http.NotFound(w, r) |
| 27 | return |
| 28 | } |
| 29 | service := r.URL.Query().Get("service") |
| 30 | write := service == "git-receive-pack" || strings.HasSuffix(r.URL.Path, "/git-receive-pack") |
| 31 | if !write && s.runnerMayClone(r, repo.ID) { |
| 32 | rest := strings.TrimPrefix(r.URL.Path, "/"+owner+"/"+repoSeg) |
| 33 | if err := gitrepo.ServeSmartHTTP(w, r, s.Git, gitrepo.ProjectRoot(s.StoragePath()), gitrepo.PathInfo(owner, repoSeg, rest), nil); err != nil && s.Log != nil { |
| 34 | s.Log.Error("git http", "err", err, "path", r.URL.Path) |
| 35 | } |
| 36 | return |
| 37 | } |
| 38 | user, scopes, authed := s.gitUser(r) |
| 39 | var role access.Role |
| 40 | if authed { |
| 41 | role, err = s.Store.EffectiveRole(r.Context(), user.ID, repo.ID) |
| 42 | if err != nil { |
| 43 | http.Error(w, s.tr(r, "err.rights"), http.StatusInternalServerError) |
| 44 | return |
| 45 | } |
| 46 | } |
| 47 | if write { |
| 48 | if !authed { |
| 49 | s.askAuth(w) |
| 50 | return |
| 51 | } |
| 52 | if !scopes.RepoWrite() { |
| 53 | http.Error(w, s.tr(r, "err.apiScope"), http.StatusForbidden) |
| 54 | return |
| 55 | } |
| 56 | if !role.CanWriteCode() { |
| 57 | http.Error(w, s.tr(r, "err.noWrite"), http.StatusForbidden) |
| 58 | return |
| 59 | } |
| 60 | } else if !role.CanRead() && repo.Visibility != "public" { |
| 61 | if !authed { |
| 62 | s.askAuth(w) |
| 63 | return |
| 64 | } |
| 65 | http.Error(w, s.tr(r, "err.noRead"), http.StatusForbidden) |
| 66 | return |
| 67 | } else if authed && !scopes.RepoRead() { |
| 68 | http.Error(w, s.tr(r, "err.apiScope"), http.StatusForbidden) |
| 69 | return |
| 70 | } |
| 71 | dir := gitrepo.Dir(s.StoragePath(), repo.OwnerPath, repo.Name) |
| 72 | var before map[string]string |
| 73 | var extra []string |
| 74 | if write { |
| 75 | before, _ = gitrepo.Refs(r.Context(), s.Git, dir) |
| 76 | patterns, _ := s.Store.ProtectedPatterns(r.Context(), repo.ID) |
| 77 | tagPatterns, _ := s.Store.ProtectedTagPatterns(r.Context(), repo.ID) |
| 78 | extra = gitrepo.PushEnv(role, patterns, tagPatterns) |
| 79 | } |
| 80 | rest := strings.TrimPrefix(r.URL.Path, "/"+owner+"/"+repoSeg) |
| 81 | pathInfo := gitrepo.PathInfo(owner, repoSeg, rest) |
| 82 | if err := gitrepo.ServeSmartHTTP(w, r, s.Git, gitrepo.ProjectRoot(s.StoragePath()), pathInfo, extra); err != nil && s.Log != nil { |
| 83 | s.Log.Error("git http", "err", err, "path", r.URL.Path) |
| 84 | } |
| 85 | if write { |
| 86 | after, err := gitrepo.Refs(r.Context(), s.Git, dir) |
| 87 | if err == nil { |
| 88 | s.AfterPush(repo, user, before, after) |
| 89 | } |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | func (s *Server) gitUser(r *http.Request) (store.User, access.Scopes, bool) { |
| 94 | _, pass, ok := r.BasicAuth() |
| 95 | if !ok { |
| 96 | h := r.Header.Get("Authorization") |
| 97 | if strings.HasPrefix(strings.ToLower(h), "bearer ") { |
| 98 | pass = strings.TrimSpace(h[7:]) |
| 99 | ok = pass != "" |
| 100 | } |
| 101 | } |
| 102 | if !ok || !strings.HasPrefix(pass, "bg_") { |
| 103 | return store.User{}, access.Scopes{}, false |
| 104 | } |
| 105 | auth, err := s.Store.AuthenticateToken(r.Context(), pass) |
| 106 | if err != nil { |
| 107 | return store.User{}, access.Scopes{}, false |
| 108 | } |
| 109 | return auth.User, access.StoredScopes(auth.Token.Scopes), true |
| 110 | } |
| 111 | |
| 112 | // runnerMayClone: een aparte runner (bgr_-token) mag alleen lezen uit de |
| 113 | // repo van een job die hij nu draait. Job-tokens krijgen geen git-toegang. |
| 114 | func (s *Server) runnerMayClone(r *http.Request, repoID int64) bool { |
| 115 | h := r.Header.Get("Authorization") |
| 116 | if !strings.HasPrefix(strings.ToLower(h), "bearer ") { |
| 117 | return false |
| 118 | } |
| 119 | tok := strings.TrimSpace(h[7:]) |
| 120 | if !strings.HasPrefix(tok, "bgr_") { |
| 121 | return false |
| 122 | } |
| 123 | rn, err := s.Store.RunnerByToken(r.Context(), tok) |
| 124 | return err == nil && s.Store.RunnerRunsRepo(r.Context(), rn.ID, repoID) |
| 125 | } |
| 126 | |
| 127 | func (s *Server) askAuth(w http.ResponseWriter) { |
| 128 | w.Header().Set("WWW-Authenticate", `Basic realm="BlueGit"`) |
| 129 | http.Error(w, "aanmelden met een toegangstoken als wachtwoord", http.StatusUnauthorized) |
| 130 | } |