fix(postgres): event sources, types, mappings and bindings stay in their org

This commit is contained in:
sam committed 2026-10-10 20:14:23 +11:00
1 parent 8fb1ccf2c9
commit 9e9506fa7b
9 files changed
+405 -151

No files matched your search

+15
View File
@@ -22,3 +22,18 @@ func OrgFromContext(ctx context.Context) OrgID {
}
return DefaultOrgID
}
type scopeContextKey struct{}
// WithScope confines a request's reads to one org. A request without a scope
// is an internal service acting across orgs, such as the daemon running every
// org's work or webhook intake resolving a source by its path.
func WithScope(ctx context.Context, id OrgID) context.Context {
return context.WithValue(ctx, scopeContextKey{}, id)
}
// Scope returns the org WithScope confined ctx to.
func Scope(ctx context.Context) (OrgID, bool) {
id, ok := ctx.Value(scopeContextKey{}).(OrgID)
return id, ok && id != ""
}
@@ -0,0 +1,106 @@
package postgres_test
import (
"context"
"testing"
"github.com/samcharles93/archie-core/internal/domain/binding"
"github.com/samcharles93/archie-core/internal/domain/eventtype"
"github.com/samcharles93/archie-core/internal/domain/mapping"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/source"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
)
// An org's sources, event types, mappings and bindings are invisible to a
// request scoped to another org, which can neither change nor delete them.
// An unscoped internal service still sees them.
func TestEDARecordsStayInTheirOrg(t *testing.T) {
scoped := func(id org.OrgID) context.Context {
return org.WithScope(org.WithOrg(t.Context(), id), id)
}
acme := scoped("acme")
eda := postgres.NewEDA(pgstore.Pool(t), nil)
if err := eda.InsertSource(acme, source.Source{Path: "acme-hook", Signing: source.SigningUnsigned}); err != nil {
t.Fatal(err)
}
typeID, err := eda.InsertEventType(acme, eventtype.EventType{Source: "acme-hook", Name: "push"})
if err != nil {
t.Fatal(err)
}
mappingID, err := eda.InsertMapping(acme, mapping.Mapping{Name: "push", EventTypeID: typeID})
if err != nil {
t.Fatal(err)
}
bindingID, err := eda.InsertBinding(acme, binding.Binding{Name: "deploy", Matcher: binding.Matcher{Source: "acme-hook"}, MappingID: mappingID, Workflow: "deploy"})
if err != nil {
t.Fatal(err)
}
for _, tt := range []struct {
name string
ctx func() context.Context //nolint:containedctx // table input, not stored state
sees bool
}{
{"the owning org", func() context.Context { return acme }, true},
{"another org", func() context.Context { return scoped("other") }, false},
{"an internal service", t.Context, true},
} {
t.Run(tt.name, func(t *testing.T) {
ctx := tt.ctx()
sources, err := eda.ListSources(ctx)
if err != nil {
t.Fatal(err)
}
src, err := eda.GetSource(ctx, "acme-hook")
if err != nil {
t.Fatal(err)
}
types, err := eda.ListEventTypes(ctx)
if err != nil {
t.Fatal(err)
}
mappings, err := eda.ListMappings(ctx)
if err != nil {
t.Fatal(err)
}
m, err := eda.GetMapping(ctx, mappingID)
if err != nil {
t.Fatal(err)
}
bindings, err := eda.ListBindings(ctx)
if err != nil {
t.Fatal(err)
}
b, err := eda.GetBinding(ctx, bindingID)
if err != nil {
t.Fatal(err)
}
seen := []bool{len(sources) == 1, src != nil, len(types) == 1, len(mappings) == 1, m != nil, len(bindings) == 1, b != nil}
for i, s := range seen {
if s != tt.sees {
t.Fatalf("read %d visible = %v, want %v", i, s, tt.sees)
}
}
if tt.sees {
return
}
if err := eda.ApproveBinding(ctx, bindingID); err == nil {
t.Fatal("approved another org's binding")
}
if err := eda.SetSourceSecret(ctx, "acme-hook", "stolen"); err == nil {
t.Fatal("rotated another org's source secret")
}
if err := eda.DeleteBinding(ctx, bindingID); err == nil {
t.Fatal("deleted another org's binding")
}
if err := eda.DeleteMapping(ctx, mappingID); err == nil {
t.Fatal("deleted another org's mapping")
}
if err := eda.DeleteEventType(ctx, typeID); err == nil {
t.Fatal("deleted another org's event type")
}
})
}
}
+16 -12
View File
@@ -225,6 +225,7 @@ func (s *EDA) InsertMapping(ctx context.Context, m mapping.Mapping) (string, err
}
id := newRecordID()
if err := s.q.InsertMapping(ctx, postgresdb.InsertMappingParams{
OrgID: string(org.OrgFromContext(ctx)),
ID: id,
Name: m.Name,
SourceHint: m.SourceHint,
@@ -240,7 +241,7 @@ func (s *EDA) InsertMapping(ctx context.Context, m mapping.Mapping) (string, err
// GetMapping returns (nil, nil) for an absent mapping, the found=false
// convention the gRPC layer translates.
func (s *EDA) GetMapping(ctx context.Context, id string) (*mapping.Mapping, error) {
r, err := s.q.GetMapping(ctx, id)
r, err := s.q.GetMapping(ctx, postgresdb.GetMappingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
@@ -255,7 +256,7 @@ func (s *EDA) GetMapping(ctx context.Context, id string) (*mapping.Mapping, erro
}
func (s *EDA) ListMappings(ctx context.Context) ([]mapping.Mapping, error) {
rows, err := s.q.ListMappings(ctx)
rows, err := s.q.ListMappings(ctx, scopeOrg(ctx))
if err != nil {
return nil, fmt.Errorf("edastore: list mappings: %w", err)
}
@@ -276,6 +277,7 @@ func (s *EDA) UpdateMapping(ctx context.Context, m mapping.Mapping) error {
return err
}
n, err := s.q.UpdateMapping(ctx, postgresdb.UpdateMappingParams{
ScopeOrg: scopeOrg(ctx),
ID: m.ID,
Name: m.Name,
SourceHint: m.SourceHint,
@@ -293,7 +295,7 @@ func (s *EDA) UpdateMapping(ctx context.Context, m mapping.Mapping) error {
}
func (s *EDA) DeleteMapping(ctx context.Context, id string) error {
n, err := s.q.DeleteMapping(ctx, id)
n, err := s.q.DeleteMapping(ctx, postgresdb.DeleteMappingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return err
}
@@ -363,6 +365,7 @@ func (s *EDA) InsertBinding(ctx context.Context, b binding.Binding) (string, err
return "", err
}
err = s.q.InsertBinding(ctx, postgresdb.InsertBindingParams{
OrgID: string(org.OrgFromContext(ctx)),
ID: id,
Name: b.Name,
Source: b.Matcher.Source,
@@ -385,7 +388,7 @@ func (s *EDA) InsertBinding(ctx context.Context, b binding.Binding) (string, err
// GetBinding returns (nil, nil) for an absent binding, the found=false
// convention the gRPC layer translates.
func (s *EDA) GetBinding(ctx context.Context, id string) (*binding.Binding, error) {
r, err := s.q.GetBinding(ctx, id)
r, err := s.q.GetBinding(ctx, postgresdb.GetBindingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
@@ -400,7 +403,7 @@ func (s *EDA) GetBinding(ctx context.Context, id string) (*binding.Binding, erro
}
func (s *EDA) ListBindings(ctx context.Context) ([]binding.Binding, error) {
rows, err := s.q.ListBindings(ctx)
rows, err := s.q.ListBindings(ctx, scopeOrg(ctx))
if err != nil {
return nil, fmt.Errorf("edastore: list bindings: %w", err)
}
@@ -417,7 +420,7 @@ func (s *EDA) ListBindings(ctx context.Context) ([]binding.Binding, error) {
// ArmedBindingsForSource returns the armed bindings a source can dispatch.
func (s *EDA) ArmedBindingsForSource(ctx context.Context, source string) ([]binding.Binding, error) {
rows, err := s.q.ArmedBindingsForSource(ctx, source)
rows, err := s.q.ArmedBindingsForSource(ctx, postgresdb.ArmedBindingsForSourceParams{Source: source, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return nil, fmt.Errorf("edastore: list armed bindings: %w", err)
}
@@ -440,6 +443,7 @@ func (s *EDA) UpdateBinding(ctx context.Context, b binding.Binding) error {
return err
}
n, err := s.q.UpdateBinding(ctx, postgresdb.UpdateBindingParams{
ScopeOrg: scopeOrg(ctx),
ID: b.ID,
Name: b.Name,
Source: b.Matcher.Source,
@@ -463,7 +467,7 @@ func (s *EDA) UpdateBinding(ctx context.Context, b binding.Binding) error {
}
func (s *EDA) DeleteBinding(ctx context.Context, id string) error {
n, err := s.q.DeleteBinding(ctx, id)
n, err := s.q.DeleteBinding(ctx, postgresdb.DeleteBindingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return err
}
@@ -476,7 +480,7 @@ func (s *EDA) DeleteBinding(ctx context.Context, id string) error {
// ApproveBinding is the only transition that arms a binding.
func (s *EDA) ApproveBinding(ctx context.Context, id string) error {
r, err := s.q.GetBinding(ctx, id)
r, err := s.q.GetBinding(ctx, postgresdb.GetBindingParams{ID: id, ScopeOrg: scopeOrg(ctx)})
if errors.Is(err, pgx.ErrNoRows) {
return storecontract.ErrBindingNotFound
}
@@ -486,7 +490,7 @@ func (s *EDA) ApproveBinding(ctx context.Context, id string) error {
if r.Status != string(binding.StatusPendingApproval) {
return storecontract.ErrBindingTransition
}
n, err := s.q.SetBindingArmed(ctx, id)
n, err := s.q.SetBindingArmed(ctx, postgresdb.SetBindingArmedParams{ID: id, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return err
}
@@ -510,13 +514,13 @@ 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 {
n, err := s.q.TransitionBindingStatus(ctx, postgresdb.TransitionBindingStatusParams{
ID: id, FromStatus: string(from), ToStatus: string(to),
ID: id, FromStatus: string(from), ToStatus: string(to), ScopeOrg: scopeOrg(ctx),
})
if err != nil {
return err
}
if n == 0 {
if _, err := s.q.GetBinding(ctx, id); errors.Is(err, pgx.ErrNoRows) {
if _, err := s.q.GetBinding(ctx, postgresdb.GetBindingParams{ID: id, ScopeOrg: scopeOrg(ctx)}); errors.Is(err, pgx.ErrNoRows) {
return storecontract.ErrBindingNotFound
}
return storecontract.ErrBindingTransition
@@ -685,7 +689,7 @@ func (s *EDA) RecordCaptureRefusal(ctx context.Context, source, addr string, at
// CaptureRefusals summarises refusals since a time for every source that had
// one, beside the captures it accepted in the same window.
func (s *EDA) CaptureRefusals(ctx context.Context, since time.Time) ([]storecontract.CaptureRefusals, error) {
rows, err := s.q.CaptureRefusalSummary(ctx, since.UTC())
rows, err := s.q.CaptureRefusalSummary(ctx, postgresdb.CaptureRefusalSummaryParams{Since: since.UTC(), ScopeOrg: scopeOrg(ctx)})
if err != nil {
return nil, fmt.Errorf("store: capture refusals: %w", err)
}
@@ -9,6 +9,7 @@ import (
"github.com/jackc/pgx/v5"
"github.com/samcharles93/archie-core/internal/domain/eventtype"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/storecontract"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
)
@@ -101,6 +102,7 @@ func (s *EDA) InsertEventType(ctx context.Context, t eventtype.EventType) (strin
err = s.saveEventType(ctx, t, func(q *postgresdb.Queries, rule string) error {
return q.InsertEventType(ctx, postgresdb.InsertEventTypeParams{
ID: id, Source: t.Source, Name: t.Name, Rule: rule, Schema: string(schema),
OrgID: string(org.OrgFromContext(ctx)),
})
})
if err != nil {
@@ -112,7 +114,7 @@ func (s *EDA) InsertEventType(ctx context.Context, t eventtype.EventType) (strin
// UpdateEventType rewrites a type's name and rule. Its source and schema are
// the stored ones.
func (s *EDA) UpdateEventType(ctx context.Context, t eventtype.EventType) error {
stored, err := s.q.GetEventType(ctx, t.ID)
stored, err := s.q.GetEventType(ctx, postgresdb.GetEventTypeParams{ID: t.ID, ScopeOrg: scopeOrg(ctx)})
if errors.Is(err, pgx.ErrNoRows) {
return storecontract.ErrEventTypeNotFound
}
@@ -132,7 +134,7 @@ func (s *EDA) UpdateEventType(ctx context.Context, t eventtype.EventType) error
// DeleteEventType removes a type. Captures already identified as it keep the
// ID they were stored with.
func (s *EDA) DeleteEventType(ctx context.Context, id string) error {
n, err := s.q.DeleteEventType(ctx, id)
n, err := s.q.DeleteEventType(ctx, postgresdb.DeleteEventTypeParams{ID: id, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return err
}
@@ -144,7 +146,7 @@ func (s *EDA) DeleteEventType(ctx context.Context, id string) error {
// ListEventTypes returns every event type, by source then name.
func (s *EDA) ListEventTypes(ctx context.Context) ([]eventtype.EventType, error) {
rows, err := s.q.ListEventTypes(ctx)
rows, err := s.q.ListEventTypes(ctx, scopeOrg(ctx))
if err != nil {
return nil, fmt.Errorf("edastore: list event types: %w", err)
}
@@ -14,9 +14,14 @@ import (
const armedBindingsForSource = `-- name: ArmedBindingsForSource :many
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
FROM bindings WHERE source = $1 AND status = 'armed' ORDER BY created_at DESC
FROM bindings WHERE source = $1 AND status = 'armed' AND ($2::text IS NULL OR org_id = $2) ORDER BY created_at DESC
`
type ArmedBindingsForSourceParams struct {
Source string
ScopeOrg pgtype.Text
}
type ArmedBindingsForSourceRow struct {
ID string
Name string
@@ -35,8 +40,8 @@ type ArmedBindingsForSourceRow struct {
OrgID string
}
func (q *Queries) ArmedBindingsForSource(ctx context.Context, source string) ([]ArmedBindingsForSourceRow, error) {
rows, err := q.db.Query(ctx, armedBindingsForSource, source)
func (q *Queries) ArmedBindingsForSource(ctx context.Context, arg ArmedBindingsForSourceParams) ([]ArmedBindingsForSourceRow, error) {
rows, err := q.db.Query(ctx, armedBindingsForSource, arg.Source, arg.ScopeOrg)
if err != nil {
return nil, err
}
@@ -74,7 +79,10 @@ func (q *Queries) ArmedBindingsForSource(ctx context.Context, source string) ([]
const captureRefusalSummary = `-- name: CaptureRefusalSummary :many
WITH r AS (
SELECT source, count(*) AS refused, array_agg(DISTINCT addr ORDER BY addr)::text[] AS addrs
FROM capture_refusals WHERE refused_at >= $1 GROUP BY source
FROM capture_refusals cr WHERE refused_at >= $1
AND ($2::text IS NULL
OR EXISTS (SELECT 1 FROM sources s WHERE s.path = cr.source AND s.org_id = $2))
GROUP BY source
), a AS (
SELECT source, count(*) AS accepted FROM captures WHERE received_at >= $1 GROUP BY source
)
@@ -83,6 +91,11 @@ FROM r LEFT JOIN a ON a.source = r.source
ORDER BY r.source
`
type CaptureRefusalSummaryParams struct {
Since time.Time
ScopeOrg pgtype.Text
}
type CaptureRefusalSummaryRow struct {
Source string
Refused int64
@@ -90,8 +103,8 @@ type CaptureRefusalSummaryRow struct {
Addrs []string
}
func (q *Queries) CaptureRefusalSummary(ctx context.Context, refusedAt time.Time) ([]CaptureRefusalSummaryRow, error) {
rows, err := q.db.Query(ctx, captureRefusalSummary, refusedAt)
func (q *Queries) CaptureRefusalSummary(ctx context.Context, arg CaptureRefusalSummaryParams) ([]CaptureRefusalSummaryRow, error) {
rows, err := q.db.Query(ctx, captureRefusalSummary, arg.Since, arg.ScopeOrg)
if err != nil {
return nil, err
}
@@ -116,11 +129,16 @@ func (q *Queries) CaptureRefusalSummary(ctx context.Context, refusedAt time.Time
}
const deleteBinding = `-- name: DeleteBinding :execrows
DELETE FROM bindings WHERE id = $1
DELETE FROM bindings WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
`
func (q *Queries) DeleteBinding(ctx context.Context, id string) (int64, error) {
result, err := q.db.Exec(ctx, deleteBinding, id)
type DeleteBindingParams struct {
ID string
ScopeOrg pgtype.Text
}
func (q *Queries) DeleteBinding(ctx context.Context, arg DeleteBindingParams) (int64, error) {
result, err := q.db.Exec(ctx, deleteBinding, arg.ID, arg.ScopeOrg)
if err != nil {
return 0, err
}
@@ -169,11 +187,16 @@ func (q *Queries) DeleteCapturesOlderThan(ctx context.Context, receivedAt time.T
}
const deleteEventType = `-- name: DeleteEventType :execrows
DELETE FROM event_types WHERE id = $1
DELETE FROM event_types WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
`
func (q *Queries) DeleteEventType(ctx context.Context, id string) (int64, error) {
result, err := q.db.Exec(ctx, deleteEventType, id)
type DeleteEventTypeParams struct {
ID string
ScopeOrg pgtype.Text
}
func (q *Queries) DeleteEventType(ctx context.Context, arg DeleteEventTypeParams) (int64, error) {
result, err := q.db.Exec(ctx, deleteEventType, arg.ID, arg.ScopeOrg)
if err != nil {
return 0, err
}
@@ -181,11 +204,16 @@ func (q *Queries) DeleteEventType(ctx context.Context, id string) (int64, error)
}
const deleteMapping = `-- name: DeleteMapping :execrows
DELETE FROM mappings WHERE id = $1
DELETE FROM mappings WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
`
func (q *Queries) DeleteMapping(ctx context.Context, id string) (int64, error) {
result, err := q.db.Exec(ctx, deleteMapping, id)
type DeleteMappingParams struct {
ID string
ScopeOrg pgtype.Text
}
func (q *Queries) DeleteMapping(ctx context.Context, arg DeleteMappingParams) (int64, error) {
result, err := q.db.Exec(ctx, deleteMapping, arg.ID, arg.ScopeOrg)
if err != nil {
return 0, err
}
@@ -202,14 +230,19 @@ func (q *Queries) DeletePlaybookDispatches(ctx context.Context, playbookID strin
}
const deleteUnboundSource = `-- name: DeleteUnboundSource :execrows
DELETE FROM sources s WHERE s.path = $1
DELETE FROM sources s WHERE s.path = $1 AND ($2::text IS NULL OR s.org_id = $2)
AND NOT EXISTS (SELECT 1 FROM bindings b WHERE b.source = s.path AND b.status = 'armed')
`
type DeleteUnboundSourceParams struct {
Path string
ScopeOrg pgtype.Text
}
// A source an armed binding fires on is not deleted: the binding would
// silently stop receiving events.
func (q *Queries) DeleteUnboundSource(ctx context.Context, path string) (int64, error) {
result, err := q.db.Exec(ctx, deleteUnboundSource, path)
func (q *Queries) DeleteUnboundSource(ctx context.Context, arg DeleteUnboundSourceParams) (int64, error) {
result, err := q.db.Exec(ctx, deleteUnboundSource, arg.Path, arg.ScopeOrg)
if err != nil {
return 0, err
}
@@ -262,9 +295,14 @@ func (q *Queries) EventTypesForSource(ctx context.Context, source string) ([]Eve
const getBinding = `-- name: GetBinding :one
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
FROM bindings WHERE id = $1
FROM bindings WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
`
type GetBindingParams struct {
ID string
ScopeOrg pgtype.Text
}
type GetBindingRow struct {
ID string
Name string
@@ -283,8 +321,8 @@ type GetBindingRow struct {
OrgID string
}
func (q *Queries) GetBinding(ctx context.Context, id string) (GetBindingRow, error) {
row := q.db.QueryRow(ctx, getBinding, id)
func (q *Queries) GetBinding(ctx context.Context, arg GetBindingParams) (GetBindingRow, error) {
row := q.db.QueryRow(ctx, getBinding, arg.ID, arg.ScopeOrg)
var i GetBindingRow
err := row.Scan(
&i.ID,
@@ -308,9 +346,14 @@ func (q *Queries) GetBinding(ctx context.Context, id string) (GetBindingRow, err
const getEventType = `-- name: GetEventType :one
SELECT id, source, name, rule, schema, created_at, updated_at
FROM event_types WHERE id = $1
FROM event_types WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
`
type GetEventTypeParams struct {
ID string
ScopeOrg pgtype.Text
}
type GetEventTypeRow struct {
ID string
Source string
@@ -321,8 +364,8 @@ type GetEventTypeRow struct {
UpdatedAt time.Time
}
func (q *Queries) GetEventType(ctx context.Context, id string) (GetEventTypeRow, error) {
row := q.db.QueryRow(ctx, getEventType, id)
func (q *Queries) GetEventType(ctx context.Context, arg GetEventTypeParams) (GetEventTypeRow, error) {
row := q.db.QueryRow(ctx, getEventType, arg.ID, arg.ScopeOrg)
var i GetEventTypeRow
err := row.Scan(
&i.ID,
@@ -340,17 +383,22 @@ const getMapping = `-- name: GetMapping :one
SELECT mappings.id, mappings.name, mappings.source_hint, mappings.fields, mappings.created_at, mappings.updated_at, mappings.event_type, mappings.org_id, mappings.workspace_id,
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE id = $1
FROM mappings WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
`
type GetMappingParams struct {
ID string
ScopeOrg pgtype.Text
}
type GetMappingRow struct {
Mapping Mapping
MatchCount int64
LastMatchedAt time.Time
}
func (q *Queries) GetMapping(ctx context.Context, id string) (GetMappingRow, error) {
row := q.db.QueryRow(ctx, getMapping, id)
func (q *Queries) GetMapping(ctx context.Context, arg GetMappingParams) (GetMappingRow, error) {
row := q.db.QueryRow(ctx, getMapping, arg.ID, arg.ScopeOrg)
var i GetMappingRow
err := row.Scan(
&i.Mapping.ID,
@@ -369,11 +417,16 @@ func (q *Queries) GetMapping(ctx context.Context, id string) (GetMappingRow, err
}
const getSource = `-- name: GetSource :one
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE path = $1
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE path = $1 AND ($2::text IS NULL OR org_id = $2)
`
func (q *Queries) GetSource(ctx context.Context, path string) (Source, error) {
row := q.db.QueryRow(ctx, getSource, path)
type GetSourceParams struct {
Path string
ScopeOrg pgtype.Text
}
func (q *Queries) GetSource(ctx context.Context, arg GetSourceParams) (Source, error) {
row := q.db.QueryRow(ctx, getSource, arg.Path, arg.ScopeOrg)
var i Source
err := row.Scan(
&i.Path,
@@ -390,8 +443,8 @@ func (q *Queries) GetSource(ctx context.Context, path string) (Source, error) {
}
const insertBinding = `-- name: InsertBinding :exec
INSERT INTO bindings (id, name, source, mapping, filter, workflow, owner, repo, version, status, inputs, repo_param)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 1, $9, $10, $11)
INSERT INTO bindings (id, name, source, mapping, filter, workflow, owner, repo, version, status, inputs, repo_param, org_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 1, $9, $10, $11, $12)
`
type InsertBindingParams struct {
@@ -406,6 +459,7 @@ type InsertBindingParams struct {
Status string
Inputs string
RepoParam string
OrgID string
}
func (q *Queries) InsertBinding(ctx context.Context, arg InsertBindingParams) error {
@@ -421,6 +475,7 @@ func (q *Queries) InsertBinding(ctx context.Context, arg InsertBindingParams) er
arg.Status,
arg.Inputs,
arg.RepoParam,
arg.OrgID,
)
return err
}
@@ -513,8 +568,8 @@ func (q *Queries) InsertCaptureRefusal(ctx context.Context, arg InsertCaptureRef
}
const insertEventType = `-- name: InsertEventType :exec
INSERT INTO event_types (id, source, name, rule, schema)
VALUES ($1, $2, $3, $4, $5)
INSERT INTO event_types (id, source, name, rule, schema, org_id)
VALUES ($1, $2, $3, $4, $5, $6)
`
type InsertEventTypeParams struct {
@@ -523,6 +578,7 @@ type InsertEventTypeParams struct {
Name string
Rule string
Schema string
OrgID string
}
func (q *Queries) InsertEventType(ctx context.Context, arg InsertEventTypeParams) error {
@@ -532,13 +588,14 @@ func (q *Queries) InsertEventType(ctx context.Context, arg InsertEventTypeParams
arg.Name,
arg.Rule,
arg.Schema,
arg.OrgID,
)
return err
}
const insertMapping = `-- name: InsertMapping :exec
INSERT INTO mappings (id, name, source_hint, event_type, fields)
VALUES ($1, $2, $3, $4, $5)
INSERT INTO mappings (id, name, source_hint, event_type, fields, org_id)
VALUES ($1, $2, $3, $4, $5, $6)
`
type InsertMappingParams struct {
@@ -547,6 +604,7 @@ type InsertMappingParams struct {
SourceHint string
EventType string
Fields string
OrgID string
}
func (q *Queries) InsertMapping(ctx context.Context, arg InsertMappingParams) error {
@@ -556,6 +614,7 @@ func (q *Queries) InsertMapping(ctx context.Context, arg InsertMappingParams) er
arg.SourceHint,
arg.EventType,
arg.Fields,
arg.OrgID,
)
return err
}
@@ -649,16 +708,18 @@ func (q *Queries) InsertToolCall(ctx context.Context, arg InsertToolCallParams)
}
const listBindingDispatches = `-- name: ListBindingDispatches :many
SELECT binding, binding_version, capture, task_id, reason, dispatched_at
FROM binding_dispatches
WHERE ($1::text IS NULL OR binding = $1)
AND ($2::text IS NULL OR capture = $2)
AND ($3::bigint IS NULL OR task_id = $3)
ORDER BY dispatched_at DESC
LIMIT $4
SELECT d.binding, d.binding_version, d.capture, d.task_id, d.reason, d.dispatched_at
FROM binding_dispatches d JOIN bindings b ON b.id = d.binding
WHERE ($1::text IS NULL OR b.org_id = $1)
AND ($2::text IS NULL OR d.binding = $2)
AND ($3::text IS NULL OR d.capture = $3)
AND ($4::bigint IS NULL OR d.task_id = $4)
ORDER BY d.dispatched_at DESC
LIMIT $5
`
type ListBindingDispatchesParams struct {
ScopeOrg pgtype.Text
Binding pgtype.Text
Capture pgtype.Text
TaskID pgtype.Int8
@@ -676,6 +737,7 @@ type ListBindingDispatchesRow struct {
func (q *Queries) ListBindingDispatches(ctx context.Context, arg ListBindingDispatchesParams) ([]ListBindingDispatchesRow, error) {
rows, err := q.db.Query(ctx, listBindingDispatches,
arg.ScopeOrg,
arg.Binding,
arg.Capture,
arg.TaskID,
@@ -708,7 +770,7 @@ func (q *Queries) ListBindingDispatches(ctx context.Context, arg ListBindingDisp
const listBindings = `-- name: ListBindings :many
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
FROM bindings ORDER BY created_at DESC
FROM bindings WHERE ($1::text IS NULL OR org_id = $1) ORDER BY created_at DESC
`
type ListBindingsRow struct {
@@ -729,8 +791,8 @@ type ListBindingsRow struct {
OrgID string
}
func (q *Queries) ListBindings(ctx context.Context) ([]ListBindingsRow, error) {
rows, err := q.db.Query(ctx, listBindings)
func (q *Queries) ListBindings(ctx context.Context, scopeOrg pgtype.Text) ([]ListBindingsRow, error) {
rows, err := q.db.Query(ctx, listBindings, scopeOrg)
if err != nil {
return nil, err
}
@@ -814,11 +876,11 @@ func (q *Queries) ListCaptures(ctx context.Context, arg ListCapturesParams) ([]C
const listEventTypes = `-- name: ListEventTypes :many
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
FROM event_types ORDER BY source, name
FROM event_types WHERE ($1::text IS NULL OR org_id = $1) ORDER BY source, name
`
func (q *Queries) ListEventTypes(ctx context.Context) ([]EventType, error) {
rows, err := q.db.Query(ctx, listEventTypes)
func (q *Queries) ListEventTypes(ctx context.Context, scopeOrg pgtype.Text) ([]EventType, error) {
rows, err := q.db.Query(ctx, listEventTypes, scopeOrg)
if err != nil {
return nil, err
}
@@ -851,7 +913,7 @@ const listMappings = `-- name: ListMappings :many
SELECT mappings.id, mappings.name, mappings.source_hint, mappings.fields, mappings.created_at, mappings.updated_at, mappings.event_type, mappings.org_id, mappings.workspace_id,
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
FROM mappings ORDER BY created_at DESC
FROM mappings WHERE ($1::text IS NULL OR org_id = $1) ORDER BY created_at DESC
`
type ListMappingsRow struct {
@@ -860,8 +922,8 @@ type ListMappingsRow struct {
LastMatchedAt time.Time
}
func (q *Queries) ListMappings(ctx context.Context) ([]ListMappingsRow, error) {
rows, err := q.db.Query(ctx, listMappings)
func (q *Queries) ListMappings(ctx context.Context, scopeOrg pgtype.Text) ([]ListMappingsRow, error) {
rows, err := q.db.Query(ctx, listMappings, scopeOrg)
if err != nil {
return nil, err
}
@@ -893,11 +955,11 @@ func (q *Queries) ListMappings(ctx context.Context) ([]ListMappingsRow, error) {
}
const listSources = `-- name: ListSources :many
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources ORDER BY created_at DESC, path
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE ($1::text IS NULL OR org_id = $1) ORDER BY created_at DESC, path
`
func (q *Queries) ListSources(ctx context.Context) ([]Source, error) {
rows, err := q.db.Query(ctx, listSources)
func (q *Queries) ListSources(ctx context.Context, scopeOrg pgtype.Text) ([]Source, error) {
rows, err := q.db.Query(ctx, listSources, scopeOrg)
if err != nil {
return nil, err
}
@@ -992,11 +1054,16 @@ func (q *Queries) LockEventTypeSource(ctx context.Context, source string) error
}
const setBindingArmed = `-- name: SetBindingArmed :execrows
UPDATE bindings SET status = 'armed', updated_at = now() WHERE id = $1
UPDATE bindings SET status = 'armed', updated_at = now() WHERE id = $1 AND ($2::text IS NULL OR org_id = $2)
`
func (q *Queries) SetBindingArmed(ctx context.Context, id string) (int64, error) {
result, err := q.db.Exec(ctx, setBindingArmed, id)
type SetBindingArmedParams struct {
ID string
ScopeOrg pgtype.Text
}
func (q *Queries) SetBindingArmed(ctx context.Context, arg SetBindingArmedParams) (int64, error) {
result, err := q.db.Exec(ctx, setBindingArmed, arg.ID, arg.ScopeOrg)
if err != nil {
return 0, err
}
@@ -1019,16 +1086,17 @@ func (q *Queries) SetBindingDispatchTask(ctx context.Context, arg SetBindingDisp
}
const setSourceDeliveryHeader = `-- name: SetSourceDeliveryHeader :execrows
UPDATE sources SET delivery_header = $2, updated_at = now() WHERE path = $1
UPDATE sources SET delivery_header = $1, updated_at = now() WHERE path = $2 AND ($3::text IS NULL OR org_id = $3)
`
type SetSourceDeliveryHeaderParams struct {
Path string
DeliveryHeader string
Path string
ScopeOrg pgtype.Text
}
func (q *Queries) SetSourceDeliveryHeader(ctx context.Context, arg SetSourceDeliveryHeaderParams) (int64, error) {
result, err := q.db.Exec(ctx, setSourceDeliveryHeader, arg.Path, arg.DeliveryHeader)
result, err := q.db.Exec(ctx, setSourceDeliveryHeader, arg.DeliveryHeader, arg.Path, arg.ScopeOrg)
if err != nil {
return 0, err
}
@@ -1036,16 +1104,17 @@ func (q *Queries) SetSourceDeliveryHeader(ctx context.Context, arg SetSourceDeli
}
const setSourceName = `-- name: SetSourceName :execrows
UPDATE sources SET name = $2, updated_at = now() WHERE path = $1
UPDATE sources SET name = $1, updated_at = now() WHERE path = $2 AND ($3::text IS NULL OR org_id = $3)
`
type SetSourceNameParams struct {
Path string
Name string
Name string
Path string
ScopeOrg pgtype.Text
}
func (q *Queries) SetSourceName(ctx context.Context, arg SetSourceNameParams) (int64, error) {
result, err := q.db.Exec(ctx, setSourceName, arg.Path, arg.Name)
result, err := q.db.Exec(ctx, setSourceName, arg.Name, arg.Path, arg.ScopeOrg)
if err != nil {
return 0, err
}
@@ -1053,16 +1122,17 @@ func (q *Queries) SetSourceName(ctx context.Context, arg SetSourceNameParams) (i
}
const setSourceSecret = `-- name: SetSourceSecret :execrows
UPDATE sources SET secret = $2, updated_at = now() WHERE path = $1
UPDATE sources SET secret = $1, updated_at = now() WHERE path = $2 AND ($3::text IS NULL OR org_id = $3)
`
type SetSourceSecretParams struct {
Path string
Secret string
Secret string
Path string
ScopeOrg pgtype.Text
}
func (q *Queries) SetSourceSecret(ctx context.Context, arg SetSourceSecretParams) (int64, error) {
result, err := q.db.Exec(ctx, setSourceSecret, arg.Path, arg.Secret)
result, err := q.db.Exec(ctx, setSourceSecret, arg.Secret, arg.Path, arg.ScopeOrg)
if err != nil {
return 0, err
}
@@ -1071,17 +1141,23 @@ func (q *Queries) SetSourceSecret(ctx context.Context, arg SetSourceSecretParams
const setSourceSigning = `-- name: SetSourceSigning :execrows
UPDATE sources SET signing = $1, updated_at = now()
WHERE path = $2 AND signing = $3
WHERE path = $2 AND signing = $3 AND ($4::text IS NULL OR org_id = $4)
`
type SetSourceSigningParams struct {
ToSigning string
Path string
FromSigning string
ScopeOrg pgtype.Text
}
func (q *Queries) SetSourceSigning(ctx context.Context, arg SetSourceSigningParams) (int64, error) {
result, err := q.db.Exec(ctx, setSourceSigning, arg.ToSigning, arg.Path, arg.FromSigning)
result, err := q.db.Exec(ctx, setSourceSigning,
arg.ToSigning,
arg.Path,
arg.FromSigning,
arg.ScopeOrg,
)
if err != nil {
return 0, err
}
@@ -1144,17 +1220,23 @@ func (q *Queries) TaskToolCalls(ctx context.Context, taskID int64) ([]TaskToolCa
const transitionBindingStatus = `-- name: TransitionBindingStatus :execrows
UPDATE bindings SET status = $1, updated_at = now()
WHERE id = $2 AND status = $3
WHERE id = $2 AND status = $3 AND ($4::text IS NULL OR org_id = $4)
`
type TransitionBindingStatusParams struct {
ToStatus string
ID string
FromStatus string
ScopeOrg pgtype.Text
}
func (q *Queries) TransitionBindingStatus(ctx context.Context, arg TransitionBindingStatusParams) (int64, error) {
result, err := q.db.Exec(ctx, transitionBindingStatus, arg.ToStatus, arg.ID, arg.FromStatus)
result, err := q.db.Exec(ctx, transitionBindingStatus,
arg.ToStatus,
arg.ID,
arg.FromStatus,
arg.ScopeOrg,
)
if err != nil {
return 0, err
}
@@ -1163,13 +1245,13 @@ func (q *Queries) TransitionBindingStatus(ctx context.Context, arg TransitionBin
const updateBinding = `-- name: UpdateBinding :execrows
UPDATE bindings
SET name = $2, source = $3, mapping = $4, filter = $5, workflow = $6, owner = $7, repo = $8,
version = version + 1, status = $9, inputs = $10, repo_param = $11, updated_at = now()
WHERE id = $1
SET name = $1, source = $2, mapping = $3, filter = $4,
workflow = $5, owner = $6, repo = $7,
version = version + 1, status = $8, inputs = $9, repo_param = $10, updated_at = now()
WHERE id = $11 AND ($12::text IS NULL OR org_id = $12)
`
type UpdateBindingParams struct {
ID string
Name string
Source string
Mapping string
@@ -1180,11 +1262,12 @@ type UpdateBindingParams struct {
Status string
Inputs string
RepoParam string
ID string
ScopeOrg pgtype.Text
}
func (q *Queries) UpdateBinding(ctx context.Context, arg UpdateBindingParams) (int64, error) {
result, err := q.db.Exec(ctx, updateBinding,
arg.ID,
arg.Name,
arg.Source,
arg.Mapping,
@@ -1195,6 +1278,8 @@ func (q *Queries) UpdateBinding(ctx context.Context, arg UpdateBindingParams) (i
arg.Status,
arg.Inputs,
arg.RepoParam,
arg.ID,
arg.ScopeOrg,
)
if err != nil {
return 0, err
@@ -1203,17 +1288,24 @@ func (q *Queries) UpdateBinding(ctx context.Context, arg UpdateBindingParams) (i
}
const updateEventType = `-- name: UpdateEventType :execrows
UPDATE event_types SET name = $2, rule = $3, updated_at = now() WHERE id = $1
UPDATE event_types SET name = $1, rule = $2, updated_at = now()
WHERE id = $3 AND ($4::text IS NULL OR org_id = $4)
`
type UpdateEventTypeParams struct {
ID string
Name string
Rule string
Name string
Rule string
ID string
ScopeOrg pgtype.Text
}
func (q *Queries) UpdateEventType(ctx context.Context, arg UpdateEventTypeParams) (int64, error) {
result, err := q.db.Exec(ctx, updateEventType, arg.ID, arg.Name, arg.Rule)
result, err := q.db.Exec(ctx, updateEventType,
arg.Name,
arg.Rule,
arg.ID,
arg.ScopeOrg,
)
if err != nil {
return 0, err
}
@@ -1222,25 +1314,27 @@ func (q *Queries) UpdateEventType(ctx context.Context, arg UpdateEventTypeParams
const updateMapping = `-- name: UpdateMapping :execrows
UPDATE mappings
SET name = $2, source_hint = $3, event_type = $4, fields = $5, updated_at = now()
WHERE id = $1
SET name = $1, source_hint = $2, event_type = $3, fields = $4, updated_at = now()
WHERE id = $5 AND ($6::text IS NULL OR org_id = $6)
`
type UpdateMappingParams struct {
ID string
Name string
SourceHint string
EventType string
Fields string
ID string
ScopeOrg pgtype.Text
}
func (q *Queries) UpdateMapping(ctx context.Context, arg UpdateMappingParams) (int64, error) {
result, err := q.db.Exec(ctx, updateMapping,
arg.ID,
arg.Name,
arg.SourceHint,
arg.EventType,
arg.Fields,
arg.ID,
arg.ScopeOrg,
)
if err != nil {
return 0, err
@@ -42,64 +42,65 @@ WHERE c.org_id = (SELECT m.org_id FROM captures AS m WHERE m.id = sqlc.arg(captu
);
-- name: InsertMapping :exec
INSERT INTO mappings (id, name, source_hint, event_type, fields)
VALUES ($1, $2, $3, $4, $5);
INSERT INTO mappings (id, name, source_hint, event_type, fields, org_id)
VALUES ($1, $2, $3, $4, $5, $6);
-- name: GetMapping :one
SELECT sqlc.embed(mappings),
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
FROM mappings WHERE id = $1;
FROM mappings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: ListMappings :many
SELECT sqlc.embed(mappings),
(SELECT count(*) FROM mapping_matches mm WHERE mm.mapping = mappings.id)::bigint AS match_count,
COALESCE((SELECT max(mm.matched_at) FROM mapping_matches mm WHERE mm.mapping = mappings.id), 'epoch')::timestamptz AS last_matched_at
FROM mappings ORDER BY created_at DESC;
FROM mappings WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC;
-- name: UpdateMapping :execrows
UPDATE mappings
SET name = $2, source_hint = $3, event_type = $4, fields = $5, updated_at = now()
WHERE id = $1;
SET name = sqlc.arg(name), source_hint = sqlc.arg(source_hint), event_type = sqlc.arg(event_type), fields = sqlc.arg(fields), updated_at = now()
WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: InsertMappingMatch :exec
INSERT INTO mapping_matches (mapping, capture) VALUES ($1, $2)
ON CONFLICT DO NOTHING;
-- name: DeleteMapping :execrows
DELETE FROM mappings WHERE id = $1;
DELETE FROM mappings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: InsertBinding :exec
INSERT INTO bindings (id, name, source, mapping, filter, workflow, owner, repo, version, status, inputs, repo_param)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 1, $9, $10, $11);
INSERT INTO bindings (id, name, source, mapping, filter, workflow, owner, repo, version, status, inputs, repo_param, org_id)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, 1, $9, $10, $11, $12);
-- name: GetBinding :one
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
FROM bindings WHERE id = $1;
FROM bindings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: ListBindings :many
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
FROM bindings ORDER BY created_at DESC;
FROM bindings WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC;
-- name: ArmedBindingsForSource :many
SELECT id, name, source, mapping, workflow, owner, repo, version, status, created_at, updated_at, filter, inputs, repo_param, org_id
FROM bindings WHERE source = $1 AND status = 'armed' ORDER BY created_at DESC;
FROM bindings WHERE source = sqlc.arg(source) AND status = 'armed' AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY created_at DESC;
-- name: UpdateBinding :execrows
UPDATE bindings
SET name = $2, source = $3, mapping = $4, filter = $5, workflow = $6, owner = $7, repo = $8,
version = version + 1, status = $9, inputs = $10, repo_param = $11, updated_at = now()
WHERE id = $1;
SET name = sqlc.arg(name), source = sqlc.arg(source), mapping = sqlc.arg(mapping), filter = sqlc.arg(filter),
workflow = sqlc.arg(workflow), owner = sqlc.arg(owner), repo = sqlc.arg(repo),
version = version + 1, status = sqlc.arg(status), inputs = sqlc.arg(inputs), repo_param = sqlc.arg(repo_param), updated_at = now()
WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: SetBindingArmed :execrows
UPDATE bindings SET status = 'armed', updated_at = now() WHERE id = $1;
UPDATE bindings SET status = 'armed', updated_at = now() WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: TransitionBindingStatus :execrows
UPDATE bindings SET status = @to_status, updated_at = now()
WHERE id = @id AND status = @from_status;
WHERE id = @id AND status = @from_status AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: DeleteBinding :execrows
DELETE FROM bindings WHERE id = $1;
DELETE FROM bindings WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: InsertBindingDispatch :execrows
INSERT INTO binding_dispatches (binding, binding_version, capture, task_id, reason, delivery)
@@ -110,12 +111,13 @@ FROM captures c WHERE c.id = @capture;
UPDATE binding_dispatches SET task_id = $3 WHERE binding = $1 AND capture = $2;
-- name: ListBindingDispatches :many
SELECT binding, binding_version, capture, task_id, reason, dispatched_at
FROM binding_dispatches
WHERE (sqlc.narg(binding)::text IS NULL OR binding = sqlc.narg(binding))
AND (sqlc.narg(capture)::text IS NULL OR capture = sqlc.narg(capture))
AND (sqlc.narg(task_id)::bigint IS NULL OR task_id = sqlc.narg(task_id))
ORDER BY dispatched_at DESC
SELECT d.binding, d.binding_version, d.capture, d.task_id, d.reason, d.dispatched_at
FROM binding_dispatches d JOIN bindings b ON b.id = d.binding
WHERE (sqlc.narg(scope_org)::text IS NULL OR b.org_id = sqlc.narg(scope_org))
AND (sqlc.narg(binding)::text IS NULL OR d.binding = sqlc.narg(binding))
AND (sqlc.narg(capture)::text IS NULL OR d.capture = sqlc.narg(capture))
AND (sqlc.narg(task_id)::bigint IS NULL OR d.task_id = sqlc.narg(task_id))
ORDER BY d.dispatched_at DESC
LIMIT @entry_limit;
-- name: InsertPlaybookDispatch :exec
@@ -134,26 +136,27 @@ SELECT id, task_id, attempt, tool, result, error, called_at
FROM tool_calls WHERE task_id = $1 ORDER BY called_at ASC;
-- name: InsertEventType :exec
INSERT INTO event_types (id, source, name, rule, schema)
VALUES ($1, $2, $3, $4, $5);
INSERT INTO event_types (id, source, name, rule, schema, org_id)
VALUES ($1, $2, $3, $4, $5, $6);
-- name: GetEventType :one
SELECT id, source, name, rule, schema, created_at, updated_at
FROM event_types WHERE id = $1;
FROM event_types WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: ListEventTypes :many
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
FROM event_types ORDER BY source, name;
FROM event_types WHERE (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org)) ORDER BY source, name;
-- name: EventTypesForSource :many
SELECT id, source, name, rule, schema, created_at, updated_at, org_id, workspace_id
FROM event_types WHERE source = $1 ORDER BY name;
-- name: UpdateEventType :execrows
UPDATE event_types SET name = $2, rule = $3, updated_at = now() WHERE id = $1;
UPDATE event_types SET name = sqlc.arg(name), rule = sqlc.arg(rule), updated_at = now()
WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: DeleteEventType :execrows
DELETE FROM event_types WHERE id = $1;
DELETE FROM event_types WHERE id = sqlc.arg(id) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: LockEventTypeSource :exec
-- Serialises saves per source so two concurrent saves cannot each pass the
@@ -163,28 +166,28 @@ SELECT pg_advisory_xact_lock(hashtext('event_types:' || sqlc.arg(source)::text))
INSERT INTO sources (path, signing, secret, org_id, workspace_id) VALUES ($1, $2, $3, $4, $5);
-- name: GetSource :one
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources WHERE path = $1;
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: ListSources :many
SELECT path, signing, secret, created_at, updated_at, org_id, workspace_id, name, delivery_header FROM sources 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;
-- name: SetSourceSigning :execrows
UPDATE sources SET signing = sqlc.arg(to_signing), updated_at = now()
WHERE path = sqlc.arg(path) AND signing = sqlc.arg(from_signing);
WHERE path = sqlc.arg(path) AND signing = sqlc.arg(from_signing) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: SetSourceSecret :execrows
UPDATE sources SET secret = $2, updated_at = now() WHERE path = $1;
UPDATE sources SET secret = sqlc.arg(secret), updated_at = now() WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: SetSourceName :execrows
UPDATE sources SET name = $2, updated_at = now() WHERE path = $1;
UPDATE sources SET name = sqlc.arg(name), updated_at = now() WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: SetSourceDeliveryHeader :execrows
UPDATE sources SET delivery_header = $2, updated_at = now() WHERE path = $1;
UPDATE sources SET delivery_header = sqlc.arg(delivery_header), updated_at = now() WHERE path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR org_id = sqlc.narg(scope_org));
-- name: DeleteUnboundSource :execrows
-- A source an armed binding fires on is not deleted: the binding would
-- silently stop receiving events.
DELETE FROM sources s WHERE s.path = $1
DELETE FROM sources s WHERE s.path = sqlc.arg(path) AND (sqlc.narg(scope_org)::text IS NULL OR s.org_id = sqlc.narg(scope_org))
AND NOT EXISTS (SELECT 1 FROM bindings b WHERE b.source = s.path AND b.status = 'armed');
-- name: SourceExists :one
@@ -202,9 +205,12 @@ DELETE FROM capture_refusals WHERE refused_at < $1;
-- name: CaptureRefusalSummary :many
WITH r AS (
SELECT source, count(*) AS refused, array_agg(DISTINCT addr ORDER BY addr)::text[] AS addrs
FROM capture_refusals WHERE refused_at >= $1 GROUP BY source
FROM capture_refusals cr WHERE refused_at >= sqlc.arg(since)
AND (sqlc.narg(scope_org)::text IS NULL
OR EXISTS (SELECT 1 FROM sources s WHERE s.path = cr.source AND s.org_id = sqlc.narg(scope_org)))
GROUP BY source
), a AS (
SELECT source, count(*) AS accepted FROM captures WHERE received_at >= $1 GROUP BY source
SELECT source, count(*) AS accepted FROM captures WHERE received_at >= sqlc.arg(since) GROUP BY source
)
SELECT r.source, r.refused, COALESCE(a.accepted, 0)::bigint AS accepted, r.addrs
FROM r LEFT JOIN a ON a.source = r.source
+16
View File
@@ -0,0 +1,16 @@
package postgres
import (
"context"
"github.com/jackc/pgx/v5/pgtype"
"github.com/samcharles93/archie-core/internal/domain/org"
)
// scopeOrg is the org a request's reads are confined to, or NULL for an
// internal service acting across orgs.
func scopeOrg(ctx context.Context) pgtype.Text {
id, ok := org.Scope(ctx)
return pgtype.Text{String: string(id), Valid: ok}
}
+7 -7
View File
@@ -38,7 +38,7 @@ func (s *EDA) InsertSource(ctx context.Context, src source.Source) error {
// GetSource returns (nil, nil) for an unknown path.
func (s *EDA) GetSource(ctx context.Context, path string) (*source.Source, error) {
r, err := s.q.GetSource(ctx, path)
r, err := s.q.GetSource(ctx, postgresdb.GetSourceParams{Path: path, ScopeOrg: scopeOrg(ctx)})
if errors.Is(err, pgx.ErrNoRows) {
return nil, nil
}
@@ -53,7 +53,7 @@ func (s *EDA) GetSource(ctx context.Context, path string) (*source.Source, error
}
func (s *EDA) ListSources(ctx context.Context) ([]source.Source, error) {
rows, err := s.q.ListSources(ctx)
rows, err := s.q.ListSources(ctx, scopeOrg(ctx))
if err != nil {
return nil, fmt.Errorf("edastore: list sources: %w", err)
}
@@ -72,7 +72,7 @@ func (s *EDA) ListSources(ctx context.Context) ([]source.Source, error) {
// refuses when the stored state is no longer from.
func (s *EDA) SetSourceSigning(ctx context.Context, path string, from, to source.Signing) error {
n, err := s.q.SetSourceSigning(ctx, postgresdb.SetSourceSigningParams{
Path: path, FromSigning: string(from), ToSigning: string(to),
Path: path, FromSigning: string(from), ToSigning: string(to), ScopeOrg: scopeOrg(ctx),
})
if err != nil {
return fmt.Errorf("edastore: set source signing: %w", err)
@@ -95,7 +95,7 @@ func (s *EDA) SetSourceSecret(ctx context.Context, path, secret string) error {
if err != nil {
return err
}
n, err := s.q.SetSourceSecret(ctx, postgresdb.SetSourceSecretParams{Path: path, Secret: sealed})
n, err := s.q.SetSourceSecret(ctx, postgresdb.SetSourceSecretParams{Path: path, Secret: sealed, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return fmt.Errorf("edastore: set source secret: %w", err)
}
@@ -107,7 +107,7 @@ func (s *EDA) SetSourceSecret(ctx context.Context, path, secret string) error {
// SetSourceName relabels a source.
func (s *EDA) SetSourceName(ctx context.Context, path, name string) error {
n, err := s.q.SetSourceName(ctx, postgresdb.SetSourceNameParams{Path: path, Name: name})
n, err := s.q.SetSourceName(ctx, postgresdb.SetSourceNameParams{Path: path, Name: name, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return fmt.Errorf("edastore: set source name: %w", err)
}
@@ -119,7 +119,7 @@ func (s *EDA) SetSourceName(ctx context.Context, path, name string) error {
// SetSourceDeliveryHeader sets the header that names a source's deliveries.
func (s *EDA) SetSourceDeliveryHeader(ctx context.Context, path, header string) error {
n, err := s.q.SetSourceDeliveryHeader(ctx, postgresdb.SetSourceDeliveryHeaderParams{Path: path, DeliveryHeader: header})
n, err := s.q.SetSourceDeliveryHeader(ctx, postgresdb.SetSourceDeliveryHeaderParams{Path: path, DeliveryHeader: header, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return fmt.Errorf("edastore: set source delivery header: %w", err)
}
@@ -131,7 +131,7 @@ func (s *EDA) SetSourceDeliveryHeader(ctx context.Context, path, header string)
// DeleteSource deletes a source no armed binding fires on.
func (s *EDA) DeleteSource(ctx context.Context, path string) error {
n, err := s.q.DeleteUnboundSource(ctx, path)
n, err := s.q.DeleteUnboundSource(ctx, postgresdb.DeleteUnboundSourceParams{Path: path, ScopeOrg: scopeOrg(ctx)})
if err != nil {
return fmt.Errorf("edastore: delete source: %w", err)
}
@@ -12,6 +12,7 @@ import (
"github.com/samcharles93/archie-core/internal/domain/access"
"github.com/samcharles93/archie-core/internal/domain/identity"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/presence"
)
// CallerKey names the service making the call.
@@ -36,8 +37,8 @@ func Outgoing(ctx context.Context, caller string) context.Context {
// Callers resolves who a call is made by. It runs after authentication,
// which is what makes the metadata trustworthy: only holders of the instance
// credential reach it with their metadata intact (a task grant's is
// stripped). A call naming a principal acts in that principal's org; any
// other acts in the default org.
// stripped). A call naming a principal acts in, and reads only, that principal's
// org; any other acts in the default org.
type Callers struct {
Principals access.PrincipalSource
}
@@ -50,6 +51,9 @@ func (c Callers) resolve(ctx context.Context) (context.Context, error) {
}
id := first(md, PrincipalKey)
if id == "" {
if servesChannels(caller) {
ctx = org.WithScope(ctx, org.DefaultOrgID)
}
return ctx, nil
}
if c.Principals == nil {
@@ -59,7 +63,8 @@ func (c Callers) resolve(ctx context.Context) (context.Context, error) {
if err != nil {
return nil, status.Errorf(codes.Unavailable, "resolve principal: %v", err)
}
return org.WithOrg(access.WithActor(access.WithPrincipal(ctx, p), id), p.Org), nil
ctx = org.WithScope(org.WithOrg(access.WithActor(access.WithPrincipal(ctx, p), id), p.Org), p.Org)
return ctx, nil
}
// Unary attributes unary calls.
@@ -97,3 +102,9 @@ func first(md metadata.MD, key string) string {
}
return ""
}
// servesChannels reports whether caller serves chat channels. A channel call
// acting for no one, such as a Telegram message, reads only the system org.
func servesChannels(caller string) bool {
return caller == presence.Gateway || caller == presence.Messaging
}