fix(postgres): correct package and secret audit coverage

This commit is contained in:
sam committed 2026-10-25 21:03:00 +11:00
1 parent f7caf2c40e
commit 36cd4b09b4
6 files changed
+111 -77

No files matched your search

+12 -2
View File
@@ -65,14 +65,24 @@ type Repository interface {
UpdateRecords
}
// Actions a swap of the live pin records in the audit trail.
const (
// ActionUpdate applies a catalogue update, or stages one as pending.
ActionUpdate = "update"
// ActionApproveUpdate applies the pending update and accepts its authority.
ActionApproveUpdate = "approve_update"
// ActionRollback restores the previous pin and its accepted authority.
ActionRollback = "rollback"
)
// UpdateRecords is the part of Repository the update lifecycle writes.
type UpdateRecords interface {
// SetPending records an update waiting for approval, leaving the live pin.
SetPending(ctx context.Context, orgID, name string, pending Pin, declared Authority) error
// Replace swaps the live pin, descriptor, layer and accepted authority for
// next's, keeping the replaced pin and its accepted authority as Previous
// and clearing Pending.
Replace(ctx context.Context, next Installed) error
// and clearing Pending. action names the operation in the audit trail.
Replace(ctx context.Context, next Installed, action string) error
// SetUpdatePolicy sets whether a package follows the catalogue on its own.
SetUpdatePolicy(ctx context.Context, orgID, name, policy string) error
// ListAutoUpdate names every installed package, in any org, whose policy
+11 -6
View File
@@ -4,6 +4,8 @@ import (
"context"
"errors"
"fmt"
"github.com/samcharles93/archie-core/internal/domain/access"
)
// Update policies. Manual is the default; an auto package follows the
@@ -30,8 +32,11 @@ func (s Service) SetPackageUpdatePolicy(ctx context.Context, orgID, name, policy
return s.Store.Get(ctx, orgID, name)
}
// UpdateAuto runs UpdatePackage for every auto package.
// UpdateAuto runs UpdatePackage for every auto package. No caller is behind
// it, so the audit trail attributes it to the process doing the work.
func (s Service) UpdateAuto(ctx context.Context) error {
ctx = access.WithActor(ctx, "archie-state-store")
ctx = access.WithSource(ctx, "package auto-update")
return updateAuto(ctx, s.Store, s)
}
@@ -102,7 +107,7 @@ func (s Service) UpdatePackage(ctx context.Context, orgID, name string) (Install
return s.Store.Get(ctx, orgID, name)
}
next.AcceptedAuthority = installed.AcceptedAuthority
return s.swap(ctx, installed, next, contents)
return s.swap(ctx, installed, next, contents, ActionUpdate)
}
return Installed{}, fmt.Errorf("catalogue package %q: %w", name, ErrNotFound)
}
@@ -123,7 +128,7 @@ func (s Service) ApprovePackageUpdate(ctx context.Context, orgID, name string) (
}
accepted := next.Descriptor.Authority
next.AcceptedAuthority = &accepted
return s.swap(ctx, installed, next, contents)
return s.swap(ctx, installed, next, contents, ActionApproveUpdate)
}
// RollbackPackage reinstalls the previous digest with the authority accepted
@@ -141,7 +146,7 @@ func (s Service) RollbackPackage(ctx context.Context, orgID, name string) (Insta
return Installed{}, err
}
next.AcceptedAuthority = installed.PreviousAccepted
return s.swap(ctx, installed, next, contents)
return s.swap(ctx, installed, next, contents, ActionRollback)
}
// prepare fetches and validates the package at pin and resolves its
@@ -167,13 +172,13 @@ func (s Service) prepare(ctx context.Context, orgID, name string, pin Pin) (Inst
// swap replaces installed with next: old contributions come out, new ones go
// in, then the record flips. A failure puts the old contributions back so
// the record, which still names the old pin, stays true.
func (s Service) swap(ctx context.Context, installed, next Installed, contents map[string]map[string][]byte) (Installed, error) {
func (s Service) swap(ctx context.Context, installed, next Installed, contents map[string]map[string][]byte, action string) (Installed, error) {
if err := s.withdraw(ctx, installed); err != nil {
return Installed{}, err
}
err := s.project(ctx, next, contents)
if err == nil {
err = s.Store.Replace(ctx, next)
err = s.Store.Replace(ctx, next, action)
}
if err != nil {
return Installed{}, errors.Join(err, s.withdraw(ctx, next), s.restore(ctx, installed))
@@ -71,24 +71,27 @@ func (s *EDA) PutHarnessSecret(ctx context.Context, secret harnesssecret.Secret)
}
defer func() { _ = tx.Rollback(ctx) }()
q := postgresdb.New(tx)
exists, err := q.HarnessSecretExists(ctx, postgresdb.HarnessSecretExistsParams{Org: secret.Org, Service: secret.Service})
if err != nil {
return fmt.Errorf("postgres: check harness secret: %w", err)
}
if err := q.PutHarnessSecret(ctx, postgresdb.PutHarnessSecretParams{
inserted, err := q.PutHarnessSecret(ctx, postgresdb.PutHarnessSecretParams{
Org: secret.Org, Service: secret.Service, SecretEnc: sealed,
}); err != nil {
})
if err != nil {
return fmt.Errorf("postgres: put harness secret: %w", err)
}
// The trail records that the credential changed, never any part of it: no
// access token, refresh token or ciphertext. A first capture has a nil old
// side, which the read model reports as a create.
// access token, refresh token or ciphertext. The insert flag is read by the
// same statement, so a concurrent first capture is an update, not a second
// create. A first capture has a nil old side.
var oldValue []byte
if exists {
oldValue = auditJSON(map[string]bool{"changed": true})
if !inserted {
if oldValue, err = auditJSON(map[string]bool{"changed": true}); err != nil {
return err
}
}
if err := insertAudit(ctx, q, "harness_secrets", secret.Service, "credential", oldValue,
auditJSON(map[string]bool{"changed": true}), 0, secret.Org); err != nil {
newValue, err := auditJSON(map[string]bool{"changed": true})
if err != nil {
return err
}
if err := insertAudit(ctx, q, "harness_secrets", secret.Service, "credential", oldValue, newValue, 0, secret.Org); err != nil {
return err
}
return tx.Commit(ctx)
@@ -59,21 +59,37 @@ func (s *InstalledPackages) Install(ctx context.Context, p storepkg.Installed) e
if err := insertInstalledRequirements(ctx, q, p); err != nil {
return err
}
if err := insertAudit(ctx, q, "installed_packages", p.Name, "install", nil,
auditJSON(packageAuditValue{Reference: p.Reference, Digest: p.Digest, UpdatePolicy: p.UpdatePolicy}), 0, p.OrgID); err != nil {
if err := auditPackage(ctx, q, p.OrgID, p.Name, "install", nil,
&packageAuditValue{Reference: p.Reference, Digest: p.Digest, UpdatePolicy: p.UpdatePolicy}); err != nil {
return err
}
return tx.Commit(ctx)
}
// packageAuditValue is the non-secret state an installed_packages audit row
// records: the pin and the policy that decides whether it follows the
// catalogue. Pending marks a pin waiting for approval.
// records: the pin, the policy that decides whether it follows the catalogue,
// and a pending update's declared authority. Pending marks a pin waiting for
// approval.
type packageAuditValue struct {
Reference string `json:"reference"`
Digest string `json:"digest"`
UpdatePolicy string `json:"update_policy,omitempty"`
Pending bool `json:"pending,omitempty"`
Reference string `json:"reference"`
Digest string `json:"digest"`
UpdatePolicy string `json:"update_policy,omitempty"`
Pending bool `json:"pending,omitempty"`
Authority json.RawMessage `json:"authority,omitempty"`
}
// auditPackage writes one installed_packages row. A nil side stays NULL, which
// the read model treats as a create or a delete.
func auditPackage(ctx context.Context, q *postgresdb.Queries, orgID, name, action string, oldValue, newValue *packageAuditValue) error {
oldJSON, err := auditJSON(oldValue)
if err != nil {
return err
}
newJSON, err := auditJSON(newValue)
if err != nil {
return err
}
return insertAudit(ctx, q, "installed_packages", name, action, oldJSON, newJSON, 0, orgID)
}
// getInstalledRow reads one package inside the caller's transaction, mapping
@@ -167,8 +183,8 @@ func (s *InstalledPackages) Remove(ctx context.Context, orgID, name string) erro
if rows == 0 {
return storepkg.ErrNotFound
}
if err := insertAudit(ctx, q, "installed_packages", name, "remove",
auditJSON(packageAuditValue{Reference: current.Reference, Digest: current.Digest, UpdatePolicy: current.UpdatePolicy}), nil, 0, orgID); err != nil {
if err := auditPackage(ctx, q, orgID, name, "remove",
&packageAuditValue{Reference: current.Reference, Digest: current.Digest, UpdatePolicy: current.UpdatePolicy}, nil); err != nil {
return err
}
return tx.Commit(ctx)
@@ -191,6 +207,9 @@ func (s *InstalledPackages) Accept(ctx context.Context, orgID, name string, auth
}
defer func() { _ = tx.Rollback(ctx) }()
q := postgresdb.New(tx)
if err := q.LockInstalledPackages(ctx, orgID); err != nil {
return err
}
current, err := getInstalledRow(ctx, q, orgID, name)
if err != nil {
return err
@@ -235,6 +254,9 @@ func (s *InstalledPackages) SetPending(ctx context.Context, orgID, name string,
}
defer func() { _ = tx.Rollback(ctx) }()
q := postgresdb.New(tx)
if err := q.LockInstalledPackages(ctx, orgID); err != nil {
return err
}
current, err := getInstalledRow(ctx, q, orgID, name)
if err != nil {
return err
@@ -248,9 +270,11 @@ func (s *InstalledPackages) SetPending(ctx context.Context, orgID, name string,
if rows == 0 {
return storepkg.ErrNotFound
}
if err := insertAudit(ctx, q, "installed_packages", name, "update",
auditJSON(packageAuditValue{Reference: current.Reference, Digest: current.Digest, UpdatePolicy: current.UpdatePolicy}),
auditJSON(packageAuditValue{Reference: pending.Reference, Digest: pending.Digest, UpdatePolicy: current.UpdatePolicy, Pending: true}), 0, orgID); err != nil {
// The declared authority is recorded here because this is the only place
// it is known before approval.
if err := auditPackage(ctx, q, orgID, name, "update",
&packageAuditValue{Reference: current.Reference, Digest: current.Digest, UpdatePolicy: current.UpdatePolicy},
&packageAuditValue{Reference: pending.Reference, Digest: pending.Digest, UpdatePolicy: current.UpdatePolicy, Pending: true, Authority: encoded}); err != nil {
return err
}
return tx.Commit(ctx)
@@ -258,8 +282,8 @@ func (s *InstalledPackages) SetPending(ctx context.Context, orgID, name string,
// Replace swaps the live pin under the org's package lock. Another package
// pinning this one's digest, or a requirement the new descriptor cannot
// meet, refuses the swap.
func (s *InstalledPackages) Replace(ctx context.Context, p storepkg.Installed) error {
// meet, refuses the swap. action names the operation in the audit trail.
func (s *InstalledPackages) Replace(ctx context.Context, p storepkg.Installed, action string) error {
if err := p.Descriptor.Validate(); err != nil {
return err
}
@@ -309,26 +333,26 @@ func (s *InstalledPackages) Replace(ctx context.Context, p storepkg.Installed) e
if err := replaceInstalledRequirements(ctx, q, p); err != nil {
return err
}
// Replace serves three actions; the row it replaced says which.
if err := insertAudit(ctx, q, "installed_packages", p.Name, replaceAction(current, p.Digest),
auditJSON(packageAuditValue{Reference: current.Reference, Digest: current.Digest, UpdatePolicy: current.UpdatePolicy, Pending: current.PendingDigest.Valid}),
auditJSON(packageAuditValue{Reference: p.Reference, Digest: p.Digest, UpdatePolicy: current.UpdatePolicy}), 0, p.OrgID); err != nil {
// Approvals and rollbacks change the accepted authority as well as the pin.
if err := auditReplace(ctx, q, p, current, accepted, action); err != nil {
return err
}
return tx.Commit(ctx)
}
// replaceAction names the operation behind a Replace. A pending pin matching
// the new one is the approval of that update, and the pin being restored from
// the previous one is a rollback; anything else is a plain update.
func replaceAction(current postgresdb.GetInstalledPackageRow, digest string) string {
switch {
case current.PendingDigest.Valid && current.PendingDigest.String == digest:
return "approve_update"
case current.PreviousDigest.Valid && current.PreviousDigest.String == digest:
return "rollback"
// auditReplace records the pin swap, and for the actions that also change the
// accepted authority, that change as its own row. Both rows share the
// transaction's timestamp, so the read model groups them as one change set.
func auditReplace(ctx context.Context, q *postgresdb.Queries, p storepkg.Installed, current postgresdb.GetInstalledPackageRow, accepted []byte, action string) error {
if err := auditPackage(ctx, q, p.OrgID, p.Name, action,
&packageAuditValue{Reference: current.Reference, Digest: current.Digest, UpdatePolicy: current.UpdatePolicy, Pending: current.PendingDigest.Valid},
&packageAuditValue{Reference: p.Reference, Digest: p.Digest, UpdatePolicy: current.UpdatePolicy}); err != nil {
return err
}
return "update"
if action != storepkg.ActionApproveUpdate && action != storepkg.ActionRollback {
return nil
}
return insertAudit(ctx, q, "installed_packages", p.Name, "accept_authority", current.AcceptedAuthority, accepted, 0, p.OrgID)
}
// replaceInstalledRequirements rebuilds p's requirement edges.
@@ -389,6 +413,9 @@ func (s *InstalledPackages) SetUpdatePolicy(ctx context.Context, orgID, name, po
}
defer func() { _ = tx.Rollback(ctx) }()
q := postgresdb.New(tx)
if err := q.LockInstalledPackages(ctx, orgID); err != nil {
return err
}
current, err := getInstalledRow(ctx, q, orgID, name)
if err != nil {
return err
@@ -402,9 +429,9 @@ func (s *InstalledPackages) SetUpdatePolicy(ctx context.Context, orgID, name, po
if rows == 0 {
return storepkg.ErrNotFound
}
if err := insertAudit(ctx, q, "installed_packages", name, "update_policy",
auditJSON(packageAuditValue{Reference: current.Reference, Digest: current.Digest, UpdatePolicy: current.UpdatePolicy}),
auditJSON(packageAuditValue{Reference: current.Reference, Digest: current.Digest, UpdatePolicy: policy}), 0, orgID); err != nil {
if err := auditPackage(ctx, q, orgID, name, "update_policy",
&packageAuditValue{Reference: current.Reference, Digest: current.Digest, UpdatePolicy: current.UpdatePolicy},
&packageAuditValue{Reference: current.Reference, Digest: current.Digest, UpdatePolicy: policy}); err != nil {
return err
}
return tx.Commit(ctx)
@@ -36,26 +36,11 @@ func (q *Queries) GetHarnessSecret(ctx context.Context, arg GetHarnessSecretPara
return i, err
}
const harnessSecretExists = `-- name: HarnessSecretExists :one
SELECT EXISTS (SELECT 1 FROM harness_secrets WHERE org = $1 AND service = $2)
`
type HarnessSecretExistsParams struct {
Org string
Service string
}
func (q *Queries) HarnessSecretExists(ctx context.Context, arg HarnessSecretExistsParams) (bool, error) {
row := q.db.QueryRow(ctx, harnessSecretExists, arg.Org, arg.Service)
var exists bool
err := row.Scan(&exists)
return exists, err
}
const putHarnessSecret = `-- name: PutHarnessSecret :exec
const putHarnessSecret = `-- name: PutHarnessSecret :one
INSERT INTO harness_secrets (org, service, secret_enc, updated_at)
VALUES ($1, $2, $3, now())
ON CONFLICT (org, service) DO UPDATE SET secret_enc = EXCLUDED.secret_enc, updated_at = now()
RETURNING (xmax = 0) AS inserted
`
type PutHarnessSecretParams struct {
@@ -64,7 +49,11 @@ type PutHarnessSecretParams struct {
SecretEnc string
}
func (q *Queries) PutHarnessSecret(ctx context.Context, arg PutHarnessSecretParams) error {
_, err := q.db.Exec(ctx, putHarnessSecret, arg.Org, arg.Service, arg.SecretEnc)
return err
// inserted is true only when this statement created the row: xmax is zero for
// a fresh insert and nonzero for the upsert's update side.
func (q *Queries) PutHarnessSecret(ctx context.Context, arg PutHarnessSecretParams) (bool, error) {
row := q.db.QueryRow(ctx, putHarnessSecret, arg.Org, arg.Service, arg.SecretEnc)
var inserted bool
err := row.Scan(&inserted)
return inserted, err
}
@@ -7,10 +7,10 @@
SELECT org, service, secret_enc, updated_at
FROM harness_secrets WHERE org = $1 AND service = $2;
-- name: HarnessSecretExists :one
SELECT EXISTS (SELECT 1 FROM harness_secrets WHERE org = $1 AND service = $2);
-- name: PutHarnessSecret :exec
-- inserted is true only when this statement created the row: xmax is zero for
-- a fresh insert and nonzero for the upsert's update side.
-- name: PutHarnessSecret :one
INSERT INTO harness_secrets (org, service, secret_enc, updated_at)
VALUES ($1, $2, $3, now())
ON CONFLICT (org, service) DO UPDATE SET secret_enc = EXCLUDED.secret_enc, updated_at = now();
ON CONFLICT (org, service) DO UPDATE SET secret_enc = EXCLUDED.secret_enc, updated_at = now()
RETURNING (xmax = 0) AS inserted;