oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package thin
2
3import (
4 "context"
5 "encoding/json"
6 "io"
7 "net/http"
8 "slices"
9 "strings"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/runner/workspace"
13 "amadan.net/oficina/equipo/internal/wire"
14)
15
16// Named read grants supplement the writable /repos list. Verify them with
17// the hub on each refresh; an old approval alone is never current access.
18func (f *fileForge) readGrants(ctx context.Context) []string {
19 if f.accessTargets == nil {
20 return nil
21 }
22 f.mu.Lock()
23 defer f.mu.Unlock()
24 if time.Since(f.accessAt) < reachEvery {
25 return slices.Clone(f.accessRepos)
26 }
27 targets, err := f.accessTargets(ctx)
28 if err != nil {
29 return nil
30 }
31 credential, err := f.agent(ctx)
32 if err != nil {
33 return nil
34 }
35 ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
36 defer cancel()
37 out := []string{}
38 client := *f.client
39 client.CheckRedirect = func(*http.Request, []*http.Request) error { return http.ErrUseLastResponse }
40 for _, target := range targets {
41 if ctx.Err() != nil {
42 break
43 }
44 if !wire.ValidAccessTarget(target) || !strings.Contains(target, "/") {
45 continue
46 }
47 req, err := http.NewRequestWithContext(ctx, http.MethodGet, f.hub+"/api/v1/repos/"+target, nil)
48 if err != nil {
49 continue
50 }
51 req.Header.Set("Authorization", "Bearer "+credential.Bearer)
52 res, err := client.Do(req)
53 if err != nil {
54 continue
55 }
56 var info struct {
57 NS string `json:"ns"`
58 Name string `json:"name"`
59 Tier string `json:"tier"`
60 }
61 valid := res.StatusCode == http.StatusOK && json.NewDecoder(io.LimitReader(res.Body, 16<<10)).Decode(&info) == nil && info.NS+"/"+info.Name == target && info.Tier != "private_e2ee"
62 res.Body.Close()
63 if valid {
64 out = append(out, nsPrefix+target)
65 }
66 }
67 f.accessRepos, f.accessAt = out, time.Now()
68 return slices.Clone(out)
69}
70
71func (s *State) accessReadTargets(ctx context.Context) ([]string, error) {
72 rows, err := s.db.QueryContext(ctx, "SELECT command FROM access_requests WHERE state IN ('approved','already') AND json_extract(command, '$.role') <> 'agent' ORDER BY rowid DESC LIMIT ?", wire.MaxCatalog)
73 if err != nil {
74 return nil, err
75 }
76 defer rows.Close()
77 out := []string{}
78 for rows.Next() {
79 var raw string
80 if err := rows.Scan(&raw); err != nil {
81 return nil, err
82 }
83 var c wire.AccessCommand
84 if json.Unmarshal([]byte(raw), &c) == nil && c.Role != "agent" && strings.Contains(c.Target, "/") && !slices.Contains(out, c.Target) {
85 out = append(out, c.Target)
86 }
87 }
88 return out, rows.Err()
89}
90
91func readRemote(ctx context.Context, f Forge, repo string) (workspace.Remote, error) {
92 return f.Remote(ctx, repo, Read)
93}
94