Initialization policy»
Warning
This feature is deprecated. New users should not use this feature and existing users are encouraged to migrate to the approval policy, which offers a much more flexible and powerful way to control which runs are allowed to proceed. A migration guide is available here.
Purpose»
Initialization policy can prevent a Run or a Task from being initialized, thus blocking any custom code or commands from being executed. It superficially looks like a plan policy in that it affects an existing Run and prints feedback to logs, but it does not get access to the plan. Instead, it can be used to protect your stack from unwanted changes or enforce organizational rules concerning how and when runs are supposed to be triggered.
Warning
Server-side initialization policies are being deprecated. We will be replacing them with worker-side policies that can be set by using the launcher run initialization policy flag (SPACELIFT_LAUNCHER_RUN_INITIALIZATION_POLICY
).
For a limited time period we will be running both types of initialization policy checks but ultimately we're planning to move the pre-flight checks to the worker node, thus allowing customers to block suspicious looking jobs on their end.
Let's create a simple initialization policy, attach it to the stack, and see what gives:
1 2 3 4 5 |
|
...and boom:
Rules»
Initialization policies are simple in that they only use a single rule - deny - with a string message. A single result for that rule will fail the run before it has a chance to start - as we've just witnessed above.
Data input»
This is the schema of the data input that each policy request will receive:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
Aliases»
In addition to our helper functions, we provide aliases for commonly used parts of the input data:
Alias | Source |
---|---|
commit |
input.commit |
run |
input.run |
runtime_config |
input.run.runtime_config |
stack |
input.stack |
Use cases»
There are two main use cases for run initialization policies - protecting your stack from unwanted changes and enforcing organizational rules. Let's look at these one by one.
Protect your stack from unwanted changes»
While specialized, Spacelift is still a CI/CD platform and thus allows running custom code before Terraform initialization phase using before_init
scripts. This is a very powerful feature, but as always, with great power comes great responsibility. Since those scripts get full access to your Terraform environment, how hard is it to create a commit on a feature branch that would run terraform destroy -auto-approve
? Sure, all Spacelift runs are tracked and this prank will sooner or later be tracked down to the individual who ran it, but at that point do you still have a business?
That's where initialization policies can help. Let's explicitly blacklist all Terraform commands if they're running as before_init
scripts. OK, let's maybe add a single exception for a formatting check.
1 2 3 4 5 6 7 8 |
|
Feel free to play with this example in the Rego playground.
OK, but what if someone gets clever and creates a Docker image that symlinks something very innocent-looking to terraform
? Well, you have two choices - you could replace a blacklist with a whitelist, but a clever attacker can be really clever. So the other choice is to make sure that a known good Docker is used to execute the run. Here's an example:
1 2 3 4 5 6 7 |
|
Here's the above example in the Rego playground.
Danger
Obviously, if you're using an image other than what we control, you still have to ensure that the attacker can't push bad code to your Docker repo. Alas, this is beyond our control.
Enforce organizational rules»
While the previous section was all about making sure that bad stuff does not get executed, this use case presents run initialization policies as a way to ensure best practices - ensuring that the right things get executed the right way and at the right time.
One of the above examples explicitly whitelisted Terraform formatting check. Keeping your code formatted in a standard way is generally a good idea, so let's make sure that this command always gets executed first. Note that as per Anna Karenina principle this check is most elegantly defined as a negation of another rule matching the required state of affairs:
1 2 3 4 5 6 7 8 9 10 |
|
Here's this example in the Rego playground.
This time we'll skip the mandatory "don't deploy on weekends" check because while it could also be implemented here, there are probably better places to do it. Instead, let's enforce a feature branch naming convention. We'll keep this example simple, requiring that feature branches start with either feature/
or fix/
, but you can go fancy and require references to Jira tickets or even look at commit messages:
1 2 3 4 5 6 7 8 |
|
Here's this example in the Rego playground.
Migration guide»
A run initialization policy can be expressed as an approval policy if it defines a single reject
rule, and an approve
rule that is its negation. Below you will find equivalents of the examples above expressed as approval policies.
Migration example: enforcing Terraform check»
1 2 3 4 5 6 7 8 9 10 |
|
Migration example: disallowing before-init Terraform commands other than formatting»
1 2 3 4 5 6 7 8 |
|
Migration example: enforcing runner image»
1 2 3 4 5 6 7 |
|
Migration example: enforcing feature branch naming convention»
1 2 3 4 5 6 7 8 9 |
|