| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "io" |
| 6 | "net/http" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "gitlab.com/Timfa/bluegit/internal/store" |
| 11 | ) |
| 12 | |
| 13 | type secretBody struct { |
| 14 | Name string `json:"name"` |
| 15 | Value string `json:"value"` |
| 16 | Protected bool `json:"protected"` |
| 17 | Invisible bool `json:"invisible"` |
| 18 | } |
| 19 | |
| 20 | func (s *Server) repoSecretsAPI(w http.ResponseWriter, r *http.Request) { |
| 21 | repo, _, ok := s.secretAPIRepo(w, r) |
| 22 | if !ok { |
| 23 | return |
| 24 | } |
| 25 | s.secretListAPI(w, r, "repo", repo.ID) |
| 26 | } |
| 27 | |
| 28 | func (s *Server) groupSecretsAPI(w http.ResponseWriter, r *http.Request) { |
| 29 | g, _, ok := s.secretAPIGroup(w, r) |
| 30 | if !ok { |
| 31 | return |
| 32 | } |
| 33 | s.secretListAPI(w, r, "group", g.ID) |
| 34 | } |
| 35 | |
| 36 | func (s *Server) repoSecretCreateAPI(w http.ResponseWriter, r *http.Request) { |
| 37 | repo, _, ok := s.secretAPIRepo(w, r) |
| 38 | if !ok { |
| 39 | return |
| 40 | } |
| 41 | s.secretWriteAPI(w, r, "repo", repo.ID, "create") |
| 42 | } |
| 43 | |
| 44 | func (s *Server) groupSecretCreateAPI(w http.ResponseWriter, r *http.Request) { |
| 45 | g, _, ok := s.secretAPIGroup(w, r) |
| 46 | if !ok { |
| 47 | return |
| 48 | } |
| 49 | s.secretWriteAPI(w, r, "group", g.ID, "create") |
| 50 | } |
| 51 | |
| 52 | func (s *Server) repoSecretRevealAPI(w http.ResponseWriter, r *http.Request) { |
| 53 | repo, _, ok := s.secretAPIRepo(w, r) |
| 54 | if !ok { |
| 55 | return |
| 56 | } |
| 57 | s.secretRevealAPI(w, r, "repo", repo.ID) |
| 58 | } |
| 59 | |
| 60 | func (s *Server) groupSecretRevealAPI(w http.ResponseWriter, r *http.Request) { |
| 61 | g, _, ok := s.secretAPIGroup(w, r) |
| 62 | if !ok { |
| 63 | return |
| 64 | } |
| 65 | s.secretRevealAPI(w, r, "group", g.ID) |
| 66 | } |
| 67 | |
| 68 | func (s *Server) repoSecretUpdateAPI(w http.ResponseWriter, r *http.Request) { |
| 69 | repo, _, ok := s.secretAPIRepo(w, r) |
| 70 | if !ok { |
| 71 | return |
| 72 | } |
| 73 | s.secretWriteAPI(w, r, "repo", repo.ID, "update") |
| 74 | } |
| 75 | |
| 76 | func (s *Server) groupSecretUpdateAPI(w http.ResponseWriter, r *http.Request) { |
| 77 | g, _, ok := s.secretAPIGroup(w, r) |
| 78 | if !ok { |
| 79 | return |
| 80 | } |
| 81 | s.secretWriteAPI(w, r, "group", g.ID, "update") |
| 82 | } |
| 83 | |
| 84 | func (s *Server) repoSecretReplaceAPI(w http.ResponseWriter, r *http.Request) { |
| 85 | repo, _, ok := s.secretAPIRepo(w, r) |
| 86 | if !ok { |
| 87 | return |
| 88 | } |
| 89 | s.secretWriteAPI(w, r, "repo", repo.ID, "replace") |
| 90 | } |
| 91 | |
| 92 | func (s *Server) groupSecretReplaceAPI(w http.ResponseWriter, r *http.Request) { |
| 93 | g, _, ok := s.secretAPIGroup(w, r) |
| 94 | if !ok { |
| 95 | return |
| 96 | } |
| 97 | s.secretWriteAPI(w, r, "group", g.ID, "replace") |
| 98 | } |
| 99 | |
| 100 | func (s *Server) repoSecretDeleteAPI(w http.ResponseWriter, r *http.Request) { |
| 101 | repo, _, ok := s.secretAPIRepo(w, r) |
| 102 | if !ok { |
| 103 | return |
| 104 | } |
| 105 | s.secretDeleteAPI(w, r, "repo", repo.ID) |
| 106 | } |
| 107 | |
| 108 | func (s *Server) groupSecretDeleteAPI(w http.ResponseWriter, r *http.Request) { |
| 109 | g, _, ok := s.secretAPIGroup(w, r) |
| 110 | if !ok { |
| 111 | return |
| 112 | } |
| 113 | s.secretDeleteAPI(w, r, "group", g.ID) |
| 114 | } |
| 115 | |
| 116 | func (s *Server) secretAPIRepo(w http.ResponseWriter, r *http.Request) (store.Repo, store.User, bool) { |
| 117 | u, ok := s.webhookAPIUser(w, r) |
| 118 | if !ok { |
| 119 | return store.Repo{}, store.User{}, false |
| 120 | } |
| 121 | repo, err := s.Store.RepoByPath(r.Context(), r.PathValue("owner"), r.PathValue("repo")) |
| 122 | if err != nil { |
| 123 | http.NotFound(w, r) |
| 124 | return store.Repo{}, store.User{}, false |
| 125 | } |
| 126 | role, err := s.Store.EffectiveRole(r.Context(), u.ID, repo.ID) |
| 127 | if err != nil || !role.CanManageSecrets() { |
| 128 | http.Error(w, s.tr(r, "err.onlyAdmin"), http.StatusForbidden) |
| 129 | return store.Repo{}, store.User{}, false |
| 130 | } |
| 131 | return repo, u, true |
| 132 | } |
| 133 | |
| 134 | func (s *Server) secretAPIGroup(w http.ResponseWriter, r *http.Request) (store.Group, store.User, bool) { |
| 135 | u, ok := s.webhookAPIUser(w, r) |
| 136 | if !ok { |
| 137 | return store.Group{}, store.User{}, false |
| 138 | } |
| 139 | g, err := s.Store.GroupByPath(r.Context(), r.PathValue("path")) |
| 140 | if err != nil { |
| 141 | http.NotFound(w, r) |
| 142 | return store.Group{}, store.User{}, false |
| 143 | } |
| 144 | role, err := s.Store.GroupRole(r.Context(), g.ID, u.ID) |
| 145 | if err != nil || !role.CanManageSecrets() { |
| 146 | http.Error(w, s.tr(r, "err.onlyAdmin"), http.StatusForbidden) |
| 147 | return store.Group{}, store.User{}, false |
| 148 | } |
| 149 | return g, u, true |
| 150 | } |
| 151 | |
| 152 | func (s *Server) secretListAPI(w http.ResponseWriter, r *http.Request, kind string, scopeID int64) { |
| 153 | list, err := s.Store.ListSecrets(r.Context(), kind, scopeID) |
| 154 | if err != nil { |
| 155 | http.Error(w, s.trErr(r, err), http.StatusInternalServerError) |
| 156 | return |
| 157 | } |
| 158 | out := make([]map[string]any, 0, len(list)) |
| 159 | for _, m := range list { |
| 160 | out = append(out, secretMetaJSON(m)) |
| 161 | } |
| 162 | w.Header().Set("Content-Type", "application/json") |
| 163 | _ = json.NewEncoder(w).Encode(map[string]any{"secrets": out}) |
| 164 | } |
| 165 | |
| 166 | func (s *Server) secretWriteAPI(w http.ResponseWriter, r *http.Request, kind string, scopeID int64, mode string) { |
| 167 | var body secretBody |
| 168 | if err := json.NewDecoder(io.LimitReader(r.Body, 1<<20)).Decode(&body); err != nil { |
| 169 | http.Error(w, s.tr(r, "err.json"), http.StatusBadRequest) |
| 170 | return |
| 171 | } |
| 172 | name := strings.TrimSpace(body.Name) |
| 173 | if mode != "create" { |
| 174 | name = strings.TrimSpace(r.PathValue("name")) |
| 175 | } |
| 176 | var err error |
| 177 | switch mode { |
| 178 | case "update": |
| 179 | err = s.Store.UpdateSecret(r.Context(), kind, scopeID, name, body.Value, body.Protected) |
| 180 | case "replace": |
| 181 | err = s.Store.ReplaceSecret(r.Context(), kind, scopeID, name, body.Value, body.Protected) |
| 182 | default: |
| 183 | err = s.Store.CreateSecret(r.Context(), kind, scopeID, name, body.Value, body.Protected, body.Invisible) |
| 184 | } |
| 185 | if err != nil { |
| 186 | s.writeSecretErr(w, r, err) |
| 187 | return |
| 188 | } |
| 189 | meta, err := s.secretMeta(r, kind, scopeID, name) |
| 190 | if err != nil { |
| 191 | s.writeSecretErr(w, r, err) |
| 192 | return |
| 193 | } |
| 194 | w.Header().Set("Content-Type", "application/json") |
| 195 | if mode == "create" { |
| 196 | w.WriteHeader(http.StatusCreated) |
| 197 | } |
| 198 | _ = json.NewEncoder(w).Encode(secretMetaJSON(meta)) |
| 199 | } |
| 200 | |
| 201 | func (s *Server) secretRevealAPI(w http.ResponseWriter, r *http.Request, kind string, scopeID int64) { |
| 202 | name := strings.TrimSpace(r.PathValue("name")) |
| 203 | value, err := s.Store.RevealSecret(r.Context(), kind, scopeID, name) |
| 204 | if err != nil { |
| 205 | s.writeSecretErr(w, r, err) |
| 206 | return |
| 207 | } |
| 208 | w.Header().Set("Content-Type", "application/json") |
| 209 | _ = json.NewEncoder(w).Encode(map[string]string{"name": name, "value": value}) |
| 210 | } |
| 211 | |
| 212 | func (s *Server) secretDeleteAPI(w http.ResponseWriter, r *http.Request, kind string, scopeID int64) { |
| 213 | if err := s.Store.DeleteSecret(r.Context(), kind, scopeID, strings.TrimSpace(r.PathValue("name"))); err != nil { |
| 214 | s.writeSecretErr(w, r, err) |
| 215 | return |
| 216 | } |
| 217 | w.WriteHeader(http.StatusNoContent) |
| 218 | } |
| 219 | |
| 220 | func (s *Server) secretMeta(r *http.Request, kind string, scopeID int64, name string) (store.SecretMeta, error) { |
| 221 | list, err := s.Store.ListSecrets(r.Context(), kind, scopeID) |
| 222 | if err != nil { |
| 223 | return store.SecretMeta{}, err |
| 224 | } |
| 225 | for _, m := range list { |
| 226 | if m.Name == name { |
| 227 | return m, nil |
| 228 | } |
| 229 | } |
| 230 | return store.SecretMeta{}, store.NotFound() |
| 231 | } |
| 232 | |
| 233 | func secretMetaJSON(m store.SecretMeta) map[string]any { |
| 234 | created := "" |
| 235 | if !m.CreatedAt.IsZero() { |
| 236 | created = m.CreatedAt.UTC().Format(time.RFC3339) |
| 237 | } |
| 238 | return map[string]any{ |
| 239 | "name": m.Name, |
| 240 | "protected": m.Protected, |
| 241 | "invisible": m.Invisible, |
| 242 | "created_at": created, |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | func (s *Server) writeSecretErr(w http.ResponseWriter, r *http.Request, err error) { |
| 247 | http.Error(w, s.trErr(r, err), secretStatus(err)) |
| 248 | } |