Skip to content
Open
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 @@
---

- 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
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
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 }}"
5 changes: 5 additions & 0 deletions ansible/azure-requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
azure-identity>=1.19.0
azure-mgmt-core>=1.4.0
azure-mgmt-managementgroups>=1.0.0
azure-mgmt-network>=25.0.0
azure-mgmt-resource>=23.0.0
156 changes: 156 additions & 0 deletions ansible/configure-spire-server-x509pop.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
---
# 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: This playbook enables "create-only mode" on the SpireServer CR to prevent
# the operator from reverting our manual patches. This is done via the annotation:
# ztwim.openshift.io/create-only: "true"
#
# NOTE: The create-only mode is enabled AFTER verifying the ConfigMap has the correct
# cluster name. This prevents a race condition where the operator hasn't fully reconciled
# the ConfigMap before we lock it.

- 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 SpireServer CR to determine expected cluster name
kubernetes.core.k8s_info:
api_version: operator.openshift.io/v1alpha1
kind: SpireServer
name: cluster
namespace: "{{ spire_namespace }}"
register: spire_server_cr
retries: 30
delay: 10
until: spire_server_cr.resources | length > 0

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

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

- name: Wait for SPIRE Server ConfigMap with correct cluster name
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
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
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
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
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
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: Enable create-only mode on SpireServer CR
ansible.builtin.debug:
msg: "Enabling create-only mode to prevent operator from reverting x509pop configuration"

- name: Set create-only annotation on SpireServer CR
kubernetes.core.k8s:
state: patched
api_version: operator.openshift.io/v1alpha1
kind: SpireServer
name: cluster
namespace: "{{ spire_namespace }}"
definition:
metadata:
annotations:
ztwim.openshift.io/create-only: "true"
Copy link
Collaborator

Choose a reason for hiding this comment

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

@sabre1041 - Could you have a look this. Give your comfort level given coco will be an optional feature right now?


- name: Final status
ansible.builtin.debug:
msg: "x509pop configuration complete. Create-only mode enabled to preserve manual patches."
Loading