Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions ansible/azure-nat-gateway.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
---

Comment on lines +1 to +2
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flag for future work - We should create an issue to add this playbook to the VP ansible collection. @mhjacks

- name: Configure Azure NAT Gateway
become: false
connection: local
hosts: localhost
gather_facts: false
vars:
kubeconfig: "{{ lookup('env', 'KUBECONFIG') }}"
resource_prefix: "coco"
tasks:
- name: Get Azure credentials # noqa: syntax-check[unknown-module]
kubernetes.core.k8s_info:
kind: Secret
namespace: openshift-cloud-controller-manager
name: azure-cloud-credentials
register: azure_credentials
retries: 20
delay: 5

- name: Get Azure configuration # noqa: syntax-check[unknown-module]
kubernetes.core.k8s_info:
kind: ConfigMap
namespace: openshift-cloud-controller-manager
name: cloud-conf
register: azure_cloud_conf
retries: 20
delay: 5

- name: Set facts
ansible.builtin.set_fact:
azure_subscription_id: "{{ (azure_cloud_conf.resources[0]['data']['cloud.conf'] | from_json)['subscriptionId'] }}"
azure_tenant_id: "{{ (azure_cloud_conf.resources[0]['data']['cloud.conf'] | from_json)['tenantId'] }}"
azure_resource_group: "{{ (azure_cloud_conf.resources[0]['data']['cloud.conf'] | from_json)['vnetResourceGroup'] }}"
azure_client_id: "{{ azure_credentials.resources[0]['data']['azure_client_id'] | b64decode }}"
azure_client_secret: "{{ azure_credentials.resources[0]['data']['azure_client_secret'] | b64decode }}"
azure_vnet: "{{ (azure_cloud_conf.resources[0]['data']['cloud.conf'] | from_json)['vnetName'] }}"
azure_subnet: "{{ (azure_cloud_conf.resources[0]['data']['cloud.conf'] | from_json)['subnetName'] }}"
coco_public_ip_name: "{{ resource_prefix }}-pip"
coco_nat_gateway_name: "{{ resource_prefix }}-nat-gateway"
no_log: true

- name: Create Public IP for NAT Gateway
azure.azcollection.azure_rm_publicipaddress:
subscription_id: "{{ azure_subscription_id }}"
tenant: "{{ azure_tenant_id }}"
client_id: "{{ azure_client_id }}"
secret: "{{ azure_client_secret }}"
resource_group: "{{ azure_resource_group }}"
name: "{{ coco_public_ip_name }}"
sku: "standard"
allocation_method: "static"

- name: Retrieve Public IP for NAT Gateway
azure.azcollection.azure_rm_publicipaddress_info:
subscription_id: "{{ azure_subscription_id }}"
tenant: "{{ azure_tenant_id }}"
client_id: "{{ azure_client_id }}"
secret: "{{ azure_client_secret }}"
resource_group: "{{ azure_resource_group }}"
name: "{{ coco_public_ip_name }}"
register: coco_gw_public_ip

- name: Create NAT Gateway
azure.azcollection.azure_rm_natgateway:
subscription_id: "{{ azure_subscription_id }}"
tenant: "{{ azure_tenant_id }}"
client_id: "{{ azure_client_id }}"
secret: "{{ azure_client_secret }}"
resource_group: "{{ azure_resource_group }}"
name: "{{ coco_nat_gateway_name }}"
idle_timeout_in_minutes: 10
sku:
name: standard
public_ip_addresses:
- "{{ coco_gw_public_ip.publicipaddresses[0].id }}"
register: coco_natgw

- name: Update the worker subnet to associate NAT gateway
azure.azcollection.azure_rm_subnet:
subscription_id: "{{ azure_subscription_id }}"
tenant: "{{ azure_tenant_id }}"
client_id: "{{ azure_client_id }}"
secret: "{{ azure_client_secret }}"
resource_group: "{{ azure_resource_group }}"
name: "{{ azure_subnet }}"
virtual_network_name: "{{ azure_vnet }}"
nat_gateway: "{{ coco_nat_gateway_name }}"
144 changes: 144 additions & 0 deletions ansible/configure-spire-server-x509pop.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
---
# Configure SPIRE Server to support x509pop node attestation for CoCo pods
# The Red Hat SPIRE Operator's SpireServer CRD does not expose x509pop plugin configuration
# This job patches the operator-generated ConfigMap and StatefulSet to add x509pop support
#
# IMPORTANT: The operator must have CREATE_ONLY_MODE=true env var set (via subscription
# config) to prevent it from reverting our manual patches. Without this, the operator
# continuously reconciles and overwrites x509pop changes.
# Note: In v0.2.0 (tech-preview) this was done via a CR annotation
# (ztwim.openshift.io/create-only). In v1.0.0 (GA) it changed to the env var.

