feat(state): audit package and harness secret writes
This commit is contained in:
6 files changed
+213
-27
No files matched your search
@@ -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
|
||||
|
||||
@@ -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))
|
||||
|
||||
@@ -65,12 +65,36 @@ func (s *EDA) PutHarnessSecret(ctx context.Context, secret harnesssecret.Secret)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := s.q.PutHarnessSecret(ctx, postgresdb.PutHarnessSecretParams{
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = tx.Rollback(ctx) }()
|
||||
q := postgresdb.New(tx)
|
||||
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)
|
||||
}
|
||||
return nil
|
||||
// The trail records that the credential changed, never any part of it: no
|
||||
// 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 !inserted {
|
||||
if oldValue, err = auditJSON(map[string]bool{"changed": true}); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
// sealHarnessSecret encrypts the token set under HarnessSecretDomain. An
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package postgres
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -59,9 +60,52 @@ func (s *InstalledPackages) Install(ctx context.Context, p storepkg.Installed) e
|
||||
if err := insertInstalledRequirements(ctx, q, p); err != nil {
|
||||
return err
|
||||
}
|
||||
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, 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"`
|
||||
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
|
||||
// an absent row to storepkg.ErrNotFound.
|
||||
func getInstalledRow(ctx context.Context, q *postgresdb.Queries, orgID, name string) (postgresdb.GetInstalledPackageRow, error) {
|
||||
row, err := q.GetInstalledPackage(ctx, postgresdb.GetInstalledPackageParams{OrgID: orgID, Name: name})
|
||||
if errors.Is(err, pgx.ErrNoRows) {
|
||||
return postgresdb.GetInstalledPackageRow{}, storepkg.ErrNotFound
|
||||
}
|
||||
if err != nil {
|
||||
return postgresdb.GetInstalledPackageRow{}, err
|
||||
}
|
||||
return row, nil
|
||||
}
|
||||
|
||||
func checkInstalledRequirements(ctx context.Context, q *postgresdb.Queries, p storepkg.Installed) error {
|
||||
for _, required := range p.Descriptor.Requires {
|
||||
existing, err := q.GetInstalledPackage(ctx, postgresdb.GetInstalledPackageParams{OrgID: p.OrgID, Name: required.Name})
|
||||
@@ -126,6 +170,10 @@ func (s *InstalledPackages) Remove(ctx context.Context, orgID, name string) erro
|
||||
if err := q.LockInstalledPackages(ctx, orgID); err != nil {
|
||||
return err
|
||||
}
|
||||
current, err := getInstalledRow(ctx, q, orgID, name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows, err := q.DeleteInstalledPackage(ctx, postgresdb.DeleteInstalledPackageParams{OrgID: orgID, Name: name})
|
||||
if pgCode(err) == "23001" {
|
||||
return storepkg.ErrRequired
|
||||
@@ -136,6 +184,10 @@ func (s *InstalledPackages) Remove(ctx context.Context, orgID, name string) erro
|
||||
if rows == 0 {
|
||||
return storepkg.ErrNotFound
|
||||
}
|
||||
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)
|
||||
}
|
||||
|
||||
@@ -150,7 +202,20 @@ func (s *InstalledPackages) Accept(ctx context.Context, orgID, name string, auth
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows, err := postgresdb.New(s.pool).AcceptInstalledPackageAuthority(ctx, postgresdb.AcceptInstalledPackageAuthorityParams{
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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
|
||||
}
|
||||
rows, err := q.AcceptInstalledPackageAuthority(ctx, postgresdb.AcceptInstalledPackageAuthorityParams{
|
||||
OrgID: orgID, Name: name, AcceptedAuthority: encoded,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -159,7 +224,11 @@ func (s *InstalledPackages) Accept(ctx context.Context, orgID, name string, auth
|
||||
if rows == 0 {
|
||||
return storepkg.ErrNotFound
|
||||
}
|
||||
return nil
|
||||
if err := insertAudit(ctx, q, "installed_packages", name, "accept_authority",
|
||||
current.AcceptedAuthority, encoded, 0, orgID); err != nil {
|
||||
return err
|
||||
}
|
||||
return tx.Commit(ctx)
|
||||
}
|
||||
|
||||
// updateColumns are the nullable pending and previous pin columns of a row.
|
||||
@@ -180,7 +249,20 @@ func (s *InstalledPackages) SetPending(ctx context.Context, orgID, name string,
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows, err := postgresdb.New(s.pool).SetInstalledPackagePending(ctx, postgresdb.SetInstalledPackagePendingParams{
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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
|
||||
}
|
||||
rows, err := q.SetInstalledPackagePending(ctx, postgresdb.SetInstalledPackagePendingParams{
|
||||
OrgID: orgID, Name: name, PendingReference: pgtype.Text{String: pending.Reference, Valid: true}, PendingDigest: pgtype.Text{String: pending.Digest, Valid: true}, PendingAuthority: encoded,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -189,13 +271,20 @@ func (s *InstalledPackages) SetPending(ctx context.Context, orgID, name string,
|
||||
if rows == 0 {
|
||||
return storepkg.ErrNotFound
|
||||
}
|
||||
return 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)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
@@ -218,6 +307,10 @@ func (s *InstalledPackages) Replace(ctx context.Context, p storepkg.Installed) e
|
||||
if err := q.LockInstalledPackages(ctx, p.OrgID); err != nil {
|
||||
return err
|
||||
}
|
||||
current, err := getInstalledRow(ctx, q, p.OrgID, p.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
dependents, err := q.CountInstalledDependents(ctx, postgresdb.CountInstalledDependentsParams{OrgID: p.OrgID, RequiredName: p.Name})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -238,15 +331,43 @@ func (s *InstalledPackages) Replace(ctx context.Context, p storepkg.Installed) e
|
||||
if rows == 0 {
|
||||
return storepkg.ErrNotFound
|
||||
}
|
||||
if err := q.DeleteInstalledRequirements(ctx, postgresdb.DeleteInstalledRequirementsParams{OrgID: p.OrgID, PackageName: p.Name}); err != nil {
|
||||
if err := replaceInstalledRequirements(ctx, q, p); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := insertInstalledRequirements(ctx, q, p); 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)
|
||||
}
|
||||
|
||||
// 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
|
||||
}
|
||||
if action != storepkg.ActionApproveUpdate && action != storepkg.ActionRollback {
|
||||
return nil
|
||||
}
|
||||
// An unchanged authority, including none at all, is not an approval.
|
||||
if bytes.Equal(current.AcceptedAuthority, accepted) {
|
||||
return nil
|
||||
}
|
||||
return insertAudit(ctx, q, "installed_packages", p.Name, "accept_authority", current.AcceptedAuthority, accepted, 0, p.OrgID)
|
||||
}
|
||||
|
||||
// replaceInstalledRequirements rebuilds p's requirement edges.
|
||||
func replaceInstalledRequirements(ctx context.Context, q *postgresdb.Queries, p storepkg.Installed) error {
|
||||
if err := q.DeleteInstalledRequirements(ctx, postgresdb.DeleteInstalledRequirementsParams{OrgID: p.OrgID, PackageName: p.Name}); err != nil {
|
||||
return err
|
||||
}
|
||||
return insertInstalledRequirements(ctx, q, p)
|
||||
}
|
||||
|
||||
func installedFromRow(orgID, name, reference, digest string, raw, layer []byte, updatePolicy string, accepted []byte, update updateColumns) (storepkg.Installed, error) {
|
||||
var descriptor storepkg.Descriptor
|
||||
if err := json.Unmarshal(raw, &descriptor); err != nil {
|
||||
@@ -291,7 +412,20 @@ func pgCode(err error) string {
|
||||
}
|
||||
|
||||
func (s *InstalledPackages) SetUpdatePolicy(ctx context.Context, orgID, name, policy string) error {
|
||||
rows, err := postgresdb.New(s.pool).SetInstalledPackageUpdatePolicy(ctx, postgresdb.SetInstalledPackageUpdatePolicyParams{
|
||||
tx, err := s.pool.Begin(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
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
|
||||
}
|
||||
rows, err := q.SetInstalledPackageUpdatePolicy(ctx, postgresdb.SetInstalledPackageUpdatePolicyParams{
|
||||
OrgID: orgID, Name: name, UpdatePolicy: policy,
|
||||
})
|
||||
if err != nil {
|
||||
@@ -300,7 +434,12 @@ func (s *InstalledPackages) SetUpdatePolicy(ctx context.Context, orgID, name, po
|
||||
if rows == 0 {
|
||||
return storepkg.ErrNotFound
|
||||
}
|
||||
return 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)
|
||||
}
|
||||
|
||||
func (s *InstalledPackages) ListAutoUpdate(ctx context.Context) ([]storepkg.OrgPackage, error) {
|
||||
|
||||
@@ -36,10 +36,11 @@ func (q *Queries) GetHarnessSecret(ctx context.Context, arg GetHarnessSecretPara
|
||||
return i, 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 {
|
||||
@@ -48,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,7 +7,10 @@
|
||||
SELECT org, service, secret_enc, updated_at
|
||||
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;
|
||||
Reference in new issue
Block a user