oficina / alta Public

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

Plain git — no account needed to clone.

Download

Download this file

1package main
2
3// alta-adopt's configuration: one JSON file, mode 0600 and owned by the
4// user who runs the command. It holds Alta's operator material, so
5// nothing here ever names a value: errors name fields.
6
7import (
8 "bytes"
9 "encoding/base64"
10 "encoding/json"
11 "errors"
12 "io"
13 "os"
14 "path/filepath"
15 "regexp"
16 "syscall"
17
18 "amadan.net/oficina/alta/adopt"
19 "amadan.net/oficina/alta/homeop"
20 "amadan.net/oficina/alta/provision"
21 family "amadan.net/rastrillo/rastrillo/crypto"
22)
23
24// fileConfig is the file as written.
25type fileConfig struct {
26 AltaDEK string `json:"alta_dek"`
27 OperatorKey string `json:"operator_key"`
28 OperatorPrefilter string `json:"operator_prefilter"`
29 Pins struct {
30 Installation string `json:"installation"`
31 Registry string `json:"registry"`
32 Device string `json:"device"`
33 } `json:"pins"`
34 Provisioner string `json:"directory_provisioning_public_key"`
35 HomeIssuer string `json:"home_issuer"`
36 HomeDirectory string `json:"home_directory"`
37 CarlosConsole string `json:"carlos_console"`
38 CarlosAccount string `json:"carlos_account"`
39 State string `json:"state"`
40 Artifacts map[string]string `json:"artifacts"`
41}
42
43// config is the file, checked and decoded.
44type config struct {
45 dek []byte
46 key *family.Keypair
47 prefilter string
48 installation, registry, dev string
49 provisioner string
50 issuer, directory string
51 console, account string
52 state string
53 artifacts map[string]string
54}
55
56// artifactHex is a reviewed artifact SHA-256, as the Observer takes it.
57var artifactHex = regexp.MustCompile(`^[0-9a-f]{64}$`)
58
59// configLimit bounds the file: it is a few hundred bytes.
60const configLimit = 64 << 10
61
62// owned is a file's owner, as the platform reports it.
63type owned interface{ Owner() (uint32, bool) }
64
65type statOwner struct{ fi os.FileInfo }
66
67func (s statOwner) Owner() (uint32, bool) {
68 st, ok := s.fi.Sys().(*syscall.Stat_t)
69 if !ok {
70 return 0, false
71 }
72 return st.Uid, true
73}
74
75// ownedByUser refuses a file another user owns, or whose owner the
76// platform does not say.
77func ownedByUser(fi owned) error {
78 uid, ok := fi.Owner()
79 if !ok || uid != uint32(os.Getuid()) {
80 return errors.New(adopt.RefuseConfigOwner)
81 }
82 return nil
83}
84
85// loadConfig opens path and checks it before reading a byte of it: a
86// regular file, mode 0600, owned by the user. The checks are on the
87// opened file, so what is checked is what is read.
88func loadConfig(path string) (*config, error) {
89 if path == "" {
90 return nil, errors.New(adopt.RefuseConfigMissing)
91 }
92 f, err := os.Open(path)
93 if err != nil {
94 return nil, errors.New(adopt.RefuseConfigOpen)
95 }
96 defer f.Close()
97 fi, err := f.Stat()
98 if err != nil {
99 return nil, errors.New(adopt.RefuseConfigOpen)
100 }
101 if !fi.Mode().IsRegular() {
102 return nil, errors.New(adopt.RefuseConfigRegular)
103 }
104 if fi.Mode().Perm() != 0o600 {
105 return nil, errors.New(adopt.RefuseConfigMode)
106 }
107 if err := ownedByUser(statOwner{fi}); err != nil {
108 return nil, err
109 }
110 raw, err := io.ReadAll(io.LimitReader(f, configLimit+1))
111 if err != nil || len(raw) > configLimit {
112 return nil, errors.New(adopt.RefuseConfigRead)
113 }
114 defer clear(raw)
115 var fc fileConfig
116 dec := json.NewDecoder(bytes.NewReader(raw))
117 dec.DisallowUnknownFields()
118 if err := dec.Decode(&fc); err != nil {
119 // The decoder's message can quote the file: it is not repeated.
120 return nil, errors.New(adopt.RefuseConfigJSON)
121 }
122 c, err := fc.check()
123 if err != nil {
124 return nil, err
125 }
126 if !filepath.IsAbs(c.state) {
127 c.state = filepath.Join(filepath.Dir(path), c.state)
128 }
129 return c, nil
130}
131
132func (fc *fileConfig) check() (*config, error) {
133 bad := func(field string) error { return errors.New(adopt.RefuseConfigField(field)) }
134 c := &config{prefilter: fc.OperatorPrefilter, installation: fc.Pins.Installation, registry: fc.Pins.Registry, dev: fc.Pins.Device,
135 provisioner: fc.Provisioner, issuer: fc.HomeIssuer, directory: fc.HomeDirectory, console: fc.CarlosConsole,
136 account: fc.CarlosAccount, state: fc.State, artifacts: map[string]string{}}
137 dek, err := base64.StdEncoding.Strict().DecodeString(fc.AltaDEK)
138 if err != nil || len(dek) != 32 {
139 return nil, bad("alta_dek")
140 }
141 c.dek = dek
142 raw, err := base64.StdEncoding.Strict().DecodeString(fc.OperatorKey)
143 if err != nil {
144 return nil, bad("operator_key")
145 }
146 if c.key, err = family.UnmarshalKeypair(raw); err != nil {
147 clear(raw)
148 return nil, bad("operator_key")
149 }
150 clear(raw)
151 for field, v := range map[string]string{"operator_prefilter": c.prefilter, "pins.installation": c.installation, "pins.registry": c.registry, "pins.device": c.dev} {
152 if !homeop.CanonicalToken(v) {
153 return nil, bad(field)
154 }
155 }
156 if b, err := base64.RawURLEncoding.Strict().DecodeString(c.provisioner); err != nil || len(b) != 65 {
157 return nil, bad("directory_provisioning_public_key")
158 }
159 // The apps trust exactly this issuer; a run against any other Home
160 // would adopt them into a team they do not answer to.
161 if c.issuer != adopt.HomeIssuerOrigin {
162 return nil, bad("home_issuer")
163 }
164 if !homeop.Origin(c.directory) {
165 return nil, bad("home_directory")
166 }
167 if c.console == "" {
168 return nil, bad("carlos_console")
169 }
170 if c.account == "" {
171 return nil, bad("carlos_account")
172 }
173 if c.state == "" {
174 return nil, bad("state")
175 }
176 for app, sum := range fc.Artifacts {
177 if _, ok := provision.AppBySlug(app); !ok {
178 return nil, bad("artifacts")
179 }
180 if !artifactHex.MatchString(sum) {
181 return nil, bad("artifacts." + app)
182 }
183 c.artifacts[app] = sum
184 }
185 return c, nil
186}
187