Home / OCI

OCIChecklistIntermediate

Auditing OCI user credentials - a checklist for old API keys, auth tokens, customer secret keys and SMTP credentials

Inventory every API key, auth token, customer secret key and SMTP credential in your OCI tenancy, find the old and unused ones with Audit logs, and remove them without breaking automation.

Deepak Bhardwaj7 min read

Applies toOCI IAM with identity domains (Default domain), OCI Audit and Logging Search, OCI CLI 3.x, bash and jq

Goal

A security review asks: "Which long-lived credentials exist in OCI, how old are they, and are they still used?" The Console shows them one user at a time. This checklist builds a tenancy-wide inventory from the CLI, uses Audit to tell used keys from dead ones, and removes the dead ones safely.

Credential types covered:

Credential Used for Per-user limit
API signing key CLI, SDKs, Terraform Small fixed number; check the current limits
Auth token OCIR docker login, Streaming (Kafka), some third-party tools Small fixed number
Customer secret key Object Storage S3 Compatibility API Small fixed number
SMTP credential Email Delivery Small fixed number

Warning

Deleting a credential is immediate and can't be undone. A CI pipeline, a backup job or a Kafka client that still uses it fails at its next call. Disable or confirm non-use first, and delete in a change window.

1. Check your scope and access

  • You are working in the home region. IAM writes go there.
  • Your group can read users and their credentials. inspect users and read users in the tenancy cover the list calls. Deletes need manage users (or membership of Administrators).
  • You know which identity domains exist. The oci iam user ... commands below cover the Default domain. Users in other domains are managed through the domain endpoint with oci identity-domains ...; run oci identity-domains --help to see the commands in your CLI version.
# The tenancy OCID is in ~/.oci/config
TENANCY=ocid1.tenancy.oc1..aaaa...

oci iam domain list --compartment-id "$TENANCY" \
  --query 'data[].{name:"display-name",type:type,url:url}' --output table

2. Build the inventory

This script writes one CSV line per credential: user, type, ID, created, expiry and state.

#!/usr/bin/env bash
set -euo pipefail
OUT=credentials-$(date +%F).csv
echo "user,user_ocid,type,id,created,expires,state" > "$OUT"

oci iam user list --compartment-id "$TENANCY" --all \
  --query 'data[].[id,name]' --output json |
jq -r '.[] | @tsv' |
while IFS=$'\t' read -r USER_OCID UNAME; do
  oci iam user api-key list --user-id "$USER_OCID" --output json 2>/dev/null |
    jq -r --arg u "$UNAME" --arg id "$USER_OCID" \
      '.data[]? | [$u,$id,"api-key",.fingerprint,."time-created","",."lifecycle-state"] | @csv' >> "$OUT"

  oci iam auth-token list --user-id "$USER_OCID" --output json 2>/dev/null |
    jq -r --arg u "$UNAME" --arg id "$USER_OCID" \
      '.data[]? | [$u,$id,"auth-token",.id,."time-created",(."time-expires"//""),."lifecycle-state"] | @csv' >> "$OUT"

  oci iam customer-secret-key list --user-id "$USER_OCID" --output json 2>/dev/null |
    jq -r --arg u "$UNAME" --arg id "$USER_OCID" \
      '.data[]? | [$u,$id,"customer-secret-key",.id,."time-created",(."time-expires"//""),."lifecycle-state"] | @csv' >> "$OUT"

  oci iam smtp-credential list --user-id "$USER_OCID" --output json 2>/dev/null |
    jq -r --arg u "$UNAME" --arg id "$USER_OCID" \
      '.data[]? | [$u,$id,"smtp-credential",.id,."time-created",(."time-expires"//""),."lifecycle-state"] | @csv' >> "$OUT"
done

wc -l "$OUT"

Large tenancies make hundreds of calls. If you hit 429 TooManyRequests, add a short sleep per user or run it outside business hours.

Filter for anything older than 90 days:

