Force scope usage even for insert

Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
gearnode
2025-03-10 10:27:30 +01:00
parent dc2cac9317
commit a4611f5433
22 changed files with 145 additions and 50 deletions

View File

@@ -59,7 +59,7 @@ func (c Control) CursorKey() page.CursorKey {
func (c *Control) LoadByID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
controlID gid.GID,
) error {
q := `
@@ -121,10 +121,12 @@ LIMIT 1;
func (c Control) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
controls (
tenant_id,
id,
framework_id,
category,
@@ -136,6 +138,7 @@ INSERT INTO
version
)
VALUES (
@tenant_id,
@control_id,
@framework_id,
@category,
@@ -149,6 +152,7 @@ VALUES (
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"control_id": c.ID,
"framework_id": c.FrameworkID,
"category": c.Category,
@@ -166,7 +170,7 @@ VALUES (
func (c *Controls) LoadByFrameworkID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
frameworkID gid.GID,
cursor *page.Cursor,
) error {
@@ -227,7 +231,7 @@ WHERE
func (c *Control) Update(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
params UpdateControlParams,
) error {
q := `

View File

@@ -42,10 +42,12 @@ func (cst ControlStateTransition) CursorKey() page.CursorKey {
func (cst ControlStateTransition) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
control_state_transitions (
tenant_id,
id,
control_id,
from_state,
@@ -55,6 +57,7 @@ INSERT INTO
updated_at
)
VALUES (
@tenant_id,
@control_state_transition_id,
@control_id,
@from_state,
@@ -66,6 +69,7 @@ VALUES (
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"control_state_transition_id": cst.ID,
"control_id": cst.ControlID,
"from_state": cst.FromState,
@@ -81,7 +85,7 @@ VALUES (
func (cst *ControlStateTransitions) LoadByControlID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
controlID gid.GID,
cursor *page.Cursor,
) error {

View File

@@ -49,10 +49,12 @@ func (e Evidence) CursorKey() page.CursorKey {
func (e Evidence) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
evidences (
tenant_id,
id,
task_id,
object_key,
@@ -63,6 +65,7 @@ INSERT INTO
updated_at
)
VALUES (
@tenant_id,
@evidence_id,
@task_id,
@object_key,
@@ -75,6 +78,7 @@ VALUES (
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"evidence_id": e.ID,
"task_id": e.TaskID,
"object_key": e.ObjectKey,
@@ -91,7 +95,7 @@ VALUES (
func (e *Evidence) LoadByID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
evidenceID gid.GID,
) error {
q := `
@@ -153,7 +157,7 @@ LIMIT 1;
func (e *Evidences) LoadByTaskID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
taskID gid.GID,
cursor *page.Cursor,
) error {
@@ -215,7 +219,7 @@ WHERE
func (e Evidence) Delete(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
) error {
q := `
DELETE FROM

View File

@@ -42,10 +42,12 @@ func (cst EvidenceStateTransition) CursorKey() page.CursorKey {
func (est EvidenceStateTransition) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
evidence_state_transitions (
tenant_id,
id,
evidence_id,
from_state,
@@ -55,6 +57,7 @@ INSERT INTO
updated_at
)
VALUES (
@tenant_id,
@evidence_state_transition_id,
@evidence_id,
@from_state,
@@ -66,6 +69,7 @@ VALUES (
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"evidence_state_transition_id": est.ID,
"evidence_id": est.EvidenceID,
"from_state": est.FromState,
@@ -81,7 +85,7 @@ VALUES (
func (cst *EvidenceStateTransitions) LoadByEvidenceID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
evidenceID gid.GID,
cursor *page.Cursor,
) error {
@@ -125,7 +129,7 @@ WHERE
func (cst *EvidenceStateTransitions) DeleteForEvidenceID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
evidenceID gid.GID,
) error {
q := `

View File

@@ -54,7 +54,7 @@ func (f Framework) CursorKey() page.CursorKey {
func (f *Frameworks) LoadByOrganizationID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor,
) error {
@@ -100,7 +100,7 @@ WHERE
func (f *Framework) LoadByID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
frameworkID gid.GID,
) error {
q := `
@@ -143,10 +143,12 @@ LIMIT 1;
func (f Framework) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
frameworks (
tenant_id,
id,
organization_id,
name,
@@ -157,6 +159,7 @@ INSERT INTO
version
)
VALUES (
@tenant_id,
@framework_id,
@organization_id,
@name,
@@ -169,6 +172,7 @@ VALUES (
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"framework_id": f.ID,
"organization_id": f.OrganizationID,
"name": f.Name,
@@ -185,7 +189,7 @@ VALUES (
func (f Framework) Delete(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
) error {
q := `
DELETE
@@ -207,7 +211,7 @@ WHERE
func (f *Framework) Update(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
params UpdateFrameworkParams,
) error {
q := `

View File

@@ -38,7 +38,7 @@ type (
func (o *Organization) LoadByID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
organizationID gid.GID,
) error {
q := `
@@ -79,18 +79,21 @@ LIMIT 1;
func (o *Organization) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO organizations (
tenant_id,
id,
name,
logo_url,
created_at,
updated_at
) VALUES (@id, @name, @logo_url, @created_at, @updated_at)
) VALUES (@tenant_id, @id, @name, @logo_url, @created_at, @updated_at)
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"id": o.ID,
"name": o.Name,
"logo_url": o.LogoURL,

View File

@@ -57,7 +57,7 @@ func (p People) CursorKey() page.CursorKey {
func (p *People) LoadByID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
peopleID gid.GID,
) error {
q := `
@@ -102,10 +102,12 @@ LIMIT 1;
func (p People) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
peoples (
tenant_id,
id,
organization_id,
kind,
@@ -117,6 +119,7 @@ INSERT INTO
version
)
VALUES (
@tenant_id,
@people_id,
@organization_id,
@kind,
@@ -130,6 +133,7 @@ VALUES (
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"people_id": p.ID,
"organization_id": p.OrganizationID,
"kind": p.Kind,
@@ -147,7 +151,7 @@ VALUES (
func (p People) Delete(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
) error {
q := `
DELETE FROM peoples WHERE %s AND id = @people_id
@@ -165,7 +169,7 @@ DELETE FROM peoples WHERE %s AND id = @people_id
func (p *Peoples) LoadByOrganizationID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor,
) error {
@@ -212,7 +216,7 @@ WHERE
func (p *People) Update(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
params UpdatePeopleParams,
) error {
q := `

View File

@@ -45,7 +45,7 @@ func (p Policy) CursorKey() page.CursorKey {
func (p *Policy) LoadByID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
policyID gid.GID,
) error {
q := `
@@ -91,7 +91,7 @@ LIMIT 1;
func (p *Policies) LoadByOrganizationID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor,
) error {
@@ -139,10 +139,12 @@ WHERE
func (p Policy) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
policies (
tenant_id,
id,
organization_id,
owner_id,
@@ -155,6 +157,7 @@ INSERT INTO
version
)
VALUES (
@tenant_id,
@policy_id,
@organization_id,
@owner_id,
@@ -169,6 +172,7 @@ VALUES (
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"policy_id": p.ID,
"organization_id": p.OrganizationID,
"owner_id": p.OwnerID,
@@ -187,7 +191,7 @@ VALUES (
func (p Policy) Delete(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
) error {
q := `
DELETE FROM policies WHERE %s AND id = @policy_id
@@ -205,7 +209,7 @@ DELETE FROM policies WHERE %s AND id = @policy_id
func (p *Policy) Update(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
params UpdatePolicyParams,
) error {
q := `

View File

@@ -19,17 +19,56 @@ import (
)
type (
Scope struct{}
Scoper interface {
SQLArguments() pgx.StrictNamedArgs
SQLFragment() string
GetTenantID() *string
}
NoScope struct{}
Scope struct {
TenantID string
}
)
func NewScope() *Scope {
return &Scope{}
var (
_ Scoper = (*NoScope)(nil)
_ Scoper = (*Scope)(nil)
)
func NewNoScope() *NoScope {
return &NoScope{}
}
func (*Scope) SQLArguments() pgx.StrictNamedArgs {
func (*NoScope) SQLArguments() pgx.StrictNamedArgs {
return pgx.StrictNamedArgs{}
}
func (*Scope) SQLFragment() string {
func (*NoScope) SQLFragment() string {
return "TRUE"
}
func (*NoScope) GetTenantID() *string {
return nil
}
func NewScope(tenantID string) *Scope {
return &Scope{
TenantID: tenantID,
}
}
func (s *Scope) SQLArguments() pgx.StrictNamedArgs {
return pgx.StrictNamedArgs{
"tenant_id": s.TenantID,
}
}
func (*Scope) SQLFragment() string {
return "tenant_id = @tenant_id"
}
func (s *Scope) GetTenantID() *string {
return &s.TenantID
}

View File

@@ -49,7 +49,7 @@ func (t Task) CursorKey() page.CursorKey {
func (t *Task) LoadByID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
taskID gid.GID,
) error {
q := `
@@ -127,10 +127,12 @@ LIMIT 1;
func (t Task) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
WITH task_insert AS (
INSERT INTO tasks (
tenant_id,
id,
name,
description,
@@ -139,6 +141,7 @@ WITH task_insert AS (
updated_at
)
VALUES (
@tenant_id,
@task_id,
@name,
@description,
@@ -161,6 +164,7 @@ VALUES (
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"task_id": t.ID,
"control_id": t.ControlID,
"name": t.Name,
@@ -176,7 +180,7 @@ VALUES (
func (t *Tasks) LoadByControlID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
controlID gid.GID,
cursor *page.Cursor,
) error {
@@ -253,7 +257,7 @@ WHERE
func (t *Task) Delete(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
) error {
q := `
WITH control_count AS (

View File

@@ -41,10 +41,12 @@ func (tst TaskStateTransition) CursorKey() page.CursorKey {
func (tst TaskStateTransition) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
task_state_transitions (
tenant_id,
id,
task_id,
from_state,
@@ -54,6 +56,7 @@ INSERT INTO
updated_at
)
VALUES (
@tenant_id,
@task_state_transition_id,
@task_id,
@from_state,
@@ -65,6 +68,7 @@ VALUES (
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"task_state_transition_id": tst.ID,
"task_id": tst.TaskID,
"from_state": tst.FromState,
@@ -80,7 +84,7 @@ VALUES (
func (tst *TaskStateTransitions) LoadByTaskID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
taskID gid.GID,
cursor *page.Cursor,
) error {

View File

@@ -70,7 +70,7 @@ func (v Vendor) CursorKey() page.CursorKey {
func (v *Vendor) LoadByID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
vendorID gid.GID,
) error {
q := `
@@ -121,10 +121,12 @@ LIMIT 1;
func (v Vendor) Insert(
ctx context.Context,
conn pg.Conn,
scope Scoper,
) error {
q := `
INSERT INTO
vendors (
tenant_id,
id,
organization_id,
name,
@@ -141,6 +143,7 @@ INSERT INTO
version
)
VALUES (
@tenant_id,
@vendor_id,
@organization_id,
@name,
@@ -159,6 +162,7 @@ VALUES (
`
args := pgx.StrictNamedArgs{
"tenant_id": scope.GetTenantID(),
"vendor_id": v.ID,
"organization_id": v.OrganizationID,
"name": v.Name,
@@ -180,7 +184,7 @@ VALUES (
func (v Vendor) Delete(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
) error {
q := `
DELETE FROM vendors WHERE %s AND id = @vendor_id
@@ -198,7 +202,7 @@ DELETE FROM vendors WHERE %s AND id = @vendor_id
func (v *Vendors) LoadByOrganizationID(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
organizationID gid.GID,
cursor *page.Cursor,
) error {
@@ -250,7 +254,7 @@ WHERE
func (v *Vendor) Update(
ctx context.Context,
conn pg.Conn,
scope *Scope,
scope Scoper,
params UpdateVendorParams,
) error {
q := `

View File

@@ -81,11 +81,11 @@ func (s Service) CreateControl(
return fmt.Errorf("cannot load framework %q: %w", req.FrameworkID, err)
}
if err := control.Insert(ctx, conn); err != nil {
if err := control.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert control: %w", err)
}
if err := controlStateTransition.Insert(ctx, conn); err != nil {
if err := controlStateTransition.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert control state transition: %w", err)
}

View File

@@ -117,11 +117,11 @@ func (s Service) CreateEvidence(
return fmt.Errorf("cannot load task %q: %w", req.TaskID, err)
}
if err := evidence.Insert(ctx, conn); err != nil {
if err := evidence.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert evidence: %w", err)
}
if err := evidenceStateTransition.Insert(ctx, conn); err != nil {
if err := evidenceStateTransition.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert evidence state transition: %w", err)
}

View File

@@ -56,7 +56,7 @@ func (s Service) CreateFramework(
err = s.pg.WithConn(
ctx,
func(conn pg.Conn) error {
return framework.Insert(ctx, conn)
return framework.Insert(ctx, conn, s.scope)
},
)

View File

@@ -50,7 +50,7 @@ func (s Service) CreateOrganization(
err = s.pg.WithConn(
ctx,
func(conn pg.Conn) error {
if err := organization.Insert(ctx, conn); err != nil {
if err := organization.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert control: %w", err)
}

View File

@@ -63,7 +63,7 @@ func (s Service) CreatePeople(
return fmt.Errorf("cannot load organization %q: %w", req.OrganizationID, err)
}
if err := people.Insert(ctx, conn); err != nil {
if err := people.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert people: %w", err)
}

View File

@@ -79,11 +79,11 @@ func (s Service) CreateTask(
return fmt.Errorf("cannot laod control %q: %w", req.ControlID, err)
}
if err := task.Insert(ctx, conn); err != nil {
if err := task.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert task: %w", err)
}
if err := taskStateTransition.Insert(ctx, conn); err != nil {
if err := taskStateTransition.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert task state transition: %w", err)
}

View File

@@ -73,7 +73,7 @@ func (s Service) CreateVendor(
return fmt.Errorf("cannot load organization %q: %w", req.OrganizationID, err)
}
if err := vendor.Insert(ctx, conn); err != nil {
if err := vendor.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert vendor: %w", err)
}

View File

@@ -86,7 +86,7 @@ func (s *PolicyService) Create(
return fmt.Errorf("cannot load organization %q: %w", req.OrganizationID, err)
}
if err := policy.Insert(ctx, conn); err != nil {
if err := policy.Insert(ctx, conn, s.svc.scope); err != nil {
return fmt.Errorf("cannot insert policy: %w", err)
}

View File

@@ -27,9 +27,9 @@ import (
type (
Service struct {
pg *pg.Client
scope *coredata.Scope
s3 *s3.Client
bucket string
scope coredata.Scoper
Policies *PolicyService
}
@@ -53,11 +53,24 @@ func NewService(
svc := &Service{
pg: pgClient,
s3: s3Client,
scope: coredata.NewScope(), // must be created from auth
bucket: bucket,
scope: coredata.NewNoScope(),
}
svc.Policies = &PolicyService{svc: svc}
return svc, nil
}
func (s *Service) WithTenant(tenantID string) *Service {
newSvc := &Service{
pg: s.pg,
s3: s.s3,
bucket: s.bucket,
scope: coredata.NewScope(tenantID),
}
newSvc.Policies = &PolicyService{svc: newSvc}
return newSvc
}

View File

@@ -72,7 +72,7 @@ func (s Service) UpdateTaskState(
err = s.pg.WithConn(
ctx,
func(conn pg.Conn) error {
if err := taskStateTransition.Insert(ctx, conn); err != nil {
if err := taskStateTransition.Insert(ctx, conn, s.scope); err != nil {
return fmt.Errorf("cannot insert task state transition: %w", err)
}