此内容没有您所选择的语言版本。
Chapter 3. Creating a policy
A Conforma policy is a rule or set of rules and Conforma-specific annotations. Conforma can perform several types of policy checks, including checking all of the policy rules required for Red Hat products. Conforma uses the general purpose policy engine called Open Policy Agent (OPA). OPA defines its policy rules by using Rego. This means that the policy rules from OPA that are in a Conforma policy are also defined in Rego.
Procedure
Create a Rego file to define a new policy rule, as in the following example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow - METADATA
-
The first 10 lines of code comprise the
METADATAcomment block, which is how rego specifies rules annotations so that Conforma can include those annotation details in its successes and violations report. short_name-
This single policy rule verifies that the
builder.idin your new policy rule matches thebuilder.idin your Supply-chain Levels for Software Artifacts (SLSA) provenance. input.attestationsinputis a Rego object that contains all of the information about your container image, its signature, and its attestations. Theinput.attestationsattribute of theinputobject contains a list of attestations associated with the image.TipYou can save the
inputobject to a JSON file to view the available values, which is helpful in writing new policy rules. To save theinputobject as a JSON file namedinput.json, run a command similar to the following example:ec validate image --public-key cosign.pub \ --image "$REPOSITORY:latest" \ --policy policy.yaml \ --output policy-input=input.json
$ ec validate image --public-key cosign.pub \ --image "$REPOSITORY:latest" \ --policy policy.yaml \ --output policy-input=input.jsonCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Create a policy configuration to use your new policy rule:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow Use the new policy to validate your container image, and to display additional information in the successes and violations report, as in the following example:
ec validate image --public-key cosign.pub \ --image "$REPOSITORY:latest" \ --policy policy.yaml \ --show-successes --info --output yaml
$ ec validate image --public-key cosign.pub \ --image "$REPOSITORY:latest" \ --policy policy.yaml \ --show-successes --info --output yamlCopy to Clipboard Copied! Toggle word wrap Toggle overflow
Verification
-
Check the successes and violations report to make sure that your new rule is in the
successeslist.
3.1. Configuring a policy 复制链接链接已复制到粘贴板!
You can configure a Conforma policy with an inline JSON or YAML string. This policy, sometimes called a config or a contract, specifies where Conforma should find the rules and data to use to apply the policies you want to enforce. You can also include or exclude a single rule or a particular package of rules.
Procedure
Configure your policy in the command line as a JSON or YAML string, as in the following example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow (Optional) Exclude a particular package of rules from your Conforma policy, as in the following example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This command includes every rule from every package except for the rules in the specified packages.
(Optional) Exclude a single rule, as in the following example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This commands includes every rule from the
attestation_task_bundlepackage except for theunacceptable_task_bundlerule.(Optional) Include rules from only a particular package, as in the following example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This command includes only the rules from the specified packages.
(Optional) Include only some rules from a particular package. This means that you can specify both
includeandexcludeto select only the rules you want your Conforma policy to include, as in the following example:Copy to Clipboard Copied! Toggle word wrap Toggle overflow asterisk (*)The asterisk (*) acts as a wildcard to match any package. It does not match partial names. For example, you cannot specify "s*" to match every package that starts with "s".
These commands specify that you want to include only the
unacceptable_task_bundlerule from theattestation_task_bundlepackage, and exclude all the other rules in that package.
(Optional) Exclude certain checks so that Conforma can validate your container image even if those checks fail or don’t complete, as in the following example:
Copy to Clipboard Copied! Toggle word wrap Toggle overflow This command specifies that, if either of the identified checks fails or does not complete, Conforma can still finish to validate your container image.
(Optional) Modify the defaults for rules in a package by running either the
config.policy.includecommand or theconfig.policy.excludecommand, along with a list of strings.Your list of strings should include one of the following:
- package name
- Choose from the packages in the "Available rule collections" list.
- rule name
-
Specify a rule name by entering the name of the package and the rule code, separated by a dot (.), as in this example:
attestation_type.unknown_att_type. You can find rule codes under "Attestation type" in the upstream Conforma documentation. - package name:term
- Some policy rules process a list of items. When you add "term" to the "package name" string, you can exclude or include a particular item from that list. This works similarly to "package name," except that it applies only to policy rules in the package that match that term. For example, if you run the test package, you can choose to ignore a given test case but include all the others.
- rule name:term
- This is similar to "package name:term" except that, instead of including or excluding an item from a package, you can include or exclude a particular package policy rule.
- @collection name
-
Add this to your string to specify a predefined collection of rules. Prefix the collection name with the
@symbol. Choose from the available rule collections.