oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package equipo
2
3import (
4 "amadan.net/oficina/equipo/internal/equipo/personapi"
5 "context"
6 "crypto/rand"
7 "crypto/sha256"
8 "encoding/base64"
9 "encoding/hex"
10 "errors"
11 "time"
12
13 "amadan.net/oficina/equipo/internal/equipo/setup"
14 "amadan.net/oficina/equipo/internal/runnerproto"
15 "gorm.io/gorm"
16)
17
18var (
19 ErrEnrolmentPending = &refusal{copyRunnerPending, ErrConflict}
20 ErrRunnerRateLimited = &refusal{copyRunnerRateLimited, ErrForbidden}
21 // ErrRunnerNeedsPrimary: a Mac runs checks for a person's primary
22 // runner, so it cannot be their first.
23 ErrRunnerNeedsPrimary = &refusal{pageCopy["runner.needs_primary"], ErrConflict}
24)
25
26func runnerSecret() string {
27 b := make([]byte, 32)
28 rand.Read(b)
29 return base64.RawURLEncoding.EncodeToString(b)
30}
31func runnerHash(audience, secret string) string {
32 h := sha256.Sum256([]byte(audience + "\x00" + secret))
33 return hex.EncodeToString(h[:])
34}
35func runnerInstance(tx *gorm.DB) (string, error) {
36 var instance Team
37 if err := tx.First(&instance).Error; err != nil {
38 return "", storeError(err)
39 }
40 if instance.ID == "" {
41 return "", ErrForbidden
42 }
43 return instance.ID, nil
44}
45func currentRunnerMember(tx *gorm.DB, person string) (Member, error) {
46 m, err := memberAt(tx, person)
47 if errors.Is(err, ErrNotFound) {
48 return Member{}, ErrForbidden
49 }
50 if err != nil {
51 return Member{}, err
52 }
53 if !m.Active || (m.Role != RoleOwner && m.Role != RoleMember) {
54 return Member{}, ErrForbidden
55 }
56 return m, nil
57}
58
59// Denials are returned separately so a rejected guess still commits its rate count.
60func runnerRateAt(tx *gorm.DB, key string, now time.Time, period time.Duration, limit int) (bool, error) {
61 var rate runnerRate
62 err := tx.Where("key = ?", key).First(&rate).Error
63 if errors.Is(err, gorm.ErrRecordNotFound) {
64 return true, storeError(tx.Create(&runnerRate{Key: key, WindowStart: now, Count: 1}).Error)
65 }
66 if err != nil {
67 return false, storeError(err)
68 }
69 if now.Before(rate.WindowStart) {
70 return false, nil
71 }
72 if !now.Before(rate.WindowStart.Add(period)) {
73 rate.WindowStart = now
74 rate.Count = 0
75 }
76 if rate.Count >= limit {
77 return false, nil
78 }
79 rate.Count++
80 return true, storeError(tx.Model(&runnerRate{}).Where("key = ?", key).Select("WindowStart", "Count").Updates(&rate).Error)
81}
82
83func (s *Store) enrolments() *setup.Enrolments {
84 return &setup.Enrolments{Replay: func(tx *gorm.DB, ctx context.Context, out *Runner) (bool, error) {
85 replay, err := personapi.Replay(tx, ctx, out, ErrConflict)
86 if err != nil || !replay {
87 return replay, err
88 }
89 instance, err := runnerInstance(tx)
90 if err != nil {
91 return false, err
92 }
93 _, err = runnerAt(tx, instance, out.ID)
94 return true, err
95 }, SaveResult: func(tx *gorm.DB, ctx context.Context, out Runner) error {
96 return personapi.SaveResult(tx, ctx, out, ErrConflict)
97 }, Write: s.write, Now: s.runnerClock.Now, Instance: runnerInstance, Rate: runnerRateAt, Secret: runnerSecret, Hash: runnerHash, Member: currentRunnerMember, Audit: appendAudit, Error: storeError, Runner: runnerAt, Mint: mintRunnerCredential, Invalid: ErrInvalid, Forbidden: ErrForbidden, Conflict: ErrConflict, RunnerRateLimited: ErrRunnerRateLimited, EnrolmentPending: ErrEnrolmentPending, NeedsPrimary: ErrRunnerNeedsPrimary}
98}
99func (s *Store) BeginEnrolment(ctx context.Context, key, label string, workerOnly ...bool) (runnerproto.DeviceCode, error) {
100 return s.enrolments().BeginEnrolment(ctx, key, label, workerOnly...)
101}
102func (s *Store) ApproveEnrolment(ctx context.Context, a Actor, code string) error {
103 return s.enrolments().ApproveEnrolment(ctx, a, code)
104}
105func (s *Store) ExchangeEnrolment(ctx context.Context, secret string, p runnerproto.ExchangeProof) (runnerproto.RunnerCredential, error) {
106 return s.enrolments().ExchangeEnrolment(ctx, secret, p)
107}
108func (s *Store) approveOrInspect(ctx context.Context, a Actor, code string, approve bool) (Runner, error) {
109 return s.enrolments().ApproveOrInspect(ctx, a, code, approve)
110}
111