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

fix: cleaned whitespace+newline in rego #671

Merged
merged 1 commit into from
Sep 17, 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
24 changes: 24 additions & 0 deletions src/pkg/common/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (

const multiValidationPath = "../../test/e2e/scenarios/remote-validations/multi-validations.yaml"
const singleValidationPath = "../../test/e2e/scenarios/remote-validations/validation.opa.yaml"
const whitespaceValidationPath = "../../test/e2e/scenarios/remote-validations/validation.whitespace.yaml"

// Helper function to load test data
func loadTestData(t *testing.T, path string) []byte {
Expand Down Expand Up @@ -392,6 +393,29 @@ func TestValidationToResource(t *testing.T) {
t.Errorf("ToResource() description = \"\", want a valid UUID")
}
})

t.Run("It trims whitespace from the validation", func(t *testing.T) {
t.Parallel()
validationBytes := loadTestData(t, whitespaceValidationPath)

validation, err := common.ReadValidationsFromYaml(validationBytes)
if err != nil {
t.Fatalf("yaml.Unmarshal failed: %v", err)
}

if len(validation) > 1 {
t.Errorf("Expected 1 validation, got %d", len(validation))
}

resource, err := validation[0].ToResource()
if err != nil {
t.Errorf("ToResource() error = %v", err)
}
strings.Contains(resource.Description, " \n")
if strings.Contains(resource.Description, " \n") {
t.Errorf("ToResource() description = should not contain whitespace followed by newline")
}
})
}

func TestReadValidationsFromYaml(t *testing.T) {
Expand Down
7 changes: 7 additions & 0 deletions src/pkg/common/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"regexp"
"strings"

"github.com/defenseunicorns/go-oscal/src/pkg/uuid"
Expand Down Expand Up @@ -55,6 +56,12 @@ func (v *Validation) ToResource() (resource *oscalTypes_1_1_2.Resource, err erro
} else {
resource.UUID = uuid.NewUUID()
}
// If the provider is opa, trim whitespace from the rego
if v.Provider != nil && v.Provider.OpaSpec != nil {
re := regexp.MustCompile(`[ \t]+\r?\n`)
v.Provider.OpaSpec.Rego = re.ReplaceAllString(v.Provider.OpaSpec.Rego, "\n")
}

validationBytes, err := v.MarshalYaml()
if err != nil {
return nil, err
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
lula-version: ">=v0.2.0"
metadata:
name: Validate pods with label foo=bar
uuid: 7f4c3b2a-1c3d-4a2b-8b64-3b1f76a8e36f
domain:
type: kubernetes
kubernetes-spec:
resources:
- name: podsvt
resource-rule:
version: v1
resource: pods
namespaces: [validation-test]
provider:
type: opa
opa-spec:
rego: |
package validate

import future.keywords.every

validate {
every pod in input.podsvt {
podLabel := pod.metadata.labels.foo
podLabel == "bar"
}
}