package main // alta-adopt's configuration: one JSON file, mode 0600 and owned by the // user who runs the command. It holds Alta's operator material, so // nothing here ever names a value: errors name fields. import ( "bytes" "encoding/base64" "encoding/json" "errors" "io" "os" "path/filepath" "regexp" "syscall" "amadan.net/oficina/alta/adopt" "amadan.net/oficina/alta/homeop" "amadan.net/oficina/alta/provision" family "amadan.net/rastrillo/rastrillo/crypto" ) // fileConfig is the file as written. type fileConfig struct { AltaDEK string `json:"alta_dek"` OperatorKey string `json:"operator_key"` OperatorPrefilter string `json:"operator_prefilter"` Pins struct { Installation string `json:"installation"` Registry string `json:"registry"` Device string `json:"device"` } `json:"pins"` Provisioner string `json:"directory_provisioning_public_key"` HomeIssuer string `json:"home_issuer"` HomeDirectory string `json:"home_directory"` CarlosConsole string `json:"carlos_console"` CarlosAccount string `json:"carlos_account"` State string `json:"state"` Artifacts map[string]string `json:"artifacts"` } // config is the file, checked and decoded. type config struct { dek []byte key *family.Keypair prefilter string installation, registry, dev string provisioner string issuer, directory string console, account string state string artifacts map[string]string } // artifactHex is a reviewed artifact SHA-256, as the Observer takes it. var artifactHex = regexp.MustCompile(`^[0-9a-f]{64}$`) // configLimit bounds the file: it is a few hundred bytes. const configLimit = 64 << 10 // owned is a file's owner, as the platform reports it. type owned interface{ Owner() (uint32, bool) } type statOwner struct{ fi os.FileInfo } func (s statOwner) Owner() (uint32, bool) { st, ok := s.fi.Sys().(*syscall.Stat_t) if !ok { return 0, false } return st.Uid, true } // ownedByUser refuses a file another user owns, or whose owner the // platform does not say. func ownedByUser(fi owned) error { uid, ok := fi.Owner() if !ok || uid != uint32(os.Getuid()) { return errors.New(adopt.RefuseConfigOwner) } return nil } // loadConfig opens path and checks it before reading a byte of it: a // regular file, mode 0600, owned by the user. The checks are on the // opened file, so what is checked is what is read. func loadConfig(path string) (*config, error) { if path == "" { return nil, errors.New(adopt.RefuseConfigMissing) } f, err := os.Open(path) if err != nil { return nil, errors.New(adopt.RefuseConfigOpen) } defer f.Close() fi, err := f.Stat() if err != nil { return nil, errors.New(adopt.RefuseConfigOpen) } if !fi.Mode().IsRegular() { return nil, errors.New(adopt.RefuseConfigRegular) } if fi.Mode().Perm() != 0o600 { return nil, errors.New(adopt.RefuseConfigMode) } if err := ownedByUser(statOwner{fi}); err != nil { return nil, err } raw, err := io.ReadAll(io.LimitReader(f, configLimit+1)) if err != nil || len(raw) > configLimit { return nil, errors.New(adopt.RefuseConfigRead) } defer clear(raw) var fc fileConfig dec := json.NewDecoder(bytes.NewReader(raw)) dec.DisallowUnknownFields() if err := dec.Decode(&fc); err != nil { // The decoder's message can quote the file: it is not repeated. return nil, errors.New(adopt.RefuseConfigJSON) } c, err := fc.check() if err != nil { return nil, err } if !filepath.IsAbs(c.state) { c.state = filepath.Join(filepath.Dir(path), c.state) } return c, nil } func (fc *fileConfig) check() (*config, error) { bad := func(field string) error { return errors.New(adopt.RefuseConfigField(field)) } c := &config{prefilter: fc.OperatorPrefilter, installation: fc.Pins.Installation, registry: fc.Pins.Registry, dev: fc.Pins.Device, provisioner: fc.Provisioner, issuer: fc.HomeIssuer, directory: fc.HomeDirectory, console: fc.CarlosConsole, account: fc.CarlosAccount, state: fc.State, artifacts: map[string]string{}} dek, err := base64.StdEncoding.Strict().DecodeString(fc.AltaDEK) if err != nil || len(dek) != 32 { return nil, bad("alta_dek") } c.dek = dek raw, err := base64.StdEncoding.Strict().DecodeString(fc.OperatorKey) if err != nil { return nil, bad("operator_key") } if c.key, err = family.UnmarshalKeypair(raw); err != nil { clear(raw) return nil, bad("operator_key") } clear(raw) for field, v := range map[string]string{"operator_prefilter": c.prefilter, "pins.installation": c.installation, "pins.registry": c.registry, "pins.device": c.dev} { if !homeop.CanonicalToken(v) { return nil, bad(field) } } if b, err := base64.RawURLEncoding.Strict().DecodeString(c.provisioner); err != nil || len(b) != 65 { return nil, bad("directory_provisioning_public_key") } // The apps trust exactly this issuer; a run against any other Home // would adopt them into a team they do not answer to. if c.issuer != adopt.HomeIssuerOrigin { return nil, bad("home_issuer") } if !homeop.Origin(c.directory) { return nil, bad("home_directory") } if c.console == "" { return nil, bad("carlos_console") } if c.account == "" { return nil, bad("carlos_account") } if c.state == "" { return nil, bad("state") } for app, sum := range fc.Artifacts { if _, ok := provision.AppBySlug(app); !ok { return nil, bad("artifacts") } if !artifactHex.MatchString(sum) { return nil, bad("artifacts." + app) } c.artifacts[app] = sum } return c, nil }