Skip to content

Commit

Permalink
Merge pull request #1105 from stacklok/status-pol-name
Browse files Browse the repository at this point in the history
cleanup: Use policy name to fetch status
  • Loading branch information
JAORMX authored Oct 5, 2023
2 parents f8a3532 + 45e7dad commit a26cba3
Show file tree
Hide file tree
Showing 15 changed files with 1,101 additions and 1,055 deletions.
12 changes: 6 additions & 6 deletions cmd/cli/app/policy_status/policy_status_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ mediator control plane for an specific provider/project or policy id, entity typ

provider := viper.GetString("provider")
project := viper.GetString("project")
policyId := viper.GetString("policy")
policyName := viper.GetString("policy")
entityId := viper.GetString("entity")
entityType := viper.GetString("entity-type")
format := viper.GetString("output")
Expand All @@ -66,12 +66,12 @@ mediator control plane for an specific provider/project or policy id, entity typ
return fmt.Errorf("provider must be set")
}

req := &mediatorv1.GetPolicyStatusByIdRequest{
req := &mediatorv1.GetPolicyStatusByNameRequest{
Context: &mediatorv1.Context{
Provider: provider,
},
PolicyId: policyId,
Entity: &mediatorv1.GetPolicyStatusByIdRequest_EntityTypedId{
Name: policyName,
Entity: &mediatorv1.GetPolicyStatusByNameRequest_EntityTypedId{
Id: entityId,
Type: mediatorv1.EntityFromString(entityType),
},
Expand All @@ -81,7 +81,7 @@ mediator control plane for an specific provider/project or policy id, entity typ
req.Context.Project = &project
}

resp, err := client.GetPolicyStatusById(ctx, req)
resp, err := client.GetPolicyStatusByName(ctx, req)
if err != nil {
return fmt.Errorf("error getting policy status: %w", err)
}
Expand All @@ -107,7 +107,7 @@ func init() {
PolicyStatusCmd.AddCommand(policystatus_getCmd)
policystatus_getCmd.Flags().StringP("provider", "p", "github", "Provider to get policy status for")
policystatus_getCmd.Flags().StringP("project", "g", "", "project id to get policy status for")
policystatus_getCmd.Flags().StringP("policy", "i", "", "policy id to get policy status for")
policystatus_getCmd.Flags().StringP("policy", "i", "", "policy name 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().StringP("entity", "e", "", "entity id to get policy status for")
Expand Down
18 changes: 9 additions & 9 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/project or policy id.`,

provider := viper.GetString("provider")
project := viper.GetString("project")
policyId := viper.GetString("policy")
policyName := viper.GetString("policy")
format := viper.GetString("output")
all := viper.GetBool("detailed")
rule := viper.GetString("rule")
Expand All @@ -65,20 +65,20 @@ mediator control plane for an specific provider/project or policy id.`,
return fmt.Errorf("provider must be set")
}

req := &pb.GetPolicyStatusByIdRequest{
req := &pb.GetPolicyStatusByNameRequest{
Context: &pb.Context{
Provider: provider,
},
PolicyId: policyId,
All: all,
Rule: rule,
Name: policyName,
All: all,
Rule: rule,
}

if project != "" {
req.Context.Project = &project
}

resp, err := client.GetPolicyStatusById(ctx, req)
resp, err := client.GetPolicyStatusByName(ctx, req)
if err != nil {
return fmt.Errorf("error getting policy status: %w", err)
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func init() {
PolicyStatusCmd.AddCommand(policystatus_listCmd)
policystatus_listCmd.Flags().StringP("provider", "p", "github", "Provider to list policy status for")
policystatus_listCmd.Flags().StringP("project", "g", "", "project id to list policy status for")
policystatus_listCmd.Flags().StringP("policy", "i", "", "policy id to list policy status for")
policystatus_listCmd.Flags().StringP("policy", "i", "", "policy name 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")
policystatus_listCmd.Flags().StringP("rule", "r", "", "Filter policy status list by rule")
Expand All @@ -119,15 +119,15 @@ func init() {
}
}

func handlePolicyStatusListTable(cmd *cobra.Command, resp *pb.GetPolicyStatusByIdResponse) {
func handlePolicyStatusListTable(cmd *cobra.Command, resp *pb.GetPolicyStatusByNameResponse) {
table := initializePolicyStatusTable(cmd)

renderPolicyStatusTable(resp.PolicyStatus, table)

table.Render()
}

func handleRuleEvaluationStatusListTable(cmd *cobra.Command, resp *pb.GetPolicyStatusByIdResponse) {
func handleRuleEvaluationStatusListTable(cmd *cobra.Command, resp *pb.GetPolicyStatusByNameResponse) {
table := initializeRuleEvaluationStatusTable(cmd)

for idx := range resp.RuleEvaluationStatus {
Expand Down
3 changes: 1 addition & 2 deletions cmd/cli/app/policy_status/table_render.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func renderPolicyStatusTable(
func initializeRuleEvaluationStatusTable(cmd *cobra.Command) *tablewriter.Table {
table := tablewriter.NewWriter(cmd.OutOrStdout())
table.SetHeader([]string{
"Policy ID", "Rule ID", "Rule Name", "Entity", "Status", "Remediation Status", "Entity Info", "Guidance"})
"Rule ID", "Rule Name", "Entity", "Status", "Remediation Status", "Entity Info", "Guidance"})
table.SetRowLine(true)
table.SetRowSeparator("-")
table.SetAutoMergeCellsByColumnIndex([]int{0})
Expand All @@ -84,7 +84,6 @@ func renderRuleEvaluationStatusTable(
table *tablewriter.Table,
) {
row := []string{
reval.PolicyId,
reval.RuleId,
reval.RuleName,
reval.Entity,
Expand Down
3 changes: 2 additions & 1 deletion database/migrations/000001_init.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,8 @@ CREATE TABLE policies (
FOREIGN KEY (project_id, provider) REFERENCES providers(project_id, name) ON DELETE CASCADE
);

CREATE UNIQUE INDEX ON policies(project_id, name);

CREATE TYPE entities as enum ('repository', 'build_environment', 'artifact', 'pull_request');

CREATE TABLE entity_policies (
Expand Down Expand Up @@ -244,7 +246,6 @@ CREATE INDEX idx_roles_project_id ON roles(project_id);
CREATE UNIQUE INDEX roles_organization_id_name_lower_idx ON roles (organization_id, LOWER(name));
CREATE INDEX idx_provider_access_tokens_project_id ON provider_access_tokens(project_id);
CREATE UNIQUE INDEX repositories_repo_id_idx ON repositories(repo_id);
CREATE UNIQUE INDEX policies_policy_name_idx ON policies(provider, name);
CREATE UNIQUE INDEX rule_type_idx ON rule_type(provider, project_id, name);
CREATE UNIQUE INDEX rule_evaluation_status_results_idx ON rule_evaluation_status(policy_id, repository_id, COALESCE(artifact_id, '00000000-0000-0000-0000-000000000000'::UUID), entity, rule_type_id);
CREATE UNIQUE INDEX artifact_name_lower_idx ON artifacts (repository_id, LOWER(artifact_name));
Expand Down
15 changes: 15 additions & 0 deletions database/mock/store.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions database/query/policy_status.sql
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ SELECT p.id, p.name, ps.policy_status, ps.last_updated FROM policy_status ps
INNER JOIN policies p ON p.id = ps.policy_id
WHERE p.id = $1 AND p.project_id = $2;

-- name: GetPolicyStatusByNameAndProject :one
SELECT p.id, p.name, ps.policy_status, ps.last_updated FROM policy_status ps
INNER JOIN policies p ON p.id = ps.policy_id
WHERE p.name = $1 AND p.project_id = $2;

-- name: GetPolicyStatusByProject :many
SELECT p.id, p.name, ps.policy_status, ps.last_updated FROM policy_status ps
INNER JOIN policies p ON p.id = ps.policy_id
Expand Down
24 changes: 12 additions & 12 deletions docs/docs/protodocs/proto.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 8 additions & 13 deletions internal/controlplane/handlers_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,25 +401,20 @@ func getRuleEvalEntityInfo(
return entityInfo
}

// GetPolicyStatusById is a method to get policy status
// GetPolicyStatusByName is a method to get policy status
// nolint:gocyclo // TODO: Refactor this to be more readable
func (s *Server) GetPolicyStatusById(ctx context.Context,
in *mediatorv1.GetPolicyStatusByIdRequest) (*mediatorv1.GetPolicyStatusByIdResponse, error) {
func (s *Server) GetPolicyStatusByName(ctx context.Context,
in *mediatorv1.GetPolicyStatusByNameRequest) (*mediatorv1.GetPolicyStatusByNameResponse, error) {
ctx, err := s.authAndContextValidation(ctx, in.GetContext())
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, "error ensuring default group: %v", err)
}

entityCtx := engine.EntityFromContext(ctx)

parsedPolicyID, err := uuid.Parse(in.PolicyId)
if err != nil {
return nil, util.UserVisibleError(codes.InvalidArgument, "invalid policy ID")
}

dbstat, err := s.store.GetPolicyStatusByIdAndProject(ctx, db.GetPolicyStatusByIdAndProjectParams{
dbstat, err := s.store.GetPolicyStatusByNameAndProject(ctx, db.GetPolicyStatusByNameAndProjectParams{
ProjectID: entityCtx.Project.ID,
ID: parsedPolicyID,
Name: in.Name,
})
if err != nil {
if errors.Is(err, sql.ErrNoRows) {
Expand Down Expand Up @@ -458,7 +453,7 @@ func (s *Server) GetPolicyStatusById(ctx context.Context,
// TODO: Handle retrieving status for other types of entities
if selector != nil {
dbrulestat, err := s.store.ListRuleEvaluationStatusByPolicyId(ctx, db.ListRuleEvaluationStatusByPolicyIdParams{
PolicyID: parsedPolicyID,
PolicyID: dbstat.ID,
EntityID: *selector,
EntityType: *dbEntity,
RuleName: *rule,
Expand All @@ -482,7 +477,7 @@ func (s *Server) GetPolicyStatusById(ctx context.Context,
}

st := &mediatorv1.RuleEvaluationStatus{
PolicyId: in.PolicyId,
PolicyId: dbstat.ID.String(),
RuleId: rs.RuleTypeID.String(),
RuleName: rs.RuleTypeName,
Entity: string(rs.Entity),
Expand All @@ -505,7 +500,7 @@ func (s *Server) GetPolicyStatusById(ctx context.Context,
// TODO: Add other entities once we have database entries for them
}

return &mediatorv1.GetPolicyStatusByIdResponse{
return &mediatorv1.GetPolicyStatusByNameResponse{
PolicyStatus: &mediatorv1.PolicyStatus{
PolicyId: dbstat.ID.String(),
PolicyName: dbstat.Name,
Expand Down
30 changes: 30 additions & 0 deletions internal/db/policy_status.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/db/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit a26cba3

Please sign in to comment.