One certificate, three names

The day before a website cutover for a local business I needed one ACM certificate that covered three names: the bare domain, its www name, and the hostname under my own domain where the site had been reviewed. One certificate on the distribution serves all three, so the preview keeps working through the nameserver change. My infrastructure tool wraps an Ansible role that requests certificates with DNS validation, and that is what I reached for.

Gap one: the names never left the wrapper

The role's header said it took a required domain and an optional list of alternate names. The task that calls aws acm request-certificate appended --subject-alternative-names whenever that list was non-empty. The Makefile target that drives it forwarded the domain and the region, and nothing else. Every certificate ever requested through the tool had been single-name, and I had not noticed because the next step was always the console.

There is a history to that. In August I had rewritten this role to call the CLI, because the collection route cannot request a DNS-validated certificate at all: one collection ships no ACM module, and the other's module is an import module that rejects both the alternate-names and the validation-method arguments. The rewrite accepted alternate names and, as written, would have passed them correctly. The wrapper was never updated to match. A code path that nothing calls is a code path that has never run.

Gap two: the space

The first fix was the obvious one line in the Makefile:

-e acm_san="www.example.com staging.example.com"

Ansible parses a plain -e argument as key=value pairs separated by whitespace. The value after the equals sign ended at the first space. The role received one alternate name, the request went to ACM with two names instead of three, and the certificate was issued. I did not read the names on it. I requested again, got the same two names, and still did not read them. Only on the third look did I run describe and count the subject alternative names. Two live certificates with the third name missing, in an account that was hours from a cutover. The third request, after the fix below, was issued that morning with all three names.

The miss was mine twice over. The wrapper bug was a day-one defect, but the whitespace split is documented Ansible behaviour. What made it dangerous here is that nothing failed. The request was valid, the certificate was valid, and the only evidence was a list one item shorter than it should have been.

The fix

Pass the value as JSON, which Ansible parses as a structure and never splits, and let the task turn the string into a list. The Makefile line became:

$(if $(acm_san),-e '{"acm_san":"$(acm_san)"}',)

And the role gained one task at the top, which accepts either a space-separated string from the wrapper or a real list from a playbook:

- name: "[aws_acm] Normalise acm_san to a list"
  ansible.builtin.set_fact:
    acm_san_list: >-
      {{ acm_san.split() if (acm_san is defined and acm_san is string)
         else (acm_san | default([])) }}

The request task now joins acm_san_list back into the CLI argument. The verification is the one I should have done the first time: after the request, describe the certificate and count the names and the validation records. Three names, three CNAMEs to publish.

The second bug in the same task

Fixing the names exposed a second problem a few lines down. After a request, the role polls describe-certificate until the DNS validation records appear, ten tries three seconds apart. The condition read the DomainValidationOptions field and filtered it for entries with a ResourceRecord. For the first few seconds after a request that field does not exist yet. Reading it raised an error inside the condition, so the task failed on its first attempt instead of retrying. One filter fixes it:

until: >-
  (acm_detail_raw.stdout | from_json).Certificate.DomainValidationOptions
  | default([]) | selectattr('ResourceRecord', 'defined') | list | length > 0

With default([]) the condition is simply false until the field arrives, and the retry loop does what it was written to do.

What I do differently now

  • Any extra-var whose value can contain a space goes in as JSON: -e '{"key":"a b c"}'. Never -e key="a b c".
  • The task that receives it normalises to a list, so a playbook caller and a shell caller both work.
  • After every certificate request, describe it and count the names before publishing a single validation record.
  • An until: condition that reads a field the API fills in later gets default([]) or default({}), or the first retry is the last.
  • When a role grows an option, the wrapper gets the option the same day, and the option gets run once end to end. A documented parameter that nothing passes is a promise the code has never kept.