| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "strconv" |
| 6 | "strings" |
| 7 | "time" |
| 8 | |
| 9 | "gitlab.com/Timfa/bluegit/internal/access" |
| 10 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 11 | "gitlab.com/Timfa/bluegit/internal/store" |
| 12 | ) |
| 13 | |
| 14 | // Runnerbeheer via de API: dezelfde regels als de UI. Account-runners |
| 15 | // alleen voor de eigenaar zelf, groepsrunners alleen voor maintainers. |
| 16 | // Scope runners (of api; read_api voor lezen). Job-tokens (bgj_) en |
| 17 | // runner-tokens (bgr_) komen hier niet in: authUser accepteert alleen bg_. |
| 18 | |
| 19 | func runnerJSON(r store.Runner) map[string]any { |
| 20 | status := "offline" |
| 21 | if r.Paused { |
| 22 | status = "paused" |
| 23 | } else if r.Draining && r.Online(time.Now()) { |
| 24 | status = "draining" |
| 25 | } else if r.Online(time.Now()) { |
| 26 | status = "online" |
| 27 | } |
| 28 | return map[string]any{ |
| 29 | "id": r.ID, "name": r.Name, "owner_kind": r.OwnerKind, "owner_id": r.OwnerID, |
| 30 | "status": status, "online": r.Online(time.Now()), "paused": r.Paused, "draining": r.Draining, |
| 31 | "last_seen": rfcTime(r.LastSeen), "created_at": rfcTime(r.CreatedAt), "version": r.Version, |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // runnerScope bepaalt de eigenaar uit het pad: /user/... of /groups/{path}/... |
| 36 | func (s *Server) runnerScope(w http.ResponseWriter, r *http.Request) (string, int64, bool) { |
| 37 | if strings.HasPrefix(r.URL.Path, "/api/v1/admin/runners") { |
| 38 | if !apiUser(r).InstanceAdmin { |
| 39 | s.writeAPIErr(w, r, fail(http.StatusForbidden, i18n.E("err.onlyAdmin"))) |
| 40 | return "", 0, false |
| 41 | } |
| 42 | return store.RunnerGlobal, 0, true |
| 43 | } |
| 44 | if r.PathValue("path") != "" { |
| 45 | g, ok := s.apiGroup(w, r, access.RoleMaintainer) |
| 46 | if !ok { |
| 47 | return "", 0, false |
| 48 | } |
| 49 | return "group", g.ID, true |
| 50 | } |
| 51 | return "user", apiUser(r).ID, true |
| 52 | } |
| 53 | |
| 54 | func (s *Server) scopedRunner(w http.ResponseWriter, r *http.Request) (string, int64, store.Runner, bool) { |
| 55 | kind, owner, ok := s.runnerScope(w, r) |
| 56 | if !ok { |
| 57 | return "", 0, store.Runner{}, false |
| 58 | } |
| 59 | id, err := strconv.ParseInt(r.PathValue("id"), 10, 64) |
| 60 | rn, rerr := s.Store.RunnerByID(r.Context(), id) |
| 61 | if err != nil || rerr != nil || rn.Revoked || rn.OwnerKind != kind || rn.OwnerID != owner { |
| 62 | s.writeAPIErr(w, r, fail(http.StatusNotFound, i18n.E("err.notFound"))) |
| 63 | return "", 0, store.Runner{}, false |
| 64 | } |
| 65 | return kind, owner, rn, true |
| 66 | } |
| 67 | |
| 68 | func (s *Server) apiRunners(w http.ResponseWriter, r *http.Request) { |
| 69 | kind, owner, ok := s.runnerScope(w, r) |
| 70 | if !ok { |
| 71 | return |
| 72 | } |
| 73 | rs, err := s.Store.ListRunners(r.Context(), kind, owner) |
| 74 | if !s.apiOK(w, r, err) { |
| 75 | return |
| 76 | } |
| 77 | out := make([]map[string]any, 0, len(rs)) |
| 78 | for _, rn := range rs { |
| 79 | out = append(out, runnerJSON(rn)) |
| 80 | } |
| 81 | writeJSON(w, http.StatusOK, out) |
| 82 | } |
| 83 | |
| 84 | func (s *Server) apiRunnerRegToken(w http.ResponseWriter, r *http.Request) { |
| 85 | kind, owner, ok := s.runnerScope(w, r) |
| 86 | if !ok { |
| 87 | return |
| 88 | } |
| 89 | plain, err := s.Store.CreateRunnerRegToken(r.Context(), kind, owner, apiUser(r).ID) |
| 90 | if !s.apiOK(w, r, err) { |
| 91 | return |
| 92 | } |
| 93 | writeJSON(w, http.StatusCreated, map[string]any{ |
| 94 | "token": plain, "owner_kind": kind, "owner_id": owner, |
| 95 | "expires_at": rfcTime(time.Now().Add(24 * time.Hour)), |
| 96 | "warning": globalWarning(kind), |
| 97 | "register": "bluegit-runner register --url " + s.Cfg.BaseURL + " --token <token>", |
| 98 | }) |
| 99 | } |
| 100 | |
| 101 | func (s *Server) apiRunnerGet(w http.ResponseWriter, r *http.Request) { |
| 102 | if _, _, rn, ok := s.scopedRunner(w, r); ok { |
| 103 | writeJSON(w, http.StatusOK, runnerJSON(rn)) |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | func (s *Server) apiRunnerPut(w http.ResponseWriter, r *http.Request) { |
| 108 | kind, owner, rn, ok := s.scopedRunner(w, r) |
| 109 | if !ok { |
| 110 | return |
| 111 | } |
| 112 | var body struct { |
| 113 | Name *string `json:"name"` |
| 114 | Paused *bool `json:"paused"` |
| 115 | } |
| 116 | if err := readJSON(r, &body); err != nil { |
| 117 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 118 | return |
| 119 | } |
| 120 | upd, err := s.Store.UpdateRunner(r.Context(), kind, owner, rn.ID, body.Name, body.Paused) |
| 121 | if err != nil { |
| 122 | s.writeAPIErr(w, r, fail(400, err)) |
| 123 | return |
| 124 | } |
| 125 | writeJSON(w, http.StatusOK, runnerJSON(upd)) |
| 126 | } |
| 127 | |
| 128 | func (s *Server) setPaused(w http.ResponseWriter, r *http.Request, paused bool) { |
| 129 | kind, owner, rn, ok := s.scopedRunner(w, r) |
| 130 | if !ok { |
| 131 | return |
| 132 | } |
| 133 | upd, err := s.Store.UpdateRunner(r.Context(), kind, owner, rn.ID, nil, &paused) |
| 134 | if !s.apiOK(w, r, err) { |
| 135 | return |
| 136 | } |
| 137 | writeJSON(w, http.StatusOK, runnerJSON(upd)) |
| 138 | } |
| 139 | |
| 140 | func (s *Server) apiRunnerPause(w http.ResponseWriter, r *http.Request) { s.setPaused(w, r, true) } |
| 141 | func (s *Server) apiRunnerResume(w http.ResponseWriter, r *http.Request) { s.setPaused(w, r, false) } |
| 142 | |
| 143 | func (s *Server) apiRunnerDelete(w http.ResponseWriter, r *http.Request) { |
| 144 | kind, owner, rn, ok := s.scopedRunner(w, r) |
| 145 | if !ok { |
| 146 | return |
| 147 | } |
| 148 | if !s.apiOK(w, r, s.Store.RevokeRunner(r.Context(), kind, owner, rn.ID)) { |
| 149 | return |
| 150 | } |
| 151 | w.WriteHeader(http.StatusNoContent) |
| 152 | } |
| 153 | |
| 154 | func (s *Server) apiRunnerJobs(w http.ResponseWriter, r *http.Request) { |
| 155 | _, _, rn, ok := s.scopedRunner(w, r) |
| 156 | if !ok { |
| 157 | return |
| 158 | } |
| 159 | limit, _ := strconv.Atoi(r.URL.Query().Get("limit")) |
| 160 | jobs, err := s.Store.RunnerJobs(r.Context(), rn.ID, limit) |
| 161 | if !s.apiOK(w, r, err) { |
| 162 | return |
| 163 | } |
| 164 | uid := apiUser(r).ID |
| 165 | out := []map[string]any{} |
| 166 | for _, j := range jobs { |
| 167 | row := map[string]any{"id": j.JobID, "pipeline_id": j.PipelineID, "name": j.Name, "status": j.Status, |
| 168 | "started_at": rfcTime(j.StartedAt), "finished_at": rfcTime(j.FinishedAt), "project": nil} |
| 169 | // Alleen projectnamen die de vrager zelf mag zien. |
| 170 | if repo, err := s.Store.RepoByID(r.Context(), j.RepoID); err == nil { |
| 171 | if role, err := s.Store.EffectiveRole(r.Context(), uid, repo.ID); err == nil && (role.CanRead() || repo.Visibility == "public") { |
| 172 | row["project"] = repo.FullPath() |
| 173 | row["web_url"] = s.Cfg.BaseURL + "/" + repo.FullPath() + "/-/pipelines/" + strconv.FormatInt(j.PipelineID, 10) + "/jobs/" + strconv.FormatInt(j.JobID, 10) |
| 174 | } |
| 175 | } |
| 176 | out = append(out, row) |
| 177 | } |
| 178 | writeJSON(w, http.StatusOK, out) |
| 179 | } |
| 180 | |
| 181 | func (s *Server) apiUserRunnerSettings(w http.ResponseWriter, r *http.Request) { |
| 182 | u := apiUser(r) |
| 183 | if r.Method == http.MethodPut { |
| 184 | var body struct { |
| 185 | Preference string `json:"preference"` |
| 186 | } |
| 187 | if err := readJSON(r, &body); err != nil || (body.Preference != store.RunnerPrefOwnFirst && body.Preference != store.RunnerPrefBuiltinFirst) { |
| 188 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 189 | return |
| 190 | } |
| 191 | if !s.apiOK(w, r, s.Store.SetUserRunnerPref(r.Context(), u.ID, body.Preference)) { |
| 192 | return |
| 193 | } |
| 194 | } |
| 195 | writeJSON(w, http.StatusOK, map[string]any{"preference": s.Store.UserRunnerPref(r.Context(), u.ID)}) |
| 196 | } |
| 197 | |
| 198 | func (s *Server) apiGroupRunnerSettings(w http.ResponseWriter, r *http.Request) { |
| 199 | g, ok := s.apiGroup(w, r, access.RoleMaintainer) |
| 200 | if !ok { |
| 201 | return |
| 202 | } |
| 203 | if r.Method == http.MethodPut { |
| 204 | var body struct { |
| 205 | BuiltinFirst *bool `json:"builtin_first"` |
| 206 | } |
| 207 | if err := readJSON(r, &body); err != nil || body.BuiltinFirst == nil { |
| 208 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 209 | return |
| 210 | } |
| 211 | if !s.apiOK(w, r, s.Store.SetGroupBuiltinFirst(r.Context(), g.ID, *body.BuiltinFirst)) { |
| 212 | return |
| 213 | } |
| 214 | } |
| 215 | ids, _ := s.Store.RunnerOptIns(r.Context(), g.ID) |
| 216 | names := []string{} |
| 217 | for _, id := range ids { |
| 218 | if u, err := s.Store.UserByID(r.Context(), id); err == nil { |
| 219 | names = append(names, u.Username) |
| 220 | } |
| 221 | } |
| 222 | writeJSON(w, http.StatusOK, map[string]any{"builtin_first": s.Store.GroupBuiltinFirst(r.Context(), g.ID), "opted_in": names}) |
| 223 | } |
| 224 | |
| 225 | // apiGroupRunnerOptIn: alleen je eigen toestemming, en alleen als maintainer. |
| 226 | func (s *Server) apiGroupRunnerOptIn(w http.ResponseWriter, r *http.Request) { |
| 227 | g, ok := s.apiGroup(w, r, access.RoleReadonly) |
| 228 | if !ok { |
| 229 | return |
| 230 | } |
| 231 | u := apiUser(r) |
| 232 | if r.Method == http.MethodPut { |
| 233 | var body struct { |
| 234 | Enabled *bool `json:"enabled"` |
| 235 | } |
| 236 | if err := readJSON(r, &body); err != nil || body.Enabled == nil { |
| 237 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 238 | return |
| 239 | } |
| 240 | if !s.apiOK(w, r, s.Store.SetRunnerOptIn(r.Context(), g.ID, u.ID, *body.Enabled)) { |
| 241 | return |
| 242 | } |
| 243 | } |
| 244 | ids, _ := s.Store.RunnerOptIns(r.Context(), g.ID) |
| 245 | on := false |
| 246 | for _, id := range ids { |
| 247 | on = on || id == u.ID |
| 248 | } |
| 249 | writeJSON(w, http.StatusOK, map[string]any{"enabled": on, "username": u.Username}) |
| 250 | } |
| 251 | |
| 252 | // globalWarning: een globale runner krijgt jobs uit alle repo's, ook privé. |
| 253 | func globalWarning(kind string) string { |
| 254 | if kind != store.RunnerGlobal { |
| 255 | return "" |
| 256 | } |
| 257 | return "A global runner receives jobs (code and that job's secrets) from every repository, public and private. Only register machines you fully trust." |
| 258 | } |