diff --git a/policy/goals/entities.go b/policy/goals/entities.go index eec3ddc..d690d69 100644 --- a/policy/goals/entities.go +++ b/policy/goals/entities.go @@ -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] @@ -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 diff --git a/policy/goals/entities_test.go b/policy/goals/entities_test.go index 11af3ec..17e7d95 100644 --- a/policy/goals/entities_test.go +++ b/policy/goals/entities_test.go @@ -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/openssl@3.1.0-r4?os_name=alpine&os_version=3.18", :cve "CVE-2023-2650", :severity "HIGH", :fixed-by "3.1.1-r0"} }]` resultModel := []GoalEvaluationQueryResult{} @@ -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") } } diff --git a/policy/goals/types.go b/policy/goals/types.go index 92dbfe8..5568211 100644 --- a/policy/goals/types.go +++ b/policy/goals/types.go @@ -40,13 +40,17 @@ type ( Digest string `edn:"docker.image/digest"` } + RetractionEntity struct { + 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"`