First step of mitigation migration
Signed-off-by: gearnode <bryan@frimin.fr>
This commit is contained in:
262
pkg/coredata/control.go
Normal file
262
pkg/coredata/control.go
Normal file
@@ -0,0 +1,262 @@
|
||||
// Copyright (c) 2025 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"
|
||||
"fmt"
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/getprobo/probo/pkg/page"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
)
|
||||
|
||||
type (
|
||||
Control struct {
|
||||
ID gid.GID `db:"id"`
|
||||
ReferenceID string `db:"reference_id"`
|
||||
FrameworkID gid.GID `db:"framework_id"`
|
||||
TenantID gid.TenantID `db:"tenant_id"`
|
||||
Name string `db:"name"`
|
||||
Description string `db:"description"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
}
|
||||
|
||||
Controls []*Control
|
||||
|
||||
UpdateControlParams struct {
|
||||
ExpectedVersion int
|
||||
Name *string
|
||||
Description *string
|
||||
}
|
||||
)
|
||||
|
||||
func (c Control) CursorKey(orderBy ControlOrderField) page.CursorKey {
|
||||
switch orderBy {
|
||||
case ControlOrderFieldCreatedAt:
|
||||
return page.CursorKey{ID: c.ID, Value: c.CreatedAt}
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
|
||||
}
|
||||
|
||||
func (c *Controls) LoadByFrameworkID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
frameworkID gid.GID,
|
||||
cursor *page.Cursor[ControlOrderField],
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
reference_id,
|
||||
framework_id,
|
||||
tenant_id,
|
||||
name,
|
||||
description,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
controls
|
||||
WHERE
|
||||
%s
|
||||
AND framework_id = @framework_id
|
||||
AND %s
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"framework_id": frameworkID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query controls: %w", err)
|
||||
}
|
||||
|
||||
controls, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[Control])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect controls: %w", err)
|
||||
}
|
||||
|
||||
*c = controls
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Control) LoadByID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
controlID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
reference_id,
|
||||
framework_id,
|
||||
tenant_id,
|
||||
name,
|
||||
description,
|
||||
created_at,
|
||||
updated_at
|
||||
FROM
|
||||
controls
|
||||
WHERE
|
||||
%s
|
||||
AND id = @control_id
|
||||
LIMIT 1;
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"control_id": controlID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query controls: %w", err)
|
||||
}
|
||||
|
||||
control, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Control])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect control: %w", err)
|
||||
}
|
||||
|
||||
*c = control
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c Control) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO
|
||||
controls (
|
||||
tenant_id,
|
||||
id,
|
||||
framework_id,
|
||||
reference_id,
|
||||
name,
|
||||
description,
|
||||
created_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (
|
||||
@tenant_id,
|
||||
@control_id,
|
||||
@framework_id,
|
||||
@reference_id,
|
||||
@name,
|
||||
@description,
|
||||
@created_at,
|
||||
@updated_at
|
||||
);
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"control_id": c.ID,
|
||||
"framework_id": c.FrameworkID,
|
||||
"reference_id": c.ReferenceID,
|
||||
"name": c.Name,
|
||||
"description": c.Description,
|
||||
"created_at": c.CreatedAt,
|
||||
"updated_at": c.UpdatedAt,
|
||||
}
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c Control) Delete(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
DELETE
|
||||
FROM
|
||||
controls
|
||||
WHERE
|
||||
%s
|
||||
AND id = @control_id;
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{"control_id": c.ID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Control) Update(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
params UpdateControlParams,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE controls SET
|
||||
name = COALESCE(@name, name),
|
||||
description = COALESCE(@description, description),
|
||||
updated_at = @updated_at
|
||||
WHERE %s
|
||||
AND id = @control_id
|
||||
RETURNING
|
||||
id,
|
||||
framework_id,
|
||||
tenant_id,
|
||||
name,
|
||||
description,
|
||||
created_at,
|
||||
updated_at
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"control_id": c.ID,
|
||||
"expected_version": params.ExpectedVersion,
|
||||
"updated_at": time.Now(),
|
||||
}
|
||||
|
||||
if params.Name != nil {
|
||||
args["name"] = *params.Name
|
||||
}
|
||||
if params.Description != nil {
|
||||
args["description"] = *params.Description
|
||||
}
|
||||
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query controls: %w", err)
|
||||
}
|
||||
|
||||
control, err := pgx.CollectExactlyOneRow(rows, pgx.RowToStructByName[Control])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect control: %w", err)
|
||||
}
|
||||
|
||||
*c = control
|
||||
|
||||
return nil
|
||||
}
|
||||
168
pkg/coredata/control_mitigation.go
Normal file
168
pkg/coredata/control_mitigation.go
Normal file
@@ -0,0 +1,168 @@
|
||||
// Copyright (c) 2025 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"
|
||||
"fmt"
|
||||
"maps"
|
||||
"time"
|
||||
|
||||
"github.com/getprobo/probo/pkg/gid"
|
||||
"github.com/jackc/pgx/v5"
|
||||
"go.gearno.de/kit/pg"
|
||||
)
|
||||
|
||||
type (
|
||||
ControlMitigation struct {
|
||||
ControlID gid.GID `db:"control_id"`
|
||||
MitigationID gid.GID `db:"mitigation_id"`
|
||||
TenantID gid.TenantID `db:"tenant_id"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
}
|
||||
|
||||
ControlMitigations []*ControlMitigation
|
||||
)
|
||||
|
||||
func (cm ControlMitigation) Insert(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
INSERT INTO
|
||||
control_mitigations (
|
||||
control_id,
|
||||
mitigation_id,
|
||||
tenant_id,
|
||||
created_at
|
||||
)
|
||||
VALUES (
|
||||
@control_id,
|
||||
@mitigation_id,
|
||||
@tenant_id,
|
||||
@created_at
|
||||
);
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"control_id": cm.ControlID,
|
||||
"mitigation_id": cm.MitigationID,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"created_at": cm.CreatedAt,
|
||||
}
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
return err
|
||||
}
|
||||
|
||||
func (cm ControlMitigation) Delete(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
) error {
|
||||
q := `
|
||||
DELETE
|
||||
FROM
|
||||
control_mitigations
|
||||
WHERE
|
||||
%s
|
||||
AND control_id = @control_id
|
||||
AND mitigation_id = @mitigation_id;
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"control_id": cm.ControlID,
|
||||
"mitigation_id": cm.MitigationID,
|
||||
}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
return err
|
||||
}
|
||||
|
||||
func (cms *ControlMitigations) LoadByMitigationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
mitigationID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
control_id,
|
||||
mitigation_id,
|
||||
tenant_id,
|
||||
created_at
|
||||
FROM
|
||||
control_mitigations
|
||||
WHERE
|
||||
%s
|
||||
AND mitigation_id = @mitigation_id
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"mitigation_id": mitigationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query control_mitigations: %w", err)
|
||||
}
|
||||
|
||||
controlMitigations, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[ControlMitigation])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect control_mitigations: %w", err)
|
||||
}
|
||||
|
||||
*cms = controlMitigations
|
||||
return nil
|
||||
}
|
||||
|
||||
func (cms *ControlMitigations) LoadByControlID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
controlID gid.GID,
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
control_id,
|
||||
mitigation_id,
|
||||
tenant_id,
|
||||
created_at
|
||||
FROM
|
||||
control_mitigations
|
||||
WHERE
|
||||
%s
|
||||
AND control_id = @control_id
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"control_id": controlID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot query control_mitigations: %w", err)
|
||||
}
|
||||
|
||||
controlMitigations, err := pgx.CollectRows(rows, pgx.RowToAddrOfStructByName[ControlMitigation])
|
||||
if err != nil {
|
||||
return fmt.Errorf("cannot collect control_mitigations: %w", err)
|
||||
}
|
||||
|
||||
*cms = controlMitigations
|
||||
return nil
|
||||
}
|
||||
40
pkg/coredata/control_order_field.go
Normal file
40
pkg/coredata/control_order_field.go
Normal file
@@ -0,0 +1,40 @@
|
||||
// Copyright (c) 2025 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
|
||||
|
||||
type (
|
||||
ControlOrderField string
|
||||
)
|
||||
|
||||
const (
|
||||
ControlOrderFieldCreatedAt ControlOrderField = "CREATED_AT"
|
||||
)
|
||||
|
||||
func (p ControlOrderField) Column() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
func (p ControlOrderField) String() string {
|
||||
return string(p)
|
||||
}
|
||||
|
||||
func (p ControlOrderField) MarshalText() ([]byte, error) {
|
||||
return []byte(p.String()), nil
|
||||
}
|
||||
|
||||
func (p *ControlOrderField) UnmarshalText(text []byte) error {
|
||||
*p = ControlOrderField(text)
|
||||
return nil
|
||||
}
|
||||
@@ -29,4 +29,5 @@ const (
|
||||
UserEntityType
|
||||
SessionEntityType
|
||||
EmailEntityType
|
||||
ControlEntityType
|
||||
)
|
||||
|
||||
@@ -30,27 +30,20 @@ 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"`
|
||||
ContentRef string `db:"content_ref"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
Version int `db:"version"`
|
||||
}
|
||||
|
||||
Frameworks []*Framework
|
||||
|
||||
UpdateFrameworkParams struct {
|
||||
ExpectedVersion int
|
||||
Name *string
|
||||
Description *string
|
||||
}
|
||||
)
|
||||
|
||||
func (f Framework) CursorKey(orderBy FrameworkOrderField) page.CursorKey {
|
||||
func (f *Framework) CursorKey(orderBy FrameworkOrderField) page.CursorKey {
|
||||
switch orderBy {
|
||||
case FrameworkOrderFieldCreatedAt:
|
||||
return page.NewCursorKey(f.ID, f.CreatedAt)
|
||||
return page.CursorKey{ID: f.ID, Value: f.CreatedAt}
|
||||
}
|
||||
|
||||
panic(fmt.Sprintf("unsupported order by: %s", orderBy))
|
||||
@@ -67,25 +60,23 @@ func (f *Frameworks) LoadByOrganizationID(
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
reference_id,
|
||||
name,
|
||||
description,
|
||||
content_ref,
|
||||
created_at,
|
||||
updated_at,
|
||||
version
|
||||
updated_at
|
||||
FROM
|
||||
frameworks
|
||||
WHERE
|
||||
%s
|
||||
AND organization_id = @organization_id
|
||||
AND %s
|
||||
AND %s
|
||||
`
|
||||
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.NamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
rows, err := conn.Query(ctx, q, args)
|
||||
if err != nil {
|
||||
@@ -112,12 +103,11 @@ func (f *Framework) LoadByID(
|
||||
SELECT
|
||||
id,
|
||||
organization_id,
|
||||
reference_id,
|
||||
name,
|
||||
description,
|
||||
content_ref,
|
||||
created_at,
|
||||
updated_at,
|
||||
version
|
||||
updated_at
|
||||
FROM
|
||||
frameworks
|
||||
WHERE
|
||||
@@ -156,23 +146,21 @@ INSERT INTO
|
||||
tenant_id,
|
||||
id,
|
||||
organization_id,
|
||||
reference_id,
|
||||
name,
|
||||
description,
|
||||
content_ref,
|
||||
created_at,
|
||||
updated_at,
|
||||
version
|
||||
updated_at
|
||||
)
|
||||
VALUES (
|
||||
@tenant_id,
|
||||
@framework_id,
|
||||
@organization_id,
|
||||
@reference_id,
|
||||
@name,
|
||||
@description,
|
||||
@content_ref,
|
||||
@created_at,
|
||||
@updated_at,
|
||||
@version
|
||||
@updated_at
|
||||
);
|
||||
`
|
||||
|
||||
@@ -180,12 +168,11 @@ VALUES (
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"framework_id": f.ID,
|
||||
"organization_id": f.OrganizationID,
|
||||
"reference_id": f.ReferenceID,
|
||||
"name": f.Name,
|
||||
"description": f.Description,
|
||||
"content_ref": f.ContentRef,
|
||||
"created_at": f.CreatedAt,
|
||||
"updated_at": f.UpdatedAt,
|
||||
"version": f.Version,
|
||||
}
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
return err
|
||||
@@ -217,55 +204,28 @@ func (f *Framework) Update(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
params UpdateFrameworkParams,
|
||||
) error {
|
||||
q := `
|
||||
UPDATE frameworks SET
|
||||
name = COALESCE(@name, name),
|
||||
description = COALESCE(@description, description),
|
||||
updated_at = @updated_at,
|
||||
version = version + 1
|
||||
WHERE %s
|
||||
AND id = @framework_id
|
||||
AND version = @expected_version
|
||||
RETURNING
|
||||
id,
|
||||
organization_id,
|
||||
name,
|
||||
description,
|
||||
content_ref,
|
||||
created_at,
|
||||
updated_at,
|
||||
version
|
||||
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,
|
||||
"expected_version": params.ExpectedVersion,
|
||||
"updated_at": time.Now(),
|
||||
}
|
||||
|
||||
if params.Name != nil {
|
||||
args["name"] = *params.Name
|
||||
}
|
||||
if params.Description != nil {
|
||||
args["description"] = *params.Description
|
||||
"framework_id": f.ID,
|
||||
"updated_at": f.UpdatedAt,
|
||||
"name": f.Name,
|
||||
"description": f.Description,
|
||||
}
|
||||
|
||||
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 {
|
||||
return fmt.Errorf("cannot collect framework: %w", err)
|
||||
}
|
||||
|
||||
*f = framework
|
||||
|
||||
return nil
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
return err
|
||||
}
|
||||
|
||||
18
pkg/coredata/migrations/20250327T122105Z.sql
Normal file
18
pkg/coredata/migrations/20250327T122105Z.sql
Normal file
@@ -0,0 +1,18 @@
|
||||
-- Add organization_id column to mitigations table
|
||||
ALTER TABLE mitigations ADD COLUMN organization_id TEXT;
|
||||
|
||||
-- Update mitigations to set organization_id based on framework's organization_id
|
||||
UPDATE mitigations m
|
||||
SET organization_id = f.organization_id
|
||||
FROM frameworks f
|
||||
WHERE m.framework_id = f.id;
|
||||
|
||||
-- Make organization_id NOT NULL after update
|
||||
ALTER TABLE mitigations ALTER COLUMN organization_id SET NOT NULL;
|
||||
|
||||
-- Add foreign key constraint
|
||||
ALTER TABLE mitigations ADD CONSTRAINT fk_mitigations_organization_id
|
||||
FOREIGN KEY (organization_id) REFERENCES organizations(id) ON DELETE CASCADE;
|
||||
|
||||
-- Add index for performance
|
||||
CREATE INDEX idx_mitigations_organization_id ON mitigations(organization_id);
|
||||
8
pkg/coredata/migrations/20250327T122609Z.sql
Normal file
8
pkg/coredata/migrations/20250327T122609Z.sql
Normal file
@@ -0,0 +1,8 @@
|
||||
-- Drop the foreign key constraint first
|
||||
ALTER TABLE mitigations DROP CONSTRAINT IF EXISTS fk_mitigations_framework_id;
|
||||
|
||||
-- Drop any indices on framework_id
|
||||
DROP INDEX IF EXISTS idx_mitigations_framework_id;
|
||||
|
||||
-- Remove the framework_id column
|
||||
ALTER TABLE mitigations DROP COLUMN framework_id;
|
||||
1
pkg/coredata/migrations/20250327T210900Z.sql
Normal file
1
pkg/coredata/migrations/20250327T210900Z.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE frameworks ADD COLUMN reference_id TEXT;
|
||||
2
pkg/coredata/migrations/20250327T212900Z.sql
Normal file
2
pkg/coredata/migrations/20250327T212900Z.sql
Normal file
@@ -0,0 +1,2 @@
|
||||
ALTER TABLE controls ADD COLUMN reference_id TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE controls ALTER COLUMN reference_id DROP DEFAULT;
|
||||
1
pkg/coredata/migrations/20250327T220600Z.sql
Normal file
1
pkg/coredata/migrations/20250327T220600Z.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE frameworks DROP COLUMN content_ref;
|
||||
1
pkg/coredata/migrations/20250327T220601Z.sql
Normal file
1
pkg/coredata/migrations/20250327T220601Z.sql
Normal file
@@ -0,0 +1 @@
|
||||
ALTER TABLE frameworks DROP COLUMN version;
|
||||
19
pkg/coredata/migrations/20250607T120000Z.sql
Normal file
19
pkg/coredata/migrations/20250607T120000Z.sql
Normal file
@@ -0,0 +1,19 @@
|
||||
-- Create a new table for controls with string IDs
|
||||
CREATE TABLE controls (
|
||||
id TEXT PRIMARY KEY,
|
||||
tenant_id TEXT NOT NULL,
|
||||
framework_id TEXT NOT NULL,
|
||||
name TEXT NOT NULL,
|
||||
description TEXT NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
updated_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
version INTEGER NOT NULL DEFAULT 0
|
||||
);
|
||||
|
||||
CREATE TABLE controls_mitigations (
|
||||
control_id TEXT NOT NULL,
|
||||
mitigation_id TEXT NOT NULL,
|
||||
tenant_id TEXT NOT NULL,
|
||||
created_at TIMESTAMP WITH TIME ZONE NOT NULL,
|
||||
PRIMARY KEY (control_id, mitigation_id)
|
||||
);
|
||||
@@ -29,18 +29,18 @@ import (
|
||||
|
||||
type (
|
||||
Mitigation struct {
|
||||
ID gid.GID `db:"id"`
|
||||
FrameworkID gid.GID `db:"framework_id"`
|
||||
Category string `db:"category"`
|
||||
Name string `db:"name"`
|
||||
Description string `db:"description"`
|
||||
Importance MitigationImportance `db:"importance"`
|
||||
State MitigationState `db:"state"`
|
||||
ContentRef string `db:"content_ref"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
Version int `db:"version"`
|
||||
Standards []string `db:"standards"`
|
||||
ID gid.GID `db:"id"`
|
||||
OrganizationID gid.GID `db:"organization_id"`
|
||||
Category string `db:"category"`
|
||||
Name string `db:"name"`
|
||||
Description string `db:"description"`
|
||||
Importance MitigationImportance `db:"importance"`
|
||||
State MitigationState `db:"state"`
|
||||
ContentRef string `db:"content_ref"`
|
||||
CreatedAt time.Time `db:"created_at"`
|
||||
UpdatedAt time.Time `db:"updated_at"`
|
||||
Version int `db:"version"`
|
||||
Standards []string `db:"standards"`
|
||||
}
|
||||
|
||||
Mitigations []*Mitigation
|
||||
@@ -73,7 +73,7 @@ func (c *Mitigation) LoadByID(
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
framework_id,
|
||||
organization_id,
|
||||
category,
|
||||
name,
|
||||
description,
|
||||
@@ -122,7 +122,7 @@ INSERT INTO
|
||||
mitigations (
|
||||
tenant_id,
|
||||
id,
|
||||
framework_id,
|
||||
organization_id,
|
||||
category,
|
||||
name,
|
||||
importance,
|
||||
@@ -137,7 +137,7 @@ INSERT INTO
|
||||
VALUES (
|
||||
@tenant_id,
|
||||
@mitigation_id,
|
||||
@framework_id,
|
||||
@organization_id,
|
||||
@category,
|
||||
@name,
|
||||
@importance,
|
||||
@@ -152,35 +152,35 @@ VALUES (
|
||||
`
|
||||
|
||||
args := pgx.StrictNamedArgs{
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"mitigation_id": c.ID,
|
||||
"framework_id": c.FrameworkID,
|
||||
"category": c.Category,
|
||||
"name": c.Name,
|
||||
"version": 0,
|
||||
"description": c.Description,
|
||||
"content_ref": c.ContentRef,
|
||||
"created_at": c.CreatedAt,
|
||||
"updated_at": c.UpdatedAt,
|
||||
"state": c.State,
|
||||
"importance": c.Importance,
|
||||
"standards": c.Standards,
|
||||
"tenant_id": scope.GetTenantID(),
|
||||
"mitigation_id": c.ID,
|
||||
"organization_id": c.OrganizationID,
|
||||
"category": c.Category,
|
||||
"name": c.Name,
|
||||
"version": 0,
|
||||
"description": c.Description,
|
||||
"content_ref": c.ContentRef,
|
||||
"created_at": c.CreatedAt,
|
||||
"updated_at": c.UpdatedAt,
|
||||
"state": c.State,
|
||||
"importance": c.Importance,
|
||||
"standards": c.Standards,
|
||||
}
|
||||
_, err := conn.Exec(ctx, q, args)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Mitigations) LoadByFrameworkID(
|
||||
func (c *Mitigations) LoadByOrganizationID(
|
||||
ctx context.Context,
|
||||
conn pg.Conn,
|
||||
scope Scoper,
|
||||
frameworkID gid.GID,
|
||||
organizationID gid.GID,
|
||||
cursor *page.Cursor[MitigationOrderField],
|
||||
) error {
|
||||
q := `
|
||||
SELECT
|
||||
id,
|
||||
framework_id,
|
||||
organization_id,
|
||||
category,
|
||||
name,
|
||||
description,
|
||||
@@ -195,12 +195,12 @@ FROM
|
||||
mitigations
|
||||
WHERE
|
||||
%s
|
||||
AND framework_id = @framework_id
|
||||
AND organization_id = @organization_id
|
||||
AND %s
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment(), cursor.SQLFragment())
|
||||
|
||||
args := pgx.StrictNamedArgs{"framework_id": frameworkID}
|
||||
args := pgx.StrictNamedArgs{"organization_id": organizationID}
|
||||
maps.Copy(args, scope.SQLArguments())
|
||||
maps.Copy(args, cursor.SQLArguments())
|
||||
|
||||
@@ -239,7 +239,7 @@ WHERE %s
|
||||
AND version = @expected_version
|
||||
RETURNING
|
||||
id,
|
||||
framework_id,
|
||||
organization_id,
|
||||
category,
|
||||
name,
|
||||
description,
|
||||
@@ -248,8 +248,8 @@ RETURNING
|
||||
content_ref,
|
||||
created_at,
|
||||
updated_at,
|
||||
version,
|
||||
standards
|
||||
standards,
|
||||
version
|
||||
`
|
||||
q = fmt.Sprintf(q, scope.SQLFragment())
|
||||
|
||||
|
||||
Reference in New Issue
Block a user