|
| 1 | +# Associating Policies with AAP Resources |
| 2 | + |
| 3 | +## Table of Contents |
| 4 | +- [Overview](#overview) |
| 5 | +- [Understanding OPA Packages and Rules](#understanding-opa-packages-and-rules) |
| 6 | +- [Associating Policies with AAP Resources](#associating-policies-with-aap-resources) |
| 7 | +- [Effects of Policy Association](#effects-of-policy-association) |
| 8 | + |
| 9 | +## Overview |
| 10 | + |
| 11 | +Ansible Automation Platform (AAP) allows you to associate Open Policy Agent (OPA) policies with various resources to enforce security and compliance controls. This guide explains how to associate policies with AAP resources and their effects. |
| 12 | + |
| 13 | +## Understanding OPA Packages and Rules |
| 14 | + |
| 15 | +### OPA Package Structure |
| 16 | + |
| 17 | +An OPA policy is organized in packages, which are namespaced collections of rules. The basic structure of an OPA policy looks like this: |
| 18 | + |
| 19 | +```rego |
| 20 | +package aap_policy_examples # Package name |
| 21 | +
|
| 22 | +import rego.v1 # Import required for Rego v1 syntax |
| 23 | +
|
| 24 | +# Rules define the policy logic |
| 25 | +allowed := { |
| 26 | + "allowed": true, |
| 27 | + "violations": [] |
| 28 | +} |
| 29 | +``` |
| 30 | + |
| 31 | +### Key Components |
| 32 | + |
| 33 | +1. **Package Declaration**: Defines the namespace for your policy |
| 34 | +2. **Rules**: Define the policy logic and return a decision object |
| 35 | + |
| 36 | +## Associating Policies with AAP Resources |
| 37 | + |
| 38 | +### Available Enforcement Points |
| 39 | + |
| 40 | +You can associate policies with the following AAP resources: |
| 41 | + |
| 42 | +1. **Organization Level** |
| 43 | + - Affects all job templates within an Organization |
| 44 | + - Provides broad control over automation within organizational boundaries |
| 45 | + |
| 46 | +2. **Inventory Level** |
| 47 | + - Affects all jobs using a specified Inventory |
| 48 | + - Controls access to specific infrastructure resources |
| 49 | + |
| 50 | +3. **Job Template Level** |
| 51 | + - Affects jobs launched from a specific Job Template |
| 52 | + - Provides granular control over specific automation tasks |
| 53 | + |
| 54 | +### How to Associate Policies |
| 55 | + |
| 56 | +#### 1. Job Template Level |
| 57 | + |
| 58 | +To associate a policy with a Job Template: |
| 59 | +1. Navigate to **Templates** in the AAP UI |
| 60 | +2. Select or create a Job Template |
| 61 | +3. In the Job Template edit form, locate the **OPA policy** field |
| 62 | +4. Enter the policy in the format `{package}/{rule}` |
| 63 | + - Example: `aap_policy_examples/allowed_false` |
| 64 | +5. Click "Save job template" to apply the policy |
| 65 | + |
| 66 | +#### 2. Inventory Level |
| 67 | + |
| 68 | +To associate a policy with an Inventory: |
| 69 | +1. Navigate to **Inventories** under Infrastructure |
| 70 | +2. Select or create an Inventory |
| 71 | +3. In the Inventory edit form, find the **OPA policy** field |
| 72 | +4. Enter the policy in the format `{package}/{rule}` |
| 73 | +5. The policy will be enforced for all jobs using this inventory |
| 74 | + |
| 75 | +#### 3. Organization Level |
| 76 | + |
| 77 | +To associate a policy with an Organization: |
| 78 | +1. Navigate to **Organizations** under Access Management |
| 79 | +2. Select or create an Organization |
| 80 | +3. In the Organization edit form, locate the **OPA policy** field |
| 81 | +4. Enter the policy in the format `{package}/{rule}` |
| 82 | +5. The policy will affect all job templates within the organization |
| 83 | + |
| 84 | +Note: For all resources, the OPA policy field format must follow the pattern `{package}/{rule}`. This is a required format and will be validated by the UI. |
| 85 | + |
| 86 | +## Effects of Policy Association |
| 87 | + |
| 88 | +### Job Execution Behavior |
| 89 | + |
| 90 | +Policy evaluation is integrated into the job lifecycle as a dedicated phase called `evaluate_policy`. Here's how it works: |
| 91 | + |
| 92 | +1. **Job Launch Sequence**: |
| 93 | + - User initiates a job launch |
| 94 | + - Before playbook execution begins, the job enters the `evaluate_policy` phase |
| 95 | + |
| 96 | +2. **Policy Collection**: |
| 97 | + During the `evaluate_policy` phase, AAP gathers all relevant policies from: |
| 98 | + - The organization that owns the job template |
| 99 | + - The inventory being used in the job |
| 100 | + - The job template the job was launched from |
| 101 | + |
| 102 | +3. **Policy Evaluation**: |
| 103 | + - AAP sends the collected policies to the configured OPA server for evaluation |
| 104 | + - Each policy is evaluated against the job context |
| 105 | + - If any policy evaluation: |
| 106 | + - Returns `"allowed": false`, or |
| 107 | + - Fails to evaluate |
| 108 | + The job will be blocked |
| 109 | + |
| 110 | +4. **Job State Transition**: |
| 111 | + - If all policies allow the job: |
| 112 | + - Job proceeds to playbook execution |
| 113 | + - If any policy blocks the job: |
| 114 | + - Job transitions to "Error" state |
| 115 | + - Playbook execution is prevented |
| 116 | + - Error messages from policy violations are recorded |
0 commit comments