- name: Configure SPIRE Server for x509pop attestation
become: false
connection: local
hosts: localhost
gather_facts: false
vars:
spire_namespace: "zero-trust-workload-identity-manager"
configmap_name: "spire-server"
statefulset_name: "spire-server"
ca_configmap_name: "spire-x509pop-ca"
ca_mount_path: "/run/spire/x509pop-ca"
tasks:
- name: Get ZeroTrustWorkloadIdentityManager CR to determine expected cluster name # noqa: syntax-check[unknown-module]
kubernetes.core.k8s_info:
api_version: operator.openshift.io/v1alpha1
kind: ZeroTrustWorkloadIdentityManager
name: cluster
register: ztwim_cr
retries: 30
Comment thread
beraldoleal marked this conversation as resolved.
delay: 10
until: ztwim_cr.resources | length > 0

- name: Extract expected cluster name from ZTWIM CR
ansible.builtin.set_fact:
expected_cluster_name: "{{ ztwim_cr.resources[0].spec.clusterName }}"

- name: Display expected cluster name
ansible.builtin.debug:
msg: "Expected cluster name from ZTWIM CR: {{ expected_cluster_name }}"

- name: Wait for SPIRE Server ConfigMap with correct cluster name # noqa: syntax-check[unknown-module]
kubernetes.core.k8s_info:
kind: ConfigMap
namespace: "{{ spire_namespace }}"
name: "{{ configmap_name }}"
register: spire_configmap
retries: 60
delay: 5
until: >
spire_configmap.resources | length > 0 and
(spire_configmap.resources[0].data['server.conf'] | from_json
).plugins.NodeAttestor[0].k8s_psat.plugin_data.clusters[0][expected_cluster_name] is defined

- name: ConfigMap has correct cluster name
ansible.builtin.debug:
msg: "ConfigMap verified with correct cluster name: {{ expected_cluster_name }}"

- name: Get current SPIRE Server configuration # noqa: syntax-check[unknown-module]
kubernetes.core.k8s_info:
kind: ConfigMap
namespace: "{{ spire_namespace }}"
name: "{{ configmap_name }}"
register: spire_config

- name: Parse server configuration
ansible.builtin.set_fact:
server_conf: "{{ spire_config.resources[0].data['server.conf'] | from_json }}"

- name: Check if x509pop already configured
ansible.builtin.set_fact:
x509pop_exists: "{{ server_conf.plugins.NodeAttestor | selectattr('x509pop', 'defined') | list | length > 0 }}"

- name: Add x509pop NodeAttestor plugin # noqa: syntax-check[unknown-module]
kubernetes.core.k8s:
state: present
definition:
apiVersion: v1
kind: ConfigMap
metadata:
name: "{{ configmap_name }}"
namespace: "{{ spire_namespace }}"
data:
server.conf: >-
{{ server_conf | combine({'plugins': {'NodeAttestor':
server_conf.plugins.NodeAttestor + [{'x509pop': {'plugin_data':
{'ca_bundle_path': '/run/spire/x509pop-ca/ca-bundle.pem'}}}]}},
recursive=True) | to_json }}
when: not x509pop_exists

- name: Wait for SPIRE Server StatefulSet to exist # noqa: syntax-check[unknown-module]
kubernetes.core.k8s_info:
kind: StatefulSet
namespace: "{{ spire_namespace }}"
name: "{{ statefulset_name }}"
register: spire_statefulset
retries: 30
delay: 10
until: spire_statefulset.resources | length > 0

- name: Check if CA volume already mounted
ansible.builtin.set_fact:
ca_volume_exists: "{{ spire_statefulset.resources[0].spec.template.spec.volumes | selectattr('name', 'equalto', 'x509pop-ca') | list | length > 0 }}"

- name: Add CA volume to SPIRE Server StatefulSet # noqa: syntax-check[unknown-module]
kubernetes.core.k8s:
state: patched
kind: StatefulSet
namespace: "{{ spire_namespace }}"
name: "{{ statefulset_name }}"
definition:
spec:
template:
spec:
volumes:
- name: x509pop-ca
configMap:
name: "{{ ca_configmap_name }}"
containers:
- name: spire-server
volumeMounts:
- name: x509pop-ca
mountPath: "{{ ca_mount_path }}"
readOnly: true
when: not ca_volume_exists

- name: Restart SPIRE Server to apply configuration # noqa: syntax-check[unknown-module]
kubernetes.core.k8s:
state: absent
kind: Pod
namespace: "{{ spire_namespace }}"
label_selectors:
- app.kubernetes.io/name=server
when: (not x509pop_exists) or (not ca_volume_exists)

- name: Configuration status
ansible.builtin.debug:
msg: >-
{{ 'x509pop already configured' if (x509pop_exists and ca_volume_exists)
else 'x509pop NodeAttestor plugin and CA volume mount configured successfully' }}

- name: Final status
ansible.builtin.debug:
msg: "x509pop configuration complete. CREATE_ONLY_MODE env var on the operator prevents reverts."
Loading
Loading