package connectors import ( "context" json "encoding/json/v2" "errors" "fmt" "maps" "net/http" "slices" "github.com/danielgtaylor/huma/v2" "golang.org/x/crypto/ssh" "github.com/samcharles93/archie-core/internal/platform/httpapi" "github.com/samcharles93/archie-core/modules/connectors/internal/store" ) // Field is one field of a credential: a value Archie keeps sealed, or the key // a secret engine resolves it from when the extension using it starts. type Field struct { Value string `json:"value,omitzero" writeOnly:"true"` Engine string `json:"engine,omitzero"` Key string `json:"key,omitzero"` } // kindFields lists a credential kind's required and optional fields. func kindFields(kind string) (required, optional []string, ok bool) { switch kind { case "machine": return []string{"username", "private_key"}, []string{"private_key_passphrase"}, true case "age": return []string{"identity"}, nil, true case "basic": return []string{"username", "password"}, nil, true case "bearer": return []string{"token"}, nil, true } return nil, nil, false } // credentialKinds is every kind kindFields accepts, in the order a form // offers them. var credentialKinds = []string{"bearer", "basic", "machine", "age"} type credentialKind struct { Kind string `json:"kind"` Required []string `json:"required"` Optional []string `json:"optional,omitzero"` } // listCredentialKinds serves kindFields, so a form asks for exactly the fields // a put accepts. func listCredentialKinds(context.Context, *struct{}) (*httpapi.Reply[[]credentialKind], error) { kinds := make([]credentialKind, 0, len(credentialKinds)) for _, kind := range credentialKinds { required, optional, _ := kindFields(kind) kinds = append(kinds, credentialKind{Kind: kind, Required: required, Optional: optional}) } return httpapi.OK(kinds), nil } type credentialRequest struct { Kind string `json:"kind"` Fields map[string]Field `json:"fields"` } type credentialInput struct { Name string `path:"name" doc:"Lowercase letters, digits and hyphens"` Body credentialRequest } // credentialSaved names a stored credential and the extensions restarted to // pick it up. type credentialSaved struct { Name string `json:"name"` Kind string `json:"kind"` Restarted []string `json:"restarted"` } // credentialView shows where each field comes from, never a value. type credentialView struct { Name string `json:"name"` Kind string `json:"kind"` Fields map[string]string `json:"fields"` } // putCredential stores a credential sealed, then restarts the extensions that // use it so they are configured with the new values. func (m *Module) putCredential(ctx context.Context, in *credentialInput) (*httpapi.Reply[credentialSaved], error) { name, req := in.Name, in.Body if !validName.MatchString(name) { return nil, huma.NewError(http.StatusBadRequest, "a credential needs a lowercase name") } if err := checkCredential(req); err != nil { return nil, huma.NewError(http.StatusBadRequest, err.Error()) } plain, err := json.Marshal(req.Fields) if err != nil { return nil, huma.NewError(http.StatusBadRequest, err.Error()) } sealed, err := m.sealer.Seal(m.credentialLabel(name, req.Kind), plain) if err != nil { m.log.ErrorContext(ctx, "seal credential", "credential", name, "error", err) return nil, huma.NewError(http.StatusInternalServerError, "couldn't seal the credential") } if err := m.store.PutCredential(ctx, store.Credential{OrgID: m.orgID, Name: name, Kind: req.Kind, Sealed: sealed}); err != nil { m.log.ErrorContext(ctx, "put credential", "credential", name, "error", err) return nil, huma.NewError(http.StatusInternalServerError, "couldn't save the credential") } restarted, err := m.relaunchUsers(context.WithoutCancel(ctx), name) if err != nil { return nil, huma.NewError(http.StatusUnprocessableEntity, err.Error()) } return httpapi.OK(credentialSaved{Name: name, Kind: req.Kind, Restarted: restarted}), nil } func (m *Module) listCredentials(ctx context.Context, _ *struct{}) (*httpapi.Reply[[]credentialView], error) { creds, err := m.store.Credentials(ctx, m.orgID) if err != nil { m.log.ErrorContext(ctx, "list credentials", "error", err) return nil, huma.NewError(http.StatusInternalServerError, "couldn't list credentials") } views := make([]credentialView, 0, len(creds)) for _, c := range creds { view, err := m.credentialViewOf(c) if err != nil { m.log.ErrorContext(ctx, "open credential", "credential", c.Name, "error", err) return nil, huma.NewError(http.StatusInternalServerError, "couldn't open credential "+c.Name) } views = append(views, view) } return httpapi.OK(views), nil } func (m *Module) getCredential(ctx context.Context, in *nameInput) (*httpapi.Reply[credentialView], error) { name := in.Name c, ok, err := m.store.Credential(ctx, m.orgID, name) if err != nil { m.log.ErrorContext(ctx, "read credential", "credential", name, "error", err) return nil, huma.NewError(http.StatusInternalServerError, "couldn't read the credential") } if !ok { return nil, huma.NewError(http.StatusNotFound, "no such credential") } view, err := m.credentialViewOf(c) if err != nil { m.log.ErrorContext(ctx, "open credential", "credential", name, "error", err) return nil, huma.NewError(http.StatusInternalServerError, "couldn't open the credential") } return httpapi.OK(view), nil } // credentialViewOf says where each field comes from: stored, or the secret // engine and key that resolves it. The value is never returned. func (m *Module) credentialViewOf(c store.Credential) (credentialView, error) { fields, err := m.openCredential(c) if err != nil { return credentialView{}, err } view := credentialView{Name: c.Name, Kind: c.Kind, Fields: map[string]string{}} for name, f := range fields { view.Fields[name] = "stored" if f.Engine != "" { view.Fields[name] = f.Engine + ":" + f.Key } } return view, nil } // BearerToken returns the token a bearer credential holds, resolved through an // engine when it names one. It is how a setting references a credential by // name without the dashboard ever reading the value. func (m *Module) BearerToken(ctx context.Context, name string) (string, error) { c, ok, err := m.store.Credential(ctx, m.orgID, name) if err != nil { return "", err } if !ok { return "", fmt.Errorf("credential %q doesn't exist", name) } fields, err := m.openCredential(c) if err != nil { return "", err } f, ok := fields["token"] if !ok { return "", fmt.Errorf("credential %q has no token field", name) } return m.resolveField(ctx, store.Accepted{}, f) } func checkCredential(req credentialRequest) error { required, optional, ok := kindFields(req.Kind) if !ok { return fmt.Errorf("unknown credential kind %q", req.Kind) } for _, name := range required { if _, ok := req.Fields[name]; !ok { return fmt.Errorf("a %s credential needs %s", req.Kind, name) } } for _, name := range slices.Sorted(maps.Keys(req.Fields)) { if !slices.Contains(required, name) && !slices.Contains(optional, name) { return fmt.Errorf("a %s credential has no field %s", req.Kind, name) } f := req.Fields[name] if (f.Value == "") == (f.Engine == "" || f.Key == "") { return fmt.Errorf("field %s needs either a value or an engine and a key", name) } } return checkPrivateKey(req.Fields["private_key"], req.Fields["private_key_passphrase"]) } // checkPrivateKey catches a key that won't parse when it is stored, rather // than when a connector first needs it. A key resolved by an engine is checked // when it is resolved. func checkPrivateKey(key, passphrase Field) error { if key.Value == "" { return nil } var err error switch { case passphrase.Value != "": _, err = ssh.ParseRawPrivateKeyWithPassphrase([]byte(key.Value), []byte(passphrase.Value)) case passphrase.Engine != "": return nil default: _, err = ssh.ParseRawPrivateKey([]byte(key.Value)) } if err != nil { return fmt.Errorf("private_key: %w", err) } return nil } func (m *Module) credentialLabel(name, kind string) string { return m.orgID + "/" + name + "/" + kind } func (m *Module) openCredential(c store.Credential) (map[string]Field, error) { plain, err := m.sealer.Open(m.credentialLabel(c.Name, c.Kind), c.Sealed) if err != nil { return nil, err } var fields map[string]Field if err := json.Unmarshal(plain, &fields); err != nil { return nil, fmt.Errorf("credential %q: %w", c.Name, err) } return fields, nil } // secrets resolves an extension's credential slots into the secrets it is // configured with, keyed "slot.field". A secret engine's own credentials must // hold their values: an engine never waits on another engine. func (m *Module) secrets(ctx context.Context, a store.Accepted) (map[string]string, error) { slots, err := decodeSlots(a.Credentials) if err != nil { return nil, err } out := map[string]string{} for slot, credName := range slots { c, ok, err := m.store.Credential(ctx, m.orgID, credName) if err != nil { return nil, err } if !ok { return nil, fmt.Errorf("credential %q for slot %q doesn't exist", credName, slot) } fields, err := m.openCredential(c) if err != nil { return nil, err } for field, f := range fields { v, err := m.resolveField(ctx, a, f) if err != nil { return nil, fmt.Errorf("credential %q field %s: %w", credName, field, err) } out[slot+"."+field] = v } } return out, nil } func (m *Module) resolveField(ctx context.Context, a store.Accepted, f Field) (string, error) { if f.Engine == "" { return f.Value, nil } if a.Contract == store.ContractSecretEngine { return "", errors.New("a secret engine's own credentials must hold their values") } m.mu.RLock() engine, ok := m.engines[f.Engine] m.mu.RUnlock() if !ok { return "", fmt.Errorf("secret engine %q isn't running", f.Engine) } return engine.Resolve(ctx, f.Key) } func decodeSlots(raw []byte) (map[string]string, error) { slots := map[string]string{} if len(raw) == 0 { return slots, nil } if err := json.Unmarshal(raw, &slots); err != nil { return nil, fmt.Errorf("credential slots: %w", err) } return slots, nil } // relaunchUsers restarts every accepted extension with a slot naming the // credential, and returns their names. func (m *Module) relaunchUsers(ctx context.Context, credential string) ([]string, error) { accepted, err := m.store.List(ctx, m.orgID) if err != nil { return nil, err } var restarted []string var errs []error for _, a := range ordered(accepted) { slots, err := decodeSlots(a.Credentials) if err != nil || !slices.Contains(slices.Collect(maps.Values(slots)), credential) { continue } if err := m.launch(ctx, a); err != nil { errs = append(errs, err) continue } restarted = append(restarted, a.Name) } return restarted, errors.Join(errs...) }