@@ -129,40 +129,3 @@ WHERE
|
||||
*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
|
||||
}
|
||||
|
||||
@@ -274,70 +274,3 @@ func (s ControlService) DisconnectFromMitigation(
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
func (s ControlService) ListMitigationsForControlID(
|
||||
ctx context.Context,
|
||||
controlID gid.GID,
|
||||
) ([]*coredata.Mitigation, error) {
|
||||
var controlMitigations coredata.ControlMitigations
|
||||
var mitigations []*coredata.Mitigation
|
||||
|
||||
err := s.svc.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
if err := controlMitigations.LoadByControlID(ctx, conn, s.svc.scope, controlID); err != nil {
|
||||
return fmt.Errorf("cannot load control mitigations: %w", err)
|
||||
}
|
||||
|
||||
for _, cm := range controlMitigations {
|
||||
mitigation := &coredata.Mitigation{}
|
||||
if err := mitigation.LoadByID(ctx, conn, s.svc.scope, cm.MitigationID); err != nil {
|
||||
return fmt.Errorf("cannot load mitigation: %w", err)
|
||||
}
|
||||
mitigations = append(mitigations, mitigation)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mitigations, nil
|
||||
}
|
||||
|
||||
// ListControlsForMitigationID retrieves all controls linked to a mitigation
|
||||
func (s ControlService) ListControlsForMitigationID(
|
||||
ctx context.Context,
|
||||
mitigationID gid.GID,
|
||||
) ([]*coredata.Control, error) {
|
||||
var controlMitigations coredata.ControlMitigations
|
||||
var controls []*coredata.Control
|
||||
|
||||
err := s.svc.pg.WithConn(
|
||||
ctx,
|
||||
func(conn pg.Conn) error {
|
||||
if err := controlMitigations.LoadByMitigationID(ctx, conn, s.svc.scope, mitigationID); err != nil {
|
||||
return fmt.Errorf("cannot load control mitigations: %w", err)
|
||||
}
|
||||
|
||||
for _, cm := range controlMitigations {
|
||||
control := &coredata.Control{}
|
||||
if err := control.LoadByID(ctx, conn, s.svc.scope, cm.ControlID); err != nil {
|
||||
return fmt.Errorf("cannot load control: %w", err)
|
||||
}
|
||||
controls = append(controls, control)
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return controls, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user