Credentials are typed (machine, age), write-only through the API and sealed to the age identity in the bootstrap config. A field holds a value or names a secret engine key; connectors and engines are accepted with credential slots that resolve into their Configure secrets. The secret engine contract gains secrets so an engine can receive its own key.
258 lines
7.9 KiB
Go
258 lines
7.9 KiB
Go
package connectors
|
|
|
|
import (
|
|
"context"
|
|
json "encoding/json/v2"
|
|
"errors"
|
|
"fmt"
|
|
"maps"
|
|
"net/http"
|
|
"slices"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
"github.com/samcharles93/archie-core/internal/platform/httpjson"
|
|
"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"`
|
|
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
|
|
}
|
|
return nil, nil, false
|
|
}
|
|
|
|
type credentialRequest struct {
|
|
Kind string `json:"kind"`
|
|
Fields map[string]Field `json:"fields"`
|
|
}
|
|
|
|
// 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(w http.ResponseWriter, r *http.Request) {
|
|
name := r.PathValue("name")
|
|
var req credentialRequest
|
|
if err := httpjson.Read(r, &req); err != nil {
|
|
httpjson.Error(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
if !validName.MatchString(name) {
|
|
httpjson.Error(w, http.StatusBadRequest, "a credential needs a lowercase name")
|
|
return
|
|
}
|
|
if err := checkCredential(req); err != nil {
|
|
httpjson.Error(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
plain, err := json.Marshal(req.Fields)
|
|
if err != nil {
|
|
httpjson.Error(w, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
sealed, err := m.sealer.Seal(m.credentialLabel(name, req.Kind), plain)
|
|
if err != nil {
|
|
m.log.ErrorContext(r.Context(), "seal credential", "credential", name, "error", err)
|
|
httpjson.Error(w, http.StatusInternalServerError, "couldn't seal the credential")
|
|
return
|
|
}
|
|
if err := m.store.PutCredential(r.Context(), store.Credential{OrgID: m.orgID, Name: name, Kind: req.Kind, Sealed: sealed}); err != nil {
|
|
m.log.ErrorContext(r.Context(), "put credential", "credential", name, "error", err)
|
|
httpjson.Error(w, http.StatusInternalServerError, "couldn't save the credential")
|
|
return
|
|
}
|
|
restarted, err := m.relaunchUsers(context.WithoutCancel(r.Context()), name)
|
|
if err != nil {
|
|
httpjson.Error(w, http.StatusUnprocessableEntity, err.Error())
|
|
return
|
|
}
|
|
httpjson.Write(w, http.StatusOK, map[string]any{"name": name, "kind": req.Kind, "restarted": restarted})
|
|
}
|
|
|
|
func (m *Module) listCredentials(w http.ResponseWriter, r *http.Request) {
|
|
creds, err := m.store.Credentials(r.Context(), m.orgID)
|
|
if err != nil {
|
|
m.log.ErrorContext(r.Context(), "list credentials", "error", err)
|
|
httpjson.Error(w, http.StatusInternalServerError, "couldn't list credentials")
|
|
return
|
|
}
|
|
views := make([]credentialView, 0, len(creds))
|
|
for _, c := range creds {
|
|
fields, err := m.openCredential(c)
|
|
if err != nil {
|
|
m.log.ErrorContext(r.Context(), "open credential", "credential", c.Name, "error", err)
|
|
httpjson.Error(w, http.StatusInternalServerError, "couldn't open credential "+c.Name)
|
|
return
|
|
}
|
|
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
|
|
}
|
|
}
|
|
views = append(views, view)
|
|
}
|
|
httpjson.Write(w, http.StatusOK, views)
|
|
}
|
|
|
|
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...)
|
|
}
|