Repository navigation
Expand file tree
/
Copy pathsecrets.go
More file actions
125 lines (115 loc) · 3.7 KB
/
Copy pathsecrets.go
File metadata and controls
125 lines (115 loc) · 3.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
package sitec
import (
"errors"
"os"
"strings"
"webtyp.com/deploy"
"webtyp.com/fmt"
"webtyp.com/keyring"
keyringauto "webtyp.com/keyring/auto"
)
const (
msgSecretInScript = "sitec: the browser script"
msgContainsSecret = "contains the value of the secret"
msgSecretInScriptFix = "declared in config/deploy.go; a secret is read at run time by the server, never written in code the browser downloads (skill secrets)"
msgSecretTooShort = "sitec: not searched in the browser script, value shorter than"
msgNoKeyring = "sitec: no keyring on this machine; the browser script is searched only for secrets set as environment variables:"
)
// minSecretLen is the shortest value searched: a shorter one matches ordinary
// text of the script and would stop a release that leaks nothing.
const minSecretLen = 8
// secretSource returns the value this machine holds for a declared secret:
// "" when it holds none. err is only for a store that cannot be read.
type secretSource interface {
value(key string) (string, error)
}
// machineSecrets reads a secret where this machine may hold it: an environment
// variable named as the Key (a CI job receives its ToCI secrets that way), else
// the project's keyring (the development values of each person).
type machineSecrets struct {
keyring *keyring.Keyring // nil when the project has no go.mod
log func(...any)
noStore bool // the keyring was unavailable once: logged, not asked again
}
func (m *machineSecrets) value(key string) (string, error) {
if v := os.Getenv(key); v != "" {
return v, nil
}
if m.keyring == nil || m.noStore {
return "", nil
}
v, err := m.keyring.Get(key)
switch {
case err == nil:
return v, nil
case errors.Is(err, keyring.ErrNotFound):
return "", nil
case errors.Is(err, keyring.ErrUnavailable), errors.Is(err, keyring.ErrUnsupported):
m.noStore = true
m.log(msgNoKeyring, err)
return "", nil
}
return "", err
}
// checkSecretsInScripts fails when a JavaScript artifact of the release holds
// the value of a secret the project declares in config/deploy.go, naming the
// Key and the file, never the value. A project without config/deploy.go
// declares no secret and has nothing to check.
func checkSecretsInScripts(rootDir string, arts []Artifact, log func(...any)) error {
plan, err := deploy.Scan(rootDir)
if errors.Is(err, deploy.ErrNoPlan) {
return nil
}
if err != nil {
return err
}
if len(plan.Secrets) == 0 {
return nil
}
src := &machineSecrets{log: log}
if kr, err := keyringauto.OpenForModule(rootDir); err == nil {
src.keyring = kr
}
return findSecrets(plan.Secrets, src, arts, log)
}
func findSecrets(secrets []deploy.Secret, src secretSource, arts []Artifact, log func(...any)) error {
for _, s := range secrets {
v, err := src.value(s.Key)
if err != nil {
return err
}
if v == "" {
continue
}
pieces := searchPieces(v)
if len(pieces) == 0 {
log(msgSecretTooShort, minSecretLen, s.Key)
continue
}
for _, a := range arts {
if a.Mediatype != "text/javascript" {
continue
}
for _, p := range pieces {
if strings.Contains(string(a.Content), p) {
return fmt.Err(msgSecretInScript, a.Path, msgContainsSecret, s.Key, msgSecretInScriptFix)
}
}
}
}
return nil
}
// searchPieces is what the script would contain if the value were written in
// Go code: each line of it (a PEM key's newlines become \n escapes in a JS
// string), skipping the "-----BEGIN …-----" armor every key of a kind shares.
func searchPieces(v string) []string {
var out []string
for _, line := range strings.Split(v, "\n") {
line = strings.TrimSpace(line)
if len(line) < minSecretLen || strings.HasPrefix(line, "-----") {
continue
}
out = append(out, line)
}
return out
}