Files
probo/pkg/coredata/framework.go
Bryan Frimin b39d880c0f Switch AuthorizationAttributes to batch and add AuthorizeBatch
Change AuthorizationAttributer.AuthorizationAttributes to take a slice
of resource ids and return policy.AttributesByID, so a single SQL
round-trip can load condition attributes for a whole batch. All
coredata implementations are migrated to a single
`WHERE id = ANY(@resource_ids::text[])` query that returns only the
rows it finds.

Authorizer gains:
  - AuthorizeBatch — all-or-nothing across a homogeneous (same entity
    type, same organization) resource set; rejects mixed entity types,
    mixed organizations, and empty batches with structured errors.
  - AuthorizeMulti — heterogeneous evaluation that returns one error
    per item and writes audit log entries in a single bulk insert.

The single-resource Authorize is rewired to delegate to AuthorizeBatch
so all paths share the same condition evaluation and audit logging.
recordAuditLog is split into buildAuditLogEntry plus a batch insert.

Tests cover the new batch and multi paths, mixed/empty/unsupported
resource cases, audit log batching, and dry-run behaviour.

Signed-off-by: Bryan Frimin <bryan@probo.com>
2026-05-23 13:31:36 -07:00

451 lines
9.1 KiB
Go

// Copyright (c) 2025-2026 Probo Inc <hello@getprobo.com>.
//
// Permission to use, copy, modify, and/or distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
// AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
// OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
// PERFORMANCE OF THIS SOFTWARE.
package coredata
import (
"context"
"errors"
"fmt"
"maps"
"time"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
"go.gearno.de/kit/pg"
"go.probo.inc/probo/pkg/gid"
"go.probo.inc/probo/pkg/iam/policy"
"go.probo.inc/probo/pkg/page"
)
type (
Framework struct {
ID gid.GID `db:"id"`
OrganizationID gid.GID `db:"organization_id"`
ReferenceID string `db:"reference_id"`
Name string `db:"name"`
Description *string `db:"description"`
LightLogoFileID *gid.GID `db:"light_logo_file_id"`
DarkLogoFileID *gid.GID `db:"dark_logo_file_id"`
CreatedAt time.Time `db:"created_at"`
UpdatedAt time.Time `db:"updated_at"`
}
Frameworks []*Framework
)
func (f *Framework) CursorKey(orderBy FrameworkOrderField) page.CursorKey {
switch orderBy {
case FrameworkOrderFieldCreatedAt:
return page.NewCursorKey(f.ID, f.CreatedAt)
}
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
}
func (f *Framework) AuthorizationAttributes(
ctx context.Context,
conn pg.Querier,
frameworkIDs []gid.GID,
) (policy.AttributesByID, error) {
q := `
SELECT
id,
organization_id
FROM
frameworks
WHERE
id = ANY(@framework_ids::text[])
`
args := pgx.StrictNamedArgs{
"framework_ids": frameworkIDs,
}
rows, err := conn.Query(ctx, q, args)
if err != nil {
return nil, fmt.Errorf("cannot query framework authorization attributes batch: %w", err)
}
defer rows.Close()
attrsByID := make(policy.AttributesByID)
for rows.Next() {
var (
frameworkID gid.GID
organizationID gid.GID
)
if err := rows.Scan(&frameworkID, &organizationID); err != nil {
return nil, fmt.Errorf("cannot scan framework authorization attributes batch: %w", err)
}
attrsByID[frameworkID] = policy.Attributes{
"organization_id": organizationID.String(),
}
}
if err := rows.Err(); err != nil {
return nil, fmt.Errorf("cannot iterate framework authorization attributes batch: %w", err)
}
return attrsByID, nil
}
func (f *Frameworks) CountByOrganizationID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
organizationID gid.GID,
) (int, error) {
q := `
SELECT
COUNT(id)
FROM
frameworks
WHERE
%s
AND organization_id = @organization_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.NamedArgs{"organization_id": organizationID}
maps.Copy(args, scope.SQLArguments())
row := conn.QueryRow(ctx, q, args)
var count int
if err := row.Scan(&count); err != nil {
return 0, fmt.Errorf("cannot scan count: %w", err)
}
return count, nil
}
func uniqueGIDs(values []gid.GID) []gid.GID {
set := make(map[gid.GID]struct{}, len(values))
unique := make([]gid.GID, 0, len(values))
for _, value := range values {
if _, ok := set[value]; ok {
continue
}
set[value] = struct{}{}
unique = append(unique, value)
}
return unique
}
func (f *Frameworks) LoadByOrganizationID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor[FrameworkOrderField],
) error {
q := `
SELECT
id,
organization_id,
reference_id,
name,
description,
light_logo_file_id,
dark_logo_file_id,
created_at,
updated_at
FROM
frameworks
WHERE
%s
AND organization_id = @organization_id
AND %s
`
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
args := pgx.NamedArgs{"organization_id": organizationID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query frameworks: %w", err)
}
frameworks, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Framework])
if err != nil {
return fmt.Errorf("cannot collect frameworks: %w", err)
}
*f = frameworks
return nil
}
func (f *Framework) LoadByReferenceID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
referenceID string,
) error {
q := `
SELECT
id,
organization_id,
reference_id,
name,
description,
light_logo_file_id,
dark_logo_file_id,
created_at,
updated_at
FROM
frameworks
WHERE
%s
AND reference_id = @reference_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"reference_id": referenceID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query frameworks: %w", err)
}
framework, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Framework])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect framework: %w", err)
}
*f = framework
return nil
}
func (f *Framework) LoadByID(
ctx context.Context,
conn pg.Querier,
scope Scoper,
frameworkID gid.GID,
) error {
q := `
SELECT
id,
organization_id,
reference_id,
name,
description,
light_logo_file_id,
dark_logo_file_id,
created_at,
updated_at
FROM
frameworks
WHERE
%s
AND id = @framework_id
LIMIT 1;
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"framework_id": frameworkID}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query frameworks: %w", err)
}
framework, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Framework])
if err != nil {
if errors.Is(err, pgx.ErrNoRows) {
return ErrResourceNotFound
}
return fmt.Errorf("cannot collect framework: %w", err)
}
*f = framework
return nil
}
func (f *Frameworks) LoadByIDs(
ctx context.Context,
conn pg.Querier,
scope Scoper,
frameworkIDs []gid.GID,
) error {
q := `
SELECT
id,
organization_id,
reference_id,
name,
description,
light_logo_file_id,
dark_logo_file_id,
created_at,
updated_at
FROM
frameworks
WHERE
%s
AND id = ANY(@framework_ids)
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{"framework_ids": frameworkIDs}
maps.Copy(args, scope.SQLArguments())
rows, err := conn.Query(ctx, q, args)
if err != nil {
return fmt.Errorf("cannot query frameworks: %w", err)
}
frameworks, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Framework])
if err != nil {
return fmt.Errorf("cannot collect frameworks: %w", err)
}
*f = frameworks
return nil
}
func (f Framework) Insert(
ctx context.Context,
conn pg.Tx,
scope Scoper,
) error {
q := `
INSERT INTO
frameworks (
tenant_id,
id,
organization_id,
reference_id,
name,
description,
light_logo_file_id,
dark_logo_file_id,
created_at,
updated_at
)
VALUES (
@tenant_id,
@framework_id,
@organization_id,
@reference_id,
@name,
@description,
@light_logo_file_id,
@dark_logo_file_id,
@created_at,
@updated_at
);
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"framework_id": f.ID,
"organization_id": f.OrganizationID,
"reference_id": f.ReferenceID,
"name": f.Name,
"description": f.Description,
"light_logo_file_id": f.LightLogoFileID,
"dark_logo_file_id": f.DarkLogoFileID,
"created_at": f.CreatedAt,
"updated_at": f.UpdatedAt,
}
_, err := conn.Exec(ctx, q, args)
if err != nil {
if pgErr, ok := errors.AsType[*pgconn.PgError](err); ok {
if pgErr.Code == "23505" && pgErr.ConstraintName == "frameworks_org_ref_unique" {
return ErrResourceAlreadyExists
}
}
return err
}
return nil
}
func (f Framework) Delete(
ctx context.Context,
conn pg.Tx,
scope Scoper,
frameworkID gid.GID,
) error {
q := `
DELETE
FROM
frameworks
WHERE
%s
AND id = @framework_id;
`
args := pgx.StrictNamedArgs{"framework_id": frameworkID}
maps.Copy(args, scope.SQLArguments())
q = fmt.Sprintf(q, scope.SQLFragment())
_, err := conn.Exec(ctx, q, args)
return err
}
func (f *Framework) Update(
ctx context.Context,
conn pg.Tx,
scope Scoper,
) error {
q := `
UPDATE frameworks
SET
name = @name,
description = @description,
updated_at = @updated_at
WHERE
%s
AND id = @framework_id
`
q = fmt.Sprintf(q, scope.SQLFragment())
args := pgx.StrictNamedArgs{
"framework_id": f.ID,
"updated_at": f.UpdatedAt,
"name": f.Name,
"description": f.Description,
}
maps.Copy(args, scope.SQLArguments())
_, err := conn.Exec(ctx, q, args)
return err
}