* fix(postgres): scope OpenPRs to the caller's org A principal-carrying caller read every org's pr_open tasks. The query now honours the request scope; the daemon's unscoped review scan still sees all orgs. Issue: archie-core-kgxw.23 * fix(statestore): serve task logs only to the task's org ReadTaskLog and StreamTaskLogContent took a task ID and read the file with no org check. The State Store's task-log reader now confirms the task is visible under the caller's scope first; unscoped services read as before. Issue: archie-core-kgxw.24 * fix(postgres): scope ListPolicies to the caller's org Every org's policy text reached any principal-carrying caller. A scoped read now sees its own org's policies, plus instance policies when scoped to the system org. The engines and the lockout guard decide for every org, so they read the full chain unscoped: the dashboard engine loads without the request's principal and the guard lifts the caller's scope. Issue: archie-core-kgxw.25 * fix(postgres): scope identity reads to the caller's org ListIdentities and GetIdentity (and its alias fallback) returned every identity to any caller. A caller scoped to an org now sees only that org's members and assigned agents; the system org and unscoped services still see the whole instance. Issue: archie-core-kgxw.26 * fix(postgres): hide instance status from other orgs ListPresence, ListChannelStatus, ListApplyStatus and GetConfigSnapshot answered any org principal with instance-wide state. They now return nothing unless the request is unscoped or scoped to the system org. The dashboard reads the published config as itself, since its handlers project it for callers of every org. Issue: archie-core-kgxw.27 * feat(postgres): assign new agents an org and role at creation A bot or service account created through the API served no org until the next State Store boot granted it one. Create now assigns it to the org the request acts in with the developer role in the same transaction, so it is authorized immediately. Issue: archie-core-kgxw.14 * feat(edastore): create event records in a chosen workspace Sources are created in a workspace of the caller's org, the default one when none is named; event types and bindings take their source's workspace and mappings their event type's, the same rule the org backfill applied. The binding, mapping and event type contracts carry the workspace, the four list endpoints accept ?workspace=, and the Events page has a workspace selector that filters every step and picks where a new source goes. Issue: archie-core-kgxw.11 * feat(webui): authorize a record in its workspace The API built every access request with no workspace, so workspace policies and per-workspace roles never applied. A request naming a source, binding, mapping, event type or task now reads the record's workspace under the caller's scope and authorizes in it; tasks carry their workspace on the State Store contract for this. Issue: archie-core-kgxw.9 * feat(access): expose workspace environment to policies Policies can test resource.workspace.environment. The workspace attribute is the workspace entity, which carries its environment; the principal brings its org's workspaces from the State Store, so the dashboard and the daemon's run checks describe a resource's workspace without another lookup, and the daemon now authorizes runs in the task's or binding's workspace. Issue: archie-core-kgxw.10 * fix(webui): ask read_secret when a source secret is read Rotating a source's secret returns its value, but the route asked update, which every developer holds, so no policy could narrow who reads a secret. It now asks read_secret on the secret, which the shipped roles give admins and an object policy can narrow further. Issue: archie-core-kgxw.17 * refactor(org): delete the unused upgrade-incomplete sentinel Nothing returns org.ErrUpgradeIncomplete: the State Store finishes the org upgrade before it serves. Issue: archie-core-kgxw.18
197 lines
6.9 KiB
Go
197 lines
6.9 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/health"
|
|
"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"
|
|
)
|
|
|
|
// PutConfigSnapshot replaces the published snapshot (one row, id = 1).
|
|
func (s *Store) PutConfigSnapshot(ctx context.Context, snapshot storecontract.ConfigSnapshot) error {
|
|
published := snapshot.PublishedAt
|
|
if published.IsZero() {
|
|
published = time.Now()
|
|
}
|
|
if err := s.queries().UpsertConfigSnapshot(ctx, postgresdb.UpsertConfigSnapshotParams{
|
|
Schema: snapshot.Schema, Document: string(snapshot.Document), PublishedAt: published.UTC(),
|
|
}); err != nil {
|
|
return fmt.Errorf("store: put config snapshot: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ConfigSnapshot returns the published snapshot. found is false, with a nil
|
|
// error, before anything has published one or when the request may not read
|
|
// the instance.
|
|
func (s *Store) ConfigSnapshot(ctx context.Context) (storecontract.ConfigSnapshot, bool, error) {
|
|
if !org.InstanceScope(ctx) {
|
|
return storecontract.ConfigSnapshot{}, false, nil
|
|
}
|
|
row, err := s.queries().ConfigSnapshotByID(ctx)
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return storecontract.ConfigSnapshot{}, false, nil
|
|
}
|
|
if err != nil {
|
|
return storecontract.ConfigSnapshot{}, false, fmt.Errorf("store: read config snapshot: %w", err)
|
|
}
|
|
return storecontract.ConfigSnapshot{
|
|
Schema: row.Schema, Document: []byte(row.Document), PublishedAt: row.PublishedAt,
|
|
}, true, nil
|
|
}
|
|
|
|
// PutChannelStatus records the reporting process's whole set in one
|
|
// transaction, removing rows it no longer reports.
|
|
func (s *Store) PutChannelStatus(ctx context.Context, channels []storecontract.ChannelStatus) error {
|
|
tx, err := s.pool.Begin(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("store: put channel status: begin: %w", err)
|
|
}
|
|
defer func() { _ = tx.Rollback(ctx) }()
|
|
q := postgresdb.New(tx)
|
|
|
|
reported := make([]string, 0, len(channels))
|
|
for _, channel := range channels {
|
|
if channel.ID == "" {
|
|
return fmt.Errorf("store: put channel status: a channel needs an id")
|
|
}
|
|
observed := channel.ObservedAt
|
|
if observed.IsZero() {
|
|
observed = time.Now()
|
|
}
|
|
if err := q.UpsertChannelStatus(ctx, postgresdb.UpsertChannelStatusParams{
|
|
ID: channel.ID, Name: channel.Name, State: channel.State, Detail: channel.Detail,
|
|
Configured: channel.Configured, ReloadSupported: channel.ReloadSupported,
|
|
ObservedAt: observed.UTC(),
|
|
}); err != nil {
|
|
return fmt.Errorf("store: put channel status %s: %w", channel.ID, err)
|
|
}
|
|
reported = append(reported, channel.ID)
|
|
}
|
|
|
|
if err := q.DeleteChannelStatusNotIn(ctx, reported); err != nil {
|
|
return fmt.Errorf("store: put channel status: prune: %w", err)
|
|
}
|
|
return tx.Commit(ctx)
|
|
}
|
|
|
|
// ChannelStatus returns every reported channel, ordered by id, or none when
|
|
// the request may not read the instance.
|
|
func (s *Store) ChannelStatus(ctx context.Context) ([]storecontract.ChannelStatus, error) {
|
|
if !org.InstanceScope(ctx) {
|
|
return []storecontract.ChannelStatus{}, nil
|
|
}
|
|
rows, err := s.queries().ListChannelStatus(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: list channel status: %w", err)
|
|
}
|
|
out := make([]storecontract.ChannelStatus, 0, len(rows))
|
|
for _, r := range rows {
|
|
out = append(out, storecontract.ChannelStatus{
|
|
ID: r.ID, Name: r.Name, State: r.State, Detail: r.Detail,
|
|
Configured: r.Configured, ReloadSupported: r.ReloadSupported, ObservedAt: r.ObservedAt,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// PutApplyStatus records what one process applied for one resource kind,
|
|
// replacing its own (process, kind) row.
|
|
func (s *Store) PutApplyStatus(ctx context.Context, status storecontract.ApplyStatus) error {
|
|
reported := status.ReportedAt
|
|
if reported.IsZero() {
|
|
reported = time.Now()
|
|
}
|
|
if err := s.queries().UpsertApplyStatus(ctx, postgresdb.UpsertApplyStatusParams{
|
|
Process: status.Process, Kind: status.Kind, AppliedVersion: status.AppliedVersion,
|
|
Error: status.Error, ReportedAt: reported.UTC(),
|
|
}); err != nil {
|
|
return fmt.Errorf("store: put apply status: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// ListApplyStatus returns every process's report, ordered by (process, kind),
|
|
// or none when the request may not read the instance.
|
|
func (s *Store) ListApplyStatus(ctx context.Context) ([]storecontract.ApplyStatus, error) {
|
|
if !org.InstanceScope(ctx) {
|
|
return []storecontract.ApplyStatus{}, nil
|
|
}
|
|
rows, err := s.queries().ListApplyStatus(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: list apply status: %w", err)
|
|
}
|
|
out := make([]storecontract.ApplyStatus, 0, len(rows))
|
|
for _, r := range rows {
|
|
out = append(out, storecontract.ApplyStatus{
|
|
Process: r.Process, Kind: r.Kind, AppliedVersion: r.AppliedVersion,
|
|
Error: r.Error, ReportedAt: r.ReportedAt,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// PutPresence replaces one instance's record and drops records whose
|
|
// instance stopped re-stamping long ago, so restarts do not accumulate rows.
|
|
func (s *Store) PutPresence(ctx context.Context, presence storecontract.Presence) error {
|
|
checks := presence.Checks
|
|
if checks == nil {
|
|
checks = []health.Component{}
|
|
}
|
|
raw, err := json.Marshal(checks)
|
|
if err != nil {
|
|
return fmt.Errorf("store: encode presence checks: %w", err)
|
|
}
|
|
reported := presence.ReportedAt
|
|
if reported.IsZero() {
|
|
reported = time.Now()
|
|
}
|
|
q := s.queries()
|
|
if err := q.UpsertPresence(ctx, postgresdb.UpsertPresenceParams{
|
|
Service: presence.Service, InstanceID: presence.InstanceID, Version: presence.Version,
|
|
InstallType: presence.InstallType, StartedAt: presence.StartedAt.UTC(), ReportedAt: reported.UTC(),
|
|
Ready: presence.Ready, Detail: presence.Detail, Checks: raw,
|
|
}); err != nil {
|
|
return fmt.Errorf("store: put presence: %w", err)
|
|
}
|
|
if err := q.DeletePresenceBefore(ctx, reported.Add(-presenceRetention).UTC()); err != nil {
|
|
return fmt.Errorf("store: prune presence: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// presenceRetention is how long a stopped instance stays listed as down.
|
|
const presenceRetention = 24 * time.Hour
|
|
|
|
// ListPresence returns every instance's record, ordered by (service,
|
|
// instance), or none when the request may not read the instance.
|
|
func (s *Store) ListPresence(ctx context.Context) ([]storecontract.Presence, error) {
|
|
if !org.InstanceScope(ctx) {
|
|
return []storecontract.Presence{}, nil
|
|
}
|
|
rows, err := s.queries().ListPresence(ctx)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("store: list presence: %w", err)
|
|
}
|
|
out := make([]storecontract.Presence, 0, len(rows))
|
|
for _, r := range rows {
|
|
var checks []health.Component
|
|
if err := json.Unmarshal(r.Checks, &checks); err != nil {
|
|
return nil, fmt.Errorf("store: decode presence checks: %w", err)
|
|
}
|
|
out = append(out, storecontract.Presence{
|
|
Service: r.Service, InstanceID: r.InstanceID, Version: r.Version, InstallType: r.InstallType,
|
|
StartedAt: r.StartedAt, ReportedAt: r.ReportedAt, Ready: r.Ready, Detail: r.Detail, Checks: checks,
|
|
})
|
|
}
|
|
return out, nil
|
|
}
|