Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handlers_policy: return more information on artifacts #812

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 46 additions & 12 deletions pkg/controlplane/handlers_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,45 @@ func (s *Server) GetPolicyById(ctx context.Context,
return &resp, nil
}

func getRuleEvalEntityInfo(
ctx context.Context,
store db.Store,
entityType *db.NullEntities,
selector *sql.NullInt32,
rs db.ListRuleEvaluationStatusByPolicyIdRow,
) map[string]string {
entityInfo := map[string]string{
"provider": rs.Provider,
}

if rs.RepositoryID.Valid {
// this is always true now but might not be when we support entities not tied to a repo
entityInfo["repo_name"] = rs.RepoName
entityInfo["repo_owner"] = rs.RepoOwner
entityInfo["repository_id"] = fmt.Sprintf("%d", rs.RepositoryID.Int32)
}

if !selector.Valid || !entityType.Valid {
return entityInfo
}

// linter complaints that switches with one-case are bad, but I think that a switch is more extensible for future
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you want a default case that returns the entityType.Entities?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's already in the struct that wraps this map:

st := &pb.RuleEvaluationStatus{
				PolicyId:    in.PolicyId,
				RuleId:      rs.RuleTypeID,
				RuleName:    rs.RuleTypeName,
				Entity:      string(rs.Entity), <-----

// nolint:revive
switch entityType.Entities {
case db.EntitiesArtifact:
artifact, err := store.GetArtifactByID(ctx, selector.Int32)
if err != nil {
log.Printf("error getting artifact: %v", err)
return entityInfo
}
entityInfo["artifact_id"] = fmt.Sprintf("%d", artifact.ID)
entityInfo["artifact_name"] = artifact.ArtifactName
entityInfo["artifact_type"] = artifact.ArtifactType
}

return entityInfo
}

// GetPolicyStatusById is a method to get policy status
func (s *Server) GetPolicyStatusById(ctx context.Context,
in *pb.GetPolicyStatusByIdRequest) (*pb.GetPolicyStatusByIdResponse, error) {
Expand Down Expand Up @@ -419,18 +458,13 @@ func (s *Server) GetPolicyStatusById(ctx context.Context,
}

st := &pb.RuleEvaluationStatus{
PolicyId: in.PolicyId,
RuleId: rs.RuleTypeID,
RuleName: rs.RuleTypeName,
Entity: string(rs.Entity),
Status: string(rs.EvalStatus),
Details: rs.Details,
EntityInfo: map[string]string{
"repository_id": fmt.Sprintf("%d", rs.RepositoryID.Int32),
"repo_name": rs.RepoName,
"repo_owner": rs.RepoOwner,
"provider": rs.Provider,
},
PolicyId: in.PolicyId,
RuleId: rs.RuleTypeID,
RuleName: rs.RuleTypeName,
Entity: string(rs.Entity),
Status: string(rs.EvalStatus),
Details: rs.Details,
EntityInfo: getRuleEvalEntityInfo(ctx, s.store, dbEntity, selector, rs),
Guidance: guidance,
LastUpdated: timestamppb.New(rs.LastUpdated),
}
Expand Down