|
| 1 | +--- |
| 2 | +# vault_app_policies.yaml |
| 3 | +# Creates fine-grained Vault policies for application-level isolation |
| 4 | +# |
| 5 | +# This task file creates individual policies for each vaultPrefix provided. |
| 6 | +# Each application gets its own policy that only allows access to its |
| 7 | +# specific path, enabling application-level secret isolation. |
| 8 | +# |
| 9 | +# Required variables: |
| 10 | +# app_prefixes: List of app configs with 'prefix' key and optional 'jwt_role' |
| 11 | +# e.g., [{prefix: "apps/qtodo"}, {prefix: "hub/infra/keycloak"}] |
| 12 | +# |
| 13 | +# Optional variables: |
| 14 | +# app_capabilities: Capabilities for app policies (default: read) |
| 15 | +# app_update_hub_role: Whether to update hub-role with new policies (default: true) |
| 16 | +# app_create_jwt_roles: Whether to create JWT roles per app (default: false) |
| 17 | +# |
| 18 | +# Example usage: |
| 19 | +# - name: Create app-specific vault policies |
| 20 | +# ansible.builtin.include_tasks: |
| 21 | +# file: vault_app_policies.yaml |
| 22 | +# vars: |
| 23 | +# app_prefixes: |
| 24 | +# - prefix: apps/qtodo |
| 25 | +# jwt_role: |
| 26 | +# service_account_names: "qtodo-sa" |
| 27 | +# service_account_namespaces: "qtodo" |
| 28 | +# - prefix: hub/infra/keycloak |
| 29 | +# app_update_hub_role: true |
| 30 | + |
| 31 | +- name: Debug - Show app prefixes to process |
| 32 | + ansible.builtin.debug: |
| 33 | + msg: "Processing app prefixes: {{ app_prefixes }}" |
| 34 | + verbosity: 1 |
| 35 | + when: app_prefixes is defined and app_prefixes | length > 0 |
| 36 | + |
| 37 | +- name: Skip if no app prefixes defined |
| 38 | + ansible.builtin.debug: |
| 39 | + msg: "No app_prefixes defined, skipping app policy creation" |
| 40 | + verbosity: 1 |
| 41 | + when: app_prefixes is not defined or app_prefixes | length == 0 |
| 42 | + |
| 43 | +# Build list of policy names we need |
| 44 | +- name: Build app policy names list |
| 45 | + ansible.builtin.set_fact: |
| 46 | + _app_policy_names: "{{ app_prefixes | map(attribute='prefix') | map('replace', '/', '-') | map('regex_replace', '$', '-k8s-secret') | list }}" |
| 47 | + when: app_prefixes is defined and app_prefixes | length > 0 |
| 48 | + |
| 49 | +# Get existing policies from Vault to check what needs to be created |
| 50 | +- name: Get existing Vault policies |
| 51 | + kubernetes.core.k8s_exec: |
| 52 | + namespace: "{{ vault_ns }}" |
| 53 | + pod: "{{ vault_pod }}" |
| 54 | + command: vault policy list -format=json |
| 55 | + register: _existing_policies_result |
| 56 | + changed_when: false |
| 57 | + when: app_prefixes is defined and app_prefixes | length > 0 |
| 58 | + |
| 59 | +- name: Parse existing policies |
| 60 | + ansible.builtin.set_fact: |
| 61 | + _existing_policies: "{{ _existing_policies_result.stdout | from_json }}" |
| 62 | + when: |
| 63 | + - app_prefixes is defined and app_prefixes | length > 0 |
| 64 | + - _existing_policies_result.rc == 0 |
| 65 | + |
| 66 | +# Filter to only policies that don't already exist |
| 67 | +- name: Determine policies to create |
| 68 | + ansible.builtin.set_fact: |
| 69 | + _policies_to_create: "{{ _app_policy_names | difference(_existing_policies | default([])) }}" |
| 70 | + when: app_prefixes is defined and app_prefixes | length > 0 |
| 71 | + |
| 72 | +- name: Debug - Show policies to create |
| 73 | + ansible.builtin.debug: |
| 74 | + msg: "Policies to create: {{ _policies_to_create | default([]) }} (existing: {{ _existing_policies | default([]) }})" |
| 75 | + verbosity: 1 |
| 76 | + when: app_prefixes is defined and app_prefixes | length > 0 |
| 77 | + |
| 78 | +# Create only policies that don't exist |
| 79 | +- name: Configure app policy in Vault |
| 80 | + kubernetes.core.k8s_exec: |
| 81 | + namespace: "{{ vault_ns }}" |
| 82 | + pod: "{{ vault_pod }}" |
| 83 | + command: > |
| 84 | + bash -e -c "echo 'path \"secret/data/{{ _prefix }}/*\" { capabilities = [\"read\"] }' | |
| 85 | + vault policy write {{ _prefix | replace('/', '-') }}-k8s-secret -" |
| 86 | + loop: "{{ app_prefixes }}" |
| 87 | + loop_control: |
| 88 | + label: "Writing policy {{ _prefix | replace('/', '-') }}-k8s-secret" |
| 89 | + vars: |
| 90 | + _prefix: "{{ item.prefix }}" |
| 91 | + _policy_name: "{{ item.prefix | replace('/', '-') }}-k8s-secret" |
| 92 | + when: |
| 93 | + - app_prefixes is defined and app_prefixes | length > 0 |
| 94 | + - _policy_name in (_policies_to_create | default([])) |
| 95 | + |
| 96 | +# Get current hub-role policies |
| 97 | +- name: Get current hub-role policies |
| 98 | + kubernetes.core.k8s_exec: |
| 99 | + namespace: "{{ vault_ns }}" |
| 100 | + pod: "{{ vault_pod }}" |
| 101 | + command: > |
| 102 | + vault read -format=json auth/{{ vault_hub }}/role/{{ vault_hub }}-role |
| 103 | + register: _hub_role_result |
| 104 | + failed_when: false |
| 105 | + changed_when: false |
| 106 | + when: |
| 107 | + - app_prefixes is defined and app_prefixes | length > 0 |
| 108 | + - app_update_hub_role | default(true) | bool |
| 109 | + |
| 110 | +# Parse current policies and merge with new ones |
| 111 | +- name: Set current policies fact |
| 112 | + ansible.builtin.set_fact: |
| 113 | + _current_policies: "{{ (_hub_role_result.stdout | from_json).data.token_policies | default(vault_hub_role_default_policies) }}" |
| 114 | + when: |
| 115 | + - app_prefixes is defined and app_prefixes | length > 0 |
| 116 | + - app_update_hub_role | default(true) | bool |
| 117 | + - _hub_role_result.rc == 0 |
| 118 | + |
| 119 | +- name: Set default policies if hub-role not found |
| 120 | + ansible.builtin.set_fact: |
| 121 | + _current_policies: "{{ vault_hub_role_default_policies }}" |
| 122 | + when: |
| 123 | + - app_prefixes is defined and app_prefixes | length > 0 |
| 124 | + - app_update_hub_role | default(true) | bool |
| 125 | + - _hub_role_result.rc != 0 |
| 126 | + |
| 127 | +# Merge current and new policies (removing duplicates) |
| 128 | +- name: Merge policies |
| 129 | + ansible.builtin.set_fact: |
| 130 | + _merged_policies: "{{ (_current_policies + _app_policy_names) | unique }}" |
| 131 | + when: |
| 132 | + - app_prefixes is defined and app_prefixes | length > 0 |
| 133 | + - app_update_hub_role | default(true) | bool |
| 134 | + |
| 135 | +# Check if hub-role policies need updating |
| 136 | +- name: Check if hub-role policies changed |
| 137 | + ansible.builtin.set_fact: |
| 138 | + _hub_role_policies_changed: "{{ _current_policies | sort != _merged_policies | sort }}" |
| 139 | + when: |
| 140 | + - app_prefixes is defined and app_prefixes | length > 0 |
| 141 | + - app_update_hub_role | default(true) | bool |
| 142 | + |
| 143 | +- name: Debug - Hub role policy change status |
| 144 | + ansible.builtin.debug: |
| 145 | + msg: "Hub role policies changed: {{ _hub_role_policies_changed }} (current: {{ _current_policies | sort }}, merged: {{ _merged_policies | sort }})" |
| 146 | + verbosity: 1 |
| 147 | + when: |
| 148 | + - app_prefixes is defined and app_prefixes | length > 0 |
| 149 | + - app_update_hub_role | default(true) | bool |
| 150 | + |
| 151 | +# Update hub-role only if policies have changed |
| 152 | +- name: Update hub-role with app policies |
| 153 | + kubernetes.core.k8s_exec: |
| 154 | + namespace: "{{ vault_ns }}" |
| 155 | + pod: "{{ vault_pod }}" |
| 156 | + command: > |
| 157 | + vault write auth/{{ vault_hub }}/role/{{ vault_hub }}-role |
| 158 | + bound_service_account_names="{{ active_external_secrets_sa | default('golang-external-secrets') }}" |
| 159 | + bound_service_account_namespaces="{{ active_external_secrets_ns | default('golang-external-secrets') }}" |
| 160 | + policies="{{ _merged_policies | join(',') }}" |
| 161 | + ttl="{{ vault_hub_ttl }}" |
| 162 | + when: |
| 163 | + - app_prefixes is defined and app_prefixes | length > 0 |
| 164 | + - app_update_hub_role | default(true) | bool |
| 165 | + - _hub_role_policies_changed | bool |
| 166 | + |
| 167 | +- name: Display updated hub-role policies |
| 168 | + ansible.builtin.debug: |
| 169 | + msg: "hub-role policies updated to: {{ _merged_policies | join(', ') }}" |
| 170 | + verbosity: 1 |
| 171 | + when: |
| 172 | + - app_prefixes is defined and app_prefixes | length > 0 |
| 173 | + - app_update_hub_role | default(true) | bool |
| 174 | + - _hub_role_policies_changed | bool |
| 175 | + |
| 176 | +# Optionally create JWT roles for app-level isolation |
| 177 | +# Only creates roles for entries that have jwt_role defined |
| 178 | +- name: Configure JWT role for app (if configured) |
| 179 | + kubernetes.core.k8s_exec: |
| 180 | + namespace: "{{ vault_ns }}" |
| 181 | + pod: "{{ vault_pod }}" |
| 182 | + command: > |
| 183 | + vault write auth/"{{ vault_hub }}"/role/"{{ _app_name }}"-role |
| 184 | + bound_service_account_names="{{ item.jwt_role.service_account_names }}" |
| 185 | + bound_service_account_namespaces="{{ item.jwt_role.service_account_namespaces }}" |
| 186 | + policies="default,{{ vault_global_policy }}-secret,{{ item.prefix | replace('/', '-') }}-k8s-secret" |
| 187 | + ttl="{{ item.jwt_role.ttl | default(vault_hub_ttl) }}" |
| 188 | + loop: "{{ app_prefixes }}" |
| 189 | + loop_control: |
| 190 | + label: "Creating JWT role for {{ _app_name }}" |
| 191 | + vars: |
| 192 | + _app_name: "{{ item.prefix | basename }}" |
| 193 | + when: |
| 194 | + - app_prefixes is defined and app_prefixes | length > 0 |
| 195 | + - app_create_jwt_roles | default(false) | bool |
| 196 | + - item.jwt_role is defined |
0 commit comments