From 7e482bc028c82caded1ebf986be1e6c4b6432b01 Mon Sep 17 00:00:00 2001 From: Zoran Regvart Date: Tue, 5 Nov 2024 17:02:15 +0100 Subject: [PATCH] Support `time.Time` annotation values The new yaml library for values formated as date/timestamps now returns a `time.Time` typed value, this change makes sure we support it. --- internal/opa/rule/rule.go | 12 ++++++++++-- internal/opa/rule/rule_test.go | 10 ++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/internal/opa/rule/rule.go b/internal/opa/rule/rule.go index f18a87526..5b17b7fda 100644 --- a/internal/opa/rule/rule.go +++ b/internal/opa/rule/rule.go @@ -20,6 +20,7 @@ import ( "fmt" "regexp" "strings" + "time" "github.com/open-policy-agent/opa/ast" ) @@ -48,9 +49,16 @@ func customAnnotationString(a *ast.AnnotationsRef, fieldName string) string { if a == nil || a.Annotations == nil || a.Annotations.Custom == nil { return "" } - if value, ok := a.Annotations.Custom[fieldName].(string); ok { - return value + + if value, ok := a.Annotations.Custom[fieldName]; ok { + switch value := value.(type) { + case string: + return value + case time.Time: + return value.Format(time.RFC3339) + } } + return "" } diff --git a/internal/opa/rule/rule_test.go b/internal/opa/rule/rule_test.go index 071becea4..41e4c7e88 100644 --- a/internal/opa/rule/rule_test.go +++ b/internal/opa/rule/rule_test.go @@ -285,6 +285,16 @@ func TestEffectiveOn(t *testing.T) { }, { name: "with effective_on annotation", + annotation: annotationRef(heredoc.Doc(` + package a + # METADATA + # custom: + # effective_on: 2022-01-01T00:00:00Z + deny() { true }`)), + expected: "2022-01-01T00:00:00Z", + }, + { + name: "with effective_on annotation as string", annotation: annotationRef(heredoc.Doc(` package a # METADATA