| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "net/http" |
| 6 | "strconv" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 11 | "gitlab.com/Timfa/bluegit/internal/store" |
| 12 | ) |
| 13 | |
| 14 | // Beheer > Gebruikers: elke sitebeheerder kan een ander beheerder maken of |
| 15 | // die rechten weer afnemen. Jezelf degraderen kan niet, en er blijft altijd |
| 16 | // minstens één beheerder (store.SetInstanceAdmin). Elke wijziging komt als |
| 17 | // auditregel in de log ("admin audit"). |
| 18 | |
| 19 | // AdminTab is één subtab onder Beheer. |
| 20 | type AdminTab struct { |
| 21 | Key, Href string |
| 22 | On bool |
| 23 | } |
| 24 | |
| 25 | func adminTabs(page string) []AdminTab { |
| 26 | tabs := []AdminTab{ |
| 27 | {Key: "admin.tab.settings", Href: "/admin/settings"}, |
| 28 | {Key: "admin.tab.users", Href: "/admin/users"}, |
| 29 | {Key: "admin.tab.runner", Href: "/admin/runner"}, |
| 30 | {Key: "admin.tab.storage", Href: "/setup"}, |
| 31 | {Key: "admin.tab.pipelines", Href: "/admin/pipelines"}, |
| 32 | {Key: "admin.tab.backup", Href: "/admin/backup"}, |
| 33 | } |
| 34 | on := map[string]string{ |
| 35 | "adminsettings": "/admin/settings", |
| 36 | "adminusers": "/admin/users", |
| 37 | "adminrunner": "/admin/runner", |
| 38 | "setup": "/setup", |
| 39 | "adminpipelines": "/admin/pipelines", |
| 40 | "adminbackup": "/admin/backup", |
| 41 | "adminbackupserver": "/admin/backup", |
| 42 | } |
| 43 | href := on[page] |
| 44 | for i := range tabs { |
| 45 | tabs[i].On = tabs[i].Href == href |
| 46 | } |
| 47 | return tabs |
| 48 | } |
| 49 | |
| 50 | func isAdminPage(page string) bool { |
| 51 | return page == "adminsettings" || page == "adminusers" || page == "adminrunner" || page == "setup" || page == "adminpipelines" || page == "adminbackup" || page == "adminbackupserver" |
| 52 | } |
| 53 | |
| 54 | func (s *Server) mountAdmin(mux *http.ServeMux) { |
| 55 | mux.HandleFunc("GET /admin", s.adminHome) |
| 56 | mux.HandleFunc("GET /admin/{$}", s.adminHome) |
| 57 | mux.HandleFunc("GET /admin/settings", s.adminSettings) |
| 58 | mux.HandleFunc("POST /admin/settings", s.adminSettingsSave) |
| 59 | mux.HandleFunc("GET /admin/users", s.adminUsers) |
| 60 | mux.HandleFunc("POST /admin/users/role", s.adminUserRole) |
| 61 | mux.HandleFunc("POST /admin/users/token", s.adminUserToken) |
| 62 | mux.HandleFunc("GET /admin/pipelines", s.adminPipelines) |
| 63 | mux.HandleFunc("POST /admin/pipelines", s.adminPipelinesSave) |
| 64 | mux.HandleFunc("GET /admin/storage", func(w http.ResponseWriter, r *http.Request) { |
| 65 | if ok, _ := s.adminOnly(w, r); ok { |
| 66 | http.Redirect(w, r, "/setup", http.StatusFound) |
| 67 | } |
| 68 | }) |
| 69 | } |
| 70 | |
| 71 | func (s *Server) adminHome(w http.ResponseWriter, r *http.Request) { |
| 72 | if ok, _ := s.adminOnly(w, r); ok { |
| 73 | http.Redirect(w, r, "/admin/settings", http.StatusFound) |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | // AdminUserRow is één rij in Beheer > Gebruikers. |
| 78 | type AdminUserRow struct { |
| 79 | User store.User |
| 80 | Self bool |
| 81 | } |
| 82 | |
| 83 | func (s *Server) adminUsers(w http.ResponseWriter, r *http.Request) { |
| 84 | if ok, _ := s.adminOnly(w, r); !ok { |
| 85 | return |
| 86 | } |
| 87 | users, err := s.Store.ListUsers(r.Context()) |
| 88 | if err != nil { |
| 89 | http.Error(w, err.Error(), http.StatusInternalServerError) |
| 90 | return |
| 91 | } |
| 92 | v := s.view(w, r, "title.admin") |
| 93 | v.Page = "adminusers" |
| 94 | v.NewAdminToken = cookieVal(r, "bluegit_admin_token_once") |
| 95 | v.AdminTokenUser = cookieVal(r, "bluegit_admin_token_user") |
| 96 | if v.NewAdminToken != "" { |
| 97 | http.SetCookie(w, &http.Cookie{Name: "bluegit_admin_token_once", Value: "", Path: "/", MaxAge: -1}) |
| 98 | http.SetCookie(w, &http.Cookie{Name: "bluegit_admin_token_user", Value: "", Path: "/", MaxAge: -1}) |
| 99 | } |
| 100 | me := s.who(r).User.ID |
| 101 | for _, u := range users { |
| 102 | v.AdminUsers = append(v.AdminUsers, AdminUserRow{User: u, Self: u.ID == me}) |
| 103 | } |
| 104 | s.render(w, "adminusers", v) |
| 105 | } |
| 106 | |
| 107 | func adminRoleErr(err error) error { |
| 108 | switch { |
| 109 | case errors.Is(err, store.ErrSelfDemote): |
| 110 | return i18n.E("admin.err.self") |
| 111 | case errors.Is(err, store.ErrLastAdmin): |
| 112 | return i18n.E("admin.err.last") |
| 113 | } |
| 114 | return i18n.E("admin.err.user") |
| 115 | } |
| 116 | |
| 117 | func (s *Server) setAdmin(r *http.Request, target int64, admin bool) (store.User, error) { |
| 118 | by := s.who(r).User |
| 119 | if by.ID == 0 { |
| 120 | by = apiUser(r) |
| 121 | } |
| 122 | u, err := s.Store.SetInstanceAdmin(r.Context(), by.ID, target, admin) |
| 123 | if err != nil { |
| 124 | return u, err |
| 125 | } |
| 126 | if s.Log != nil { |
| 127 | s.Log.Info("admin audit", "action", map[bool]string{true: "grant_admin", false: "revoke_admin"}[admin], "by", by.Username, "target", u.Username) |
| 128 | } |
| 129 | return u, nil |
| 130 | } |
| 131 | |
| 132 | func (s *Server) adminUserRole(w http.ResponseWriter, r *http.Request) { |
| 133 | if ok, _ := s.adminOnly(w, r); !ok || !s.csrfOK(w, r) { |
| 134 | return |
| 135 | } |
| 136 | id, _ := strconv.ParseInt(r.FormValue("id"), 10, 64) |
| 137 | u, err := s.setAdmin(r, id, r.FormValue("admin") == "1") |
| 138 | if err != nil { |
| 139 | s.redirect(w, r, "/admin/users", "", s.trErr(r, adminRoleErr(err))) |
| 140 | return |
| 141 | } |
| 142 | key := "admin.ok.granted" |
| 143 | if !u.InstanceAdmin { |
| 144 | key = "admin.ok.revoked" |
| 145 | } |
| 146 | s.redirect(w, r, "/admin/users", s.tr(r, key, u.Username), "") |
| 147 | } |
| 148 | |
| 149 | func adminUserJSON(u store.User) map[string]any { |
| 150 | return map[string]any{"id": u.ID, "username": u.Username, "admin": u.InstanceAdmin, "created_at": rfcTime(u.CreatedAt)} |
| 151 | } |
| 152 | |
| 153 | func (s *Server) apiAdminUsers(w http.ResponseWriter, r *http.Request) { |
| 154 | if !apiUser(r).InstanceAdmin { |
| 155 | s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden"))) |
| 156 | return |
| 157 | } |
| 158 | users, err := s.Store.ListUsers(r.Context()) |
| 159 | if err != nil { |
| 160 | s.writeAPIErr(w, r, err) |
| 161 | return |
| 162 | } |
| 163 | out := []map[string]any{} |
| 164 | for _, u := range users { |
| 165 | out = append(out, adminUserJSON(u)) |
| 166 | } |
| 167 | writeJSON(w, http.StatusOK, out) |
| 168 | } |
| 169 | |
| 170 | func (s *Server) apiAdminUserRole(w http.ResponseWriter, r *http.Request) { |
| 171 | if !apiUser(r).InstanceAdmin { |
| 172 | s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden"))) |
| 173 | return |
| 174 | } |
| 175 | var in struct { |
| 176 | Admin *bool `json:"admin"` |
| 177 | } |
| 178 | if err := readJSON(r, &in); err != nil || in.Admin == nil { |
| 179 | s.writeAPIErr(w, r, fail(400, i18n.E("admin.err.body"))) |
| 180 | return |
| 181 | } |
| 182 | target, err := s.Store.UserByUsername(r.Context(), strings.TrimSpace(r.PathValue("username"))) |
| 183 | if err != nil { |
| 184 | s.writeAPIErr(w, r, fail(404, i18n.E("admin.err.user"))) |
| 185 | return |
| 186 | } |
| 187 | u, err := s.setAdmin(r, target.ID, *in.Admin) |
| 188 | if err != nil { |
| 189 | code := 409 |
| 190 | if errors.Is(err, store.ErrSelfDemote) { |
| 191 | code = 403 |
| 192 | } |
| 193 | s.writeAPIErr(w, r, fail(code, adminRoleErr(err))) |
| 194 | return |
| 195 | } |
| 196 | writeJSON(w, http.StatusOK, adminUserJSON(u)) |
| 197 | } |
| 198 | |
| 199 | func (s *Server) issueTokenForUser(r *http.Request, u store.User, name string) (string, store.TokenInfo, error) { |
| 200 | plain, info, err := s.opIssueToken(r.Context(), u, name, nil, time.Time{}) |
| 201 | if err != nil { |
| 202 | return "", info, err |
| 203 | } |
| 204 | if s.Log != nil { |
| 205 | s.Log.Info("admin audit", "action", "create_token", "by", s.who(r).User.Username, "target", u.Username, "name", info.Name) |
| 206 | } |
| 207 | return plain, info, nil |
| 208 | } |
| 209 | |
| 210 | func (s *Server) adminUserToken(w http.ResponseWriter, r *http.Request) { |
| 211 | if ok, _ := s.adminOnly(w, r); !ok || !s.csrfOK(w, r) { |
| 212 | return |
| 213 | } |
| 214 | id, _ := strconv.ParseInt(r.FormValue("id"), 10, 64) |
| 215 | u, err := s.Store.UserByID(r.Context(), id) |
| 216 | if err != nil { |
| 217 | s.redirect(w, r, "/admin/users", "", s.trErr(r, i18n.E("admin.err.user"))) |
| 218 | return |
| 219 | } |
| 220 | plain, _, err := s.issueTokenForUser(r, u, r.FormValue("name")) |
| 221 | if err != nil { |
| 222 | s.redirect(w, r, "/admin/users", "", s.trErr(r, err)) |
| 223 | return |
| 224 | } |
| 225 | secure := strings.HasPrefix(s.Cfg.BaseURL, "https://") |
| 226 | http.SetCookie(w, &http.Cookie{Name: "bluegit_admin_token_once", Value: plain, Path: "/", MaxAge: 120, HttpOnly: true, Secure: secure, SameSite: http.SameSiteLaxMode}) |
| 227 | http.SetCookie(w, &http.Cookie{Name: "bluegit_admin_token_user", Value: u.Username, Path: "/", MaxAge: 120, HttpOnly: true, Secure: secure, SameSite: http.SameSiteLaxMode}) |
| 228 | s.redirect(w, r, "/admin/users", s.tr(r, "admin.token.created", u.Username), "") |
| 229 | } |
| 230 | |
| 231 | func (s *Server) apiAdminUserToken(w http.ResponseWriter, r *http.Request) { |
| 232 | if !apiUser(r).InstanceAdmin { |
| 233 | s.writeAPIErr(w, r, fail(403, i18n.E("err.forbidden"))) |
| 234 | return |
| 235 | } |
| 236 | var in struct { |
| 237 | Name string `json:"name"` |
| 238 | } |
| 239 | if err := readJSON(r, &in); err != nil { |
| 240 | s.writeAPIErr(w, r, fail(400, i18n.E("err.json"))) |
| 241 | return |
| 242 | } |
| 243 | u, err := s.Store.UserByUsername(r.Context(), strings.TrimSpace(r.PathValue("username"))) |
| 244 | if err != nil { |
| 245 | s.writeAPIErr(w, r, fail(404, i18n.E("admin.err.user"))) |
| 246 | return |
| 247 | } |
| 248 | plain, info, err := s.issueTokenForUser(r, u, in.Name) |
| 249 | if !s.apiOK(w, r, err) { |
| 250 | return |
| 251 | } |
| 252 | writeJSON(w, http.StatusCreated, tokenJSON(info, plain)) |
| 253 | } |