Skip to content

Instantly share code, notes, and snippets.

@RickCraig
RickCraig / check_namespace.rego
Last active May 9, 2023 16:46
Checks if the allowed namespace is used, denies if it isn't.
package pipeline
deny[msg] {
# Find all deployment stages
stage = input.pipeline.stages[_].stage
stage.type == "Deployment"
# Get my namespace from the environment infra definition
namespace := stage.spec.infrastructure.infrastructureDefinition.spec.namespace
@RickCraig
RickCraig / option1.rego
Last active May 9, 2023 16:46
Options for blocking connectors that aren't artifactory
package connector
# Block saves on any connector but Artifactory
deny[msg] {
# Check that the type is artifactory
input.entity.type != "Artifactory"
# Show a human-friendly error message
msg := sprintf("Artifactory is the only connector allowed, you have tried to create %s", [input.entity.type])
}
@RickCraig
RickCraig / deployment_freeze.rego
Last active May 9, 2023 16:47
A rego script to block out a range date
package pipeline
# Deny when the current date is after a start date and before an end date
# Could be used for deployment freezes
deny[msg] {
freezeStart := time.parse_rfc3339_ns("2022-11-18T00:00:00+00:00")
freezeEnd := time.parse_rfc3339_ns("2022-11-20T00:00:00+00:00")
now := time.now_ns()
now > freezeStart
@RickCraig
RickCraig / secrets_allowed_providers.rego
Created October 28, 2022 10:21
Allowed secrets providers
package secrets
import future.keywords.in
# Choose one or more allowed providers based on there identifier
allowedProviders := ["harnessSecretManager"]
deny[msg] {
# Check that the secret manager identifier exists in the white list
not input.secret.spec.secretManagerIdentifier in allowedProviders
@RickCraig
RickCraig / secrets_naming_convention.rego
Last active May 9, 2023 16:47
Naming conventions for secrets
package secrets
# Deny secrets whose names do not follow the correct naming convention
# e.g. "Lion - MongoDB Password" is allowed but "Cool secret" is not
# NOTE: Try setting the name to "Test" to see the policy fail
deny[msg] {
not regex.match("[Cheetah|Tiger|Lion]\\s[-]\\s[a-zA-Z0-9\\s]+", input.secret.name)
msg := sprintf("Secret name '%s' must follow the correct naming convention 'Team - Purpose'", [input.secret.name])
}
@RickCraig
RickCraig / secrets_allowed_principals.rego
Created October 28, 2022 10:09
No principal can create secrets in Harness Secret Manager
package secret
import future.keywords.in
# The identifiers for one or more principals allowed to save secrets
allowedPrincipals = ["1234abcd"]
deny["Principal is not allowed to save secrets"] {
# If the principal is not in the allowed principals list, deny.
not input.metadata.principalIdentifier in allowedPrincipals
@RickCraig
RickCraig / connector_naming_convention.rego
Created October 28, 2022 09:59
Naming Conventions For Connectors
package connectors
# Deny connectors whose names do not follow the correct naming convention
# e.g. "Lion - Data Store" is allowed but "Cool connector" is not
# NOTE: Try setting the name to "Test" to see the policy fail
deny[msg] {
not regex.match("[Cheetah|Tiger|Lion]\\s[-]\\s[a-zA-Z0-9\\s]+", input.entity.name)
msg := sprintf("Connector name '%s' must follow the correct naming convention 'Team - Account'", [input.entity.name])
}
@RickCraig
RickCraig / connecto_allowed_users.rego
Created October 28, 2022 09:51
Allowed users to create connector
package connector
# Choose a connector type to check
connectorType := "K8sCluster"
# Choose one or more user groups, identified by the "indentifier" property
AllowedUserGroups := ["_project_all_users"]
deny[msg] {
cType := input.entity.type
@RickCraig
RickCraig / connector_allowed_auths.rego
Last active May 9, 2023 16:47
Allowed Authorization Types for a Connector Type
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 / ff-no-permanent.rego
Created October 11, 2022 12:27
Feature Flags: No Permanent Flags
package feature_flags
# Deny flags that have the permanent label set to true
deny[msg] {
input.flag.permanent == true
msg := "Permanent flags are not allowed"
}