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

retract attributes when no-data #40

Merged
merged 1 commit into from
Jan 12, 2024
Merged
Show file tree
Hide file tree
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
16 changes: 9 additions & 7 deletions policy/goals/entities.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ import "time"

func CreateEntitiesFromResults(results []GoalEvaluationQueryResult, goalDefinition string, goalConfiguration string, image string, storageId string, configHash string, evaluationTs time.Time, tx int64) GoalEvaluationResultEntity {
entity := GoalEvaluationResultEntity{
Definition: goalDefinition,
Configuration: goalConfiguration,
Subject: DockerImageEntity{Digest: image},
ConfigHash: configHash,
CreatedAt: evaluationTs,
Definition: goalDefinition,
Configuration: goalConfiguration,
Subject: DockerImageEntity{Digest: image},
DeviationCount: RetractionEntity{Retract: true},
StorageId: RetractionEntity{Retract: true},
ConfigHash: configHash,
CreatedAt: evaluationTs,
TransactionCondition: TransactionConditionEntity{
Args: map[string]interface{}{"tx-arg": tx},
Where: []byte(`[[?entity :goal.result/created-at _ ?tx true]
Expand All @@ -35,8 +37,8 @@ func CreateEntitiesFromResults(results []GoalEvaluationQueryResult, goalDefiniti
if storageId != "no-data" {
deviationCount := len(results)

entity.DeviationCount = &deviationCount
entity.StorageId = &storageId
entity.DeviationCount = deviationCount
entity.StorageId = storageId
}

return entity
Expand Down
8 changes: 4 additions & 4 deletions policy/goals/entities_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@ func TestCreateEntitiesFromResult(t *testing.T) {

entity := CreateEntitiesFromResults(resultModel, "test-definition", "test-configuration", "test-image", "storage-id", "config-hash", evaluationTs, 123)

if entity.Definition != "test-definition" || entity.Configuration != "test-configuration" || *entity.StorageId != "storage-id" || entity.CreatedAt.Format("2006-01-02T15:04:05.000Z") != "2023-07-10T20:01:41.000Z" {
if entity.Definition != "test-definition" || entity.Configuration != "test-configuration" || entity.StorageId != "storage-id" || entity.CreatedAt.Format("2006-01-02T15:04:05.000Z") != "2023-07-10T20:01:41.000Z" {
t.Errorf("metadata not set correctly")
}

if *entity.DeviationCount != 1 {
if entity.DeviationCount != 1 {
t.Errorf("incorrect number of deviations, expected %d, got %d", 1, entity.DeviationCount)
}
}

func TestNoDataDoesntSetFields(t *testing.T) {
func TestNoDataSetsRetraction(t *testing.T) {
result := `[{:name "CVE-2023-2650", :details {:purl "pkg:alpine/[email protected]?os_name=alpine&os_version=3.18", :cve "CVE-2023-2650", :severity "HIGH", :fixed-by "3.1.1-r0"} }]`

resultModel := []GoalEvaluationQueryResult{}
Expand All @@ -54,7 +54,7 @@ func TestNoDataDoesntSetFields(t *testing.T) {

entity := CreateEntitiesFromResults(resultModel, "test-definition", "test-configuration", "test-image", "no-data", "config-hash", evaluationTs, 123)

if entity.StorageId != nil || entity.DeviationCount != nil {
if !entity.StorageId.(RetractionEntity).Retract || !entity.DeviationCount.(RetractionEntity).Retract {
t.Errorf("metadata not set correctly")
}
}
8 changes: 6 additions & 2 deletions policy/goals/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,17 @@ type (
Digest string `edn:"docker.image/digest"`
}

RetractionEntity struct {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This could probably be moved to somewhere more common since it isn't a policy specific concept. But fine for now.

Retract bool `edn:"retract"`
}

GoalEvaluationResultEntity struct {
skill.Entity `entity-type:"goal/result"`
Definition string `edn:"goal.definition/name"`
Configuration string `edn:"goal.configuration/name"`
Subject DockerImageEntity `edn:"goal.result/subject"`
DeviationCount *int `edn:"goal.result/deviation-count"`
StorageId *string `edn:"goal.result/storage-id"`
DeviationCount interface{} `edn:"goal.result/deviation-count,omitempty"`
StorageId interface{} `edn:"goal.result/storage-id,omitempty"`
ConfigHash string `edn:"goal.result/config-hash"`
CreatedAt time.Time `edn:"goal.result/created-at"`
TransactionCondition TransactionConditionEntity `edn:"atomist/tx-iff"`
Expand Down