* 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
186 lines
5.6 KiB
Go
186 lines
5.6 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"github.com/jackc/pgx/v5"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/source"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
|
|
)
|
|
|
|
var _ storecontract.SourceStore = (*EDA)(nil)
|
|
|
|
// InsertSource stores a new source in one of the acting org's workspaces,
|
|
// its default one when src names none; a workspace the org does not have is
|
|
// ErrInvalidWorkspace. The path is the primary key, so a taken path is
|
|
// refused by the database and surfaced as ErrSourcePathTaken.
|
|
func (s *EDA) InsertSource(ctx context.Context, src source.Source) error {
|
|
secret, err := s.sealSecret(src.Secret)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
workspace := src.WorkspaceID
|
|
if workspace == "" {
|
|
workspace = org.DefaultWorkspaceID
|
|
}
|
|
n, err := s.q.InsertSource(ctx, postgresdb.InsertSourceParams{
|
|
Path: src.Path, Signing: string(src.Signing), Secret: secret,
|
|
OrgID: string(org.OrgFromContext(ctx)), WorkspaceID: string(workspace),
|
|
})
|
|
if isUniqueViolation(err) {
|
|
return storecontract.ErrSourcePathTaken
|
|
}
|
|
if err != nil {
|
|
return fmt.Errorf("edastore: insert source: %w", err)
|
|
}
|
|
if n == 0 {
|
|
return fmt.Errorf("%w: %s is not a workspace of this org", org.ErrInvalidWorkspace, workspace)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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, postgresdb.GetSourceParams{Path: path, ScopeOrg: scopeOrg(ctx)})
|
|
if errors.Is(err, pgx.ErrNoRows) {
|
|
return nil, nil
|
|
}
|
|
if err != nil {
|
|
return nil, fmt.Errorf("edastore: get source: %w", err)
|
|
}
|
|
src, err := s.sourceValue(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &src, nil
|
|
}
|
|
|
|
func (s *EDA) ListSources(ctx context.Context) ([]source.Source, error) {
|
|
rows, err := s.q.ListSources(ctx, scopeOrg(ctx))
|
|
if err != nil {
|
|
return nil, fmt.Errorf("edastore: list sources: %w", err)
|
|
}
|
|
out := make([]source.Source, 0, len(rows))
|
|
for _, r := range rows {
|
|
src, err := s.sourceValue(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
out = append(out, src)
|
|
}
|
|
return out, nil
|
|
}
|
|
|
|
// SetSourceSigning moves a source from one signing state to another, and
|
|
// 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), ScopeOrg: scopeOrg(ctx),
|
|
})
|
|
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 {
|
|
sealed, err := s.sealSecret(secret)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
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)
|
|
}
|
|
if n == 0 {
|
|
return storecontract.ErrSourceNotFound
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// 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, ScopeOrg: scopeOrg(ctx)})
|
|
if err != 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.
|
|
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)})
|
|
if err != 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.
|
|
func (s *EDA) DeleteSource(ctx context.Context, path string) error {
|
|
n, err := s.q.DeleteUnboundSource(ctx, postgresdb.DeleteUnboundSourceParams{Path: path, ScopeOrg: scopeOrg(ctx)})
|
|
if err != nil {
|
|
return fmt.Errorf("edastore: delete source: %w", err)
|
|
}
|
|
if n > 0 {
|
|
return nil
|
|
}
|
|
exists, err := s.q.SourceExists(ctx, path)
|
|
if err != nil {
|
|
return fmt.Errorf("edastore: delete source: %w", err)
|
|
}
|
|
if exists {
|
|
return storecontract.ErrSourceInUse
|
|
}
|
|
return storecontract.ErrSourceNotFound
|
|
}
|
|
|
|
// sealSecret encrypts a non-empty secret when a cipher is configured.
|
|
func (s *EDA) sealSecret(secret string) (string, error) {
|
|
if s.cipher == nil || secret == "" {
|
|
return secret, nil
|
|
}
|
|
sealed, err := s.cipher.Encrypt(secret)
|
|
if err != nil {
|
|
return "", fmt.Errorf("edastore: encrypt source secret: %w", err)
|
|
}
|
|
return sealed, nil
|
|
}
|
|
|
|
func (s *EDA) sourceValue(r postgresdb.Source) (source.Source, error) {
|
|
secret := r.Secret
|
|
if s.cipher != nil && secret != "" {
|
|
plain, err := s.cipher.Decrypt(secret)
|
|
if err != nil {
|
|
return source.Source{}, fmt.Errorf("edastore: decrypt source secret: %w", err)
|
|
}
|
|
secret = plain
|
|
}
|
|
return source.Source{
|
|
Path: r.Path, Name: r.Name, DeliveryHeader: r.DeliveryHeader, Signing: source.Signing(r.Signing), Secret: secret,
|
|
OrgID: org.OrgID(r.OrgID), WorkspaceID: org.WorkspaceID(r.WorkspaceID),
|
|
CreatedAt: r.CreatedAt, UpdatedAt: r.UpdatedAt,
|
|
}, nil
|
|
}
|