diff --git a/aap_policy_examples/jt_naming_validation.rego b/aap_policy_examples/jt_naming_validation.rego new file mode 100644 index 0000000..7aef665 --- /dev/null +++ b/aap_policy_examples/jt_naming_validation.rego @@ -0,0 +1,28 @@ +package aap_policy_examples + +import rego.v1 + +# Default policy response indicating allowed status with no violations +default jt_naming_validation := { + "allowed": true, + "violations": [], +} + +# Validate that job template name has correct organization and project name prefixes +jt_naming_validation := result if { + # Extract values from input + org_name := object.get(input, ["organization", "name"], "") + project_name := object.get(input, ["project", "name"], "") + jt_name := object.get(input, ["job_template", "name"], "") + + # Construct the expected prefix + expected_prefix := concat("_", [org_name, project_name]) + + # Check if job template name starts with expected prefix + not startswith(jt_name, expected_prefix) + + result := { + "allowed": false, + "violations": [sprintf("Job template naming for '%v' does not comply with standards", [jt_name])] + } +} diff --git a/test_aap_policy_examples/jt_naming_validation_test.rego b/test_aap_policy_examples/jt_naming_validation_test.rego new file mode 100644 index 0000000..984f98b --- /dev/null +++ b/test_aap_policy_examples/jt_naming_validation_test.rego @@ -0,0 +1,30 @@ +package test_aap_policy_examples + +import data.aap_policy_examples + +test_valid_jt_naming_allowed if { + test_input := { + "organization": {"name": "org1"}, + "project": {"name": "proj1"}, + "job_template": {"name": "org1_proj1_my_template"}, + } + aap_policy_examples.jt_naming_validation.allowed == true with input as test_input +} + +test_invalid_jt_naming_not_allowed if { + test_input := { + "organization": {"name": "org1"}, + "project": {"name": "proj1"}, + "job_template": {"name": "my_template"}, + } + aap_policy_examples.jt_naming_validation.allowed == false with input as test_input +} + +test_violation_message if { + test_input := { + "organization": {"name": "org1"}, + "project": {"name": "proj1"}, + "job_template": {"name": "my_template"}, + } + aap_policy_examples.jt_naming_validation.violations[0] == "Job template naming for 'my_template' does not comply with standards" with input as test_input +}