Goal
Internal services such as admin consoles, service-to-service APIs and databases behind private load balancers need TLS certificates that a public CA will not issue. Self-signed certificates on each host expire without warning and every client needs its own trust exception. The OCI Certificates service can run a private CA for you: it signs certificates, renews them on a schedule and keeps every version.
This guide covers:
- Creating an HSM-protected Vault key and a root CA.
- Issuing a TLS server certificate with SANs and a renewal rule.
- Pulling the current certificate and private key onto a Linux server with instance principals, and reloading the service when a new version appears.
OCI load balancers and API Gateway can use these certificates directly and pick up renewed versions themselves. Compute instances can't, and that's where certificates expire unnoticed.
Before you start
| Requirement | Why |
|---|---|
| A vault with an HSM-protected asymmetric key (RSA or ECDSA) | The Certificates service does not support software-protected keys for a CA. Software keys don't appear in the console picker. |
| Vault key in the same tenancy as the CA | Cross-tenancy key use isn't supported. |
| Dynamic group for CAs plus policy | The CA uses a resource principal to sign with the key. |
OCI CLI 3.x, jq, openssl | All commands below. |
Set some variables first:
export C=ocid1.compartment.oc1..aaaa... # compartment for vault, CA and certs
export VAULT_ID=ocid1.vault.oc1.eu-frankfurt-1.xxxx
export MGMT_EP=$(oci kms management vault get --vault-id "$VAULT_ID" \
--query 'data."management-endpoint"' --raw-output)
Step 1: IAM for the CA and its administrators
Create a dynamic group (in the Default identity domain, or whichever domain holds your policies) named CertificateAuthority-DG with this matching rule:
resource.type='certificateauthority'
Then add the policies. use keys lets the CA sign. manage objects is only needed if you publish a certificate revocation list (CRL) to a bucket.
Allow dynamic-group CertificateAuthority-DG to use keys in compartment security
Allow dynamic-group CertificateAuthority-DG to manage objects in compartment security
Allow group CertificateAuthorityAdmins to manage certificate-authority-family in compartment security
Allow group CertificateAuthorityAdmins to read keys in compartment security
Allow group CertificateAuthorityAdmins to use key-delegate in compartment security
Allow group CertificateAuthorityAdmins to read vaults in compartment security
Allow group CertificateAuthorityAdmins to manage leaf-certificate-family in compartment security
Tip
Most "CA creation failed" and NotAuthorizedOrNotFound errors come from a missing dynamic group statement. Check it first.
Step 2: Create the HSM key and the root CA
Create an RSA 2048 key (length is in bytes) in HSM protection mode:
KEY_ID=$(oci kms management key create \
--endpoint "$MGMT_EP" \
--compartment-id "$C" \
--display-name internal-root-ca-key \
--key-shape '{"algorithm":"RSA","length":256}' \
--protection-mode HSM \
--wait-for-state ENABLED \
--query 'data.id' --raw-output)
Write the CA subject and an issuance rule. The rule caps how long the CA itself and the leaf certificates it signs can be valid:
cat > ca-subject.json <<'EOF'
{ "commonName": "Example Internal Root CA", "organization": "Example Ltd", "country": "GB" }
EOF
cat > ca-rules.json <<'EOF'
[ { "ruleType": "CERTIFICATE_AUTHORITY_ISSUANCE_EXPIRY_RULE",
"leafCertificateMaxValidityDuration": "P397D",
"certificateAuthorityMaxValidityDuration": "P3650D" } ]
EOF
CA_ID=$(oci certs-mgmt certificate-authority create-root-ca-by-generating-config-details \
--compartment-id "$C" \
--name internal-root-ca \
--subject file://ca-subject.json \
--kms-key-id "$KEY_ID" \
--signing-algorithm SHA256_WITH_RSA \
--certificate-authority-rules file://ca-rules.json \
--wait-for-state ACTIVE \
--query 'data.id' --raw-output)
If you need different flags for your CLI version, run oci certs-mgmt certificate-authority create-root-ca-by-generating-config-details --generate-param-json-input subject to get a subject template and --help for the current options.
Warning
The CA can only sign while its Vault key is enabled. Disabling or scheduling deletion of
internal-root-ca-keystops issuance and renewal for every certificate under it. Protect the key with a tight policy and a Security Zone or Cloud Guard detector.
Many teams create a subordinate CA (create-subordinate-ca-issued-by-internal-ca) under the root and issue leaf certificates only from the subordinate. If the subordinate is compromised, you revoke it without having to redistribute the root.
Step 3: Issue a server certificate with a renewal rule
cat > leaf-subject.json <<'EOF'
{ "commonName": "grafana.internal.example.com" }
EOF
cat > leaf-sans.json <<'EOF'
[ { "type": "DNS", "value": "grafana.internal.example.com" },
{ "type": "DNS", "value": "grafana" },
{ "type": "IP", "value": "10.0.2.15" } ]
EOF
cat > leaf-rules.json <<'EOF'
[ { "ruleType": "CERTIFICATE_RENEWAL_RULE",
"renewalInterval": "P90D",
"advanceRenewalPeriod": "P30D" } ]
EOF
CERT_ID=$(oci certs-mgmt certificate create-certificate-issued-by-internal-ca \
--compartment-id "$C" \
--issuer-certificate-authority-id "$CA_ID" \
--name grafana-internal \
--certificate-profile-type TLS_SERVER \
--key-algorithm RSA2048 \
--signature-algorithm SHA256_WITH_RSA \
--subject file://leaf-subject.json \
--subject-alternative-names file://leaf-sans.json \
--certificate-rules file://leaf-rules.json \
--wait-for-state ACTIVE \
--query 'data.id' --raw-output)
Durations use ISO 8601 days (P<n>D). Valid profile types are TLS_SERVER, TLS_CLIENT, TLS_SERVER_OR_CLIENT and TLS_CODE_SIGN. The service generates the private key and keeps it, so it never sits on an admin laptop.
Step 4: Deliver the certificate to a Linux server
Grant the instance read access
Put the servers in a dynamic group, for example grafana-hosts with instance.compartment.id = '<app compartment OCID>'. Then allow them to read only this kind of bundle, including the private key:
Allow dynamic-group grafana-hosts to read leaf-certificate-bundles in compartment security where target.leaf-certificate.bundle-type='CERTIFICATE_CONTENT_WITH_PRIVATE_KEY'
Allow dynamic-group grafana-hosts to read certificate-authority-bundles in compartment security
Caution
Any principal that can read a bundle with
CERTIFICATE_CONTENT_WITH_PRIVATE_KEYcan impersonate the service. Scope the dynamic group to the hosts that need it, and add atarget.leaf-certificate.idor tag condition if one compartment holds certificates for several apps.
Sync script
Save this as /usr/local/sbin/oci-cert-sync.sh. It writes files only when the version number changes, then reloads the service:
#!/usr/bin/env bash
set -euo pipefail
CERT_ID="ocid1.certificate.oc1.eu-frankfurt-1.xxxx"
CA_ID="ocid1.certificateauthority.oc1.eu-frankfurt-1.xxxx"
DIR=/etc/pki/oci/grafana
SERVICE=grafana-server
mkdir -p "$DIR"; chmod 700 "$DIR"
B=$(oci certificates certificate-bundle get --auth instance_principal \
--certificate-id "$CERT_ID" \
--bundle-type CERTIFICATE_CONTENT_WITH_PRIVATE_KEY --stage CURRENT)
NEW_VER=$(jq -r '.data."version-number"' <<<"$B")
OLD_VER=$(cat "$DIR/version" 2>/dev/null || echo 0)
[ "$NEW_VER" = "$OLD_VER" ] && exit 0
umask 077
jq -r '.data."certificate-pem"' <<<"$B" > "$DIR/tls.crt.new"
jq -r '.data."cert-chain-pem" // empty' <<<"$B" >> "$DIR/tls.crt.new"
jq -r '.data."private-key-pem"' <<<"$B" > "$DIR/tls.key.new"
oci certificates certificate-authority-bundle get --auth instance_principal \
--certificate-authority-id "$CA_ID" --query 'data."certificate-pem"' --raw-output > "$DIR/ca.crt"
# Refuse to install a key that doesn't match the certificate
[ "$(openssl x509 -noout -pubkey -in "$DIR/tls.crt.new" | sha256sum)" = \
"$(openssl pkey -pubout -in "$DIR/tls.key.new" | sha256sum)" ] || { echo "key mismatch"; exit 1; }
mv "$DIR/tls.crt.new" "$DIR/tls.crt"; mv "$DIR/tls.key.new" "$DIR/tls.key"
echo "$NEW_VER" > "$DIR/version"
chown root:grafana "$DIR"/tls.*; chmod 640 "$DIR"/tls.*
systemctl reload-or-restart "$SERVICE"
logger -t oci-cert-sync "installed certificate version $NEW_VER"
Run it daily with a systemd timer or cron (15 3 * * * root /usr/local/sbin/oci-cert-sync.sh). With a 30-day advance renewal period, a daily pull leaves plenty of time to fix a broken host.
Trust the root on clients
sudo cp /etc/pki/oci/grafana/ca.crt /etc/pki/ca-trust/source/anchors/example-internal-root.crt
sudo update-ca-trust
openssl s_client -connect grafana.internal.example.com:443 -servername grafana.internal.example.com </dev/null 2>/dev/null \
| openssl x509 -noout -subject -issuer -enddate
Java, Python virtualenvs (certifi) and some containers have their own trust stores. Add the root there as well.
Check renewal is working
# Versions and their stages (CURRENT, PENDING, PREVIOUS, DEPRECATED)
oci certs-mgmt certificate-version list --certificate-id "$CERT_ID" \
--query 'data.items[].{v:"version-number",stages:stages,notAfter:validity."time-of-validity-not-after"}' --output table
# Leaf certificates in the compartment and their state
oci certs-mgmt certificate list --compartment-id "$C" --all \
--query 'data.items[].{name:name,state:"lifecycle-state",profile:"certificate-profile-type"}' --output table
Field names in the output can differ between CLI versions. If a --query returns null, run the command without it once and adjust the path.
| Symptom | Cause | Fix |
|---|---|---|
| Key not listed when creating the CA | Software-protected or symmetric key | Create an asymmetric key with --protection-mode HSM |
| CA stuck in or failing at creation | Dynamic group or use keys policy missing | Add resource.type='certificateauthority' DG and policy |
Host gets NotAuthorizedOrNotFound on bundle get | Policy lacks the private-key condition or wrong compartment | Use the leaf-certificate-bundles ... bundle-type statement above |
| New version in OCI, old cert still served | Sync not running, or service not reloaded | Check timer logs (journalctl -t oci-cert-sync) |
| Clients report "unable to get local issuer certificate" | Root not in the client trust store | Install ca.crt with update-ca-trust or the app's store |
| Renewal never happened | No renewal rule, or CA key disabled | Add CERTIFICATE_RENEWAL_RULE; check key state in Vault |
Prevent it next time
- Put a renewal rule on every internally issued certificate, and keep leaf validity at 397 days or less with the CA issuance rule.
- Create an OCI Events rule for Certificates service events and route it to a Notifications topic your on-call team reads. Pick the event types from the Certificates list in the Events console, because the set changes over time.
- Keep the CA key in a dedicated security compartment. Only the CA dynamic group and a small admin group should have permissions on it.
- Use the version file and
loggerline from the sync script as a monitoring signal. Alert if a host's installed version is older than the CURRENT version in OCI. - Document the root CA's OCID and fingerprint in your runbook. If you ever rebuild the CA, every client trust store has to change.
Get the weekly HubDatAI email
One email a week with everything new: posts, learning sessions and videos. Free, no spam, unsubscribe in one click. What you get