Skip to content

Instantly share code, notes, and snippets.

@RickCraig
RickCraig / enforce_ci_branch.rego
Created May 4, 2023 14:15
Enforces a specific CI branch
package pipeline
deny[msg]{
input.pipeline.tags.release
branch := input.pipeline.properties.ci.codebase.build.spec.branch
not check_branch(branch)
msg := sprintf("Branch %s is not allowed, please use Release/* or <+trigger.payload.branch>", [branch])
}
check_branch(branch) {
@RickCraig
RickCraig / enforce_step_order_inc_parallels.rego
Created May 4, 2023 09:26
Enforce Step Order, this one also takes into consideration parallel steps
package pipeline
step_order := ["TerraformPlan", "HarnessApproval", "TerraformApply"]
# Deny a pipeline if steps do not execute in the
# correct order, this will check the steps in every
# stage, but not across stages
deny[msg] {
stage := input.pipeline.stages[_].stage
@RickCraig
RickCraig / bonus_deny_ci_stages.rego
Last active May 9, 2023 16:44
Guide: How to deal with parallel pipeline stages
package pipeline
# Check serial stages
check_pipeline {
stage := input.pipeline.stages[_].stage
stage.type == "CI"
}
# Check parallel stages
check_pipeline {
@RickCraig
RickCraig / ci_connector_check_parallel.rego
Created May 3, 2023 08:06
Check the connector for ci_cluster on serial and paralllel CI stages
package pipeline
# Ensure all CI stages in serial use the ci_cluster connector
deny[msg] {
stage := input.pipeline.stages[_].stage
stage.type == "CI"
stage.spec.infrastructure.spec.connector.identifier != "ci_cluster"
msg := sprintf("CI stage '%s' is not using CI cluster", [stage.name])
}
@RickCraig
RickCraig / block_delegates.rego
Created May 2, 2023 12:00
Blocks a list of delegates being used
package pipeline
blocked_delegates := ["cti-shared-us-east-1-svc-eks-tfg7-swat", "my-other-delegate"]
# block any pipeline which uses a particular delegate
deny[msg] {
# Get the stage
stage := input.pipeline.stages[_].stage
# Loop through blocked delegates
@RickCraig
RickCraig / check_required_steps.rego
Last active May 9, 2023 16:45
This rego script checks all steps inside of targeted stage types (in this example, CI stages). It takes into consideration both serial and parallel stages, and all configurations of step (serial, stepgroups & parallels)
package pipeline
# Stage Type to Check
stage_type = "CI"
# Steps that required in every "stage_type" stage
required_steps = ["BuildAndPushDockerRegistry", "Run"]
# Walks through a stage and return all step objects
walk_steps(stage) = { value |
@RickCraig
RickCraig / disallow_environment.rego
Last active May 9, 2023 16:45
Disallow environments by identifier
package pipeline
# Deny pipelines that use disallowed environments
deny[msg] {
# Find all deployment stages
stage = input.pipeline.stages[_].stage
stage.type == "Deployment"
# ... where the environment is on the disallow list
contains(disallowed_environments, stage.spec.infrastructure.environment.identifier)
@RickCraig
RickCraig / required_step.rego
Created April 24, 2023 12:04
A rego script that checks for required steps in both serial and parallel stages
package pipeline
# Steps that must be present in every deployment
required_steps = ["BuildAndPushDockerRegistry"]
# Stage Type to Check
stage_type = "CI"
# Checks the required step for non-parallel stages
has_required_step[msg] {
@RickCraig
RickCraig / allowed_auth_type.rego
Last active May 9, 2023 16:46
Harness Sample Policies
package connector
import future.keywords.in
# Choose a connector type to check
connectorType := "K8sCluster"
# Choose one or more allowed auth types for the above connector type
allowedAuthTypes := ["UsernamePassword"]
deny[msg] {
@RickCraig
RickCraig / enforce_stage_order.rego
Created March 15, 2023 19:22
Enforce Step & Stage Order
package pipeline
stage_order := ["OPA check", "deploy"]
# Deny a pipeline if stages do not execute in the
# correct order. This will check that the named stages
# in the array above are in the right order, ignoring
# other stages.
deny[msg] {
# Run through the order rules array