CUTOFF=$(date -u -d '90 days ago' +%Y-%m-%dT%H:%M:%S)
awk -F',' -v c="\"$CUTOFF" 'NR>1 && $5 < c' "$OUT" | sort -t',' -k5
  • Every credential older than your rotation period is listed with its owner.
  • Credentials on users who have left, or on shared "service" users nobody owns, are flagged.
  • Users with credentials but no group membership are flagged. Their keys either do nothing or rely on a policy written for the user directly. Check with oci iam user list-groups --user-id <ocid>.

3. Find out whether each credential is still used

The legacy IAM API doesn't return a "last used" time. OCI Audit does record which credential signed a request: API-key calls carry the key ID in data.identity.credentials, built from the tenancy OCID, user OCID and fingerprint.

Search Audit across the tenancy for one key:

FP="aa:bb:cc:dd:ee:ff:00:11:22:33:44:55:66:77:88:99"

oci logging-search search-logs \
  --search-query "search \"$TENANCY/_Audit_Include_Subcompartment\" | where data.identity.credentials = '*$FP*' | select datetime, data.eventName, data.identity.ipAddress | sort by datetime desc" \
  --time-start "$(date -u -d '14 days ago' +%Y-%m-%dT%H:%M:%SZ)" \
  --time-end "$(date -u +%Y-%m-%dT%H:%M:%SZ)" \
  --limit 20

Search one window at a time; check the maximum query time range in your region's Logging Search limits. Audit retention is configurable at tenancy level, so you can only look back as far as it allows.

  • For every flagged API key: no Audit events in the retention window means it is a candidate for removal.
  • For a key with events: the source IP and event names tell you which system uses it. Write that down as the owner.
  • For auth tokens and customer secret keys, Audit matching is less direct. Ask the owning team, check OCIR and Streaming client configs, and look for S3-compatible endpoints (<namespace>.compat.objectstorage.<region>.oraclecloud.com) in application configs.
  • Field names inside Audit events vary by event version. Open one event in the Console first and confirm the path before you script on it.

4. Remove safely

For a user who should never use a credential type again, turn the capability off first. This is reversible, unlike deleting:

oci iam user update-user-capabilities --user-id "$USER_OCID" \
  --can-use-api-keys false \
  --can-use-customer-secret-keys false

Leave it a few days. If nothing breaks, delete the credentials themselves:

oci iam user api-key delete --user-id "$USER_OCID" --fingerprint "$FP" --force
oci iam auth-token delete --user-id "$USER_OCID" --auth-token-id "$TOKEN_ID" --force
oci iam customer-secret-key delete --user-id "$USER_OCID" --customer-secret-key-id "$KEY_ID" --force
oci iam smtp-credential delete --user-id "$USER_OCID" --smtp-credential-id "$SMTP_ID" --force

Caution

Turning capabilities off on a shared automation user affects every system that uses it. If the Audit search showed several source IPs, contact each owner before you change anything.

  • Every removal has a ticket that records owner, last-seen date and who approved it.
  • You re-ran the inventory script and the credential is gone.
  • Monitoring for the systems that used the user is green 24 hours later.

5. Replace what you can with no-key options

Current setup Better option
CLI or SDK on an OCI instance with a user API key Instance principal and a dynamic group
Functions or other OCI services calling OCI APIs Resource principal
OKE pods with a mounted ~/.oci/config OKE Workload Identity
Engineers' laptops with long-lived API keys oci session authenticate (session tokens)
Passwords in scripts OCI Vault secrets read at runtime

Each key you replace is one fewer to rotate and one fewer to leak.

Prevent it next time

  • Run the inventory script monthly from a scheduled job, and diff the CSV against last month's.
  • Set an expiry on auth tokens and customer secret keys where your domain allows it, so forgotten ones stop working.
  • Make one person or team the owner of each automation user, and put the owner in a freeform tag on the user.
  • Add an Events rule on credential create events (API key, auth token, customer secret key) that notifies the security channel.
  • Include credential removal in the offboarding checklist, and run this audit after every reorganisation.