EC2 Host Identity and SSH Certificates
Let EC2 instances prove who they are to Prism with their instance profile, then
issue each instance an X.509 certificate and SSH host certificates for
<instance-id>.<your-domain>. Optionally, sshd can also trust Prism's
SSH user CA, so people log in with short-lived Prism certificates instead of
authorized_keys.
Overview
The prism-ec2-bootstrap.py script runs as root on the instance and uses
only the Python 3 standard library. It:
- Reads the instance profile credentials and the AWS-signed instance identity document from the instance metadata service (IMDSv2).
- Presigns an STS
GetCallerIdentityrequest 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. - Keeps the service account's JWT in
/etc/prism/token.jwt, so other software on the instance can use the instance identity too. - Issues an X.509 certificate and private key, and writes
key.pem,cert.pemandca.pemto/etc/prism. - Issues an SSH host certificate for every host key in
/etc/ssh, and writes it next to the key asssh_host_*_key-cert.pub. - Points sshd at the certificates in a managed drop-in
(
/etc/ssh/sshd_config.d/40-prism.conf, or a marked block insshd_configwhen there are no drop-ins), checks it withsshd -t(and restores the old file if that fails), and reloads sshd. - With
--trust-user-ca, also writes Prism's SSH user CA to/etc/ssh/prism_user_ca.puband setsTrustedUserCAKeys.
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
- A Prism project with a logged-in admin CLI profile
- A domain you control, verified on the project (e.g.
hosts.example.com) - EC2 instances with an instance profile (IAM role) and IMDSv2 enabled
- Python 3.8+ and OpenSSH on the instance (both ship with Amazon Linux 2023)
-
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 -
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' -
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 -
Run the bootstrap script on the instance
Download it next to the
prismCLI (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--hostnamecan 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-cato only install host certificates. Other options:--x509-validity-days(30),--ssh-validity-seconds(30 days),--cert-dir,--ssh-key-types,--ca-file,--no-sshdand--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 -
Renew on a timer
Add
--install-timerto the command above. After running, the script installs itself as/usr/local/sbin/prism-ec2-bootstrapand addsprism-ec2-bootstrap.serviceandprism-ec2-bootstrap.timer. The service reruns the script with the same options every--timer-intervalseconds (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 dueTo change the options, run the command again with
--install-timer. If the change affects the credentials, everything is reissued right away.--forcerenews everything now, and--uninstall-timerremoves the timer. Keep the interval well below half of the shortest credential lifetime. -
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 -
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
| File | Contents |
|---|---|
/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.pem | Certificate with subject ec2-host@service-accounts.my-project… and the hostnames as DNS SANs |
/etc/prism/ca.pem | Organization CA certificate |
/etc/prism/ssh_ca.pub | Organization SSH CA public key |
/etc/prism/state.json | Fingerprint of the options used, so that changing them causes reissuance |
/etc/ssh/ssh_host_*_key-cert.pub | SSH host certificates for the hostnames |
/etc/ssh/prism_user_ca.pub | Org 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.conf | HostKey, 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
-
With a wildcard hostname such as
*.hosts.example.com, Prism checks that the instance holds the IAM role, but not that it asks only for its own instance ID. So any instance with the role can get a certificate for another name under the wildcard. Give each group of hosts its own IAM role, service account and subdomain to limit how far that reaches. - Require IMDSv2 (
HttpTokens=required) so that workloads on the instance cannot be tricked into leaking role credentials through SSRF. - Use short user certificate lifetimes. sshd does not consult Prism at login, so a user certificate stays valid until it expires.
- Reruns replace the managed block, so dropping
--trust-user-caremovesTrustedUserCAKeysagain.
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.