Skip to content

Commit 0dd9978

Browse files
trustcenter compliance implementation (#1069)
* trust center compliance * more stuff * regen Signed-off-by: Sarah Funkhouser <[email protected]> * fix missing builders, generate db update Signed-off-by: Sarah Funkhouser <[email protected]> * rebase Signed-off-by: Sarah Funkhouser <[email protected]> * fix test Signed-off-by: Sarah Funkhouser <[email protected]> * revert db to rebase Signed-off-by: Sarah Funkhouser <[email protected]> * regen db migrations Signed-off-by: Sarah Funkhouser <[email protected]> --------- Signed-off-by: Sarah Funkhouser <[email protected]> Co-authored-by: Sarah Funkhouser <[email protected]>
1 parent ce149f1 commit 0dd9978

File tree

84 files changed

+8750
-582
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

84 files changed

+8750
-582
lines changed

cmd/cli/Taskfile.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ tasks:
178178
STORE_ID:
179179
sh: fga store list --api-token={{ .FGA_API_TOKEN }} | jq -r '.stores.[].id'
180180
USER_ID:
181-
sh: ../../openlane-cli user get -z json --host {{ .OPENLANE_API_HOST }} | jq -r '.self.id'
181+
sh: go run main.go user get -z json --host {{ .OPENLANE_API_HOST }} | jq -r '.self.id'
182182
cmds:
183183
- fga tuple write --store-id={{ .STORE_ID }} user:{{ .USER_ID }} system_admin system:openlane_core --api-token={{ .FGA_API_TOKEN }}
184184

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package trustcentercompliance
2+
3+
import (
4+
"context"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.com/theopenlane/core/cmd/cli/cmd"
9+
"github.com/theopenlane/core/pkg/openlaneclient"
10+
)
11+
12+
var createCmd = &cobra.Command{
13+
Use: "create",
14+
Short: "create a new trust center compliance",
15+
Run: func(cmd *cobra.Command, args []string) {
16+
err := create(cmd.Context())
17+
cobra.CheckErr(err)
18+
},
19+
}
20+
21+
func init() {
22+
command.AddCommand(createCmd)
23+
24+
createCmd.Flags().StringP("standard-id", "s", "", "standard id for the compliance (required)")
25+
createCmd.Flags().StringP("trust-center-id", "t", "", "trust center id for the compliance")
26+
createCmd.Flags().StringSliceP("tags", "", []string{}, "tags associated with the trust center compliance")
27+
}
28+
29+
// createValidation validates the required fields for the command
30+
func createValidation() (*openlaneclient.CreateTrustCenterComplianceInput, error) {
31+
standardID := cmd.Config.String("standard-id")
32+
if standardID == "" {
33+
return nil, cmd.NewRequiredFieldMissingError("standard id")
34+
}
35+
36+
input := &openlaneclient.CreateTrustCenterComplianceInput{
37+
StandardID: standardID,
38+
}
39+
40+
trustCenterID := cmd.Config.String("trust-center-id")
41+
if trustCenterID != "" {
42+
input.TrustCenterID = &trustCenterID
43+
}
44+
45+
tags := cmd.Config.Strings("tags")
46+
if len(tags) > 0 {
47+
input.Tags = tags
48+
}
49+
50+
return input, nil
51+
}
52+
53+
// create a new trust center compliance
54+
func create(ctx context.Context) error {
55+
// attempt to setup with token, otherwise fall back to JWT with session
56+
client, err := cmd.TokenAuth(ctx, cmd.Config)
57+
if err != nil || client == nil {
58+
// setup http client
59+
client, err = cmd.SetupClientWithAuth(ctx)
60+
cobra.CheckErr(err)
61+
defer cmd.StoreSessionCookies(client)
62+
}
63+
64+
input, err := createValidation()
65+
cobra.CheckErr(err)
66+
67+
o, err := client.CreateTrustCenterCompliance(ctx, *input)
68+
cobra.CheckErr(err)
69+
70+
return consoleOutput(o)
71+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package trustcentercompliance
2+
3+
import (
4+
"context"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.com/theopenlane/core/cmd/cli/cmd"
9+
)
10+
11+
var deleteCmd = &cobra.Command{
12+
Use: "delete",
13+
Short: "delete an existing trust center compliance",
14+
Run: func(cmd *cobra.Command, args []string) {
15+
err := delete(cmd.Context())
16+
cobra.CheckErr(err)
17+
},
18+
}
19+
20+
func init() {
21+
command.AddCommand(deleteCmd)
22+
23+
deleteCmd.Flags().StringP("id", "i", "", "trust center compliance id to delete")
24+
}
25+
26+
// deleteValidation validates the required fields for the command
27+
func deleteValidation() (string, error) {
28+
id := cmd.Config.String("id")
29+
if id == "" {
30+
return "", cmd.NewRequiredFieldMissingError("trust center compliance id")
31+
}
32+
33+
return id, nil
34+
}
35+
36+
// delete an existing trust center compliance in the platform
37+
func delete(ctx context.Context) error {
38+
// attempt to setup with token, otherwise fall back to JWT with session
39+
client, err := cmd.TokenAuth(ctx, cmd.Config)
40+
if err != nil || client == nil {
41+
// setup http client
42+
client, err = cmd.SetupClientWithAuth(ctx)
43+
cobra.CheckErr(err)
44+
defer cmd.StoreSessionCookies(client)
45+
}
46+
47+
id, err := deleteValidation()
48+
cobra.CheckErr(err)
49+
50+
o, err := client.DeleteTrustCenterCompliance(ctx, id)
51+
cobra.CheckErr(err)
52+
53+
return consoleOutput(o)
54+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package trustcentercompliance
2+
3+
import (
4+
"context"
5+
6+
"github.com/spf13/cobra"
7+
8+
"github.com/theopenlane/core/cmd/cli/cmd"
9+
)
10+
11+
var getCmd = &cobra.Command{
12+
Use: "get",
13+
Short: "get an existing trust center compliance",
14+
Run: func(cmd *cobra.Command, args []string) {
15+
err := get(cmd.Context())
16+
cobra.CheckErr(err)
17+
},
18+
}
19+
20+
func init() {
21+
command.AddCommand(getCmd)
22+
23+
getCmd.Flags().StringP("id", "i", "", "trust center compliance id to retrieve")
24+
}
25+
26+
// get an existing trust center compliance in the platform
27+
func get(ctx context.Context) error {
28+
// attempt to setup with token, otherwise fall back to JWT with session
29+
client, err := cmd.TokenAuth(ctx, cmd.Config)
30+
if err != nil || client == nil {
31+
// setup http client
32+
client, err = cmd.SetupClientWithAuth(ctx)
33+
cobra.CheckErr(err)
34+
defer cmd.StoreSessionCookies(client)
35+
}
36+
// filter options
37+
id := cmd.Config.String("id")
38+
39+
// if a trust center compliance ID is provided, filter on that trust center compliance, otherwise get all
40+
if id != "" {
41+
o, err := client.GetTrustCenterComplianceByID(ctx, id)
42+
cobra.CheckErr(err)
43+
44+
return consoleOutput(o)
45+
}
46+
47+
// get all will be filtered for the authorized organization(s)
48+
o, err := client.GetAllTrustCenterCompliances(ctx)
49+
cobra.CheckErr(err)
50+
51+
return consoleOutput(o)
52+
}
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
package trustcentercompliance
2+
3+
import (
4+
"encoding/json"
5+
"strings"
6+
7+
"github.com/spf13/cobra"
8+
9+
"github.com/theopenlane/utils/cli/tables"
10+
11+
"github.com/theopenlane/core/cmd/cli/cmd"
12+
"github.com/theopenlane/core/pkg/openlaneclient"
13+
)
14+
15+
// command represents the base trustcentercompliance command when called without any subcommands
16+
var command = &cobra.Command{
17+
Use: "trustcentercompliance",
18+
Short: "the subcommands for working with trust center compliances",
19+
}
20+
21+
func init() {
22+
cmd.RootCmd.AddCommand(command)
23+
}
24+
25+
// consoleOutput prints the output in the console
26+
func consoleOutput(e any) error {
27+
// check if the output format is JSON and print the trust center compliances in JSON format
28+
if strings.EqualFold(cmd.OutputFormat, cmd.JSONOutput) {
29+
return jsonOutput(e)
30+
}
31+
32+
// check if the output is a slice of trust center compliances
33+
if trustCenterCompliances, ok := e.(*openlaneclient.GetAllTrustCenterCompliances); ok {
34+
var nodes []*openlaneclient.GetAllTrustCenterCompliances_TrustCenterCompliances_Edges_Node
35+
36+
for _, edge := range trustCenterCompliances.TrustCenterCompliances.Edges {
37+
nodes = append(nodes, edge.Node)
38+
}
39+
40+
e = nodes
41+
}
42+
43+
// check if the output is a single trust center compliance
44+
if trustCenterCompliance, ok := e.(*openlaneclient.GetTrustCenterComplianceByID); ok {
45+
e = trustCenterCompliance.TrustCenterCompliance
46+
}
47+
48+
// check if the output is a create trust center compliance response
49+
if createResp, ok := e.(*openlaneclient.CreateTrustCenterCompliance); ok {
50+
e = createResp.CreateTrustCenterCompliance.TrustCenterCompliance
51+
}
52+
53+
// check if the output is a delete trust center compliance response
54+
if deleteResp, ok := e.(*openlaneclient.DeleteTrustCenterCompliance); ok {
55+
deletedTableOutput(deleteResp)
56+
return nil
57+
}
58+
59+
s, err := json.Marshal(e)
60+
cobra.CheckErr(err)
61+
62+
var list []openlaneclient.GetAllTrustCenterCompliances_TrustCenterCompliances_Edges_Node
63+
64+
err = json.Unmarshal(s, &list)
65+
if err != nil {
66+
var in openlaneclient.GetAllTrustCenterCompliances_TrustCenterCompliances_Edges_Node
67+
err = json.Unmarshal(s, &in)
68+
cobra.CheckErr(err)
69+
70+
list = append(list, in)
71+
}
72+
73+
tableOutput(list)
74+
75+
return nil
76+
}
77+
78+
// jsonOutput prints the output in a JSON format
79+
func jsonOutput(out any) error {
80+
s, err := json.Marshal(out)
81+
cobra.CheckErr(err)
82+
83+
return cmd.JSONPrint(s)
84+
}
85+
86+
// tableOutput prints the output in a table format
87+
func tableOutput(out []openlaneclient.GetAllTrustCenterCompliances_TrustCenterCompliances_Edges_Node) {
88+
writer := tables.NewTableWriter(command.OutOrStdout(), "ID", "STANDARD", "TAGS", "CREATED", "UPDATED")
89+
for _, i := range out {
90+
writer.AddRow(i.ID, i.Standard.Name, strings.Join(i.Tags, ", "), *i.CreatedAt, *i.UpdatedAt)
91+
}
92+
93+
writer.Render()
94+
}
95+
96+
// deletedTableOutput prints the deleted id in a table format
97+
func deletedTableOutput(e *openlaneclient.DeleteTrustCenterCompliance) {
98+
writer := tables.NewTableWriter(command.OutOrStdout(), "DeletedID")
99+
100+
writer.AddRow(e.DeleteTrustCenterCompliance.DeletedID)
101+
102+
writer.Render()
103+
}

cmd/cli/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ import (
5252
_ "github.com/theopenlane/core/cmd/cli/cmd/task"
5353
_ "github.com/theopenlane/core/cmd/cli/cmd/template"
5454
_ "github.com/theopenlane/core/cmd/cli/cmd/trustcenter"
55+
_ "github.com/theopenlane/core/cmd/cli/cmd/trustcentercompliance"
5556
_ "github.com/theopenlane/core/cmd/cli/cmd/trustcentersubprocessors"
5657
_ "github.com/theopenlane/core/cmd/cli/cmd/user"
5758
_ "github.com/theopenlane/core/cmd/cli/cmd/usersetting"
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- +goose Up
2+
-- modify "trust_center_compliance_history" table
3+
ALTER TABLE "trust_center_compliance_history" ADD COLUMN "standard_id" character varying NOT NULL, ADD COLUMN "trust_center_id" character varying NULL;
4+
-- modify "trust_center_compliances" table
5+
ALTER TABLE "trust_center_compliances" ADD COLUMN "standard_id" character varying NOT NULL, ADD COLUMN "trust_center_id" character varying NULL, ADD CONSTRAINT "trust_center_compliances_standards_trust_center_compliances" FOREIGN KEY ("standard_id") REFERENCES "standards" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION, ADD CONSTRAINT "trust_center_compliances_trust_centers_trust_center_compliances" FOREIGN KEY ("trust_center_id") REFERENCES "trust_centers" ("id") ON UPDATE NO ACTION ON DELETE SET NULL;
6+
-- create index "trustcentercompliance_standard_id_trust_center_id" to table: "trust_center_compliances"
7+
CREATE UNIQUE INDEX "trustcentercompliance_standard_id_trust_center_id" ON "trust_center_compliances" ("standard_id", "trust_center_id") WHERE (deleted_at IS NULL);
8+
9+
-- +goose Down
10+
-- reverse: create index "trustcentercompliance_standard_id_trust_center_id" to table: "trust_center_compliances"
11+
DROP INDEX "trustcentercompliance_standard_id_trust_center_id";
12+
-- reverse: modify "trust_center_compliances" table
13+
ALTER TABLE "trust_center_compliances" DROP CONSTRAINT "trust_center_compliances_trust_centers_trust_center_compliances", DROP CONSTRAINT "trust_center_compliances_standards_trust_center_compliances", DROP COLUMN "trust_center_id", DROP COLUMN "standard_id";
14+
-- reverse: modify "trust_center_compliance_history" table
15+
ALTER TABLE "trust_center_compliance_history" DROP COLUMN "trust_center_id", DROP COLUMN "standard_id";

db/migrations-goose-postgres/atlas.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:8OUTAT6kWh5Z6a4qwGuiseWnE+k6dQy4mkn2tscyPl8=
1+
h1:5jN+xyZIVdYNS2S+SXXHctPXhTl11xIbvIn+fzDp0gQ=
22
20250414203515_init.sql h1:BeTEtWy9s9mO1t9vLhKE/W3Z/NZS7kklzlmvffjgMys=
33
20250418204251_control_objective.sql h1:2zhXUjbVshiTwzY0qh87de8Wh8j2tLDO6KRbn6oohx4=
44
20250421141007_aauid_not_unique_per_machine.sql h1:bMzfq+NBG4IFyOf/YintEKVhmQ6ttnQymzRV8/RiuPo=
@@ -45,3 +45,4 @@ h1:8OUTAT6kWh5Z6a4qwGuiseWnE+k6dQy4mkn2tscyPl8=
4545
20250808163552_control_implementation_edges.sql h1:8YfsszeS31hWUxiErgURoyKc8/p40bLpeEyjrN0MYmc=
4646
20250819212131_orgsettings_and_subscriptions.sql h1:qjd2SEObD9vLu+FuAx+oTNxcRedZQoPhfZRzZLMH4l0=
4747
20250820212023_remove_product_tier.sql h1:XBx7ebP1TcTb7h7rCgbBwbtR2kQPrl9ZRs/ZJjViPhA=
48+
20250822040401_tcc.sql h1:g38P80H1CkcVMPhmk48xSWrZ9C1UA4EHE1UYZqnlDos=

db/migrations/20250822040357_tcc.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
-- Modify "trust_center_compliance_history" table
2+
ALTER TABLE "trust_center_compliance_history" ADD COLUMN "standard_id" character varying NOT NULL, ADD COLUMN "trust_center_id" character varying NULL;
3+
-- Modify "trust_center_compliances" table
4+
ALTER TABLE "trust_center_compliances" ADD COLUMN "standard_id" character varying NOT NULL, ADD COLUMN "trust_center_id" character varying NULL, ADD CONSTRAINT "trust_center_compliances_standards_trust_center_compliances" FOREIGN KEY ("standard_id") REFERENCES "standards" ("id") ON UPDATE NO ACTION ON DELETE NO ACTION, ADD CONSTRAINT "trust_center_compliances_trust_centers_trust_center_compliances" FOREIGN KEY ("trust_center_id") REFERENCES "trust_centers" ("id") ON UPDATE NO ACTION ON DELETE SET NULL;
5+
-- Create index "trustcentercompliance_standard_id_trust_center_id" to table: "trust_center_compliances"
6+
CREATE UNIQUE INDEX "trustcentercompliance_standard_id_trust_center_id" ON "trust_center_compliances" ("standard_id", "trust_center_id") WHERE (deleted_at IS NULL);

db/migrations/atlas.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
h1:pCgPmd2ziiWwFGZgl60CISu+f8iRve8mSJpWVmTO4tY=
1+
h1:B0ht6NrdfS2W1EuWRyGWbhB8DpCvWPv1mABYKvdwwt4=
22
20250414203514_init.sql h1:NmCV8ML7NQDnzWVgI8QwOPofl3cp1WGzZWzPQhilEbY=
33
20250418204249_control_objective.sql h1:NVX8qKq77/54YdYSQZMyI+HS5RXNahqyyDtNHf9oMnQ=
44
20250421141003_aauid_not_unique_per_machine.sql h1:/ZFOjmdeEiPZl23+/MIjK1sxFWSi8PrMvV3kJ4iqbW4=
@@ -45,3 +45,4 @@ h1:pCgPmd2ziiWwFGZgl60CISu+f8iRve8mSJpWVmTO4tY=
4545
20250808163548_control_implementation_edges.sql h1:wCvMCI0eS9HwVgKevhg8yHG74PGTgQ7VfMra7MT7XXQ=
4646
20250819212125_orgsettings_and_subscriptions.sql h1:KEhXsHTi/cKNUIezSXUhYb7Wr73xHEJK6U4iBBDxhoE=
4747
20250820211949_remove_product_tier.sql h1:wQaB/V3qlWxZ3l90b9Gsx8Ac2W4QQ0sdxvRTPrNCS8M=
48+
20250822040357_tcc.sql h1:rDXuxw42aFQz0I9vZFdhVhU3SKG8EF7L214z5Q7DUv8=

0 commit comments

Comments
 (0)