oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package runnerproto
2
3import (
4 "amadan.net/oficina/equipo/internal/runner/provider"
5 "context"
6 "encoding/json"
7 "html"
8 "math/rand/v2"
9 "net/http"
10 "regexp"
11 "strings"
12 "time"
13)
14
15func ReconnectDelay(attempt int) time.Duration {
16 if attempt < 0 {
17 attempt = 0
18 }
19 if attempt > 5 {
20 attempt = 5
21 }
22 base := time.Second * time.Duration(1<<attempt)
23 if base > 30*time.Second {
24 base = 30 * time.Second
25 }
26 return base + time.Duration(rand.Int64N(int64(250*time.Millisecond)))
27}
28func WaitReconnect(ctx context.Context, attempt int, leaseDeadline time.Time) error {
29 remaining := time.Until(leaseDeadline)
30 if remaining <= 0 {
31 return provider.ErrUncertain
32 }
33 delay := ReconnectDelay(attempt)
34 if delay >= remaining {
35 return provider.ErrUncertain
36 }
37 timer := time.NewTimer(delay)
38 defer timer.Stop()
39 select {
40 case <-ctx.Done():
41 return ctx.Err()
42 case <-timer.C:
43 if !time.Now().Before(leaseDeadline) {
44 return provider.ErrUncertain
45 }
46 return nil
47 }
48}
49
50var secretPattern = regexp.MustCompile(`(?i)((?:bearer|basic)\s+)[A-Za-z0-9._~+/=-]+|((?:api[_-]?key|password|secret|token)\s*[=:]\s*)[^\s,"'<>]+|(?:sk-|ghp_|ghs_|ghu_|gho_|ghr_|github_pat_)[A-Za-z0-9_-]{8,}`)
51
52var privateKeyPattern = regexp.MustCompile(`(?s)-----BEGIN (?:[A-Z0-9]+ )?PRIVATE KEY-----.*?(?:-----END (?:[A-Z0-9]+ )?PRIVATE KEY-----|$)`)
53
54func RedactText(s string, secrets []string) string {
55 s = privateKeyPattern.ReplaceAllString(s, "***")
56 for _, secret := range secrets {
57 if secret != "" {
58 s = strings.ReplaceAll(s, secret, "***")
59 }
60 }
61 return secretPattern.ReplaceAllString(s, "***")
62}
63func RedactJSON(raw json.RawMessage, secrets []string) json.RawMessage {
64 var v any
65 d := json.NewDecoder(strings.NewReader(string(raw)))
66 d.UseNumber()
67 if d.Decode(&v) != nil {
68 return json.RawMessage(`null`)
69 }
70 var walk func(any) any
71 walk = func(v any) any {
72 switch x := v.(type) {
73 case map[string]any:
74 for k, item := range x {
75 key := strings.ToLower(strings.NewReplacer("_", "", "-", "").Replace(k))
76 if strings.HasSuffix(key, "token") || strings.HasSuffix(key, "password") || strings.HasSuffix(key, "secret") || strings.HasSuffix(key, "apikey") || strings.HasSuffix(key, "privatekey") {
77 x[k] = "***"
78 continue
79 }
80 switch key {
81 case "authorization", "password", "secret", "token", "accesstoken", "refreshtoken", "apikey", "credential", "credentials", "cleanuptoken", "bearer":
82 x[k] = "***"
83 default:
84 x[k] = walk(item)
85 }
86 }
87 case []any:
88 for i := range x {
89 x[i] = walk(x[i])
90 }
91 case string:
92 return RedactText(x, secrets)
93 }
94 return v
95 }
96 out, err := json.Marshal(walk(v))
97 if err != nil {
98 return json.RawMessage(`null`)
99 }
100 return out
101}
102func DownloadLog(w http.ResponseWriter, data []byte, secrets []string) {
103 w.Header().Set("Content-Type", "text/plain; charset=utf-8")
104 w.Header().Set("Content-Disposition", `attachment; filename="runner.log"`)
105 w.Header().Set("X-Content-Type-Options", "nosniff")
106 w.Header().Set("Cache-Control", "no-store")
107 _, _ = w.Write([]byte(html.EscapeString(RedactText(string(data), secrets))))
108}
109