Skip to content

Commit

Permalink
int32 database identifiers are evil!
Browse files Browse the repository at this point in the history
This starts migrating away from that.
  • Loading branch information
JAORMX committed Sep 27, 2023
1 parent 1f28744 commit 148a1d4
Show file tree
Hide file tree
Showing 39 changed files with 505 additions and 443 deletions.
4 changes: 2 additions & 2 deletions cmd/cli/app/artifact/artifact_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ var artifact_getCmd = &cobra.Command{
},
RunE: func(cmd *cobra.Command, args []string) error {
tag := util.GetConfigValue("tag", "tag", cmd, "").(string)
artifactID := viper.GetInt32("id")
artifactID := viper.GetString("id")
latest_versions := viper.GetInt32("latest-versions")

// tag and latest versions cannot be set at same time
Expand Down Expand Up @@ -72,7 +72,7 @@ var artifact_getCmd = &cobra.Command{

func init() {
ArtifactCmd.AddCommand(artifact_getCmd)
artifact_getCmd.Flags().Int32P("id", "i", 0, "ID of the artifact to get info from")
artifact_getCmd.Flags().StringP("id", "i", "", "ID of the artifact to get info from")
artifact_getCmd.Flags().Int32P("latest-versions", "v", 1, "Latest artifact versions to retrieve")
artifact_getCmd.Flags().StringP("tag", "", "", "Specific artifact tag to retrieve")
if err := artifact_getCmd.MarkFlagRequired("id"); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions cmd/cli/app/policy/policy_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ mediator control plane.`,
},
Run: func(cmd *cobra.Command, args []string) {
// delete the policy via GRPC
id := util.GetConfigValue("id", "id", cmd, int32(0)).(int32)

id := viper.GetString("id")
provider := viper.GetString("provider")

conn, err := util.GrpcForCommand(cmd)
Expand All @@ -65,7 +64,7 @@ mediator control plane.`,

func init() {
PolicyCmd.AddCommand(policy_deleteCmd)
policy_deleteCmd.Flags().Int32P("id", "i", 0, "id of policy to delete")
policy_deleteCmd.Flags().StringP("id", "i", "", "id of policy to delete")
policy_deleteCmd.Flags().StringP("provider", "p", "github", "Provider for the policy")
err := policy_deleteCmd.MarkFlagRequired("id")
util.ExitNicelyOnError(err, "Error marking flag as required")
Expand Down
4 changes: 2 additions & 2 deletions cmd/cli/app/policy/policy_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ mediator control plane.`,
ctx, cancel := util.GetAppContext()
defer cancel()

id := viper.GetInt32("id")
id := viper.GetString("id")
policy, err := client.GetPolicyById(ctx, &pb.GetPolicyByIdRequest{
Context: &pb.Context{
Provider: provider,
Expand Down Expand Up @@ -84,7 +84,7 @@ mediator control plane.`,

func init() {
PolicyCmd.AddCommand(policy_getCmd)
policy_getCmd.Flags().Int32P("id", "i", 0, "ID for the policy to query")
policy_getCmd.Flags().StringP("id", "i", "", "ID for the policy to query")
policy_getCmd.Flags().StringP("output", "o", app.Table, "Output format (json, yaml or table)")
policy_getCmd.Flags().StringP("provider", "p", "github", "Provider for the policy")
// TODO set up group if specified
Expand Down
24 changes: 12 additions & 12 deletions cmd/cli/app/policy_status/policy_status_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,8 @@ mediator control plane for an specific provider/group or policy id, entity type

provider := viper.GetString("provider")
group := viper.GetString("group")
policyId := viper.GetInt32("policy")
entityId := viper.GetInt32("entity")
policyId := viper.GetString("policy")
entityId := viper.GetString("entity")
entityType := viper.GetString("entity-type")
format := viper.GetString("output")

Expand All @@ -66,14 +66,6 @@ mediator control plane for an specific provider/group or policy id, entity type
return fmt.Errorf("provider must be set")
}

if policyId == 0 {
return fmt.Errorf("policy-id must be set")
}

if entityId == 0 {
return fmt.Errorf("entity-id must be set")
}

req := &pb.GetPolicyStatusByIdRequest{
Context: &pb.Context{
Provider: provider,
Expand Down Expand Up @@ -117,9 +109,17 @@ func init() {
PolicyStatusCmd.AddCommand(policystatus_getCmd)
policystatus_getCmd.Flags().StringP("provider", "p", "github", "Provider to get policy status for")
policystatus_getCmd.Flags().StringP("group", "g", "", "group id to get policy status for")
policystatus_getCmd.Flags().Int32P("policy", "i", 0, "policy id to get policy status for")
policystatus_getCmd.Flags().StringP("policy", "i", "", "policy id to get policy status for")
policystatus_getCmd.Flags().StringP("entity-type", "t", "",
fmt.Sprintf("the entity type to get policy status for (one of %s)", entities.KnownTypesCSV()))
policystatus_getCmd.Flags().Int32P("entity", "e", 0, "entity id to get policy status for")
policystatus_getCmd.Flags().StringP("entity", "e", "", "entity id to get policy status for")
policystatus_getCmd.Flags().StringP("output", "o", app.Table, "Output format (json, yaml or table)")

// mark as required
if err := policystatus_getCmd.MarkFlagRequired("policy"); err != nil {
util.ExitNicelyOnError(err, "error marking flag as required")
}
if err := policystatus_getCmd.MarkFlagRequired("entity"); err != nil {
util.ExitNicelyOnError(err, "error marking flag as required")
}
}
13 changes: 7 additions & 6 deletions cmd/cli/app/policy_status/policy_status_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ mediator control plane for an specific provider/group or policy id.`,

provider := viper.GetString("provider")
group := viper.GetString("group")
policyId := viper.GetInt32("policy")
policyId := viper.GetString("policy")
format := viper.GetString("output")
all := viper.GetBool("detailed")

Expand All @@ -64,10 +64,6 @@ mediator control plane for an specific provider/group or policy id.`,
return fmt.Errorf("provider must be set")
}

if policyId == 0 {
return fmt.Errorf("policy-id must be set")
}

req := &pb.GetPolicyStatusByIdRequest{
Context: &pb.Context{
Provider: provider,
Expand Down Expand Up @@ -112,9 +108,14 @@ func init() {
PolicyStatusCmd.AddCommand(policystatus_listCmd)
policystatus_listCmd.Flags().StringP("provider", "p", "github", "Provider to list policy status for")
policystatus_listCmd.Flags().StringP("group", "g", "", "group id to list policy status for")
policystatus_listCmd.Flags().Int32P("policy", "i", 0, "policy id to list policy status for")
policystatus_listCmd.Flags().StringP("policy", "i", "", "policy id to list policy status for")
policystatus_listCmd.Flags().StringP("output", "o", app.Table, "Output format (json, yaml or table)")
policystatus_listCmd.Flags().BoolP("detailed", "d", false, "List all policy violations")

if err := policystatus_listCmd.MarkFlagRequired("policy"); err != nil {
fmt.Fprintf(os.Stderr, "Error marking flag as required: %s\n", err)
os.Exit(1)
}
}

func handlePolicyStatusListTable(cmd *cobra.Command, resp *pb.GetPolicyStatusByIdResponse) {
Expand Down
10 changes: 5 additions & 5 deletions cmd/cli/app/repo/repo_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ var repo_getCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {

provider := util.GetConfigValue("provider", "provider", cmd, "").(string)
repoid := viper.GetInt32("repo-id")
repoid := viper.GetString("repo-id")
format := viper.GetString("output")
name := util.GetConfigValue("name", "name", cmd, "").(string)

// if name is set, repo-id cannot be set
if name != "" && repoid != 0 {
if name != "" && repoid != "" {
return fmt.Errorf("cannot set both name and repo-id")
}

// either name or repoid needs to be set
if name == "" && repoid == 0 {
if name == "" && repoid == "" {
return fmt.Errorf("either name or repo-id needs to be set")
}

Expand All @@ -86,7 +86,7 @@ var repo_getCmd = &cobra.Command{

// check repo by id
var repository *pb.RepositoryRecord
if repoid != 0 {
if repoid != "" {
resp, err := client.GetRepositoryById(ctx, &pb.GetRepositoryByIdRequest{
RepositoryId: repoid,
})
Expand Down Expand Up @@ -127,6 +127,6 @@ func init() {
repo_getCmd.Flags().StringP("output", "f", "", "Output format (json or yaml)")
repo_getCmd.Flags().StringP("provider", "p", "", "Name for the provider to enroll")
repo_getCmd.Flags().StringP("name", "n", "", "Name of the repository (owner/name format)")
repo_getCmd.Flags().Int32P("repo-id", "r", 0, "ID of the repo to query")
repo_getCmd.Flags().StringP("repo-id", "r", "", "ID of the repo to query")
repo_getCmd.Flags().BoolP("status", "s", false, "Only return the status of the policies associated to this repo")
}
4 changes: 2 additions & 2 deletions cmd/cli/app/rule_type/rule_type_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ mediator control plane.`,
},
Run: func(cmd *cobra.Command, args []string) {
// delete the policy via GRPC
id := util.GetConfigValue("id", "id", cmd, int32(0)).(int32)
id := viper.GetString("id")

conn, err := util.GrpcForCommand(cmd)

Expand All @@ -61,7 +61,7 @@ mediator control plane.`,

func init() {
ruleTypeCmd.AddCommand(ruleType_deleteCmd)
ruleType_deleteCmd.Flags().Int32P("id", "i", 0, "id of rule type to delete")
ruleType_deleteCmd.Flags().StringP("id", "i", "", "id of rule type to delete")
err := ruleType_deleteCmd.MarkFlagRequired("id")
util.ExitNicelyOnError(err, "Error marking flag as required")
}
4 changes: 2 additions & 2 deletions cmd/cli/app/rule_type/rule_type_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ mediator control plane.`,
ctx, cancel := util.GetAppContext()
defer cancel()

id := viper.GetInt32("id")
id := viper.GetString("id")

rtype, err := client.GetRuleTypeById(ctx, &pb.GetRuleTypeByIdRequest{
Context: &pb.Context{
Expand Down Expand Up @@ -89,7 +89,7 @@ mediator control plane.`,

func init() {
ruleTypeCmd.AddCommand(ruleType_getCmd)
ruleType_getCmd.Flags().Int32P("id", "i", 0, "ID for the policy to query")
ruleType_getCmd.Flags().StringP("id", "i", "", "ID for the policy to query")
ruleType_getCmd.Flags().StringP("output", "o", app.Table, "Output format (json, yaml or table)")
ruleType_getCmd.Flags().StringP("provider", "p", "github", "Provider for the policy")
// TODO set up group if specified
Expand Down
34 changes: 17 additions & 17 deletions database/migrations/000001_init.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ CREATE TABLE signing_keys (

-- repositories table
CREATE TABLE repositories (
id SERIAL PRIMARY KEY,
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
provider TEXT NOT NULL,
group_id INTEGER NOT NULL,
repo_owner TEXT NOT NULL,
Expand All @@ -150,8 +150,8 @@ CREATE TABLE repositories (

-- artifacts table
CREATE TABLE artifacts (
id SERIAL PRIMARY KEY,
repository_id INTEGER NOT NULL REFERENCES repositories(id) ON DELETE CASCADE,
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
repository_id UUID NOT NULL REFERENCES repositories(id) ON DELETE CASCADE,
artifact_name TEXT NOT NULL, -- this is case insensitive
artifact_type TEXT NOT NULL,
artifact_visibility TEXT NOT NULL, -- comes from github. Can be public, private, internal
Expand All @@ -161,8 +161,8 @@ CREATE TABLE artifacts (

-- artifact versions table
CREATE TABLE artifact_versions (
id SERIAL PRIMARY KEY,
artifact_id INTEGER NOT NULL REFERENCES artifacts(id) ON DELETE CASCADE,
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
artifact_id UUID NOT NULL REFERENCES artifacts(id) ON DELETE CASCADE,
version BIGINT NOT NULL,
tags TEXT,
sha TEXT NOT NULL,
Expand All @@ -183,7 +183,7 @@ CREATE TABLE session_store (

-- table for storing rule types
CREATE TABLE rule_type (
id SERIAL PRIMARY KEY,
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
name TEXT NOT NULL,
provider TEXT NOT NULL,
group_id INTEGER NOT NULL,
Expand All @@ -196,7 +196,7 @@ CREATE TABLE rule_type (
);

CREATE TABLE policies (
id SERIAL PRIMARY KEY,
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
name TEXT NOT NULL,
provider TEXT NOT NULL,
group_id INTEGER NOT NULL,
Expand All @@ -208,9 +208,9 @@ CREATE TABLE policies (
CREATE TYPE entities as enum ('repository', 'build_environment', 'artifact', 'pull_request');

CREATE TABLE entity_policies (
id SERIAL PRIMARY KEY,
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
entity entities NOT NULL,
policy_id INTEGER NOT NULL REFERENCES policies(id) ON DELETE CASCADE,
policy_id UUID NOT NULL REFERENCES policies(id) ON DELETE CASCADE,
contextual_rules JSONB NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW()
Expand All @@ -220,25 +220,25 @@ create type eval_status_types as enum ('success', 'failure', 'error', 'skipped',

-- This table will be used to track the overall status of a policy evaluation
CREATE TABLE policy_status (
id SERIAL PRIMARY KEY,
policy_id INTEGER NOT NULL REFERENCES policies(id) ON DELETE CASCADE,
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
policy_id UUID NOT NULL REFERENCES policies(id) ON DELETE CASCADE,
policy_status eval_status_types NOT NULL,
last_updated TIMESTAMP NOT NULL DEFAULT NOW()
);

-- This table will be used to track the status of each rule evaluation
-- for a given policy
CREATE TABLE rule_evaluation_status (
id SERIAL PRIMARY KEY,
id UUID NOT NULL DEFAULT gen_random_uuid() PRIMARY KEY,
entity entities NOT NULL,
policy_id INTEGER NOT NULL REFERENCES policies(id) ON DELETE CASCADE,
rule_type_id INTEGER NOT NULL REFERENCES rule_type(id) ON DELETE CASCADE,
policy_id UUID NOT NULL REFERENCES policies(id) ON DELETE CASCADE,
rule_type_id UUID NOT NULL REFERENCES rule_type(id) ON DELETE CASCADE,
eval_status eval_status_types NOT NULL,
-- polimorphic references. A status may be associated with a repository, build environment or artifact
repository_id INTEGER REFERENCES repositories(id) ON DELETE CASCADE,
artifact_id INTEGER REFERENCES artifacts(id) ON DELETE CASCADE,
repository_id UUID REFERENCES repositories(id) ON DELETE CASCADE,
artifact_id UUID REFERENCES artifacts(id) ON DELETE CASCADE,
-- These will be added later
-- build_environment_id INTEGER REFERENCES build_environments(id) ON DELETE CASCADE,
-- build_environment_id UUID REFERENCES build_environments(id) ON DELETE CASCADE,
details TEXT NOT NULL,
last_updated TIMESTAMP NOT NULL DEFAULT NOW()
);
Expand Down
Loading

0 comments on commit 148a1d4

Please sign in to comment.