Skip to content

Commit

Permalink
opa fmt new policy
Browse files Browse the repository at this point in the history
Signed-off-by: Charlie Egan <[email protected]>
  • Loading branch information
charlieegan3 committed Aug 16, 2023
1 parent 5d1dbe1 commit 6ffa570
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pull-request.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -325,12 +325,12 @@ jobs:
run: opa eval --input files.json --format pretty input
if: ${{ failure() }}


- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
# keep this version in sync with the version in netlify.toml
hugo-version: '0.113.0'
extended: true

- name: Build docs site and test integrations data
run: |
Expand Down
106 changes: 53 additions & 53 deletions build/policy/integrations.rego
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ allowed_image_extensions := ["png", "svg"]
deny contains result if {
some id, integration in input.integrations

# some integrations are allowed to have a missing image as no suitable image is available
# some integrations are allowed to have a missing image as no suitable image is available
not integration.allow_missing_image == true

some _, ext in allowed_image_extensions
Expand All @@ -22,7 +22,7 @@ deny contains result if {
e := sprintf("%s.%s", [id, ext])
}

possible_filenames - {i| i := input.images[_] } == possible_filenames
possible_filenames - {i | i := input.images[_]} == possible_filenames

result := {
"key": "integration_image",
Expand All @@ -46,102 +46,102 @@ deny contains result if {

# check that all integrations have the required fields
deny contains result if {
some id, integration in input.integrations
some id, integration in input.integrations

missing_fields := {"title", "layout"} - object.keys(integration)
missing_fields := {"title", "layout"} - object.keys(integration)

count(missing_fields) > 0
count(missing_fields) > 0

result := {
"key": "fields",
"message": sprintf("integration %s missing required fields: %v", [id, concat(", ", sort(missing_fields))]),
}
result := {
"key": "fields",
"message": sprintf("integration %s missing required fields: %v", [id, concat(", ", sort(missing_fields))]),
}
}

# check that all integrations have content
deny contains result if {
some id, integration in input.integrations
some id, integration in input.integrations

content := trim_space(object.get(integration, "content", ""))
content := trim_space(object.get(integration, "content", ""))

content == ""
content == ""

result := {
"key": "content",
"message": sprintf("integration %s has no content", [id]),
}
result := {
"key": "content",
"message": sprintf("integration %s has no content", [id]),
}
}

# check that all integrations have a layout set to integration-single
deny contains result if {
some id, integration in input.integrations
some id, integration in input.integrations

layout := object.get(integration, "layout", "")
layout := object.get(integration, "layout", "")

layout != "integration-single"
layout != "integration-single"

result := {
"key": "layout",
"message": sprintf("integration %s does not have layout set to: integration-single", [id]),
}
result := {
"key": "layout",
"message": sprintf("integration %s does not have layout set to: integration-single", [id]),
}
}

# check that all integrations reference an existing organization
deny contains result if {
some id, integration in input.integrations
some id, integration in input.integrations

inventors := object.get(integration, "inventors", [])
inventors := object.get(integration, "inventors", [])

some _, inventor in inventors
some _, inventor in inventors

not inventor in object.keys(input.organizations)
not inventor in object.keys(input.organizations)

result := {
"key": "inventors",
"message": sprintf("integration %s references organization %s which does not exist", [id, inventor]),
}
result := {
"key": "inventors",
"message": sprintf("integration %s references organization %s which does not exist", [id, inventor]),
}
}

# check that all integrations reference existing software
deny contains result if {
some id, integration in input.integrations
some id, integration in input.integrations

softwares := object.get(integration, "software", [])
softwares := object.get(integration, "software", [])

some _, software in softwares
some _, software in softwares

not software in object.keys(input.softwares)
not software in object.keys(input.softwares)

result := {
"key": "software",
"message": sprintf("integration %s references software %s which does not exist", [id, software]),
}
result := {
"key": "software",
"message": sprintf("integration %s references software %s which does not exist", [id, software]),
}
}

# check that softwares have required fields
deny contains result if {
some id, software in input.softwares
some id, software in input.softwares

missing_fields := {"title", "layout", "link"} - object.keys(software)
missing_fields := {"title", "layout", "link"} - object.keys(software)

count(missing_fields) > 0
count(missing_fields) > 0

result := {
"key": "fields",
"message": sprintf("software %s missing required fields: %v", [id, concat(", ", sort(missing_fields))]),
}
result := {
"key": "fields",
"message": sprintf("software %s missing required fields: %v", [id, concat(", ", sort(missing_fields))]),
}
}

# check that organizations have required fields
deny contains result if {
some id, organization in input.organizations
some id, organization in input.organizations

missing_fields := {"title", "layout", "link"} - object.keys(organization)
missing_fields := {"title", "layout", "link"} - object.keys(organization)

count(missing_fields) > 0
count(missing_fields) > 0

result := {
"key": "fields",
"message": sprintf("organization %s missing required fields: %v", [id, concat(", ", sort(missing_fields))]),
}
result := {
"key": "fields",
"message": sprintf("organization %s missing required fields: %v", [id, concat(", ", sort(missing_fields))]),
}
}
48 changes: 12 additions & 36 deletions build/policy/integrations_test.rego
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@ print_if(false, key, expected, output) := false {
}

test_integration_has_required_fields_missing {
output := data.integrations.deny with input as {
"integrations": {"regal": {}},
}
output := data.integrations.deny with input as {"integrations": {"regal": {}}}

key := "fields"
message := "integration regal missing required fields: layout, title"
Expand All @@ -42,9 +40,7 @@ test_integration_has_required_fields_missing {
}

test_integration_has_required_fields_present {
output := data.integrations.deny with input as {
"integrations": {"regal": {"title": "Regal", "layout": "integration"}},
}
output := data.integrations.deny with input as {"integrations": {"regal": {"title": "Regal", "layout": "integration"}}}

key := "fields"
message := "integration regal missing required fields: layout, title"
Expand All @@ -57,9 +53,7 @@ test_integration_has_required_fields_present {
}

test_integration_has_layout_missing {
output := data.integrations.deny with input as {
"integrations": {"regal": {}},
}
output := data.integrations.deny with input as {"integrations": {"regal": {}}}

key := "layout"
message := "integration regal does not have layout set to: integration-single"
Expand All @@ -72,9 +66,7 @@ test_integration_has_layout_missing {
}

test_integration_has_layout_missing {
output := data.integrations.deny with input as {
"integrations": {"regal": {"layout": "wrong"}},
}
output := data.integrations.deny with input as {"integrations": {"regal": {"layout": "wrong"}}}

key := "layout"
message := "integration regal does not have layout set to: integration-single"
Expand All @@ -87,9 +79,7 @@ test_integration_has_layout_missing {
}

test_integration_has_layout_present {
output := data.integrations.deny with input as {
"integrations": {"regal": {"layout": "integration-single"}},
}
output := data.integrations.deny with input as {"integrations": {"regal": {"layout": "integration-single"}}}

key := "layout"

Expand All @@ -101,9 +91,7 @@ test_integration_has_layout_present {
}

test_integration_has_content_missing {
output := data.integrations.deny with input as {
"integrations": {"regal": {}},
}
output := data.integrations.deny with input as {"integrations": {"regal": {}}}

key := "content"
message := "integration regal has no content"
Expand All @@ -116,9 +104,7 @@ test_integration_has_content_missing {
}

test_integration_has_content_blank {
output := data.integrations.deny with input as {
"integrations": {"regal": {"content": "\t\t\n "}},
}
output := data.integrations.deny with input as {"integrations": {"regal": {"content": "\t\t\n "}}}

key := "content"
message := "integration regal has no content"
Expand All @@ -131,9 +117,7 @@ test_integration_has_content_blank {
}

test_integration_has_content_present {
output := data.integrations.deny with input as {
"integrations": {"regal": {"content": "foobar"}},
}
output := data.integrations.deny with input as {"integrations": {"regal": {"content": "foobar"}}}

key := "content"
got := messages_for_key(key, output)
Expand Down Expand Up @@ -279,9 +263,7 @@ test_integration_softwares_present {
}

test_software_has_required_fields_missing {
output := data.integrations.deny with input as {
"softwares": {"terraform": {}},
}
output := data.integrations.deny with input as {"softwares": {"terraform": {}}}

key := "fields"
message := "software terraform missing required fields: layout, link, title"
Expand All @@ -294,9 +276,7 @@ test_software_has_required_fields_missing {
}

test_software_has_required_fields_present {
output := data.integrations.deny with input as {
"softwares": {"terraform": {"layout": "software-single", "link": "https://www.terraform.io/", "title": "Terraform"}},
}
output := data.integrations.deny with input as {"softwares": {"terraform": {"layout": "software-single", "link": "https://www.terraform.io/", "title": "Terraform"}}}

key := "fields"

Expand All @@ -308,9 +288,7 @@ test_software_has_required_fields_present {
}

test_organization_has_required_labels {
output := data.integrations.deny with input as {
"organizations": {"styra": {}},
}
output := data.integrations.deny with input as {"organizations": {"styra": {}}}

key := "fields"
message := "organization styra missing required fields: layout, link, title"
Expand All @@ -323,9 +301,7 @@ test_organization_has_required_labels {
}

test_organization_has_required_fields_present {
output := data.integrations.deny with input as {
"organizations": {"styra": {"layout": "organization-single", "link": "https://styra.com/", "title": "Styra"}},
}
output := data.integrations.deny with input as {"organizations": {"styra": {"layout": "organization-single", "link": "https://styra.com/", "title": "Styra"}}}

key := "fields"

Expand Down

0 comments on commit 6ffa570

Please sign in to comment.