feat(state): audit access reset, org, membership and EDA writes
This commit is contained in:
14 files changed
+873
-185
No files matched your search
@@ -53,6 +53,63 @@ func auditKey(p access.Policy) string {
|
|||||||
return strings.Trim(joined, "/")
|
return strings.Trim(joined, "/")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The reset command runs on the State Store host with no authenticated caller,
|
||||||
|
// so its own audit rows name the recovery rather than reading as "unknown".
|
||||||
|
const (
|
||||||
|
resetActor = "archie-state-store access reset"
|
||||||
|
resetSource = "archie-state-store"
|
||||||
|
)
|
||||||
|
|
||||||
|
// policyFromResetRow is one policy a reset deleted, as the audit row needs it.
|
||||||
|
func policyFromResetRow(level, orgID, wsID, kind, id, policyID, text string) access.Policy {
|
||||||
|
return access.Policy{
|
||||||
|
ID: policyID, Level: access.Level(level), OrgID: org.OrgID(orgID),
|
||||||
|
WorkspaceID: org.WorkspaceID(wsID), ObjectKind: access.ResourceKind(kind),
|
||||||
|
ObjectID: id, Text: text,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// auditResetDeletes writes one delete row per policy a reset removed, at the
|
||||||
|
// same scope version a DeletePolicy would have written. The shipped policies
|
||||||
|
// the reset then re-puts carry their own PutPolicy rows.
|
||||||
|
func auditResetDeletes(ctx context.Context, q *postgresdb.Queries, policies []access.Policy) error {
|
||||||
|
type scope struct {
|
||||||
|
level, orgID, workspaceID string
|
||||||
|
}
|
||||||
|
versions := make(map[scope]int64, len(policies))
|
||||||
|
for _, p := range policies {
|
||||||
|
level, orgID, wsID, _, _, _ := accessScopeOf(p)
|
||||||
|
s := scope{level: level, orgID: orgID, workspaceID: wsID}
|
||||||
|
version, ok := versions[s]
|
||||||
|
if !ok {
|
||||||
|
bumped, err := q.UpsertAccessPolicyVersion(ctx, postgresdb.UpsertAccessPolicyVersionParams{
|
||||||
|
Level: level, OrgID: orgID, WorkspaceID: wsID,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
version = bumped
|
||||||
|
versions[s] = bumped
|
||||||
|
}
|
||||||
|
oldValue, err := auditJSON(p.Text)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := insertAudit(ctx, q, "access_policies", auditKey(p), "text", oldValue, nil, version, auditOrg(p)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for s := range versions {
|
||||||
|
// A scope with no policies left keeps no version row, as DeletePolicy does.
|
||||||
|
if err := q.DeleteAccessPolicyVersion(ctx, postgresdb.DeleteAccessPolicyVersionParams{
|
||||||
|
Level: s.level, OrgID: s.orgID, WorkspaceID: s.workspaceID,
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// ListPolicies returns the stored policies the request's scope may see:
|
// ListPolicies returns the stored policies the request's scope may see:
|
||||||
// every policy across levels for an unscoped service.
|
// every policy across levels for an unscoped service.
|
||||||
func (s *Store) ListPolicies(ctx context.Context) ([]access.Policy, error) {
|
func (s *Store) ListPolicies(ctx context.Context) ([]access.Policy, error) {
|
||||||
@@ -252,8 +309,27 @@ func (s *Store) ResetOrgPolicies(ctx context.Context, orgID org.OrgID) error {
|
|||||||
if orgID == "" {
|
if orgID == "" {
|
||||||
return fmt.Errorf("%w: no org", access.ErrInvalidPolicy)
|
return fmt.Errorf("%w: no org", access.ErrInvalidPolicy)
|
||||||
}
|
}
|
||||||
if err := s.queries().DeleteOrgPoliciesNotShipped(ctx, postgresdb.DeleteOrgPoliciesNotShippedParams{
|
ctx = access.WithActor(ctx, resetActor)
|
||||||
OrgID: string(orgID), Column2: shippedOrgPolicyIDs(),
|
ctx = access.WithSource(ctx, resetSource)
|
||||||
|
if err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
|
q := postgresdb.New(tx)
|
||||||
|
rows, err := q.DeleteOrgPoliciesNotShipped(ctx, postgresdb.DeleteOrgPoliciesNotShippedParams{
|
||||||
|
OrgID: string(orgID), Column2: shippedOrgPolicyIDs(),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
policies := make([]access.Policy, 0, len(rows))
|
||||||
|
for _, r := range rows {
|
||||||
|
policies = append(policies, policyFromResetRow(r.Level, r.OrgID, r.WorkspaceID, r.ObjectKind, r.ObjectID, r.PolicyID, r.Text))
|
||||||
|
}
|
||||||
|
if err := auditResetDeletes(ctx, q, policies); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// Commit the revision with the deletes: an authorizer must stop
|
||||||
|
// honouring a removed policy even if the re-puts never run.
|
||||||
|
_, err = q.BumpAccessPolicyRevision(ctx)
|
||||||
|
return err
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -268,8 +344,18 @@ func (s *Store) ResetOrgPolicies(ctx context.Context, orgID org.OrgID) error {
|
|||||||
// ResetInstancePolicies removes every stored instance policy. The
|
// ResetInstancePolicies removes every stored instance policy. The
|
||||||
// cross-org forbid is engine-enforced and survives any reset.
|
// cross-org forbid is engine-enforced and survives any reset.
|
||||||
func (s *Store) ResetInstancePolicies(ctx context.Context) error {
|
func (s *Store) ResetInstancePolicies(ctx context.Context) error {
|
||||||
|
ctx = access.WithActor(ctx, resetActor)
|
||||||
|
ctx = access.WithSource(ctx, resetSource)
|
||||||
_, err := s.writePolicy(ctx, func(q *postgresdb.Queries) error {
|
_, err := s.writePolicy(ctx, func(q *postgresdb.Queries) error {
|
||||||
return q.ResetInstancePolicies(ctx)
|
rows, err := q.ResetInstancePolicies(ctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
policies := make([]access.Policy, 0, len(rows))
|
||||||
|
for _, r := range rows {
|
||||||
|
policies = append(policies, policyFromResetRow(r.Level, r.OrgID, r.WorkspaceID, r.ObjectKind, r.ObjectID, r.PolicyID, r.Text))
|
||||||
|
}
|
||||||
|
return auditResetDeletes(ctx, q, policies)
|
||||||
})
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,20 @@ func insertAudit(ctx context.Context, q *postgresdb.Queries, table, recordKey, f
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// insertAuditValues encodes both sides of one change and writes the row, so a
|
||||||
|
// caller records the value it holds rather than pre-encoding it.
|
||||||
|
func insertAuditValues(ctx context.Context, q *postgresdb.Queries, table, recordKey, field string, oldValue, newValue any, version int64, orgID string) error {
|
||||||
|
oldJSON, err := auditJSON(oldValue)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
newJSON, err := auditJSON(newValue)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return insertAudit(ctx, q, table, recordKey, field, oldJSON, newJSON, version, orgID)
|
||||||
|
}
|
||||||
|
|
||||||
// auditJSON encodes one side of an audit change. An absent value stays nil,
|
// auditJSON encodes one side of an audit change. An absent value stays nil,
|
||||||
// which the column stores as NULL: absent is not the same fact as JSON null.
|
// which the column stores as NULL: absent is not the same fact as JSON null.
|
||||||
// A value that cannot be encoded is an error, never a silent NULL that would
|
// A value that cannot be encoded is an error, never a silent NULL that would
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"reflect"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@@ -73,6 +74,63 @@ func (s *EDA) notifyWrite(kind, subject, action, id string) {
|
|||||||
|
|
||||||
func newRecordID() string { return uuid.NewString() }
|
func newRecordID() string { return uuid.NewString() }
|
||||||
|
|
||||||
|
// decodedJSON keeps a stored JSON text structured in an audit value, and falls
|
||||||
|
// back to the raw string when it will not decode.
|
||||||
|
func decodedJSON(raw string) any {
|
||||||
|
var v any
|
||||||
|
if err := json.Unmarshal([]byte(raw), &v); err != nil {
|
||||||
|
return raw
|
||||||
|
}
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
// mappingAuditDoc is the record an audit row shows for a mapping.
|
||||||
|
func mappingAuditDoc(id, name, sourceHint, eventType, fields string) map[string]any {
|
||||||
|
return map[string]any{
|
||||||
|
"id": id, "name": name, "source_hint": sourceHint,
|
||||||
|
"event_type": eventType, "fields": decodedJSON(fields),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func mappingRowAuditDoc(r postgresdb.GetMappingRow) map[string]any {
|
||||||
|
return mappingAuditDoc(r.Mapping.ID, r.Mapping.Name, r.Mapping.SourceHint, r.Mapping.EventType, r.Mapping.Fields)
|
||||||
|
}
|
||||||
|
|
||||||
|
// bindingAuditDoc is the record an audit row shows for a binding.
|
||||||
|
func bindingAuditDoc(id, name, source, mappingID, filter, workflow, owner, repo, repoParam, inputs, status, workspaceID string, version int64) map[string]any {
|
||||||
|
return map[string]any{
|
||||||
|
"id": id, "name": name, "source": source, "mapping": mappingID,
|
||||||
|
"filter": filter, "workflow": workflow, "owner": owner, "repo": repo,
|
||||||
|
"repo_param": repoParam, "inputs": decodedJSON(inputs), "status": status,
|
||||||
|
"workspace_id": workspaceID, "version": version,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func bindingRowAuditDoc(r postgresdb.GetBindingRow) map[string]any {
|
||||||
|
return bindingAuditDoc(r.ID, r.Name, r.Source, r.Mapping, r.Filter, r.Workflow, r.Owner, r.Repo, r.RepoParam, r.Inputs, r.Status, r.WorkspaceID, r.Version)
|
||||||
|
}
|
||||||
|
|
||||||
|
// bindingWorkspace is the workspace the binding SQL derives: its source's, or
|
||||||
|
// "default" when the source no longer exists.
|
||||||
|
func bindingWorkspace(ctx context.Context, q *postgresdb.Queries, source, orgID string) (string, error) {
|
||||||
|
workspace, err := q.SourceWorkspace(ctx, postgresdb.SourceWorkspaceParams{Path: source, OrgID: orgID})
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return "default", nil
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return workspace, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// bindingEditableEqual reports whether b would change any field UpdateBinding
|
||||||
|
// writes, so an identical save is not recorded as a change.
|
||||||
|
func bindingEditableEqual(r postgresdb.GetBindingRow, b binding.Binding, inputs string) bool {
|
||||||
|
return r.Name == b.Name && r.Source == b.Matcher.Source && r.Mapping == b.MappingID &&
|
||||||
|
r.Filter == b.Filter && r.Workflow == b.Workflow && r.Owner == b.Owner &&
|
||||||
|
r.Repo == b.Repo && r.RepoParam == b.RepoParam && r.Inputs == inputs
|
||||||
|
}
|
||||||
|
|
||||||
// --- captures ---
|
// --- captures ---
|
||||||
|
|
||||||
// InsertCapture stores one inbound event verbatim, tagged with the event type
|
// InsertCapture stores one inbound event verbatim, tagged with the event type
|
||||||
@@ -229,15 +287,23 @@ func (s *EDA) InsertMapping(ctx context.Context, m mapping.Mapping) (string, err
|
|||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
id := newRecordID()
|
id := newRecordID()
|
||||||
if err := s.q.InsertMapping(ctx, postgresdb.InsertMappingParams{
|
orgID := string(org.OrgFromContext(ctx))
|
||||||
OrgID: string(org.OrgFromContext(ctx)),
|
if err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
ID: id,
|
q := postgresdb.New(tx)
|
||||||
Name: m.Name,
|
if err := q.InsertMapping(ctx, postgresdb.InsertMappingParams{
|
||||||
SourceHint: m.SourceHint,
|
OrgID: orgID,
|
||||||
EventType: m.EventTypeID,
|
ID: id,
|
||||||
Fields: encoded,
|
Name: m.Name,
|
||||||
|
SourceHint: m.SourceHint,
|
||||||
|
EventType: m.EventTypeID,
|
||||||
|
Fields: encoded,
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("edastore: insert mapping: %w", err)
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "mappings", id, "mapping", nil,
|
||||||
|
mappingAuditDoc(id, m.Name, m.SourceHint, m.EventTypeID, encoded), 0, orgID)
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return "", fmt.Errorf("edastore: insert mapping: %w", err)
|
return "", err
|
||||||
}
|
}
|
||||||
s.notifyWrite(events.KindMappingChanged, "mapping", "create", id)
|
s.notifyWrite(events.KindMappingChanged, "mapping", "create", id)
|
||||||
return id, nil
|
return id, nil
|
||||||
@@ -281,32 +347,71 @@ func (s *EDA) UpdateMapping(ctx context.Context, m mapping.Mapping) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
n, err := s.q.UpdateMapping(ctx, postgresdb.UpdateMappingParams{
|
err = pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
ScopeOrg: scopeOrg(ctx),
|
q := postgresdb.New(tx)
|
||||||
ID: m.ID,
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "mappings", RecordKey: m.ID}); err != nil {
|
||||||
Name: m.Name,
|
return err
|
||||||
SourceHint: m.SourceHint,
|
}
|
||||||
EventType: m.EventTypeID,
|
old, err := q.GetMapping(ctx, postgresdb.GetMappingParams{ID: m.ID, ScopeOrg: scopeOrg(ctx)})
|
||||||
Fields: encoded,
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return storecontract.ErrMappingNotFound
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
oldDoc := mappingRowAuditDoc(old)
|
||||||
|
newDoc := mappingAuditDoc(m.ID, m.Name, m.SourceHint, m.EventTypeID, encoded)
|
||||||
|
if reflect.DeepEqual(oldDoc, newDoc) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
n, err := q.UpdateMapping(ctx, postgresdb.UpdateMappingParams{
|
||||||
|
ScopeOrg: scopeOrg(ctx),
|
||||||
|
ID: m.ID,
|
||||||
|
Name: m.Name,
|
||||||
|
SourceHint: m.SourceHint,
|
||||||
|
EventType: m.EventTypeID,
|
||||||
|
Fields: encoded,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return storecontract.ErrMappingNotFound
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "mappings", m.ID, "mapping", oldDoc, newDoc, 0, old.Mapping.OrgID)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if n == 0 {
|
|
||||||
return storecontract.ErrMappingNotFound
|
|
||||||
}
|
|
||||||
s.notifyWrite(events.KindMappingChanged, "mapping", "update", m.ID)
|
s.notifyWrite(events.KindMappingChanged, "mapping", "update", m.ID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *EDA) DeleteMapping(ctx context.Context, id string) error {
|
func (s *EDA) DeleteMapping(ctx context.Context, id string) error {
|
||||||
n, err := s.q.DeleteMapping(ctx, postgresdb.DeleteMappingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
|
q := postgresdb.New(tx)
|
||||||
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "mappings", RecordKey: id}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
old, err := q.GetMapping(ctx, postgresdb.GetMappingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return storecontract.ErrMappingNotFound
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n, err := q.DeleteMapping(ctx, postgresdb.DeleteMappingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return storecontract.ErrMappingNotFound
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "mappings", id, "mapping", mappingRowAuditDoc(old), nil, 0, old.Mapping.OrgID)
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if n == 0 {
|
|
||||||
return storecontract.ErrMappingNotFound
|
|
||||||
}
|
|
||||||
s.notifyWrite(events.KindMappingChanged, "mapping", "delete", id)
|
s.notifyWrite(events.KindMappingChanged, "mapping", "delete", id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -370,22 +475,34 @@ func (s *EDA) InsertBinding(ctx context.Context, b binding.Binding) (string, err
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
err = s.q.InsertBinding(ctx, postgresdb.InsertBindingParams{
|
orgID := string(org.OrgFromContext(ctx))
|
||||||
OrgID: string(org.OrgFromContext(ctx)),
|
if err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
ID: id,
|
q := postgresdb.New(tx)
|
||||||
Name: b.Name,
|
workspace, err := bindingWorkspace(ctx, q, b.Matcher.Source, orgID)
|
||||||
Source: b.Matcher.Source,
|
if err != nil {
|
||||||
Mapping: b.MappingID,
|
return err
|
||||||
Filter: b.Filter,
|
}
|
||||||
Workflow: b.Workflow,
|
if err := q.InsertBinding(ctx, postgresdb.InsertBindingParams{
|
||||||
Owner: b.Owner,
|
OrgID: orgID,
|
||||||
Repo: b.Repo,
|
ID: id,
|
||||||
Status: string(binding.StatusPendingApproval),
|
Name: b.Name,
|
||||||
Inputs: inputs,
|
Source: b.Matcher.Source,
|
||||||
RepoParam: b.RepoParam,
|
Mapping: b.MappingID,
|
||||||
})
|
Filter: b.Filter,
|
||||||
if err != nil {
|
Workflow: b.Workflow,
|
||||||
return "", fmt.Errorf("edastore: insert binding: %w", err)
|
Owner: b.Owner,
|
||||||
|
Repo: b.Repo,
|
||||||
|
Status: string(binding.StatusPendingApproval),
|
||||||
|
Inputs: inputs,
|
||||||
|
RepoParam: b.RepoParam,
|
||||||
|
}); err != nil {
|
||||||
|
return fmt.Errorf("edastore: insert binding: %w", err)
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "bindings", id, "binding", nil,
|
||||||
|
bindingAuditDoc(id, b.Name, b.Matcher.Source, b.MappingID, b.Filter, b.Workflow,
|
||||||
|
b.Owner, b.Repo, b.RepoParam, inputs, string(binding.StatusPendingApproval), workspace, 1), 1, orgID)
|
||||||
|
}); err != nil {
|
||||||
|
return "", err
|
||||||
}
|
}
|
||||||
s.notifyWrite(events.KindBindingChanged, "binding", "create", id)
|
s.notifyWrite(events.KindBindingChanged, "binding", "create", id)
|
||||||
return id, nil
|
return id, nil
|
||||||
@@ -448,61 +565,125 @@ func (s *EDA) UpdateBinding(ctx context.Context, b binding.Binding) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
n, err := s.q.UpdateBinding(ctx, postgresdb.UpdateBindingParams{
|
err = pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
ScopeOrg: scopeOrg(ctx),
|
q := postgresdb.New(tx)
|
||||||
ID: b.ID,
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "bindings", RecordKey: b.ID}); err != nil {
|
||||||
Name: b.Name,
|
return err
|
||||||
Source: b.Matcher.Source,
|
}
|
||||||
Mapping: b.MappingID,
|
old, err := q.GetBinding(ctx, postgresdb.GetBindingParams{ID: b.ID, ScopeOrg: scopeOrg(ctx)})
|
||||||
Filter: b.Filter,
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
Workflow: b.Workflow,
|
return storecontract.ErrBindingNotFound
|
||||||
Owner: b.Owner,
|
}
|
||||||
Repo: b.Repo,
|
if err != nil {
|
||||||
Status: string(binding.StatusPendingApproval),
|
return err
|
||||||
Inputs: inputs,
|
}
|
||||||
RepoParam: b.RepoParam,
|
// The target workspace comes from the source, so hold the source's lock
|
||||||
|
// until the UPDATE has recomputed it too. Binding then source is the
|
||||||
|
// only order taken, so it cannot cycle.
|
||||||
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "sources", RecordKey: b.Matcher.Source}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
targetWorkspace, err := bindingWorkspace(ctx, q, b.Matcher.Source, old.OrgID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if bindingEditableEqual(old, b, inputs) && old.Status == string(binding.StatusPendingApproval) &&
|
||||||
|
old.WorkspaceID == targetWorkspace {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
n, err := q.UpdateBinding(ctx, postgresdb.UpdateBindingParams{
|
||||||
|
ScopeOrg: scopeOrg(ctx),
|
||||||
|
ID: b.ID,
|
||||||
|
Name: b.Name,
|
||||||
|
Source: b.Matcher.Source,
|
||||||
|
Mapping: b.MappingID,
|
||||||
|
Filter: b.Filter,
|
||||||
|
Workflow: b.Workflow,
|
||||||
|
Owner: b.Owner,
|
||||||
|
Repo: b.Repo,
|
||||||
|
Status: string(binding.StatusPendingApproval),
|
||||||
|
Inputs: inputs,
|
||||||
|
RepoParam: b.RepoParam,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return storecontract.ErrBindingNotFound
|
||||||
|
}
|
||||||
|
next := old.Version + 1
|
||||||
|
return insertAuditValues(ctx, q, "bindings", b.ID, "binding",
|
||||||
|
bindingRowAuditDoc(old),
|
||||||
|
bindingAuditDoc(b.ID, b.Name, b.Matcher.Source, b.MappingID, b.Filter, b.Workflow,
|
||||||
|
b.Owner, b.Repo, b.RepoParam, inputs, string(binding.StatusPendingApproval), targetWorkspace, next),
|
||||||
|
next, old.OrgID)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if n == 0 {
|
|
||||||
return storecontract.ErrBindingNotFound
|
|
||||||
}
|
|
||||||
s.notifyWrite(events.KindBindingChanged, "binding", "update", b.ID)
|
s.notifyWrite(events.KindBindingChanged, "binding", "update", b.ID)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *EDA) DeleteBinding(ctx context.Context, id string) error {
|
func (s *EDA) DeleteBinding(ctx context.Context, id string) error {
|
||||||
n, err := s.q.DeleteBinding(ctx, postgresdb.DeleteBindingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
|
q := postgresdb.New(tx)
|
||||||
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "bindings", RecordKey: id}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
old, err := q.GetBinding(ctx, postgresdb.GetBindingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return storecontract.ErrBindingNotFound
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n, err := q.DeleteBinding(ctx, postgresdb.DeleteBindingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return storecontract.ErrBindingNotFound
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "bindings", id, "binding", bindingRowAuditDoc(old), nil, old.Version, old.OrgID)
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if n == 0 {
|
|
||||||
return storecontract.ErrBindingNotFound
|
|
||||||
}
|
|
||||||
s.notifyWrite(events.KindBindingChanged, "binding", "delete", id)
|
s.notifyWrite(events.KindBindingChanged, "binding", "delete", id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ApproveBinding is the only transition that arms a binding.
|
// ApproveBinding is the only transition that arms a binding.
|
||||||
func (s *EDA) ApproveBinding(ctx context.Context, id string) error {
|
func (s *EDA) ApproveBinding(ctx context.Context, id string) error {
|
||||||
r, err := s.q.GetBinding(ctx, postgresdb.GetBindingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
if errors.Is(err, pgx.ErrNoRows) {
|
q := postgresdb.New(tx)
|
||||||
return storecontract.ErrBindingNotFound
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "bindings", RecordKey: id}); err != nil {
|
||||||
}
|
return err
|
||||||
|
}
|
||||||
|
r, err := q.GetBinding(ctx, postgresdb.GetBindingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return storecontract.ErrBindingNotFound
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if r.Status != string(binding.StatusPendingApproval) {
|
||||||
|
return storecontract.ErrBindingTransition
|
||||||
|
}
|
||||||
|
n, err := q.SetBindingArmed(ctx, postgresdb.SetBindingArmedParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return storecontract.ErrBindingNotFound
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "bindings", id, "status",
|
||||||
|
string(binding.StatusPendingApproval), string(binding.StatusArmed), r.Version, r.OrgID)
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if r.Status != string(binding.StatusPendingApproval) {
|
|
||||||
return storecontract.ErrBindingTransition
|
|
||||||
}
|
|
||||||
n, err := s.q.SetBindingArmed(ctx, postgresdb.SetBindingArmedParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if n == 0 {
|
|
||||||
return storecontract.ErrBindingNotFound
|
|
||||||
}
|
|
||||||
s.notifyWrite(events.KindBindingChanged, "binding", "approve", id)
|
s.notifyWrite(events.KindBindingChanged, "binding", "approve", id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -519,18 +700,32 @@ func (s *EDA) ResumeBinding(ctx context.Context, id string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *EDA) transitionBinding(ctx context.Context, id string, from, to binding.Status, verb string) error {
|
func (s *EDA) transitionBinding(ctx context.Context, id string, from, to binding.Status, verb string) error {
|
||||||
n, err := s.q.TransitionBindingStatus(ctx, postgresdb.TransitionBindingStatusParams{
|
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
ID: id, FromStatus: string(from), ToStatus: string(to), ScopeOrg: scopeOrg(ctx),
|
q := postgresdb.New(tx)
|
||||||
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "bindings", RecordKey: id}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n, err := q.TransitionBindingStatus(ctx, postgresdb.TransitionBindingStatusParams{
|
||||||
|
ID: id, FromStatus: string(from), ToStatus: string(to), ScopeOrg: scopeOrg(ctx),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
if _, err := q.GetBinding(ctx, postgresdb.GetBindingParams{ID: id, ScopeOrg: scopeOrg(ctx)}); errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
return storecontract.ErrBindingNotFound
|
||||||
|
}
|
||||||
|
return storecontract.ErrBindingTransition
|
||||||
|
}
|
||||||
|
current, err := q.GetBinding(ctx, postgresdb.GetBindingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "bindings", id, "status", string(from), string(to), current.Version, current.OrgID)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if n == 0 {
|
|
||||||
if _, err := s.q.GetBinding(ctx, postgresdb.GetBindingParams{ID: id, ScopeOrg: scopeOrg(ctx)}); errors.Is(err, pgx.ErrNoRows) {
|
|
||||||
return storecontract.ErrBindingNotFound
|
|
||||||
}
|
|
||||||
return storecontract.ErrBindingTransition
|
|
||||||
}
|
|
||||||
s.notifyWrite(events.KindBindingChanged, "binding", verb, id)
|
s.notifyWrite(events.KindBindingChanged, "binding", verb, id)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,6 +30,14 @@ func eventTypeValue(r postgresdb.EventType) (eventtype.EventType, error) {
|
|||||||
return t, nil
|
return t, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// eventTypeAuditDoc is the record an audit row shows for an event type.
|
||||||
|
func eventTypeAuditDoc(id, source, name, rule, schema string) map[string]any {
|
||||||
|
return map[string]any{
|
||||||
|
"id": id, "source": source, "name": name,
|
||||||
|
"rule": decodedJSON(rule), "schema": decodedJSON(schema),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func eventTypeValues(rows []postgresdb.EventType) ([]eventtype.EventType, error) {
|
func eventTypeValues(rows []postgresdb.EventType) ([]eventtype.EventType, error) {
|
||||||
out := make([]eventtype.EventType, 0, len(rows))
|
out := make([]eventtype.EventType, 0, len(rows))
|
||||||
for _, r := range rows {
|
for _, r := range rows {
|
||||||
@@ -113,11 +121,16 @@ func (s *EDA) InsertEventType(ctx context.Context, t eventtype.EventType) (strin
|
|||||||
return "", fmt.Errorf("edastore: encode event type schema: %w", err)
|
return "", fmt.Errorf("edastore: encode event type schema: %w", err)
|
||||||
}
|
}
|
||||||
id := newRecordID()
|
id := newRecordID()
|
||||||
|
orgID := string(org.OrgFromContext(ctx))
|
||||||
err = s.saveEventType(ctx, t, func(q *postgresdb.Queries, rule string) error {
|
err = s.saveEventType(ctx, t, func(q *postgresdb.Queries, rule string) error {
|
||||||
return q.InsertEventType(ctx, postgresdb.InsertEventTypeParams{
|
if err := q.InsertEventType(ctx, postgresdb.InsertEventTypeParams{
|
||||||
ID: id, Source: t.Source, Name: t.Name, Rule: rule, Schema: string(schema),
|
ID: id, Source: t.Source, Name: t.Name, Rule: rule, Schema: string(schema),
|
||||||
OrgID: string(org.OrgFromContext(ctx)),
|
OrgID: orgID,
|
||||||
})
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "event_types", id, "event_type", nil,
|
||||||
|
eventTypeAuditDoc(id, t.Source, t.Name, rule, string(schema)), 0, orgID)
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
@@ -137,25 +150,55 @@ func (s *EDA) UpdateEventType(ctx context.Context, t eventtype.EventType) error
|
|||||||
}
|
}
|
||||||
t.Source = stored.Source
|
t.Source = stored.Source
|
||||||
return s.saveEventType(ctx, t, func(q *postgresdb.Queries, rule string) error {
|
return s.saveEventType(ctx, t, func(q *postgresdb.Queries, rule string) error {
|
||||||
n, err := q.UpdateEventType(ctx, postgresdb.UpdateEventTypeParams{ID: t.ID, Name: t.Name, Rule: rule})
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "event_types", RecordKey: t.ID}); err != nil {
|
||||||
if err == nil && n == 0 {
|
return err
|
||||||
|
}
|
||||||
|
old, err := q.GetEventType(ctx, postgresdb.GetEventTypeParams{ID: t.ID, ScopeOrg: scopeOrg(ctx)})
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
return storecontract.ErrEventTypeNotFound
|
return storecontract.ErrEventTypeNotFound
|
||||||
}
|
}
|
||||||
return err
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n, err := q.UpdateEventType(ctx, postgresdb.UpdateEventTypeParams{ID: t.ID, Name: t.Name, Rule: rule})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return storecontract.ErrEventTypeNotFound
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "event_types", t.ID, "event_type",
|
||||||
|
eventTypeAuditDoc(old.ID, old.Source, old.Name, old.Rule, old.Schema),
|
||||||
|
eventTypeAuditDoc(t.ID, old.Source, t.Name, rule, old.Schema),
|
||||||
|
0, old.OrgID)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteEventType removes a type. Captures already identified as it keep the
|
// DeleteEventType removes a type. Captures already identified as it keep the
|
||||||
// ID they were stored with.
|
// ID they were stored with.
|
||||||
func (s *EDA) DeleteEventType(ctx context.Context, id string) error {
|
func (s *EDA) DeleteEventType(ctx context.Context, id string) error {
|
||||||
n, err := s.q.DeleteEventType(ctx, postgresdb.DeleteEventTypeParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
if err != nil {
|
q := postgresdb.New(tx)
|
||||||
return err
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "event_types", RecordKey: id}); err != nil {
|
||||||
}
|
return err
|
||||||
if n == 0 {
|
}
|
||||||
return storecontract.ErrEventTypeNotFound
|
old, err := q.GetEventType(ctx, postgresdb.GetEventTypeParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
||||||
}
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
return nil
|
return storecontract.ErrEventTypeNotFound
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n, err := q.DeleteEventType(ctx, postgresdb.DeleteEventTypeParams{ID: id, ScopeOrg: scopeOrg(ctx)})
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return storecontract.ErrEventTypeNotFound
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "event_types", id, "event_type",
|
||||||
|
eventTypeAuditDoc(old.ID, old.Source, old.Name, old.Rule, old.Schema), nil, 0, old.OrgID)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListEventTypes returns every event type, by source then name.
|
// ListEventTypes returns every event type, by source then name.
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/jackc/pgx/v5"
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
@@ -47,12 +48,20 @@ func (s *Store) CreateOrg(ctx context.Context, value org.Org, owner identity.Ide
|
|||||||
}
|
}
|
||||||
return org.Org{}, err
|
return org.Org{}, err
|
||||||
}
|
}
|
||||||
|
if err := insertAuditValues(ctx, q, "orgs", string(value.ID), "org", nil,
|
||||||
|
map[string]string{"id": string(value.ID), "name": value.Name}, 0, string(value.ID)); err != nil {
|
||||||
|
return org.Org{}, err
|
||||||
|
}
|
||||||
|
workspace := org.Workspace{ID: org.DefaultWorkspaceID, OrgID: value.ID, Name: "Default"}
|
||||||
if err := q.InsertWorkspace(ctx, postgresdb.InsertWorkspaceParams{
|
if err := q.InsertWorkspace(ctx, postgresdb.InsertWorkspaceParams{
|
||||||
ID: string(org.DefaultWorkspaceID), OrgID: string(value.ID), Name: "Default",
|
ID: string(workspace.ID), OrgID: string(workspace.OrgID), Name: workspace.Name,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return org.Org{}, err
|
return org.Org{}, err
|
||||||
}
|
}
|
||||||
if err := q.EnsureMembership(ctx, postgresdb.EnsureMembershipParams{
|
if err := auditWorkspaceCreate(ctx, q, workspace); err != nil {
|
||||||
|
return org.Org{}, err
|
||||||
|
}
|
||||||
|
if err := ensureMembershipAudited(ctx, q, membership, postgresdb.EnsureMembershipParams{
|
||||||
IdentityID: string(owner), OrgID: string(value.ID), Role: string(org.RoleOwner),
|
IdentityID: string(owner), OrgID: string(value.ID), Role: string(org.RoleOwner),
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return org.Org{}, err
|
return org.Org{}, err
|
||||||
@@ -94,18 +103,34 @@ func (s *Store) CreateWorkspace(ctx context.Context, value org.Workspace) (org.W
|
|||||||
if _, err := s.GetOrg(ctx, value.OrgID); err != nil {
|
if _, err := s.GetOrg(ctx, value.OrgID); err != nil {
|
||||||
return org.Workspace{}, err
|
return org.Workspace{}, err
|
||||||
}
|
}
|
||||||
if err := s.queries().InsertWorkspace(ctx, postgresdb.InsertWorkspaceParams{
|
err := pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
ID: string(value.ID), OrgID: string(value.OrgID),
|
q := postgresdb.New(tx)
|
||||||
Name: value.Name, Environment: value.Environment,
|
if err := q.InsertWorkspace(ctx, postgresdb.InsertWorkspaceParams{
|
||||||
}); err != nil {
|
ID: string(value.ID), OrgID: string(value.OrgID),
|
||||||
if isUniqueViolation(err) {
|
Name: value.Name, Environment: value.Environment,
|
||||||
return org.Workspace{}, fmt.Errorf("%w: %s/%s", org.ErrWorkspaceExists, value.OrgID, value.ID)
|
}); err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
return auditWorkspaceCreate(ctx, q, value)
|
||||||
|
})
|
||||||
|
if isUniqueViolation(err) {
|
||||||
|
return org.Workspace{}, fmt.Errorf("%w: %s/%s", org.ErrWorkspaceExists, value.OrgID, value.ID)
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
return org.Workspace{}, err
|
return org.Workspace{}, err
|
||||||
}
|
}
|
||||||
return value, nil
|
return value, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// auditWorkspaceCreate writes the create row for a workspace.
|
||||||
|
func auditWorkspaceCreate(ctx context.Context, q *postgresdb.Queries, value org.Workspace) error {
|
||||||
|
return insertAuditValues(ctx, q, "workspaces", string(value.OrgID)+"/"+string(value.ID), "workspace", nil,
|
||||||
|
map[string]string{
|
||||||
|
"id": string(value.ID), "org_id": string(value.OrgID),
|
||||||
|
"name": value.Name, "environment": value.Environment,
|
||||||
|
}, 0, string(value.OrgID))
|
||||||
|
}
|
||||||
|
|
||||||
// workspaceFromRow maps the generated workspace row to the domain workspace.
|
// workspaceFromRow maps the generated workspace row to the domain workspace.
|
||||||
func workspaceFromRow(w postgresdb.Workspace) org.Workspace {
|
func workspaceFromRow(w postgresdb.Workspace) org.Workspace {
|
||||||
return org.Workspace{
|
return org.Workspace{
|
||||||
@@ -147,13 +172,52 @@ func (s *Store) EnsureMembership(ctx context.Context, value org.Membership) erro
|
|||||||
Role: string(value.Role),
|
Role: string(value.Role),
|
||||||
}
|
}
|
||||||
if value.WorkspaceID != "" || value.Role == org.RoleOwner {
|
if value.WorkspaceID != "" || value.Role == org.RoleOwner {
|
||||||
return s.queries().EnsureMembership(ctx, params)
|
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
|
return ensureMembershipAudited(ctx, postgresdb.New(tx), value, params)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return s.guardOwners(ctx, string(value.IdentityID), string(value.OrgID), func(q *postgresdb.Queries) error {
|
return s.guardOwners(ctx, string(value.IdentityID), string(value.OrgID), func(q *postgresdb.Queries) error {
|
||||||
return q.EnsureMembership(ctx, params)
|
return ensureMembershipAudited(ctx, q, value, params)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// membershipAuditKey names one membership by the scope the unique index uses,
|
||||||
|
// so an org-wide grant and a workspace grant never collide.
|
||||||
|
func membershipAuditKey(m org.Membership) string {
|
||||||
|
parts := []string{string(m.OrgID)}
|
||||||
|
if m.WorkspaceID != "" {
|
||||||
|
parts = append(parts, string(m.WorkspaceID))
|
||||||
|
}
|
||||||
|
return strings.Join(append(parts, string(m.IdentityID)), "/")
|
||||||
|
}
|
||||||
|
|
||||||
|
// ensureMembershipAudited upserts one membership and records the role it held
|
||||||
|
// before and holds now, in the caller's transaction. An unchanged role is not
|
||||||
|
// a change and writes no audit row.
|
||||||
|
func ensureMembershipAudited(ctx context.Context, q *postgresdb.Queries, value org.Membership, params postgresdb.EnsureMembershipParams) error {
|
||||||
|
key := membershipAuditKey(value)
|
||||||
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "memberships", RecordKey: key}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
previous, err := q.MembershipRole(ctx, postgresdb.MembershipRoleParams{
|
||||||
|
IdentityID: string(value.IdentityID), OrgID: string(value.OrgID), WorkspaceID: string(value.WorkspaceID),
|
||||||
|
})
|
||||||
|
var oldRole any
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
if previous == string(value.Role) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
oldRole = previous
|
||||||
|
case !errors.Is(err, pgx.ErrNoRows):
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := q.EnsureMembership(ctx, params); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "memberships", key, "role", oldRole, string(value.Role), 0, string(value.OrgID))
|
||||||
|
}
|
||||||
|
|
||||||
// AssignAgent records the one org an agent or service identity serves,
|
// AssignAgent records the one org an agent or service identity serves,
|
||||||
// replacing any prior assignment for that identity.
|
// replacing any prior assignment for that identity.
|
||||||
func (s *Store) AssignAgent(ctx context.Context, value org.AgentAssignment) error {
|
func (s *Store) AssignAgent(ctx context.Context, value org.AgentAssignment) error {
|
||||||
@@ -163,8 +227,39 @@ func (s *Store) AssignAgent(ctx context.Context, value org.AgentAssignment) erro
|
|||||||
if _, err := s.GetOrg(ctx, value.OrgID); err != nil {
|
if _, err := s.GetOrg(ctx, value.OrgID); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return s.queries().UpsertAgentAssignment(ctx, postgresdb.UpsertAgentAssignmentParams{
|
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
IdentityID: string(value.IdentityID), OrgID: string(value.OrgID),
|
q := postgresdb.New(tx)
|
||||||
|
// The assignment is unique per identity, so the identity is the lock.
|
||||||
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "org_agents", RecordKey: string(value.IdentityID)}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
var oldOrg string
|
||||||
|
previous, err := q.AgentAssignmentOrg(ctx, string(value.IdentityID))
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
oldOrg = previous
|
||||||
|
if oldOrg == string(value.OrgID) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
case !errors.Is(err, pgx.ErrNoRows):
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := q.UpsertAgentAssignment(ctx, postgresdb.UpsertAgentAssignmentParams{
|
||||||
|
IdentityID: string(value.IdentityID), OrgID: string(value.OrgID),
|
||||||
|
}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
// A move is two facts: the identity joins the target org and leaves the
|
||||||
|
// old one, each filed under the org it belongs to.
|
||||||
|
if err := insertAuditValues(ctx, q, "org_agents", string(value.OrgID)+"/"+string(value.IdentityID), "org_id",
|
||||||
|
nil, string(value.OrgID), 0, string(value.OrgID)); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if oldOrg != "" {
|
||||||
|
return insertAuditValues(ctx, q, "org_agents", oldOrg+"/"+string(value.IdentityID), "org_id",
|
||||||
|
oldOrg, nil, 0, oldOrg)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -197,13 +292,43 @@ func (s *Store) RemoveMembership(ctx context.Context, value org.Membership) erro
|
|||||||
IdentityID: string(value.IdentityID), OrgID: string(value.OrgID), WorkspaceID: string(value.WorkspaceID),
|
IdentityID: string(value.IdentityID), OrgID: string(value.OrgID), WorkspaceID: string(value.WorkspaceID),
|
||||||
}
|
}
|
||||||
if value.WorkspaceID != "" {
|
if value.WorkspaceID != "" {
|
||||||
return s.queries().RemoveMembership(ctx, params)
|
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
|
return removeMembershipAudited(ctx, postgresdb.New(tx), value, params)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
return s.guardOwners(ctx, string(value.IdentityID), string(value.OrgID), func(q *postgresdb.Queries) error {
|
return s.guardOwners(ctx, string(value.IdentityID), string(value.OrgID), func(q *postgresdb.Queries) error {
|
||||||
return q.RemoveMembership(ctx, params)
|
return removeMembershipAudited(ctx, q, value, params)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// removeMembershipAudited revokes one membership and records the role it held,
|
||||||
|
// only when a row was actually removed; a membership that was absent leaves no
|
||||||
|
// row to audit.
|
||||||
|
func removeMembershipAudited(ctx context.Context, q *postgresdb.Queries, value org.Membership, params postgresdb.RemoveMembershipParams) error {
|
||||||
|
key := membershipAuditKey(value)
|
||||||
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "memberships", RecordKey: key}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
role, err := q.MembershipRole(ctx, postgresdb.MembershipRoleParams{
|
||||||
|
IdentityID: string(value.IdentityID), OrgID: string(value.OrgID), WorkspaceID: string(value.WorkspaceID),
|
||||||
|
})
|
||||||
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
|
_, err := q.RemoveMembership(ctx, params)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n, err := q.RemoveMembership(ctx, params)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "memberships", key, "role", role, nil, 0, string(value.OrgID))
|
||||||
|
}
|
||||||
|
|
||||||
// ListOrgAgents returns the agent and service identities assigned to an org.
|
// ListOrgAgents returns the agent and service identities assigned to an org.
|
||||||
func (s *Store) ListOrgAgents(ctx context.Context, id org.OrgID) ([]org.AgentAssignment, error) {
|
func (s *Store) ListOrgAgents(ctx context.Context, id org.OrgID) ([]org.AgentAssignment, error) {
|
||||||
rows, err := s.queries().ListOrgAgents(ctx, string(id))
|
rows, err := s.queries().ListOrgAgents(ctx, string(id))
|
||||||
|
|||||||
@@ -133,9 +133,10 @@ func (q *Queries) DeleteAccessPolicyVersion(ctx context.Context, arg DeleteAcces
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
const deleteOrgPoliciesNotShipped = `-- name: DeleteOrgPoliciesNotShipped :exec
|
const deleteOrgPoliciesNotShipped = `-- name: DeleteOrgPoliciesNotShipped :many
|
||||||
DELETE FROM access_policies
|
DELETE FROM access_policies
|
||||||
WHERE level = 'org' AND org_id = $1 AND NOT (policy_id = ANY($2::text[]))
|
WHERE level = 'org' AND org_id = $1 AND NOT (policy_id = ANY($2::text[]))
|
||||||
|
RETURNING level, org_id, workspace_id, object_kind, object_id, policy_id, text
|
||||||
`
|
`
|
||||||
|
|
||||||
type DeleteOrgPoliciesNotShippedParams struct {
|
type DeleteOrgPoliciesNotShippedParams struct {
|
||||||
@@ -143,9 +144,42 @@ type DeleteOrgPoliciesNotShippedParams struct {
|
|||||||
Column2 []string
|
Column2 []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) DeleteOrgPoliciesNotShipped(ctx context.Context, arg DeleteOrgPoliciesNotShippedParams) error {
|
type DeleteOrgPoliciesNotShippedRow struct {
|
||||||
_, err := q.db.Exec(ctx, deleteOrgPoliciesNotShipped, arg.OrgID, arg.Column2)
|
Level string
|
||||||
return err
|
OrgID string
|
||||||
|
WorkspaceID string
|
||||||
|
ObjectKind string
|
||||||
|
ObjectID string
|
||||||
|
PolicyID string
|
||||||
|
Text string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) DeleteOrgPoliciesNotShipped(ctx context.Context, arg DeleteOrgPoliciesNotShippedParams) ([]DeleteOrgPoliciesNotShippedRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, deleteOrgPoliciesNotShipped, arg.OrgID, arg.Column2)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []DeleteOrgPoliciesNotShippedRow
|
||||||
|
for rows.Next() {
|
||||||
|
var i DeleteOrgPoliciesNotShippedRow
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.Level,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.WorkspaceID,
|
||||||
|
&i.ObjectKind,
|
||||||
|
&i.ObjectID,
|
||||||
|
&i.PolicyID,
|
||||||
|
&i.Text,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const insertAccessPolicyAudit = `-- name: InsertAccessPolicyAudit :exec
|
const insertAccessPolicyAudit = `-- name: InsertAccessPolicyAudit :exec
|
||||||
@@ -180,7 +214,7 @@ func (q *Queries) InsertAccessPolicyAudit(ctx context.Context, arg InsertAccessP
|
|||||||
|
|
||||||
const insertAccessResetAudit = `-- name: InsertAccessResetAudit :exec
|
const insertAccessResetAudit = `-- name: InsertAccessResetAudit :exec
|
||||||
INSERT INTO sys_audit (at, table_name, record_key, field, record_version, actor, source, request_id, org_id)
|
INSERT INTO sys_audit (at, table_name, record_key, field, record_version, actor, source, request_id, org_id)
|
||||||
VALUES (now(), 'access_policies', $1, 'reset', 1, 'archied access reset', 'archied', '', $2)
|
VALUES (now(), 'access_policies', $1, 'reset', 1, 'archie-state-store access reset', 'archie-state-store', '', $2)
|
||||||
`
|
`
|
||||||
|
|
||||||
type InsertAccessResetAuditParams struct {
|
type InsertAccessResetAuditParams struct {
|
||||||
@@ -326,13 +360,47 @@ func (q *Queries) RecordDenial(ctx context.Context, arg RecordDenialParams) erro
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
const resetInstancePolicies = `-- name: ResetInstancePolicies :exec
|
const resetInstancePolicies = `-- name: ResetInstancePolicies :many
|
||||||
DELETE FROM access_policies WHERE level = 'instance'
|
DELETE FROM access_policies WHERE level = 'instance'
|
||||||
|
RETURNING level, org_id, workspace_id, object_kind, object_id, policy_id, text
|
||||||
`
|
`
|
||||||
|
|
||||||
func (q *Queries) ResetInstancePolicies(ctx context.Context) error {
|
type ResetInstancePoliciesRow struct {
|
||||||
_, err := q.db.Exec(ctx, resetInstancePolicies)
|
Level string
|
||||||
return err
|
OrgID string
|
||||||
|
WorkspaceID string
|
||||||
|
ObjectKind string
|
||||||
|
ObjectID string
|
||||||
|
PolicyID string
|
||||||
|
Text string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (q *Queries) ResetInstancePolicies(ctx context.Context) ([]ResetInstancePoliciesRow, error) {
|
||||||
|
rows, err := q.db.Query(ctx, resetInstancePolicies)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
defer rows.Close()
|
||||||
|
var items []ResetInstancePoliciesRow
|
||||||
|
for rows.Next() {
|
||||||
|
var i ResetInstancePoliciesRow
|
||||||
|
if err := rows.Scan(
|
||||||
|
&i.Level,
|
||||||
|
&i.OrgID,
|
||||||
|
&i.WorkspaceID,
|
||||||
|
&i.ObjectKind,
|
||||||
|
&i.ObjectID,
|
||||||
|
&i.PolicyID,
|
||||||
|
&i.Text,
|
||||||
|
); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
items = append(items, i)
|
||||||
|
}
|
||||||
|
if err := rows.Err(); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const upsertAccessPolicy = `-- name: UpsertAccessPolicy :exec
|
const upsertAccessPolicy = `-- name: UpsertAccessPolicy :exec
|
||||||
|
|||||||
@@ -150,3 +150,20 @@ func (q *Queries) InsertResourceAudit(ctx context.Context, arg InsertResourceAud
|
|||||||
)
|
)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const lockAuditRecord = `-- name: LockAuditRecord :exec
|
||||||
|
SELECT pg_advisory_xact_lock(hashtextextended($1::text || ':' || $2::text, 0))
|
||||||
|
`
|
||||||
|
|
||||||
|
type LockAuditRecordParams struct {
|
||||||
|
TableName string
|
||||||
|
RecordKey string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serialises a read-modify-write on one audited record so the old side the
|
||||||
|
// audit row reports cannot change between the read and the write. Row locks
|
||||||
|
// cannot hold a key that does not exist yet; this can.
|
||||||
|
func (q *Queries) LockAuditRecord(ctx context.Context, arg LockAuditRecordParams) error {
|
||||||
|
_, err := q.db.Exec(ctx, lockAuditRecord, arg.TableName, arg.RecordKey)
|
||||||
|
return err
|
||||||
|
}
|
||||||
@@ -1205,6 +1205,24 @@ func (q *Queries) SourceExists(ctx context.Context, path string) (bool, error) {
|
|||||||
return exists, err
|
return exists, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const sourceWorkspace = `-- name: SourceWorkspace :one
|
||||||
|
SELECT workspace_id FROM sources WHERE path = $1 AND org_id = $2
|
||||||
|
`
|
||||||
|
|
||||||
|
type SourceWorkspaceParams struct {
|
||||||
|
Path string
|
||||||
|
OrgID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// The workspace the binding/mapping SQL derives from a source path, so an
|
||||||
|
// audit snapshots the same value the write stores.
|
||||||
|
func (q *Queries) SourceWorkspace(ctx context.Context, arg SourceWorkspaceParams) (string, error) {
|
||||||
|
row := q.db.QueryRow(ctx, sourceWorkspace, arg.Path, arg.OrgID)
|
||||||
|
var workspace_id string
|
||||||
|
err := row.Scan(&workspace_id)
|
||||||
|
return workspace_id, err
|
||||||
|
}
|
||||||
|
|
||||||
const taskToolCalls = `-- name: TaskToolCalls :many
|
const taskToolCalls = `-- name: TaskToolCalls :many
|
||||||
SELECT id, task_id, attempt, tool, result, error, called_at
|
SELECT id, task_id, attempt, tool, result, error, called_at
|
||||||
FROM tool_calls WHERE task_id = $1 AND ($2::text IS NULL OR org_id = $2) ORDER BY called_at ASC
|
FROM tool_calls WHERE task_id = $1 AND ($2::text IS NULL OR org_id = $2) ORDER BY called_at ASC
|
||||||
|
|||||||
@@ -11,6 +11,17 @@ import (
|
|||||||
"github.com/jackc/pgx/v5/pgtype"
|
"github.com/jackc/pgx/v5/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const agentAssignmentOrg = `-- name: AgentAssignmentOrg :one
|
||||||
|
SELECT org_id FROM org_agents WHERE identity_id = $1
|
||||||
|
`
|
||||||
|
|
||||||
|
func (q *Queries) AgentAssignmentOrg(ctx context.Context, identityID string) (string, error) {
|
||||||
|
row := q.db.QueryRow(ctx, agentAssignmentOrg, identityID)
|
||||||
|
var org_id string
|
||||||
|
err := row.Scan(&org_id)
|
||||||
|
return org_id, err
|
||||||
|
}
|
||||||
|
|
||||||
const ensureAgentOrgMembership = `-- name: EnsureAgentOrgMembership :exec
|
const ensureAgentOrgMembership = `-- name: EnsureAgentOrgMembership :exec
|
||||||
INSERT INTO memberships (identity_id, org_id, role)
|
INSERT INTO memberships (identity_id, org_id, role)
|
||||||
SELECT a.identity_id, a.org_id, 'developer' FROM org_agents a
|
SELECT a.identity_id, a.org_id, 'developer' FROM org_agents a
|
||||||
@@ -297,7 +308,28 @@ func (q *Queries) ListWorkspaces(ctx context.Context, orgID string) ([]Workspace
|
|||||||
return items, nil
|
return items, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const removeMembership = `-- name: RemoveMembership :exec
|
const membershipRole = `-- name: MembershipRole :one
|
||||||
|
SELECT role FROM memberships
|
||||||
|
WHERE identity_id = $1 AND org_id = $2
|
||||||
|
AND COALESCE(workspace_id, '') = $3::text
|
||||||
|
`
|
||||||
|
|
||||||
|
type MembershipRoleParams struct {
|
||||||
|
IdentityID string
|
||||||
|
OrgID string
|
||||||
|
WorkspaceID string
|
||||||
|
}
|
||||||
|
|
||||||
|
// The current role of the membership EnsureMembership/RemoveMembership targets,
|
||||||
|
// read in the same transaction so the audit row carries both sides.
|
||||||
|
func (q *Queries) MembershipRole(ctx context.Context, arg MembershipRoleParams) (string, error) {
|
||||||
|
row := q.db.QueryRow(ctx, membershipRole, arg.IdentityID, arg.OrgID, arg.WorkspaceID)
|
||||||
|
var role string
|
||||||
|
err := row.Scan(&role)
|
||||||
|
return role, err
|
||||||
|
}
|
||||||
|
|
||||||
|
const removeMembership = `-- name: RemoveMembership :execrows
|
||||||
DELETE FROM memberships
|
DELETE FROM memberships
|
||||||
WHERE identity_id = $1 AND org_id = $2 AND COALESCE(workspace_id, '') = $3::text
|
WHERE identity_id = $1 AND org_id = $2 AND COALESCE(workspace_id, '') = $3::text
|
||||||
`
|
`
|
||||||
@@ -310,9 +342,12 @@ type RemoveMembershipParams struct {
|
|||||||
|
|
||||||
// The workspace is matched through the same COALESCE the unique index uses, so
|
// The workspace is matched through the same COALESCE the unique index uses, so
|
||||||
// an org-wide membership (NULL) and the empty string are the same target.
|
// an org-wide membership (NULL) and the empty string are the same target.
|
||||||
func (q *Queries) RemoveMembership(ctx context.Context, arg RemoveMembershipParams) error {
|
func (q *Queries) RemoveMembership(ctx context.Context, arg RemoveMembershipParams) (int64, error) {
|
||||||
_, err := q.db.Exec(ctx, removeMembership, arg.IdentityID, arg.OrgID, arg.WorkspaceID)
|
result, err := q.db.Exec(ctx, removeMembership, arg.IdentityID, arg.OrgID, arg.WorkspaceID)
|
||||||
return err
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
return result.RowsAffected(), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
const resolveIdentityOrg = `-- name: ResolveIdentityOrg :one
|
const resolveIdentityOrg = `-- name: ResolveIdentityOrg :one
|
||||||
|
|||||||
@@ -38,12 +38,14 @@ FOR UPDATE;
|
|||||||
SELECT count(*) FROM access_policies
|
SELECT count(*) FROM access_policies
|
||||||
WHERE level = 'org' AND org_id = $1 AND policy_id = ANY($2::text[]);
|
WHERE level = 'org' AND org_id = $1 AND policy_id = ANY($2::text[]);
|
||||||
|
|
||||||
-- name: DeleteOrgPoliciesNotShipped :exec
|
-- name: DeleteOrgPoliciesNotShipped :many
|
||||||
DELETE FROM access_policies
|
DELETE FROM access_policies
|
||||||
WHERE level = 'org' AND org_id = $1 AND NOT (policy_id = ANY($2::text[]));
|
WHERE level = 'org' AND org_id = $1 AND NOT (policy_id = ANY($2::text[]))
|
||||||
|
RETURNING level, org_id, workspace_id, object_kind, object_id, policy_id, text;
|
||||||
|
|
||||||
-- name: ResetInstancePolicies :exec
|
-- name: ResetInstancePolicies :many
|
||||||
DELETE FROM access_policies WHERE level = 'instance';
|
DELETE FROM access_policies WHERE level = 'instance'
|
||||||
|
RETURNING level, org_id, workspace_id, object_kind, object_id, policy_id, text;
|
||||||
|
|
||||||
-- name: UpsertAccessPolicyVersion :one
|
-- name: UpsertAccessPolicyVersion :one
|
||||||
INSERT INTO access_policy_versions (level, org_id, workspace_id, version) VALUES ($1, $2, $3, 2)
|
INSERT INTO access_policy_versions (level, org_id, workspace_id, version) VALUES ($1, $2, $3, 2)
|
||||||
@@ -74,7 +76,7 @@ FROM access_denials WHERE org_id = $1 ORDER BY id DESC LIMIT $2;
|
|||||||
|
|
||||||
-- name: InsertAccessResetAudit :exec
|
-- name: InsertAccessResetAudit :exec
|
||||||
INSERT INTO sys_audit (at, table_name, record_key, field, record_version, actor, source, request_id, org_id)
|
INSERT INTO sys_audit (at, table_name, record_key, field, record_version, actor, source, request_id, org_id)
|
||||||
VALUES (now(), 'access_policies', $1, 'reset', 1, 'archied access reset', 'archied', '', $2);
|
VALUES (now(), 'access_policies', $1, 'reset', 1, 'archie-state-store access reset', 'archie-state-store', '', $2);
|
||||||
|
|
||||||
-- name: BumpAccessPolicyRevision :one
|
-- name: BumpAccessPolicyRevision :one
|
||||||
-- The instance-wide version. It moves inside the transaction of every policy
|
-- The instance-wide version. It moves inside the transaction of every policy
|
||||||
|
|||||||
@@ -10,6 +10,12 @@ FROM audit_json_diff(
|
|||||||
) d
|
) d
|
||||||
ORDER BY d.field;
|
ORDER BY d.field;
|
||||||
|
|
||||||
|
-- name: LockAuditRecord :exec
|
||||||
|
-- Serialises a read-modify-write on one audited record so the old side the
|
||||||
|
-- audit row reports cannot change between the read and the write. Row locks
|
||||||
|
-- cannot hold a key that does not exist yet; this can.
|
||||||
|
SELECT pg_advisory_xact_lock(hashtextextended(sqlc.arg(table_name)::text || ':' || sqlc.arg(record_key)::text, 0));
|
||||||
|
|
||||||
-- name: InsertAudit :exec
|
-- name: InsertAudit :exec
|
||||||
-- The generic write for tables that are not control-plane resources. Callers
|
-- The generic write for tables that are not control-plane resources. Callers
|
||||||
-- pass the table's own name as table_name; old_value is nil for a create and
|
-- pass the table's own name as table_name; old_value is nil for a create and
|
||||||
|
|||||||
@@ -208,6 +208,11 @@ FROM workspaces w WHERE w.org_id = sqlc.arg(org_id) AND w.id = sqlc.arg(workspac
|
|||||||
-- name: GetSource :one
|
-- name: GetSource :one
|
||||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
|
||||||
|
|
||||||
|
-- name: SourceWorkspace :one
|
||||||
|
-- The workspace the binding/mapping SQL derives from a source path, so an
|
||||||
|
-- audit snapshots the same value the write stores.
|
||||||
|
SELECT workspace_id FROM sources WHERE path = sqlc.arg(path) AND org_id = sqlc.arg(org_id);
|
||||||
|
|
||||||
-- name: ListSources :many
|
-- name: ListSources :many
|
||||||
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC, path;
|
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC, path;
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,14 @@ JOIN identities i ON i.id = m.identity_id
|
|||||||
WHERE m.org_id = $1
|
WHERE m.org_id = $1
|
||||||
ORDER BY m.created_at, m.identity_id, m.workspace_id;
|
ORDER BY m.created_at, m.identity_id, m.workspace_id;
|
||||||
|
|
||||||
-- name: RemoveMembership :exec
|
-- name: MembershipRole :one
|
||||||
|
-- The current role of the membership EnsureMembership/RemoveMembership targets,
|
||||||
|
-- read in the same transaction so the audit row carries both sides.
|
||||||
|
SELECT role FROM memberships
|
||||||
|
WHERE identity_id = sqlc.arg(identity_id) AND org_id = sqlc.arg(org_id)
|
||||||
|
AND COALESCE(workspace_id, '') = sqlc.arg(workspace_id)::text;
|
||||||
|
|
||||||
|
-- name: RemoveMembership :execrows
|
||||||
-- The workspace is matched through the same COALESCE the unique index uses, so
|
-- The workspace is matched through the same COALESCE the unique index uses, so
|
||||||
-- an org-wide membership (NULL) and the empty string are the same target.
|
-- an org-wide membership (NULL) and the empty string are the same target.
|
||||||
DELETE FROM memberships
|
DELETE FROM memberships
|
||||||
@@ -46,6 +53,9 @@ SELECT identity_id, org_id FROM org_agents WHERE org_id = $1 ORDER BY identity_i
|
|||||||
SELECT identity_id, org_id, workspace_id, role, created_at
|
SELECT identity_id, org_id, workspace_id, role, created_at
|
||||||
FROM memberships WHERE identity_id = $1 ORDER BY created_at, org_id;
|
FROM memberships WHERE identity_id = $1 ORDER BY created_at, org_id;
|
||||||
|
|
||||||
|
-- name: AgentAssignmentOrg :one
|
||||||
|
SELECT org_id FROM org_agents WHERE identity_id = $1;
|
||||||
|
|
||||||
-- name: UpsertAgentAssignment :exec
|
-- name: UpsertAgentAssignment :exec
|
||||||
INSERT INTO org_agents (identity_id, org_id) VALUES ($1, $2)
|
INSERT INTO org_agents (identity_id, org_id) VALUES ($1, $2)
|
||||||
ON CONFLICT (identity_id) DO UPDATE SET org_id = excluded.org_id;
|
ON CONFLICT (identity_id) DO UPDATE SET org_id = excluded.org_id;
|
||||||
|
|||||||
@@ -79,23 +79,44 @@ func (s *EDA) ListSources(ctx context.Context) ([]source.Source, error) {
|
|||||||
// SetSourceSigning moves a source from one signing state to another, and
|
// SetSourceSigning moves a source from one signing state to another, and
|
||||||
// refuses when the stored state is no longer from.
|
// refuses when the stored state is no longer from.
|
||||||
func (s *EDA) SetSourceSigning(ctx context.Context, path string, from, to source.Signing) error {
|
func (s *EDA) SetSourceSigning(ctx context.Context, path string, from, to source.Signing) error {
|
||||||
n, err := s.q.SetSourceSigning(ctx, postgresdb.SetSourceSigningParams{
|
orgID := string(org.OrgFromContext(ctx))
|
||||||
Path: path, FromSigning: string(from), ToSigning: string(to), ScopeOrg: scopeOrg(ctx),
|
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
|
q := postgresdb.New(tx)
|
||||||
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "sources", RecordKey: path}); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if from == to {
|
||||||
|
// No transition to write; still report a missing source or a
|
||||||
|
// caller whose idea of the current state is stale.
|
||||||
|
current, err := q.GetSource(ctx, postgresdb.GetSourceParams{Path: path, ScopeOrg: scopeOrg(ctx)})
|
||||||
|
switch {
|
||||||
|
case errors.Is(err, pgx.ErrNoRows):
|
||||||
|
return storecontract.ErrSourceNotFound
|
||||||
|
case err != nil:
|
||||||
|
return fmt.Errorf("edastore: set source signing: %w", err)
|
||||||
|
case current.Signing != string(to):
|
||||||
|
return storecontract.ErrSourceSigningStale
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
n, err := q.SetSourceSigning(ctx, postgresdb.SetSourceSigningParams{
|
||||||
|
Path: path, FromSigning: string(from), ToSigning: string(to), ScopeOrg: scopeOrg(ctx),
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("edastore: set source signing: %w", err)
|
||||||
|
}
|
||||||
|
if n == 1 {
|
||||||
|
return insertAuditValues(ctx, q, "sources", path, "signing", string(from), string(to), 0, orgID)
|
||||||
|
}
|
||||||
|
exists, err := q.SourceExists(ctx, path)
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("edastore: set source signing: %w", err)
|
||||||
|
}
|
||||||
|
if !exists {
|
||||||
|
return storecontract.ErrSourceNotFound
|
||||||
|
}
|
||||||
|
return storecontract.ErrSourceSigningStale
|
||||||
})
|
})
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("edastore: set source signing: %w", err)
|
|
||||||
}
|
|
||||||
if n == 1 {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
exists, err := s.q.SourceExists(ctx, path)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("edastore: set source signing: %w", err)
|
|
||||||
}
|
|
||||||
if !exists {
|
|
||||||
return storecontract.ErrSourceNotFound
|
|
||||||
}
|
|
||||||
return storecontract.ErrSourceSigningStale
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *EDA) SetSourceSecret(ctx context.Context, path, secret string) error {
|
func (s *EDA) SetSourceSecret(ctx context.Context, path, secret string) error {
|
||||||
@@ -103,57 +124,100 @@ func (s *EDA) SetSourceSecret(ctx context.Context, path, secret string) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
n, err := s.q.SetSourceSecret(ctx, postgresdb.SetSourceSecretParams{Path: path, Secret: sealed, ScopeOrg: scopeOrg(ctx)})
|
orgID := string(org.OrgFromContext(ctx))
|
||||||
if err != nil {
|
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
return fmt.Errorf("edastore: set source secret: %w", err)
|
q := postgresdb.New(tx)
|
||||||
}
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "sources", RecordKey: path}); err != nil {
|
||||||
if n == 0 {
|
return err
|
||||||
return storecontract.ErrSourceNotFound
|
}
|
||||||
}
|
n, err := q.SetSourceSecret(ctx, postgresdb.SetSourceSecretParams{Path: path, Secret: sealed, ScopeOrg: scopeOrg(ctx)})
|
||||||
return nil
|
if err != nil {
|
||||||
|
return fmt.Errorf("edastore: set source secret: %w", err)
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return storecontract.ErrSourceNotFound
|
||||||
|
}
|
||||||
|
// The value never reaches the trail: only the fact that it changed does.
|
||||||
|
changed := map[string]bool{"changed": true}
|
||||||
|
return insertAuditValues(ctx, q, "sources", path, "secret", changed, changed, 0, orgID)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetSourceName relabels a source.
|
// SetSourceName relabels a source.
|
||||||
func (s *EDA) SetSourceName(ctx context.Context, path, name string) error {
|
func (s *EDA) SetSourceName(ctx context.Context, path, name string) error {
|
||||||
n, err := s.q.SetSourceName(ctx, postgresdb.SetSourceNameParams{Path: path, Name: name, ScopeOrg: scopeOrg(ctx)})
|
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
if err != nil {
|
q := postgresdb.New(tx)
|
||||||
return fmt.Errorf("edastore: set source name: %w", err)
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "sources", RecordKey: path}); err != nil {
|
||||||
}
|
return err
|
||||||
if n == 0 {
|
}
|
||||||
return storecontract.ErrSourceNotFound
|
n, err := q.SetSourceName(ctx, postgresdb.SetSourceNameParams{Path: path, Name: name, ScopeOrg: scopeOrg(ctx)})
|
||||||
}
|
if err != nil {
|
||||||
return nil
|
return fmt.Errorf("edastore: set source name: %w", err)
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return storecontract.ErrSourceNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetSourceDeliveryHeader sets the header that names a source's deliveries.
|
// SetSourceDeliveryHeader sets the header that names a source's deliveries.
|
||||||
func (s *EDA) SetSourceDeliveryHeader(ctx context.Context, path, header string) error {
|
func (s *EDA) SetSourceDeliveryHeader(ctx context.Context, path, header string) error {
|
||||||
n, err := s.q.SetSourceDeliveryHeader(ctx, postgresdb.SetSourceDeliveryHeaderParams{Path: path, DeliveryHeader: header, ScopeOrg: scopeOrg(ctx)})
|
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
if err != nil {
|
q := postgresdb.New(tx)
|
||||||
return fmt.Errorf("edastore: set source delivery header: %w", err)
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "sources", RecordKey: path}); err != nil {
|
||||||
}
|
return err
|
||||||
if n == 0 {
|
}
|
||||||
return storecontract.ErrSourceNotFound
|
n, err := q.SetSourceDeliveryHeader(ctx, postgresdb.SetSourceDeliveryHeaderParams{Path: path, DeliveryHeader: header, ScopeOrg: scopeOrg(ctx)})
|
||||||
}
|
if err != nil {
|
||||||
return nil
|
return fmt.Errorf("edastore: set source delivery header: %w", err)
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
return storecontract.ErrSourceNotFound
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteSource deletes a source no armed binding fires on.
|
// DeleteSource deletes a source no armed binding fires on.
|
||||||
func (s *EDA) DeleteSource(ctx context.Context, path string) error {
|
func (s *EDA) DeleteSource(ctx context.Context, path string) error {
|
||||||
n, err := s.q.DeleteUnboundSource(ctx, postgresdb.DeleteUnboundSourceParams{Path: path, ScopeOrg: scopeOrg(ctx)})
|
return pgx.BeginFunc(ctx, s.pool, func(tx pgx.Tx) error {
|
||||||
if err != nil {
|
q := postgresdb.New(tx)
|
||||||
return fmt.Errorf("edastore: delete source: %w", err)
|
if err := q.LockAuditRecord(ctx, postgresdb.LockAuditRecordParams{TableName: "sources", RecordKey: path}); err != nil {
|
||||||
}
|
return err
|
||||||
if n > 0 {
|
}
|
||||||
return nil
|
// The read is scoped like the delete: another org's source is absent,
|
||||||
}
|
// and its secret never leaves the database.
|
||||||
exists, err := s.q.SourceExists(ctx, path)
|
params := postgresdb.GetSourceParams{Path: path, ScopeOrg: scopeOrg(ctx)}
|
||||||
if err != nil {
|
old, err := q.GetSource(ctx, params)
|
||||||
return fmt.Errorf("edastore: delete source: %w", err)
|
if errors.Is(err, pgx.ErrNoRows) {
|
||||||
}
|
return storecontract.ErrSourceNotFound
|
||||||
if exists {
|
}
|
||||||
return storecontract.ErrSourceInUse
|
if err != nil {
|
||||||
}
|
return fmt.Errorf("edastore: delete source: %w", err)
|
||||||
return storecontract.ErrSourceNotFound
|
}
|
||||||
|
n, err := q.DeleteUnboundSource(ctx, postgresdb.DeleteUnboundSourceParams{Path: path, ScopeOrg: scopeOrg(ctx)})
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("edastore: delete source: %w", err)
|
||||||
|
}
|
||||||
|
if n == 0 {
|
||||||
|
// It was there a moment ago: gone means someone else removed it,
|
||||||
|
// otherwise an armed binding holds it in place.
|
||||||
|
switch _, err := q.GetSource(ctx, params); {
|
||||||
|
case errors.Is(err, pgx.ErrNoRows):
|
||||||
|
return storecontract.ErrSourceNotFound
|
||||||
|
case err != nil:
|
||||||
|
return fmt.Errorf("edastore: delete source: %w", err)
|
||||||
|
default:
|
||||||
|
return storecontract.ErrSourceInUse
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return insertAuditValues(ctx, q, "sources", path, "source",
|
||||||
|
map[string]string{
|
||||||
|
"path": old.Path, "name": old.Name, "signing": old.Signing,
|
||||||
|
"delivery_header": old.DeliveryHeader, "org_id": old.OrgID, "workspace_id": old.WorkspaceID,
|
||||||
|
}, nil, 0, old.OrgID)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// sealSecret encrypts a non-empty secret when a cipher is configured.
|
// sealSecret encrypts a non-empty secret when a cipher is configured.
|
||||||
|
|||||||
Reference in new issue
Block a user