diff --git a/roles/ocp4_workload_cert_manager/defaults/main.yml b/roles/ocp4_workload_cert_manager/defaults/main.yml index a47cf3b8..78c75722 100644 --- a/roles/ocp4_workload_cert_manager/defaults/main.yml +++ b/roles/ocp4_workload_cert_manager/defaults/main.yml @@ -79,12 +79,31 @@ ocp4_workload_cert_manager_gcp_credentials: "{{ gcp_credentials }}" # --------------------------------------------------------------- # Settings for Azure # --------------------------------------------------------------- -# Set from AgnosticV to the actual azure credentials -ocp4_workload_cert_manager_azure_client_id: "{{ azure_service_principal }}" +# Azure DNS service principal application/client ID (string). Explicit role inputs +# override these defaults, which use the canonical Azure names. +ocp4_workload_cert_manager_azure_client_id: "{{ azure_service_principal_id }}" + +# Azure DNS service principal password (secret string). Stored in a Kubernetes +# Secret in cert-manager for issuance AND renewal. Never log this value. +ocp4_workload_cert_manager_azure_client_secret: "{{ azure_service_principal_password }}" + +# Azure cloud environment recognized by cert-manager (string), used by both +# primary and fallback AzureDNS solvers. Public Azure is the default. ocp4_workload_cert_manager_azure_environment: AzurePublicCloud -ocp4_workload_cert_manager_azure_hostedzone_name: "{{ azure_user_domain }}" -ocp4_workload_cert_manager_azure_resource_group_name: "{{ az_dnszone_resource_group }}" -ocp4_workload_cert_manager_azure_subscription_id: "{{ azure_subscription_id }}" + +# Existing publicly delegated Azure DNS zone (string, not a resource ID). +# Enabled certificate targets must equal this zone or be beneath it. +ocp4_workload_cert_manager_azure_hostedzone_name: "{{ azure_dns_zone }}" + +# Resource group containing that DNS zone (string), NOT the cluster resource +# group. The service principal needs DNS record write/delete permission here. +ocp4_workload_cert_manager_azure_resource_group_name: "{{ azure_resource_group }}" + +# Subscription containing the DNS zone (string). +ocp4_workload_cert_manager_azure_subscription_id: "{{ azure_subscription }}" + +# Tenant of the DNS service principal (string). An explicit role override may +# supply a dedicated DNS identity independently of cluster provisioning. ocp4_workload_cert_manager_azure_tenant_id: "{{ azure_tenant }}" # --------------------------------------------------------------- diff --git a/roles/ocp4_workload_cert_manager/readme.adoc b/roles/ocp4_workload_cert_manager/readme.adoc index 48c5d20d..dd53926b 100644 --- a/roles/ocp4_workload_cert_manager/readme.adoc +++ b/roles/ocp4_workload_cert_manager/readme.adoc @@ -1,3 +1,98 @@ -= ocp4_workload_cert_manager - Install Red Hat Cert Manager Operator and Request and install certificates from Let's Encrypt or ZeroSSL += ocp4_workload_cert_manager -See defaults/main.yml for variables to set. +Installs the Red Hat cert-manager Operator, configures an ACME ClusterIssuer, +requests certificates, and installs ready certificates on the default ingress +controller and optionally the API server. It runs on the bastion with cluster-admin +Kubernetes access. See `defaults/main.yml` for inputs and defaults. + +== Azure DNS with Azure IPI + +Use `cloud_provider: azure` and `host_ocp4_deploy_installation_method: openshift_install`. +Add `agnosticd.core_workloads.ocp4_workload_cert_manager` to the config's existing +`workloads` list. Ingress certificates are enabled by default; API certificates +are disabled. Enabling API replacement restarts API servers and uses the existing +rollout wait and kubeconfig adjustment flow. + +The Azure config maps normalized controller facts to these workload inputs, so +workloads running on the bastion use the same sandbox credentials and delegated +DNS zone: + +[cols="1,1"] +|=== +|Role input suffix (`ocp4_workload_cert_manager_azure_`) |Controller fact +|client_id |azure_auth.app_id +|client_secret |azure_auth.password +|subscription_id |azure_auth.subscription_id +|tenant_id |azure_auth.tenant +|hostedzone_name |azure_settings.dns_zone +|resource_group_name |azure_settings.sandbox_group +|=== + +Explicit role-specific inputs override these defaults, allowing a dedicated DNS +identity or a zone in another subscription. Standalone role use prefers canonical +Azure variables and falls back to legacy aliases; missing values fail before +operator installation. The default environment is `AzurePublicCloud`; both primary +and fallback issuers honor `ocp4_workload_cert_manager_azure_environment`. + +The public zone must already exist and be delegated. The service principal must +be able to read the zone and create/delete TXT records; `DNS Zone Contributor` +on that zone is the documented AzureDNS setup. This workload creates neither +identities nor role assignments. See +https://cert-manager.io/docs/configuration/acme/dns01/azuredns/[AzureDNS authentication]. +The resource group is the one containing the DNS zone, not the cluster's group. + +Enabled certificate names must equal the zone or be descendants at a DNS-label +boundary. Case and a final dot are ignored for this check. Azure IPI API/ingress +names beneath the workshop zone work. Microsoft's managed ARO domains do not +belong to that zone; ARO custom domains and HCP are outside this integration. +Disabled targets are not requested or checked. Disabling a flag does not delete +an already installed certificate or revert an existing endpoint. + +== Example + +[source,yaml] +---- +cloud_provider: azure +host_ocp4_deploy_installation_method: openshift_install +workloads: +- agnosticd.core_workloads.ocp4_workload_cert_manager + +# Ingress is the default; opt into API replacement only when needed. +ocp4_workload_cert_manager_install_ingress_certificates: true +ocp4_workload_cert_manager_install_api_certificates: false + +# First validate issuance with the staging CA (certificates are untrusted). +# Omit this override for production Let's Encrypt certificates. +ocp4_workload_cert_manager_acme_url: https://acme-staging-v02.api.letsencrypt.org/directory + +# Optional dedicated DNS identity. Supply its password through Vault/secrets. +# ocp4_workload_cert_manager_azure_client_id: +# ocp4_workload_cert_manager_azure_client_secret: +# ocp4_workload_cert_manager_azure_tenant_id: +# ocp4_workload_cert_manager_azure_subscription_id: +# ocp4_workload_cert_manager_azure_resource_group_name: +# ocp4_workload_cert_manager_azure_hostedzone_name: .azure.redhatworkshops.io +---- + +== Secrets, renewal and troubleshooting + +The client password is stored as `client-secret` in `cert-manager-azure-creds` in +namespace `cert-manager`. Rendering and applying that Secret uses `no_log`. +Protect access to the Secret and keep credentials valid for renewals. Both issuers +reference the same Secret. Updating its credentials and rerunning the workload +does not require creating a new service principal. Do not remove the credentials +or DNS permissions after initial issuance. + +Certificates retain the existing 90-day duration and 15-day renewal window; +cert-manager updates the same TLS Secrets referenced by ingress/API configuration. +After issuance, verify the served certificate. Test renewal with `cmctl renew` +and confirm the Certificate revision and served serial change. Operator installation, +retries and optional fallback CA behavior otherwise follow the existing role. +Removal of this workload is currently not implemented. + +For failures inspect `Certificate`, `CertificateRequest`, `Order` and `Challenge` +conditions, and controller logs. Verify zone delegation, DNS permissions, tenant, +subscription and the DNS resource group. A boundary failure is a configuration +error; changing ACME providers will not fix it. Avoid dumping Secrets or credential +variables while debugging. Live issuance and renewal create temporary Azure TXT +records and require explicit approval in this project. diff --git a/roles/ocp4_workload_cert_manager/tasks/cert_manager_azure.yml b/roles/ocp4_workload_cert_manager/tasks/cert_manager_azure.yml index 8b648810..239e2fa2 100644 --- a/roles/ocp4_workload_cert_manager/tasks/cert_manager_azure.yml +++ b/roles/ocp4_workload_cert_manager/tasks/cert_manager_azure.yml @@ -3,3 +3,4 @@ kubernetes.core.k8s: state: present template: secret-azure-credentials.yaml.j2 + no_log: true diff --git a/roles/ocp4_workload_cert_manager/tasks/validate_azure.yml b/roles/ocp4_workload_cert_manager/tasks/validate_azure.yml new file mode 100644 index 00000000..9e821701 --- /dev/null +++ b/roles/ocp4_workload_cert_manager/tasks/validate_azure.yml @@ -0,0 +1,49 @@ +--- +- name: Validate public Azure DNS settings + ansible.builtin.assert: + that: + - lookup('vars', item) is string + - lookup('vars', item) | trim | length > 0 + fail_msg: "Required Azure DNS input {{ item }} is missing or empty" + loop: + - ocp4_workload_cert_manager_azure_client_id + - ocp4_workload_cert_manager_azure_environment + - ocp4_workload_cert_manager_azure_hostedzone_name + - ocp4_workload_cert_manager_azure_resource_group_name + - ocp4_workload_cert_manager_azure_subscription_id + - ocp4_workload_cert_manager_azure_tenant_id + +- name: Validate protected Azure DNS client secret + ansible.builtin.assert: + that: + - ocp4_workload_cert_manager_azure_client_secret is string + - ocp4_workload_cert_manager_azure_client_secret | length > 0 + fail_msg: Supply ocp4_workload_cert_manager_azure_client_secret or the Azure service principal password + no_log: true + +- name: Validate enabled certificate targets are nonempty hostnames + ansible.builtin.assert: + that: + - >- + not ocp4_workload_cert_manager_install_api_certificates | bool or + (_ocp4_workload_cert_manager_api_hostname is string and _ocp4_workload_cert_manager_api_hostname | length > 0) + - >- + not ocp4_workload_cert_manager_install_ingress_certificates | bool or + (_ocp4_workload_cert_manager_wildcard_domain is string and _ocp4_workload_cert_manager_wildcard_domain | length > 0) + fail_msg: Enabled certificates require openshift_api_url and/or openshift_cluster_ingress_domain + +- name: Validate enabled certificate targets belong to the Azure DNS zone + when: item | length > 0 + vars: + _ocp4_workload_cert_manager_dns_zone: "{{ ocp4_workload_cert_manager_azure_hostedzone_name | lower | regex_replace('[.]$', '') }}" + ansible.builtin.assert: + that: + - >- + (item | lower | regex_replace('[.]$', '')) == _ocp4_workload_cert_manager_dns_zone or + (item | lower | regex_replace('[.]$', '')).endswith('.' ~ _ocp4_workload_cert_manager_dns_zone) + fail_msg: >- + Certificate target {{ item }} is outside Azure DNS zone {{ ocp4_workload_cert_manager_azure_hostedzone_name }}. + Microsoft-managed ARO domains require separate custom-domain configuration; this integration targets Azure IPI. + loop: + - "{{ _ocp4_workload_cert_manager_api_hostname }}" + - "{{ _ocp4_workload_cert_manager_wildcard_domain }}" diff --git a/roles/ocp4_workload_cert_manager/tasks/workload.yml b/roles/ocp4_workload_cert_manager/tasks/workload.yml index 72848619..85c12263 100644 --- a/roles/ocp4_workload_cert_manager/tasks/workload.yml +++ b/roles/ocp4_workload_cert_manager/tasks/workload.yml @@ -1,4 +1,15 @@ --- +- name: Set DNS facts for enabled certificate targets + ansible.builtin.set_fact: + _ocp4_workload_cert_manager_api_hostname: >- + {{ (openshift_api_url | urlsplit('hostname')) if ocp4_workload_cert_manager_install_api_certificates | bool else '' }} + _ocp4_workload_cert_manager_wildcard_domain: >- + {{ openshift_cluster_ingress_domain if ocp4_workload_cert_manager_install_ingress_certificates | bool else '' }} + +- name: Validate Azure DNS inputs before installing the operator + when: ocp4_workload_cert_manager_cloud_provider == 'azure' + ansible.builtin.include_tasks: validate_azure.yml + - name: Install Red Hat Cert Manager operator ansible.builtin.include_role: name: install_operator @@ -26,13 +37,6 @@ state: present template: certmanager.yaml.j2 -- name: Set DNS facts - ansible.builtin.set_fact: - _ocp4_workload_cert_manager_api_hostname: >- - {{ openshift_api_url | urlsplit('hostname') }} - _ocp4_workload_cert_manager_wildcard_domain: >- - {{ openshift_cluster_ingress_domain }} - - name: Print API and wildcard domain ansible.builtin.debug: msg: >- @@ -64,6 +68,7 @@ - name: Setup ACME Bifrost when: ocp4_workload_cert_manager_provider == "acme-bifrost" or ocp4_workload_cert_manager_provider_fallback == "acme-bifrost" block: + - name: Deploy ACME Bifrost webhook kubernetes.core.k8s: state: present @@ -103,14 +108,10 @@ retries: 30 delay: 10 -- name: Set up ClusterIssuer and request certificates +- name: Set up ClusterIssuer kubernetes.core.k8s: state: present - template: "{{ item }}" - loop: - - clusterissuer.yaml.j2 - - certificate-ingress.yaml.j2 - - certificate-api.yaml.j2 + template: clusterissuer.yaml.j2 register: r_clusterissuer retries: 10 delay: 30 @@ -122,6 +123,26 @@ state: present template: "clusterissuer-fallback.yaml.j2" +- name: Request ingress certificate + when: ocp4_workload_cert_manager_install_ingress_certificates | bool + kubernetes.core.k8s: + state: present + template: certificate-ingress.yaml.j2 + register: r_certificate_ingress_request + retries: 10 + delay: 30 + until: r_certificate_ingress_request is success + +- name: Request API certificate + when: ocp4_workload_cert_manager_install_api_certificates | bool + kubernetes.core.k8s: + state: present + template: certificate-api.yaml.j2 + register: r_certificate_api_request + retries: 10 + delay: 30 + until: r_certificate_api_request is success + - name: Install Ingress controller certificate when: ocp4_workload_cert_manager_install_ingress_certificates | bool block: diff --git a/roles/ocp4_workload_cert_manager/templates/clusterissuer-fallback.yaml.j2 b/roles/ocp4_workload_cert_manager/templates/clusterissuer-fallback.yaml.j2 index 8cd289c3..b84b8210 100644 --- a/roles/ocp4_workload_cert_manager/templates/clusterissuer-fallback.yaml.j2 +++ b/roles/ocp4_workload_cert_manager/templates/clusterissuer-fallback.yaml.j2 @@ -20,9 +20,17 @@ spec: {% endif %} solvers: - selector: +{% if not (ocp4_workload_cert_manager_install_api_certificates | bool or ocp4_workload_cert_manager_install_ingress_certificates | bool) %} + {} +{% else %} dnsZones: +{% if ocp4_workload_cert_manager_install_api_certificates | bool %} - {{ _ocp4_workload_cert_manager_api_hostname }} +{% endif %} +{% if ocp4_workload_cert_manager_install_ingress_certificates | bool %} - {{ _ocp4_workload_cert_manager_wildcard_domain }} +{% endif %} +{% endif %} dns01: {% if ocp4_workload_cert_manager_cloud_provider == "aws" and ocp4_workload_cert_manager_provider != "acme-bifrost" %} route53: @@ -43,15 +51,15 @@ spec: {% endif %} {% if ocp4_workload_cert_manager_cloud_provider == "azure" %} azureDNS: - clientID: {{ ocp4_workload_cert_manager_azure_client_id }} + clientID: {{ ocp4_workload_cert_manager_azure_client_id | to_json }} clientSecretSecretRef: name: cert-manager-azure-creds key: client-secret - environment: AzurePublicCloud - hostedZoneName: {{ ocp4_workload_cert_manager_azure_hostedzone_name }} - resourceGroupName: {{ ocp4_workload_cert_manager_azure_resource_group_name }} - subscriptionID: {{ ocp4_workload_cert_manager_azure_subscription_id }} - tenantID: {{ ocp4_workload_cert_manager_azure_tenant_id }} + environment: {{ ocp4_workload_cert_manager_azure_environment | to_json }} + hostedZoneName: {{ ocp4_workload_cert_manager_azure_hostedzone_name | to_json }} + resourceGroupName: {{ ocp4_workload_cert_manager_azure_resource_group_name | to_json }} + subscriptionID: {{ ocp4_workload_cert_manager_azure_subscription_id | to_json }} + tenantID: {{ ocp4_workload_cert_manager_azure_tenant_id | to_json }} {% endif %} {% if ocp4_workload_cert_manager_provider == "acme-bifrost" %} webhook: diff --git a/roles/ocp4_workload_cert_manager/templates/clusterissuer.yaml.j2 b/roles/ocp4_workload_cert_manager/templates/clusterissuer.yaml.j2 index c72329b6..f852ecc0 100644 --- a/roles/ocp4_workload_cert_manager/templates/clusterissuer.yaml.j2 +++ b/roles/ocp4_workload_cert_manager/templates/clusterissuer.yaml.j2 @@ -20,9 +20,17 @@ spec: {% endif %} solvers: - selector: +{% if not (ocp4_workload_cert_manager_install_api_certificates | bool or ocp4_workload_cert_manager_install_ingress_certificates | bool) %} + {} +{% else %} dnsZones: +{% if ocp4_workload_cert_manager_install_api_certificates | bool %} - {{ _ocp4_workload_cert_manager_api_hostname }} +{% endif %} +{% if ocp4_workload_cert_manager_install_ingress_certificates | bool %} - {{ _ocp4_workload_cert_manager_wildcard_domain }} +{% endif %} +{% endif %} dns01: {% if ocp4_workload_cert_manager_cloud_provider == "aws" and ocp4_workload_cert_manager_provider != "acme-bifrost" %} route53: @@ -43,15 +51,15 @@ spec: {% endif %} {% if ocp4_workload_cert_manager_cloud_provider == "azure" %} azureDNS: - clientID: {{ ocp4_workload_cert_manager_azure_client_id }} + clientID: {{ ocp4_workload_cert_manager_azure_client_id | to_json }} clientSecretSecretRef: name: cert-manager-azure-creds key: client-secret - environment: AzurePublicCloud - hostedZoneName: {{ ocp4_workload_cert_manager_azure_hostedzone_name }} - resourceGroupName: {{ ocp4_workload_cert_manager_azure_resource_group_name }} - subscriptionID: {{ ocp4_workload_cert_manager_azure_subscription_id }} - tenantID: {{ ocp4_workload_cert_manager_azure_tenant_id }} + environment: {{ ocp4_workload_cert_manager_azure_environment | to_json }} + hostedZoneName: {{ ocp4_workload_cert_manager_azure_hostedzone_name | to_json }} + resourceGroupName: {{ ocp4_workload_cert_manager_azure_resource_group_name | to_json }} + subscriptionID: {{ ocp4_workload_cert_manager_azure_subscription_id | to_json }} + tenantID: {{ ocp4_workload_cert_manager_azure_tenant_id | to_json }} {% endif %} {% if ocp4_workload_cert_manager_provider == "acme-bifrost" %} webhook: diff --git a/roles/ocp4_workload_cert_manager/templates/secret-azure-credentials.yaml.j2 b/roles/ocp4_workload_cert_manager/templates/secret-azure-credentials.yaml.j2 index 081d3517..dcc6258a 100644 --- a/roles/ocp4_workload_cert_manager/templates/secret-azure-credentials.yaml.j2 +++ b/roles/ocp4_workload_cert_manager/templates/secret-azure-credentials.yaml.j2 @@ -6,4 +6,4 @@ metadata: namespace: cert-manager type: Opaque stringData: - client-secret: {{ azure_password }} + client-secret: {{ ocp4_workload_cert_manager_azure_client_secret | to_json }} diff --git a/tests/static/tox.ini b/tests/static/tox.ini index 608a2fdb..0908da48 100644 --- a/tests/static/tox.ini +++ b/tests/static/tox.ini @@ -1,5 +1,5 @@ [tox] -envlist = yamllint, ansible-lint +envlist = yamllint, ansible-lint, cert-manager skipsdist = True skip_missing_interpreters = True @@ -16,3 +16,12 @@ changedir = {toxinidir}/../.. commands = ansible-galaxy collection install -r {toxinidir}/collections-requirements.yml --force ansible-lint + +# Offline regression checks: writes synthetic manifests only, no cloud/cluster IO. +[testenv:cert-manager] +deps = + ansible-core + PyYAML + Jinja2 +changedir = {toxinidir}/../.. +commands = python -m unittest discover -s tests/unit -p test_cert_manager_azure.py -v diff --git a/tests/unit/test_cert_manager_azure.py b/tests/unit/test_cert_manager_azure.py new file mode 100644 index 00000000..d0b71d39 --- /dev/null +++ b/tests/unit/test_cert_manager_azure.py @@ -0,0 +1,159 @@ +"""Offline Ansible/template behavior; no Kubernetes or Azure operations.""" +import json +import os +from pathlib import Path +import shutil +import subprocess +import sys +import tempfile +import unittest + +import jinja2 +import yaml + +ROOT = Path(__file__).resolve().parents[2] +ROLE = ROOT / 'roles/ocp4_workload_cert_manager' +PREFIX = 'ocp4_workload_cert_manager_' +AZ = PREFIX + 'azure_' +SECRET = 'synthetic: "quoted" # secret\nsecond line' + + +class AzureCertManagerTests(unittest.TestCase): + def test_issuer_templates_preserve_other_solvers(self): + defaults = yaml.safe_load((ROLE / 'defaults/main.yml').read_text()) + env = jinja2.Environment(loader=jinja2.FileSystemLoader(ROLE / 'templates'), + undefined=jinja2.StrictUndefined) + env.filters.update(bool=bool, to_json=json.dumps) + for provider, solver in [('aws', 'route53'), ('gcp', 'cloudDNS'), ('azure', 'azureDNS')]: + for fallback in (False, True): + with self.subTest(provider=provider, fallback=fallback): + values = dict(defaults) + values.update({PREFIX + 'cloud_provider': provider, + PREFIX + 'provider_fallback': 'letsencrypt', + PREFIX + 'acme_url_fallback': 'https://example.test/acme', + PREFIX + 'aws_region': 'us-east-1', PREFIX + 'aws_access_key_id': 'fake', + PREFIX + 'gcp_project_id': 'fake-project', 'guid': 'test', + '_ocp4_workload_cert_manager_hostedzoneid': 'fake-zone', + '_ocp4_workload_cert_manager_api_hostname': 'api.test.example.com', + '_ocp4_workload_cert_manager_wildcard_domain': 'apps.test.example.com'}) + values.update({AZ + key: value for key, value in dict( + client_id='fake-app', environment='AzureUSGovernmentCloud', + hostedzone_name='example.com', resource_group_name='dns', + subscription_id='fake-sub', tenant_id='fake-tenant').items()}) + filename = 'clusterissuer-fallback.yaml.j2' if fallback else 'clusterissuer.yaml.j2' + document = yaml.safe_load(env.get_template(filename).render(values)) + dns = document['spec']['acme']['solvers'][0]['dns01'] + self.assertEqual(list(dns), [solver]) + if provider == 'azure': + self.assertEqual(dns[solver]['environment'], 'AzureUSGovernmentCloud') + self.assertEqual(dns[solver]['clientSecretSecretRef']['key'], 'client-secret') + + def test_real_ansible_defaults_validation_and_certificate_selection(self): + """Execute production task conditions, replacing external writes with local files.""" + with tempfile.TemporaryDirectory(prefix='cert-manager-test-') as directory: + tmp = Path(directory) + target = tmp / 'collections/ansible_collections/agnosticd/core_workloads/roles/ocp4_workload_cert_manager' + shutil.copytree(ROLE, target) + tasks = yaml.safe_load((ROLE / 'tasks/workload.yml').read_text()) + selected = [] + for task in tasks: + if task['name'] == 'Install Ingress controller certificate': + break + if task['name'] == 'Install Red Hat Cert Manager operator': + selected.append({'name': task['name'], 'ansible.builtin.copy': { + 'dest': '{{ test_output }}/operator', 'content': 'installed', 'mode': 'u=rw,go='}}) + elif 'kubernetes.core.k8s' in task: + template = task['kubernetes.core.k8s']['template'] + replacement = {k: v for k, v in task.items() if k != 'kubernetes.core.k8s'} + replacement['ansible.builtin.copy'] = { + 'dest': '{{ test_output }}/' + template.replace('.j2', ''), + 'content': "{{ lookup('ansible.builtin.template', '" + template + "') }}", + 'mode': 'u=rw,go='} + replacement.update(retries=1, delay=0) + selected.append(replacement) + elif 'ansible.builtin.include_tasks' in task: + selected.append(task) + elif 'ansible.builtin.set_fact' in task: + selected.append(task) + (target / 'tasks/workload.yml').write_text(yaml.safe_dump(selected, sort_keys=False)) + secret = yaml.safe_load((ROLE / 'tasks/cert_manager_azure.yml').read_text())[0] + self.assertTrue(secret['no_log']) + secret.pop('kubernetes.core.k8s') + secret['ansible.builtin.copy'] = { + 'dest': '{{ test_output }}/secret.yaml', + 'content': "{{ lookup('ansible.builtin.template', 'secret-azure-credentials.yaml.j2') }}", + 'mode': 'u=rw,go='} + (target / 'tasks/cert_manager_azure.yml').write_text(yaml.safe_dump([secret], sort_keys=False)) + playbook = tmp / 'playbook.yml' + playbook.write_text(yaml.safe_dump([dict(hosts='bastion', connection='local', gather_facts=False, + roles=['agnosticd.core_workloads.ocp4_workload_cert_manager'])], sort_keys=False)) + canonical = dict(azure_service_principal_id='app', azure_service_principal_password=SECRET, + azure_subscription='subscription', azure_tenant='tenant', + azure_resource_group='dns', azure_dns_zone='example.com') + explicit = {AZ + key: value for key, value in dict(client_id='app', client_secret=SECRET, + subscription_id='subscription', tenant_id='tenant', resource_group_name='dns', + hostedzone_name='example.com').items()} + normalized = dict(azure_auth=dict(app_id='app', password=SECRET, subscription_id='subscription', tenant='tenant'), + azure_settings=dict(dns_zone='example.com', sandbox_group='dns')) + v2_config = ROOT.parents[2] / 'agnosticd-v2/ansible/configs/openshift-cluster/azure/default_vars.yml' + config_mapping = {key: value for key, value in yaml.safe_load(v2_config.read_text()).items() + if key.startswith(AZ)} if v2_config.exists() else {} + if v2_config.exists(): + self.assertEqual(len(config_mapping), 6) + cases = [('canonical', canonical, True), + ('explicit-overrides', dict(canonical, **explicit, azure_dns_zone='wrong.test'), True), + ('missing-secret', dict(canonical, azure_service_principal_password=''), False), + ('missing-client', dict(canonical, azure_service_principal_id=''), False), + ('outside-zone', dict(canonical, openshift_cluster_ingress_domain='apps.aroapp.io'), False), + ('suffix-boundary', dict(canonical, openshift_cluster_ingress_domain='notexample.com'), False), + ('case-trailing-dot', dict(canonical, azure_dns_zone='EXAMPLE.COM.'), True)] + if config_mapping: + cases.append(('controller-to-bastion', config_mapping, True)) + for ingress, api in ((True, False), (False, True), (True, True), (False, False)): + cases.append(('selection-' + str(ingress) + '-' + str(api), dict(canonical, **{ + PREFIX + 'install_ingress_certificates': ingress, PREFIX + 'install_api_certificates': api}), True)) + for label, overrides, success in cases: + with self.subTest(case=label): + output = tmp / label + output.mkdir() + variables = dict(ACTION='provision', cloud_provider='azure', test_output=str(output), + openshift_api_url='https://api.test.example.com:6443', + openshift_cluster_ingress_domain='apps.test.example.com', + ansible_python_interpreter=sys.executable) + variables.update({PREFIX + 'provider_fallback': 'letsencrypt', + PREFIX + 'acme_url_fallback': 'https://example.test/acme'}) + variables.update(overrides) + # Disabled endpoints need no hostname input, even if outside the zone. + if variables.get(PREFIX + 'install_api_certificates') is False: + variables.pop('openshift_api_url') + if variables.get(PREFIX + 'install_ingress_certificates') is False: + variables.pop('openshift_cluster_ingress_domain') + inventory = tmp / 'inventory.json' + inventory.write_text(json.dumps({'all': {'hosts': { + 'localhost': normalized, 'bastion': variables}}})) + environment = dict(os.environ, ANSIBLE_COLLECTIONS_PATH=str(tmp / 'collections'), + ANSIBLE_LOCAL_TEMP=str(tmp / 'local'), ANSIBLE_REMOTE_TEMP=str(tmp / 'remote')) + result = subprocess.run([sys.executable, '-m', 'ansible.cli.playbook', + '-i', str(inventory), str(playbook)], env=environment, text=True, + capture_output=True, timeout=60, check=False) + self.assertEqual(result.returncode == 0, success, result.stdout + result.stderr) + self.assertNotIn('synthetic:', result.stdout + result.stderr) + self.assertEqual((output / 'operator').exists(), success) + if not success: + self.assertFalse((output / 'secret.yaml').exists()) + continue + credentials = yaml.safe_load((output / 'secret.yaml').read_text()) + self.assertEqual(credentials['stringData']['client-secret'], SECRET) + for kind, enabled in [('ingress', variables.get(PREFIX + 'install_ingress_certificates', True)), + ('api', variables.get(PREFIX + 'install_api_certificates', False))]: + self.assertEqual((output / ('certificate-' + kind + '.yaml')).exists(), enabled) + for name in ['clusterissuer.yaml', 'clusterissuer-fallback.yaml']: + issuer = yaml.safe_load((output / name).read_text()) + solver = issuer['spec']['acme']['solvers'][0]['dns01']['azureDNS'] + self.assertEqual(solver['clientID'], 'app') + self.assertEqual(solver['subscriptionID'], 'subscription') + self.assertEqual(solver['clientSecretSecretRef']['name'], 'cert-manager-azure-creds') + + +if __name__ == '__main__': + unittest.main()