-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Nikita Pivkin <nikita.pivkin@smartforce.io>
- Loading branch information
Showing
10 changed files
with
183 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
136 changes: 136 additions & 0 deletions
136
.regal/rules/custom/regal/rules/custom/invalid-metadata/invalid_metadata.rego
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
# METADATA | ||
# description: | | ||
# Ensures that metadata definitions adhere to the required schema by validating the following: | ||
# - Ensure all necessary fields are present in metadata. | ||
# - Detect and report any unexpected or forbidden fields. | ||
# - Validate that field values are compliant with the expected format or constraints. | ||
# schemas: | ||
# - input: schema.regal.ast | ||
package custom.regal.rules.custom["invalid-metadata"] | ||
|
||
import rego.v1 | ||
|
||
import data.regal.ast | ||
import data.regal.result | ||
|
||
report contains _violation_check(lib_metadata_schema) if _is_lib_package | ||
|
||
report contains _violation_check(check_metadata_schema) if not _is_lib_package | ||
|
||
_is_lib_package if input["package"].path[1].value == "lib" | ||
|
||
_violation_check(schema) := violation if { | ||
some annot in input["package"].annotations | ||
annot.scope == "package" | ||
|
||
[match, errors] := json.match_schema(annot.custom, schema) | ||
not match | ||
|
||
error_messages := [err.error | some err in errors] | ||
|
||
violation := result.fail( | ||
rego.metadata.chain(), | ||
object.union( | ||
result.location(annot), | ||
{"description": concat("\n", error_messages)}, | ||
), | ||
) | ||
} | ||
|
||
lib_metadata_schema := { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"properties": { | ||
"library": {"type": "boolean"}, | ||
"input": input_schema, | ||
}, | ||
"required": ["library"], | ||
"additionalProperties": false, | ||
} | ||
|
||
check_metadata_schema := { | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"type": "object", | ||
"properties": { | ||
"id": {"type": "string"}, | ||
"avd_id": {"type": "string"}, | ||
"provider": {"type": "string"}, | ||
"service": {"type": "string"}, | ||
"short_code": {"type": "string"}, | ||
"severity": { | ||
"type": "string", | ||
"enum": ["LOW", "MEDIUM", "HIGH", "CRITICAL"], | ||
}, | ||
"input": input_schema, | ||
"frameworks": {"type": "object"}, | ||
"deprecated": {"type": "boolean"}, | ||
"examples": {"type": "string"}, | ||
"aliases": { | ||
"type": "array", | ||
"items": {"type": "string"}, | ||
}, | ||
"cloud_formation": {"$ref": "#/$defs/engine_metadata"}, | ||
"terraform": {"$ref": "#/$defs/engine_metadata"}, | ||
"recommended_actions": {"type": "string"}, | ||
"recommended_action": {"type": "string"}, | ||
}, | ||
"required": ["id", "avd_id"], | ||
"additionalProperties": false, | ||
"anyOf": [ | ||
{"required": ["recommended_actions"]}, | ||
{"required": ["recommended_action"]}, | ||
{"not": {"required": ["recommended_actions", "recommended_action"]}}, | ||
], | ||
"$defs": {"engine_metadata": { | ||
"type": "object", | ||
"properties": { | ||
"good_examples": {"type": "string"}, | ||
"bad_examples": {"type": "string"}, | ||
"links": { | ||
"type": "array", | ||
"items": { | ||
"type": "string", | ||
"format": "uri", | ||
}, | ||
}, | ||
}, | ||
"additionalProperties": false, | ||
}}, | ||
} | ||
|
||
input_schema := { | ||
"type": "object", | ||
"properties": {"selector": { | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"properties": { | ||
"type": {"type": "string"}, | ||
"subtypes": { | ||
"type": "array", | ||
"items": { | ||
"type": "object", | ||
"oneOf": [ | ||
{ | ||
"properties": {"kind": {"type": "string"}}, | ||
"required": ["kind"], | ||
"additionalProperties": false, | ||
}, | ||
{ | ||
"properties": { | ||
"provider": {"type": "string"}, | ||
"service": {"type": "string"}, | ||
}, | ||
"required": ["service", "provider"], | ||
"additionalProperties": false, | ||
}, | ||
], | ||
}, | ||
}, | ||
}, | ||
"required": ["type"], | ||
"additionalProperties": false, | ||
}, | ||
}}, | ||
"additionalProperties": false, | ||
} |
40 changes: 40 additions & 0 deletions
40
.regal/rules/custom/regal/rules/custom/invalid-metadata/invalid_metadata_test.rego
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package custom.regal.rules.custom["invalid-metadata_test"] | ||
|
||
import rego.v1 | ||
|
||
import data.custom.regal.rules.custom["invalid-metadata"] as rule | ||
|
||
test_invalid_metadata if { | ||
module := regal.parse_module("example.rego", ` | ||
# METADATA | ||
# title: test title | ||
# description: test description | ||
# schemas: | ||
# - input: schema["kubernetes"] | ||
# custom: | ||
# id: TEST-001 | ||
# avdid: AVD-TEST-001 | ||
# examples: test/ff.json | ||
package policy | ||
foo := true`) | ||
|
||
r := rule.report with input as module | ||
|
||
r == {{ | ||
"category": "custom", | ||
"description": "(Root): avd_id is required\n(Root): Additional property avdid is not allowed", | ||
"level": "error", | ||
"location": { | ||
"col": 1, | ||
"end": { | ||
"col": 27, | ||
"row": 10, | ||
}, | ||
"file": "example.rego", | ||
"row": 2, | ||
"text": "# METADATA", | ||
}, | ||
"title": "invalid-metadata", | ||
}} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters