Overview

The prism-ec2-bootstrap.py script runs as root on the instance and uses only the Python 3 standard library. It:

  1. Reads the instance profile credentials and the AWS-signed instance identity document from the instance metadata service (IMDSv2).
  2. Presigns an STS GetCallerIdentity request bound to your Prism server and assumes a Prism service account with it (POST /api/v1/auth/service-accounts/assume/aws-identity). Prism checks the IAM role against the service account's AWS identity assumption method and verifies that the identity document belongs to the caller.
  3. Keeps the service account's JWT in /etc/prism/token.jwt, so other software on the instance can use the instance identity too.
  4. Issues an X.509 certificate and private key, and writes key.pem, cert.pem and ca.pem to /etc/prism.
  5. Issues an SSH host certificate for every host key in /etc/ssh, and writes it next to the key as ssh_host_*_key-cert.pub.
  6. Points sshd at the certificates in a managed drop-in (/etc/ssh/sshd_config.d/40-prism.conf, or a marked block in sshd_config when there are no drop-ins), checks it with sshd -t (and restores the old file if that fails), and reloads sshd.
  7. With --trust-user-ca, also writes Prism's SSH user CA to /etc/ssh/prism_user_ca.pub and sets TrustedUserCAKeys.

Each run checks every time-bound credential (the JWT, the X.509 certificate and each SSH host certificate) and renews only those that have passed half their lifetime (--renew-fraction). It also renews credentials that are missing, that don't match the current host key or hostnames, or whose options changed. The CA certificate and SSH CA key are fetched again daily (--ca-refresh-seconds) to pick up CA rotation. sshd is reloaded only when something it uses changed. A run with nothing to renew makes no Prism calls, so running the script often costs little. --install-timer sets it up to run on a schedule.

Prerequisites

  1. Verify the domain

    prism projects domains create -p my-project hosts.example.com
    # Publish the TXT record printed above at _prism-challenge.hosts.example.com, then:
    prism projects domains verify -p my-project hosts.example.com
  2. Create the service account for the hosts

    One service account serves every instance with the IAM role. Each instance gets a certificate for its own instance ID under the domain.

    prism service-accounts create -p my-project -n ec2-host
    
    # Instances with this IAM role may assume it
    prism service-accounts assumptions add-aws-identity -p my-project \
      --service-account ec2-host \
      --role-arn arn:aws:iam::123456789012:role/my-instance-role
    
    # Hostnames it may get certificates for
    prism service-accounts hostnames add -p my-project ec2-host '*.hosts.example.com'
  3. Grant the service account its certificate permissions

    SA=ec2-host@service-accounts.my-project.id.prism.local
    
    for perm in organizations.get organizations.get_ca_cert \
                organizations.get_ssh_ca organizations.issue_certificate; do
      prism org add-grant --org my-org --principal $SA --effect allow --permission $perm
    done
    
    prism service-accounts add-grant -p my-project --service-account ec2-host \
      --principal $SA --effect allow --permission service_accounts.issue_ssh_host_certificate
  4. Run the bootstrap script on the instance

    Download it next to the prism CLI (see Download):

    sudo curl -fsSL -o /usr/local/sbin/prism-ec2-bootstrap \
      https://www.ycoll.com/prism/releases/prism-ec2-bootstrap.py
    sudo chmod 755 /usr/local/sbin/prism-ec2-bootstrap
    
    sudo prism-ec2-bootstrap \
      --server https://prism.example.com \
      --project my-project \
      --service-account ec2-host \
      --hostname '{instance_id}.hosts.example.com' \
      --trust-user-ca

    --hostname can be repeated and accepts the placeholders {instance_id}, {region}, {availability_zone}, {account_id} and {private_ip_dashed}. Each hostname must match one of the service account's hostnames. Leave out --trust-user-ca to only install host certificates. Other options: --x509-validity-days (30), --ssh-validity-seconds (30 days), --cert-dir, --ssh-key-types, --ca-file, --no-sshd and --no-reload; see --help.

    You can do the same from user data at launch, renewal timer included:

    #!/bin/bash
    curl -fsSL -o /usr/local/sbin/prism-ec2-bootstrap https://www.ycoll.com/prism/releases/prism-ec2-bootstrap.py
    chmod 755 /usr/local/sbin/prism-ec2-bootstrap
    /usr/local/sbin/prism-ec2-bootstrap --server https://prism.example.com --project my-project \
      --service-account ec2-host --hostname '{instance_id}.hosts.example.com' --trust-user-ca \
      --install-timer
  5. Renew on a timer

    Add --install-timer to the command above. After running, the script installs itself as /usr/local/sbin/prism-ec2-bootstrap and adds prism-ec2-bootstrap.service and prism-ec2-bootstrap.timer. The service reruns the script with the same options every --timer-interval seconds (default 600). Each credential is renewed once half its lifetime has passed: the 24-hour JWT after 12 hours, and 30-day certificates after 15 days.

    sudo prism-ec2-bootstrap --server https://prism.example.com --project my-project \
      --service-account ec2-host --hostname '{instance_id}.hosts.example.com' --trust-user-ca \
      --install-timer
    
    systemctl list-timers prism-ec2-bootstrap.timer
    journalctl -u prism-ec2-bootstrap.service    # what each run renewed, and when the next renewal is due

    To change the options, run the command again with --install-timer. If the change affects the credentials, everything is reissued right away. --force renews everything now, and --uninstall-timer removes the timer. Keep the interval well below half of the shortest credential lifetime.

  6. Trust the host certificates on clients

    Add the org SSH CA as a certificate authority for the domain in known_hosts:

    echo "@cert-authority *.hosts.example.com $(prism org ssh-ca)" >> ~/.ssh/known_hosts
    
    ssh ec2-user@i-0123456789abcdef0.hosts.example.com
    # Without DNS for the name, connect by IP and name the certificate's host:
    ssh -o HostKeyAlias=i-0123456789abcdef0.hosts.example.com ec2-user@203.0.113.10
  7. Log in with a Prism user certificate (--trust-user-ca)

    sshd accepts a Prism-signed user certificate when one of its principals is the login name. Prism puts the Unix usernames assigned to a user or service account in the certificate as principals.

    # Admin: allow the user to log in as ec2-user
    prism unix-usernames create --username ec2-user
    prism users unix-usernames set alice ec2-user
    
    # User: certify a key and log in
    prism --output json org ssh-cert --public-key ~/.ssh/id_ed25519.pub --validity-seconds 28800 \
      | jq -r .certificate_openssh > ~/.ssh/id_ed25519-cert.pub
    ssh ec2-user@i-0123456789abcdef0.hosts.example.com

What gets installed

FileContents
/etc/prism/token.jwt (0600)Service account JWT (24 h), with an association claim describing the instance
/etc/prism/key.pem (0600)X.509 private key
/etc/prism/cert.pemCertificate with subject ec2-host@service-accounts.my-project… and the hostnames as DNS SANs
/etc/prism/ca.pemOrganization CA certificate
/etc/prism/ssh_ca.pubOrganization SSH CA public key
/etc/prism/state.jsonFingerprint of the options used, so that changing them causes reissuance
/etc/ssh/ssh_host_*_key-cert.pubSSH host certificates for the hostnames
/etc/ssh/prism_user_ca.pubOrg SSH CA (with --trust-user-ca)
/etc/systemd/system/prism-ec2-bootstrap.{service,timer}Renewal timer (with --install-timer)
/etc/ssh/sshd_config.d/40-prism.confHostKey, HostCertificate and TrustedUserCAKeys lines

Each issuance is audit logged as OrganizationsIssueCertificate or ServiceAccountsIssueSshHostCertificate, with the hostnames, serial and validity (prism org audit --org my-org).

Security considerations

Troubleshooting

"service account does not allow assumption from this AWS identity"

The instance's IAM role ARN is not in the service account's AWS identity assumption method. Check with prism service-accounts assumptions list -p my-project --service-account ec2-host.

HTTP 403 on certificate issuance

Either a grant from step 3 is missing, or a requested hostname matches none of the service account's hostnames. A hostname also stops working if its domain stops verifying (Prism rechecks domains hourly).

"sshd -t rejected the new configuration, reverted"

The script restored the previous sshd configuration. Run sshd -t to see why.