Writing Rules
A team agrees on things about its infrastructure: no bucket is open to the public, customer data
stays in the EU, every module ID ends the same way. Those agreements usually live in a wiki page or
in a reviewer's head, and a change that breaks one gets through on the day the reviewer is busy. A
rule writes one agreement down beside octo.yaml, with the reason for it, and Octo checks it
every time generate runs, before anything is written.
This page writes three rules for the shop from Shop Stack: its
account, its eu-west-1 region, the checkout server and the shop-receipts bucket checkout reads
and writes.
The output on this page comes from real runs of the shop, with timestamps, process IDs and each log line's repeated rule name removed.
The rules file
Rules live in rules.yaml, beside octo.yaml. Octo reads it on generate and check-rules, and
runs without rules when there is no such file; the rules
key names a file elsewhere.
rules:
- name: buckets-block-public-access
reason: Receipts hold customer data, so no bucket is open to the public.
select: modules.filter(m, m.class == '@octo/AwsS3StorageServiceModule')
assert: "!has(self.inputs.blockPublicAccess) || self.inputs.blockPublicAccess"
- name: storage-stays-in-eu-west-1
reason: Customer data stays in the EU.
select: model.service.filter(s, s.type == 'storage')
assert: self.parents.region.all(r, r.regionId == 'eu-west-1')
- name: module-ids-end-in-module
reason: A module is told apart from the nodes it builds by its ID.
select: modules
assert: self.moduleId.endsWith('-module')
Each rule has four parts, and Octo refuses a rule missing any of them. name is how results refer
to it, so no two rules share one. reason is why the rule exists, and Octo repeats it in every
result, so whoever breaks the rule reads why it matters. select chooses what the rule is about,
and assert says what must be true of each thing select chose. Both are written in
CEL, a small expression language with no loops and no side effects, so a rule
can only read the graph, never change it.
Octo keeps nothing about rules in its state. The file is the rules: commit it with octo.yaml, and
review a change to it as you would any other change to the infrastructure.
Select and assert
select returns a list, and starts from one of these:
| Start | What it holds |
|---|---|
modules | every module of the app, with its moduleId, class and inputs |
models | every model, with its kind |
overlays | every overlay, with its kind |
resources | every resource, with its kind |
model.<kind> | the models of one kind, such as model.service |
overlay['<kind>'] | the overlays of one kind |
resource['<kind>'] | the resources of one kind, such as resource['s3-storage'] |
A kind is the name a class registers under. A kind with a hyphen in it takes brackets. filter
narrows a list, as model.service.filter(s, s.type == 'storage') keeps only the services that are
storage.
assert runs once for each thing select returned, which it reads as self, and must return
true for the rule to hold. A node's fields are its schema's fields: a service has serviceId,
type and label, a region has regionId, and a resource has properties, response and
tags. The API reference lists each schema. Every node also has three relationships:
parentsandchildren, by kind, asself.parents.regionholds the regions a service sits in;module, the module that built it, asself.module.moduleId.
The vocabulary is built from the classes octo.yaml imports. A kind or field no imported class
has is refused when the rules load, so a rule about DynamoDB tables needs the DynamoDB module
among the imports.
Checking the rules
octo check-rules runs every rule against the graph generate would write, and writes nothing.
Run it after writing a rule, to see every node the rule does not hold for before anything depends
on it:
npx octo check-rules
It prints what each rule found as JSON on stdout, and says it in words on stderr:
INFO: Rule "buckets-block-public-access" holds for 1 module(s).
INFO: Rule "storage-stays-in-eu-west-1" holds for 1 node(s).
INFO: Rule "module-ids-end-in-module" holds for 12 module(s).
[
{
"name": "buckets-block-public-access",
"reason": "Receipts hold customer data, so no bucket is open to the public.",
"selected": 1,
"staleExceptions": [],
"subject": "module",
"unmatchedExceptions": [],
"violations": []
},
...
]
check-rules exits 0 whatever the rules found. A rule that does not hold fails generate
instead.
A change a rule refuses
Somebody wants to share receipts by link and opens the bucket to the public, in the receipts
module's inputs in octo.yaml:
- moduleId: receipts-module
moduleClass: AwsS3StorageServiceModule
moduleInputs:
region: "${{region-module.model.region}}"
bucketName: "${env.RECEIPTS_BUCKET_NAME}"
accessPrefixes:
- uploads
blockPublicAccess: false
generate refuses the change, names the rule, its reason and what breaks it, and exits 1 without
writing a folder or any state:
ERROR: Rule "buckets-block-public-access" does not hold for 1 module(s): Receipts hold customer data, so no bucket is open to the public.
ERROR: module "receipts-module"
INFO: Rule "storage-stays-in-eu-west-1" holds for 1 node(s).
INFO: Rule "module-ids-end-in-module" holds for 12 module(s).
ERROR: Failed "generate" after 9ms: 1 rule(s) do not hold!
The way forward is either to undo the change, or to change the rule. Changing the rule is a decision the team makes in review, like any change to what the infrastructure is allowed to be.
Exceptions
Sometimes a rule is right and one thing cannot follow it yet. An exception lets that one thing
through, with a reason of its own. It names the thing as the refusal named it, which is what
select returned: a module by its ID, a node by its context.
- name: buckets-block-public-access
reason: Receipts hold customer data, so no bucket is open to the public.
select: modules.filter(m, m.class == '@octo/AwsS3StorageServiceModule')
assert: "!has(self.inputs.blockPublicAccess) || self.inputs.blockPublicAccess"
exceptions:
- node: receipts-module
reason: The receipts move behind CloudFront in March; until then the bucket serves them.
generate now writes the folders, and says what it let through:
INFO: Rule "buckets-block-public-access" holds for 0 module(s), and lets 1 through by exception.
An exception is meant to go away. Once the bucket blocks public access again, the rule holds for
it, and check-rules and generate warn that the exception can be removed:
WARN: Rule "buckets-block-public-access" holds for its exception "receipts-module"; remove it.
An exception naming nothing select returned, such as a misspelled module ID, lets nothing
through, and is warned about too:
WARN: Rule "buckets-block-public-access" excepts "receipt-module", which it does not select; correct or remove it.
Mistakes in a rule
A kind, field or relationship the vocabulary does not have fails when the rules load, before any
rule runs, so a misspelled rule never quietly selects nothing. With s.typ for s.type:
Error: Rule "storage-stays-in-eu-west-1" select is invalid! No such key: typ
A module's inputs, and a resource's properties, response and tags, hold whatever the module
or resource puts in them, so a key inside them is checked only when the rule runs. A misspelled key
there, as in self.inputs.blockPublicAces != false, fails check-rules and generate with the
thing it failed on:
Error: Rule "buckets-block-public-access" assert cannot be evaluated on "receipts-module"! No such key: blockPublicAces
has() hides a misspelled key: has(self.inputs.blockPublicAces) is false, never an error, so a
rule guarded by it holds for every module. Spell an input exactly as the module's input schema in
the API reference names it.
A rule that selects nothing
A rule can come before the thing it is about. Written while the shop has no table yet, this rule selects nothing:
- name: databases-stay-in-eu-west-1
reason: Orders are customer data too.
select: model.service.filter(s, s.type == 'database')
assert: self.parents.region.all(r, r.regionId == 'eu-west-1')
A rule that selects nothing holds, and Octo warns, in case its select is wrong:
WARN: Rule "databases-stay-in-eu-west-1" selects nothing.
The day a table is added outside eu-west-1, generate refuses it.
Rules run only at generate and check-rules, over the graph Octo composes. Nothing checks the
infrastructure after it is applied, such as probing a database from the internet.