| 1 | package store |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "database/sql" |
| 6 | "errors" |
| 7 | "strings" |
| 8 | "time" |
| 9 | |
| 10 | "gitlab.com/Timfa/bluegit/internal/access" |
| 11 | "gitlab.com/Timfa/bluegit/internal/i18n" |
| 12 | "gitlab.com/Timfa/bluegit/internal/names" |
| 13 | ) |
| 14 | |
| 15 | // Group is een verzameling repositories en leden. |
| 16 | type Group struct { |
| 17 | ID int64 |
| 18 | Name string |
| 19 | Path string |
| 20 | Description string |
| 21 | Visibility string |
| 22 | DefaultRole access.Role |
| 23 | LogoMIME string |
| 24 | CreatedBy int64 |
| 25 | CreatedAt time.Time |
| 26 | } |
| 27 | |
| 28 | // HasLogo meldt of er een geüpload logo is. |
| 29 | func (g Group) HasLogo() bool { return g.LogoMIME != "" } |
| 30 | |
| 31 | // Repo is een git-repository van een gebruiker of een groep. |
| 32 | type Repo struct { |
| 33 | ID int64 |
| 34 | OwnerKind string |
| 35 | OwnerID int64 |
| 36 | OwnerPath string |
| 37 | Name string |
| 38 | Description string |
| 39 | DefaultBranch string |
| 40 | Visibility string |
| 41 | LogoMIME string |
| 42 | CreatedAt time.Time |
| 43 | } |
| 44 | |
| 45 | // HasLogo meldt of er een geüpload logo is. |
| 46 | func (r Repo) HasLogo() bool { return r.LogoMIME != "" } |
| 47 | |
| 48 | // FullPath is owner/naam. |
| 49 | func (r Repo) FullPath() string { return r.OwnerPath + "/" + r.Name } |
| 50 | |
| 51 | // CreateGroup maakt een groep. De maker wordt beheerder. |
| 52 | // defaultRole is de rol die leden krijgen op een repository die daarna ontstaat. |
| 53 | func (s *Store) CreateGroup(ctx context.Context, creator int64, name, path, description string, defaultRole access.Role) (Group, error) { |
| 54 | name = strings.TrimSpace(name) |
| 55 | path = strings.ToLower(strings.TrimSpace(path)) |
| 56 | if name == "" { |
| 57 | return Group{}, i18n.E("err.groupNeedsName") |
| 58 | } |
| 59 | if defaultRole == "" { |
| 60 | defaultRole = access.RoleDeveloper |
| 61 | } |
| 62 | if _, err := access.ParseRole(string(defaultRole)); err != nil { |
| 63 | return Group{}, err |
| 64 | } |
| 65 | if err := names.ValidGroupPath(path); err != nil { |
| 66 | return Group{}, err |
| 67 | } |
| 68 | if err := s.ensurePathFree(ctx, path); err != nil { |
| 69 | return Group{}, err |
| 70 | } |
| 71 | res, err := s.db.ExecContext(ctx, `INSERT INTO groups (name, path, description, visibility, default_role, created_by, created_at) VALUES (?, ?, ?, 'private', ?, ?, ?)`, |
| 72 | name, path, strings.TrimSpace(description), string(defaultRole), creator, now()) |
| 73 | if err != nil { |
| 74 | return Group{}, err |
| 75 | } |
| 76 | id, _ := res.LastInsertId() |
| 77 | if _, err := s.db.ExecContext(ctx, `INSERT INTO group_members (group_id, user_id, role) VALUES (?, ?, ?)`, id, creator, string(access.RoleMaintainer)); err != nil { |
| 78 | return Group{}, err |
| 79 | } |
| 80 | return s.GroupByID(ctx, id) |
| 81 | } |
| 82 | |
| 83 | func (s *Store) ensurePathFree(ctx context.Context, path string) error { |
| 84 | var n int |
| 85 | if err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM users WHERE username = ?`, path).Scan(&n); err != nil { |
| 86 | return err |
| 87 | } |
| 88 | if n > 0 { |
| 89 | return i18n.E("err.pathUser", path) |
| 90 | } |
| 91 | if err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM groups WHERE path = ?`, path).Scan(&n); err != nil { |
| 92 | return err |
| 93 | } |
| 94 | if n > 0 { |
| 95 | return i18n.E("err.pathGroup", path) |
| 96 | } |
| 97 | return nil |
| 98 | } |
| 99 | |
| 100 | // GroupByPath leest een groep. |
| 101 | func (s *Store) GroupByPath(ctx context.Context, path string) (Group, error) { |
| 102 | row := s.db.QueryRowContext(ctx, `SELECT `+groupCols+` FROM groups WHERE path = ?`, path) |
| 103 | return scanGroup(row) |
| 104 | } |
| 105 | |
| 106 | // GroupByID leest een groep. |
| 107 | func (s *Store) GroupByID(ctx context.Context, id int64) (Group, error) { |
| 108 | row := s.db.QueryRowContext(ctx, `SELECT `+groupCols+` FROM groups WHERE id = ?`, id) |
| 109 | return scanGroup(row) |
| 110 | } |
| 111 | |
| 112 | const groupCols = `id, name, path, description, created_by, created_at, visibility, default_role, logo_mime` |
| 113 | |
| 114 | func scanGroup(row *sql.Row) (Group, error) { |
| 115 | var g Group |
| 116 | var created, role string |
| 117 | if err := row.Scan(&g.ID, &g.Name, &g.Path, &g.Description, &g.CreatedBy, &created, &g.Visibility, &role, &g.LogoMIME); err != nil { |
| 118 | if errors.Is(err, sql.ErrNoRows) { |
| 119 | return Group{}, errNotFound |
| 120 | } |
| 121 | return Group{}, err |
| 122 | } |
| 123 | g.DefaultRole = access.Role(role) |
| 124 | g.CreatedAt = parseTime(created) |
| 125 | return g, nil |
| 126 | } |
| 127 | |
| 128 | // GroupRole is de rol van een gebruiker in een groep. |
| 129 | // Installatiebeheer geeft geen lidmaatschap. |
| 130 | func (s *Store) GroupRole(ctx context.Context, groupID, userID int64) (access.Role, error) { |
| 131 | if userID == 0 { |
| 132 | return access.RoleNone, nil |
| 133 | } |
| 134 | var role string |
| 135 | err := s.db.QueryRowContext(ctx, `SELECT role FROM group_members WHERE group_id = ? AND user_id = ?`, groupID, userID).Scan(&role) |
| 136 | if errors.Is(err, sql.ErrNoRows) { |
| 137 | return access.RoleNone, nil |
| 138 | } |
| 139 | if err != nil { |
| 140 | return access.RoleNone, err |
| 141 | } |
| 142 | return access.Role(role), nil |
| 143 | } |
| 144 | |
| 145 | // ListGroups geeft de groepen waarvan deze gebruiker lid is. |
| 146 | func (s *Store) ListGroups(ctx context.Context, userID int64) ([]Group, error) { |
| 147 | q := `SELECT ` + groupCols + ` FROM groups WHERE id IN (SELECT group_id FROM group_members WHERE user_id = ?) ORDER BY path` |
| 148 | args := []any{userID} |
| 149 | rows, err := s.db.QueryContext(ctx, q, args...) |
| 150 | if err != nil { |
| 151 | return nil, err |
| 152 | } |
| 153 | defer rows.Close() |
| 154 | var out []Group |
| 155 | for rows.Next() { |
| 156 | var g Group |
| 157 | var created, role string |
| 158 | if err := rows.Scan(&g.ID, &g.Name, &g.Path, &g.Description, &g.CreatedBy, &created, &g.Visibility, &role, &g.LogoMIME); err != nil { |
| 159 | return nil, err |
| 160 | } |
| 161 | g.DefaultRole = access.Role(role) |
| 162 | g.CreatedAt = parseTime(created) |
| 163 | out = append(out, g) |
| 164 | } |
| 165 | return out, rows.Err() |
| 166 | } |
| 167 | |
| 168 | // CreateRepo maakt de metadata. De schijfmap maakt de aanroeper. |
| 169 | func (s *Store) CreateRepo(ctx context.Context, ownerKind string, ownerID int64, name, description, defaultBranch, visibility string) (Repo, error) { |
| 170 | if ownerKind != "user" && ownerKind != "group" { |
| 171 | return Repo{}, i18n.E("err.ownerKind") |
| 172 | } |
| 173 | if err := names.ValidRepoName(name); err != nil { |
| 174 | return Repo{}, err |
| 175 | } |
| 176 | if visibility != "public" && visibility != "private" { |
| 177 | return Repo{}, i18n.E("err.visibility") |
| 178 | } |
| 179 | if defaultBranch == "" { |
| 180 | defaultBranch = "master" |
| 181 | } |
| 182 | if ownerKind == "user" { |
| 183 | u, err := s.UserByID(ctx, ownerID) |
| 184 | if err != nil { |
| 185 | return Repo{}, err |
| 186 | } |
| 187 | var n int |
| 188 | if err := s.db.QueryRowContext(ctx, `SELECT COUNT(*) FROM groups WHERE path = ?`, u.Username).Scan(&n); err != nil { |
| 189 | return Repo{}, err |
| 190 | } |
| 191 | if n > 0 { |
| 192 | return Repo{}, i18n.E("err.userGroupClash", u.Username) |
| 193 | } |
| 194 | } |
| 195 | res, err := s.db.ExecContext(ctx, `INSERT INTO repos (owner_kind, owner_id, name, description, default_branch, visibility, created_at) VALUES (?, ?, ?, ?, ?, ?, ?)`, |
| 196 | ownerKind, ownerID, name, strings.TrimSpace(description), defaultBranch, visibility, now()) |
| 197 | if err != nil { |
| 198 | if strings.Contains(err.Error(), "UNIQUE") { |
| 199 | return Repo{}, i18n.E("err.repoExists", name) |
| 200 | } |
| 201 | return Repo{}, err |
| 202 | } |
| 203 | id, _ := res.LastInsertId() |
| 204 | if err := s.AddProtected(ctx, id, defaultBranch); err != nil { |
| 205 | return Repo{}, err |
| 206 | } |
| 207 | if ownerKind == "group" { |
| 208 | if err := s.GrantDefaultRoles(ctx, ownerID, id); err != nil { |
| 209 | return Repo{}, err |
| 210 | } |
| 211 | } |
| 212 | return s.RepoByID(ctx, id) |
| 213 | } |
| 214 | |
| 215 | // RepoByID leest een repository. |
| 216 | func (s *Store) RepoByID(ctx context.Context, id int64) (Repo, error) { |
| 217 | row := s.db.QueryRowContext(ctx, repoSelect+` WHERE r.id = ?`, id) |
| 218 | return scanRepo(row) |
| 219 | } |
| 220 | |
| 221 | // RepoByPath leest owner/naam. |
| 222 | func (s *Store) RepoByPath(ctx context.Context, owner, name string) (Repo, error) { |
| 223 | row := s.db.QueryRowContext(ctx, repoSelect+` |
| 224 | WHERE r.name = ? AND ( |
| 225 | (r.owner_kind = 'user' AND r.owner_id = (SELECT id FROM users WHERE username = ?)) |
| 226 | OR (r.owner_kind = 'group' AND r.owner_id = (SELECT id FROM groups WHERE path = ?)) |
| 227 | )`, name, owner, owner) |
| 228 | return scanRepo(row) |
| 229 | } |
| 230 | |
| 231 | const repoSelect = ` |
| 232 | SELECT r.id, r.owner_kind, r.owner_id, |
| 233 | CASE r.owner_kind WHEN 'user' THEN (SELECT username FROM users WHERE id = r.owner_id) ELSE (SELECT path FROM groups WHERE id = r.owner_id) END, |
| 234 | r.name, r.description, r.default_branch, r.visibility, r.created_at, r.logo_mime |
| 235 | FROM repos r` |
| 236 | |
| 237 | func scanRepo(row *sql.Row) (Repo, error) { |
| 238 | var r Repo |
| 239 | var created string |
| 240 | var owner sql.NullString |
| 241 | if err := row.Scan(&r.ID, &r.OwnerKind, &r.OwnerID, &owner, &r.Name, &r.Description, &r.DefaultBranch, &r.Visibility, &created, &r.LogoMIME); err != nil { |
| 242 | if errors.Is(err, sql.ErrNoRows) { |
| 243 | return Repo{}, errNotFound |
| 244 | } |
| 245 | return Repo{}, err |
| 246 | } |
| 247 | r.OwnerPath = owner.String |
| 248 | r.CreatedAt = parseTime(created) |
| 249 | return r, nil |
| 250 | } |
| 251 | |
| 252 | // UpdateRepo wijzigt beschrijving en zichtbaarheid. |
| 253 | func (s *Store) UpdateRepo(ctx context.Context, id int64, description, visibility string) error { |
| 254 | if visibility != "public" && visibility != "private" { |
| 255 | return i18n.E("err.visibility") |
| 256 | } |
| 257 | _, err := s.db.ExecContext(ctx, `UPDATE repos SET description = ?, visibility = ? WHERE id = ?`, strings.TrimSpace(description), visibility, id) |
| 258 | return err |
| 259 | } |
| 260 | |
| 261 | // DeleteRepo verwijdert de metadata. Bestanden verwijdert de aanroeper. |
| 262 | func (s *Store) DeleteRepo(ctx context.Context, id int64) error { |
| 263 | _, err := s.db.ExecContext(ctx, `DELETE FROM repos WHERE id = ?`, id) |
| 264 | return err |
| 265 | } |
| 266 | |
| 267 | // ListRepoIDs geeft repositories die deze gebruiker mag zien: |
| 268 | // openbare repositories, eigen repositories, repositories van een groep |
| 269 | // waarvan hij lid is, en repositories met een rechtstreeks lidmaatschap. |
| 270 | // Installatiebeheer voegt daar niets aan toe. |
| 271 | func (s *Store) ListRepoIDs(ctx context.Context, userID int64) ([]int64, error) { |
| 272 | q := `SELECT id FROM repos WHERE visibility = 'public'` |
| 273 | args := []any{} |
| 274 | if userID != 0 { |
| 275 | q = `SELECT DISTINCT id FROM repos WHERE visibility = 'public' |
| 276 | OR (owner_kind = 'user' AND owner_id = ?) |
| 277 | OR (owner_kind = 'group' AND owner_id IN (SELECT group_id FROM group_members WHERE user_id = ?)) |
| 278 | OR id IN (SELECT repo_id FROM repo_members WHERE user_id = ?)` |
| 279 | args = []any{userID, userID, userID} |
| 280 | } |
| 281 | q += ` ORDER BY id DESC` |
| 282 | rows, err := s.db.QueryContext(ctx, q, args...) |
| 283 | if err != nil { |
| 284 | return nil, err |
| 285 | } |
| 286 | defer rows.Close() |
| 287 | var ids []int64 |
| 288 | for rows.Next() { |
| 289 | var id int64 |
| 290 | if err := rows.Scan(&id); err != nil { |
| 291 | return nil, err |
| 292 | } |
| 293 | ids = append(ids, id) |
| 294 | } |
| 295 | return ids, rows.Err() |
| 296 | } |
| 297 | |
| 298 | // ReposByGroup geeft de repositories van een groep. |
| 299 | func (s *Store) ReposByGroup(ctx context.Context, groupID int64) ([]Repo, error) { |
| 300 | rows, err := s.db.QueryContext(ctx, repoSelect+` WHERE r.owner_kind = 'group' AND r.owner_id = ? ORDER BY r.name`, groupID) |
| 301 | if err != nil { |
| 302 | return nil, err |
| 303 | } |
| 304 | defer rows.Close() |
| 305 | return scanRepos(rows) |
| 306 | } |
| 307 | |
| 308 | // ReposByIDs laadt een lijst. |
| 309 | func (s *Store) ReposByIDs(ctx context.Context, ids []int64) ([]Repo, error) { |
| 310 | var out []Repo |
| 311 | for _, id := range ids { |
| 312 | r, err := s.RepoByID(ctx, id) |
| 313 | if err != nil { |
| 314 | return nil, err |
| 315 | } |
| 316 | out = append(out, r) |
| 317 | } |
| 318 | return out, nil |
| 319 | } |
| 320 | |
| 321 | func scanRepos(rows *sql.Rows) ([]Repo, error) { |
| 322 | var out []Repo |
| 323 | for rows.Next() { |
| 324 | var r Repo |
| 325 | var created string |
| 326 | var owner sql.NullString |
| 327 | if err := rows.Scan(&r.ID, &r.OwnerKind, &r.OwnerID, &owner, &r.Name, &r.Description, &r.DefaultBranch, &r.Visibility, &created, &r.LogoMIME); err != nil { |
| 328 | return nil, err |
| 329 | } |
| 330 | r.OwnerPath = owner.String |
| 331 | r.CreatedAt = parseTime(created) |
| 332 | out = append(out, r) |
| 333 | } |
| 334 | return out, rows.Err() |
| 335 | } |
| 336 | |
| 337 | // AllRepos is voor het verversen van hooks bij het starten. |
| 338 | func (s *Store) AllRepos(ctx context.Context) ([]Repo, error) { |
| 339 | rows, err := s.db.QueryContext(ctx, repoSelect+` ORDER BY r.id`) |
| 340 | if err != nil { |
| 341 | return nil, err |
| 342 | } |
| 343 | defer rows.Close() |
| 344 | return scanRepos(rows) |
| 345 | } |
| 346 | |
| 347 | // EffectiveRole past de rolregels toe op een repository. |
| 348 | func (s *Store) EffectiveRole(ctx context.Context, userID, repoID int64) (access.Role, error) { |
| 349 | if userID == 0 { |
| 350 | return access.RoleNone, nil |
| 351 | } |
| 352 | if _, err := s.UserByID(ctx, userID); err != nil { |
| 353 | return access.RoleNone, err |
| 354 | } |
| 355 | repo, err := s.RepoByID(ctx, repoID) |
| 356 | if err != nil { |
| 357 | return access.RoleNone, err |
| 358 | } |
| 359 | in := access.Input{} |
| 360 | if repo.OwnerKind == "user" && repo.OwnerID == userID { |
| 361 | in.UserOwner = true |
| 362 | } |
| 363 | if repo.OwnerKind == "group" { |
| 364 | var role string |
| 365 | err := s.db.QueryRowContext(ctx, `SELECT role FROM group_members WHERE group_id = ? AND user_id = ?`, repo.OwnerID, userID).Scan(&role) |
| 366 | if err != nil && !errors.Is(err, sql.ErrNoRows) { |
| 367 | return access.RoleNone, err |
| 368 | } |
| 369 | if err == nil { |
| 370 | in.GroupRole = access.Role(role) |
| 371 | } |
| 372 | } |
| 373 | var role string |
| 374 | err = s.db.QueryRowContext(ctx, `SELECT role FROM repo_members WHERE repo_id = ? AND user_id = ?`, repoID, userID).Scan(&role) |
| 375 | if err != nil && !errors.Is(err, sql.ErrNoRows) { |
| 376 | return access.RoleNone, err |
| 377 | } |
| 378 | if err == nil { |
| 379 | in.RepoRole = access.Role(role) |
| 380 | } |
| 381 | return access.Resolve(in), nil |
| 382 | } |
| 383 | |
| 384 | // Member is een lid of een uitnodiging op gebruikersnaam. |
| 385 | type Member struct { |
| 386 | UserID int64 |
| 387 | Username string |
| 388 | Role access.Role |
| 389 | Pending bool |
| 390 | } |
| 391 | |
| 392 | // SetMember zet een lidmaatschap. Een onbekende naam blijft wachten tot de eerste login. |
| 393 | func (s *Store) SetMember(ctx context.Context, scopeKind string, scopeID int64, username string, role access.Role) error { |
| 394 | if scopeKind != "repo" && scopeKind != "group" { |
| 395 | return i18n.E("err.scope") |
| 396 | } |
| 397 | if _, err := access.ParseRole(string(role)); err != nil { |
| 398 | return err |
| 399 | } |
| 400 | username = strings.ToLower(strings.TrimSpace(username)) |
| 401 | if err := names.ValidUsername(username); err != nil { |
| 402 | return err |
| 403 | } |
| 404 | u, err := s.UserByUsername(ctx, username) |
| 405 | if IsNotFound(err) { |
| 406 | _, err = s.db.ExecContext(ctx, `INSERT INTO pending_members (scope_kind, scope_id, username, role) VALUES (?, ?, ?, ?) |
| 407 | ON CONFLICT(scope_kind, scope_id, username) DO UPDATE SET role = excluded.role`, scopeKind, scopeID, username, string(role)) |
| 408 | return err |
| 409 | } |
| 410 | if err != nil { |
| 411 | return err |
| 412 | } |
| 413 | table, col := "repo_members", "repo_id" |
| 414 | if scopeKind == "group" { |
| 415 | table, col = "group_members", "group_id" |
| 416 | } |
| 417 | _, err = s.db.ExecContext(ctx, `INSERT INTO `+table+` (`+col+`, user_id, role) VALUES (?, ?, ?) |
| 418 | ON CONFLICT(`+col+`, user_id) DO UPDATE SET role = excluded.role`, scopeID, u.ID, string(role)) |
| 419 | return err |
| 420 | } |
| 421 | |
| 422 | // RemoveMember haalt een lid of een uitnodiging weg. |
| 423 | func (s *Store) RemoveMember(ctx context.Context, scopeKind string, scopeID int64, username string) error { |
| 424 | username = strings.ToLower(strings.TrimSpace(username)) |
| 425 | _, err := s.db.ExecContext(ctx, `DELETE FROM pending_members WHERE scope_kind = ? AND scope_id = ? AND username = ?`, scopeKind, scopeID, username) |
| 426 | if err != nil { |
| 427 | return err |
| 428 | } |
| 429 | u, err := s.UserByUsername(ctx, username) |
| 430 | if IsNotFound(err) { |
| 431 | return nil |
| 432 | } |
| 433 | if err != nil { |
| 434 | return err |
| 435 | } |
| 436 | if scopeKind == "group" { |
| 437 | _, err = s.db.ExecContext(ctx, `DELETE FROM group_members WHERE group_id = ? AND user_id = ?`, scopeID, u.ID) |
| 438 | return err |
| 439 | } |
| 440 | _, err = s.db.ExecContext(ctx, `DELETE FROM repo_members WHERE repo_id = ? AND user_id = ?`, scopeID, u.ID) |
| 441 | return err |
| 442 | } |
| 443 | |
| 444 | // ListMembers geeft leden van een repository of groep, plus open uitnodigingen. |
| 445 | func (s *Store) ListMembers(ctx context.Context, scopeKind string, scopeID int64) ([]Member, error) { |
| 446 | var q string |
| 447 | if scopeKind == "group" { |
| 448 | q = `SELECT u.id, u.username, m.role FROM group_members m JOIN users u ON u.id = m.user_id WHERE m.group_id = ? ORDER BY u.username` |
| 449 | } else { |
| 450 | q = `SELECT u.id, u.username, m.role FROM repo_members m JOIN users u ON u.id = m.user_id WHERE m.repo_id = ? ORDER BY u.username` |
| 451 | } |
| 452 | rows, err := s.db.QueryContext(ctx, q, scopeID) |
| 453 | if err != nil { |
| 454 | return nil, err |
| 455 | } |
| 456 | defer rows.Close() |
| 457 | var out []Member |
| 458 | for rows.Next() { |
| 459 | var m Member |
| 460 | var role string |
| 461 | if err := rows.Scan(&m.UserID, &m.Username, &role); err != nil { |
| 462 | return nil, err |
| 463 | } |
| 464 | m.Role = access.Role(role) |
| 465 | out = append(out, m) |
| 466 | } |
| 467 | if err := rows.Err(); err != nil { |
| 468 | return nil, err |
| 469 | } |
| 470 | prows, err := s.db.QueryContext(ctx, `SELECT username, role FROM pending_members WHERE scope_kind = ? AND scope_id = ? ORDER BY username`, scopeKind, scopeID) |
| 471 | if err != nil { |
| 472 | return nil, err |
| 473 | } |
| 474 | defer prows.Close() |
| 475 | for prows.Next() { |
| 476 | var m Member |
| 477 | var role string |
| 478 | if err := prows.Scan(&m.Username, &role); err != nil { |
| 479 | return nil, err |
| 480 | } |
| 481 | m.Role = access.Role(role) |
| 482 | m.Pending = true |
| 483 | out = append(out, m) |
| 484 | } |
| 485 | return out, prows.Err() |
| 486 | } |
| 487 | |
| 488 | // ProtectedPatterns geeft de beschermde branchpatronen. |
| 489 | func (s *Store) ProtectedPatterns(ctx context.Context, repoID int64) ([]string, error) { |
| 490 | rows, err := s.db.QueryContext(ctx, `SELECT pattern FROM protected_branches WHERE repo_id = ? ORDER BY pattern`, repoID) |
| 491 | if err != nil { |
| 492 | return nil, err |
| 493 | } |
| 494 | defer rows.Close() |
| 495 | var out []string |
| 496 | for rows.Next() { |
| 497 | var p string |
| 498 | if err := rows.Scan(&p); err != nil { |
| 499 | return nil, err |
| 500 | } |
| 501 | out = append(out, p) |
| 502 | } |
| 503 | return out, rows.Err() |
| 504 | } |
| 505 | |
| 506 | // AddProtected voegt een patroon toe. |
| 507 | func (s *Store) AddProtected(ctx context.Context, repoID int64, pattern string) error { |
| 508 | pattern = strings.TrimSpace(pattern) |
| 509 | if pattern == "" || strings.Contains(pattern, " ") || strings.Contains(pattern, "..") { |
| 510 | return i18n.E("err.pattern", pattern) |
| 511 | } |
| 512 | _, err := s.db.ExecContext(ctx, `INSERT INTO protected_branches (repo_id, pattern) VALUES (?, ?)`, repoID, pattern) |
| 513 | if err != nil && strings.Contains(err.Error(), "UNIQUE") { |
| 514 | return nil |
| 515 | } |
| 516 | return err |
| 517 | } |
| 518 | |
| 519 | // RemoveProtected haalt een patroon weg. |
| 520 | func (s *Store) RemoveProtected(ctx context.Context, repoID int64, pattern string) error { |
| 521 | _, err := s.db.ExecContext(ctx, `DELETE FROM protected_branches WHERE repo_id = ? AND pattern = ?`, repoID, pattern) |
| 522 | return err |
| 523 | } |
| 524 | |
| 525 | // SecretMeta is een geheim zonder de waarde. |
| 526 | type SecretMeta struct { |
| 527 | Name string |
| 528 | Protected bool |
| 529 | CreatedAt time.Time |
| 530 | } |
| 531 | |
| 532 | // PutSecret bewaart een versleutelde waarde. De naam is een omgevingsvariabele. |
| 533 | func (s *Store) PutSecret(ctx context.Context, scopeKind string, scopeID int64, name, value string, protected bool) error { |
| 534 | if scopeKind != "repo" && scopeKind != "group" { |
| 535 | return i18n.E("err.scope") |
| 536 | } |
| 537 | if err := access.ValidSecretName(name); err != nil { |
| 538 | return err |
| 539 | } |
| 540 | if strings.TrimSpace(value) == "" { |
| 541 | return i18n.E("err.emptyValue") |
| 542 | } |
| 543 | nonce, ct, err := s.encrypt(value) |
| 544 | if err != nil { |
| 545 | return err |
| 546 | } |
| 547 | _, err = s.db.ExecContext(ctx, `INSERT INTO secrets (scope_kind, scope_id, name, ciphertext, nonce, protected, created_at) VALUES (?, ?, ?, ?, ?, ?, ?) |
| 548 | ON CONFLICT(scope_kind, scope_id, name) DO UPDATE SET ciphertext = excluded.ciphertext, nonce = excluded.nonce, protected = excluded.protected`, |
| 549 | scopeKind, scopeID, name, ct, nonce, boolInt(protected), now()) |
| 550 | return err |
| 551 | } |
| 552 | |
| 553 | // DeleteSecret verwijdert een geheim. |
| 554 | func (s *Store) DeleteSecret(ctx context.Context, scopeKind string, scopeID int64, name string) error { |
| 555 | _, err := s.db.ExecContext(ctx, `DELETE FROM secrets WHERE scope_kind = ? AND scope_id = ? AND name = ?`, scopeKind, scopeID, name) |
| 556 | return err |
| 557 | } |
| 558 | |
| 559 | // ListSecrets geeft namen, niet de waarden. |
| 560 | func (s *Store) ListSecrets(ctx context.Context, scopeKind string, scopeID int64) ([]SecretMeta, error) { |
| 561 | rows, err := s.db.QueryContext(ctx, `SELECT name, protected, created_at FROM secrets WHERE scope_kind = ? AND scope_id = ? ORDER BY name`, scopeKind, scopeID) |
| 562 | if err != nil { |
| 563 | return nil, err |
| 564 | } |
| 565 | defer rows.Close() |
| 566 | var out []SecretMeta |
| 567 | for rows.Next() { |
| 568 | var m SecretMeta |
| 569 | var p int |
| 570 | var created string |
| 571 | if err := rows.Scan(&m.Name, &p, &created); err != nil { |
| 572 | return nil, err |
| 573 | } |
| 574 | m.Protected = p == 1 |
| 575 | m.CreatedAt = parseTime(created) |
| 576 | out = append(out, m) |
| 577 | } |
| 578 | return out, rows.Err() |
| 579 | } |
| 580 | |
| 581 | // SecretValues ontcijfert geheimen voor een job of webhook. |
| 582 | // refProtected bepaalt of beschermde geheimen meegaan. |
| 583 | func (s *Store) SecretValues(ctx context.Context, scopeKind string, scopeID int64, refProtected bool) (map[string]string, error) { |
| 584 | rows, err := s.db.QueryContext(ctx, `SELECT name, ciphertext, nonce, protected FROM secrets WHERE scope_kind = ? AND scope_id = ?`, scopeKind, scopeID) |
| 585 | if err != nil { |
| 586 | return nil, err |
| 587 | } |
| 588 | defer rows.Close() |
| 589 | out := map[string]string{} |
| 590 | for rows.Next() { |
| 591 | var name string |
| 592 | var ct, nonce []byte |
| 593 | var p int |
| 594 | if err := rows.Scan(&name, &ct, &nonce, &p); err != nil { |
| 595 | return nil, err |
| 596 | } |
| 597 | if !access.InjectSecret(p == 1, refProtected) { |
| 598 | continue |
| 599 | } |
| 600 | val, err := s.decrypt(nonce, ct) |
| 601 | if err != nil { |
| 602 | return nil, err |
| 603 | } |
| 604 | out[name] = val |
| 605 | } |
| 606 | return out, rows.Err() |
| 607 | } |