Skip to main content
Version: 3.9.x

Secure Credentials Management

Protecting sensitive information like SSL certificates, API keys, and passwords is a critical security and compliance requirement. API7 Gateway provides robust tools for managing these credentials securely, both within the gateway and through integration with external secret management systems.

Prerequisites​

Capabilities​

  • Secure SSL Management: Centralized storage and automated renewal for SSL certificates.
  • Credential Encryption at Rest: Sensitive values are encrypted before being stored in the gateway's configuration database.
  • Secret Manager Integration: Integrate with HashiCorp Vault, AWS Secrets Manager, and Kubernetes Secrets for centralized secret control.
  • Environment Variable Support: Inject secrets as environment variables during deployment.

SSL Certificate Management​

API7 Gateway allows you to store and manage SSL certificates through the Control Plane. You can associate certificates with specific domains (SNIs) to enable HTTPS on your routes.

Prepare PEM Certificates and Keys​

Use a PEM-encoded server certificate and its matching private key without a passphrase. PEM files contain blocks such as -----BEGIN CERTIFICATE----- and -----BEGIN PRIVATE KEY-----. Certificate downloads labeled for NGINX or Apache often contain PEM files; inspect their contents instead of relying on the filename. PFX/PKCS#12 and JKS files must be converted before upload. Renaming their extensions does not convert them.

The certificate value must start with the server certificate, followed by its issuing intermediate certificates in order. Do not include unrelated certificates or the root CA. The private key must correspond to the first certificate.

Convert PFX or PKCS#12​

Use OpenSSL 3 or later to extract the server certificate, CA certificates, and private key. Run the commands in a private working directory; they prompt for the PFX password and a temporary key passphrase:

umask 077
openssl pkcs12 -in certificate.pfx -clcerts -nokeys -out leaf.pem
openssl pkcs12 -in certificate.pfx -cacerts -nokeys -out ca-chain.pem
openssl pkcs12 -in certificate.pfx -nocerts -out encrypted-key.pem
openssl x509 -in leaf.pem -out certificate.pem
openssl pkey -in encrypted-key.pem -out private-key.pem

Keep only the issuing intermediate certificate blocks from ca-chain.pem, ordered from the server's issuer toward the root, in intermediate-ca.pem. Combine them with the server certificate:

cat certificate.pem intermediate-ca.pem > certificate-chain.pem

If the server certificate is signed directly by a trusted root CA, use certificate.pem alone as certificate-chain.pem. If the PFX does not contain the intermediate certificates, obtain them from the certificate issuer. For older PFX encryption that OpenSSL cannot read, see the openssl pkcs12 legacy option.

private-key.pem has no passphrase so the gateway can load it without a password prompt. Restrict access to it, and remove temporary key files after importing and verifying the certificate.

Convert a JKS Keystore​

Use keytool from the Java Development Kit to find the alias of the server's PrivateKeyEntry, then export only that entry to PKCS#12:

keytool -list -keystore certificate.jks
keytool -importkeystore \
-srckeystore certificate.jks \
-srcstoretype JKS \
-srcalias YOUR_SERVER_KEY_ALIAS \
-destkeystore certificate.pfx \
-deststoretype PKCS12

Enter the source keystore, key, and destination keystore passwords when prompted. Then follow the PFX conversion above. A trusted-certificate entry contains no private key and cannot be used as the server's key pair. See the keytool reference for keystore and alias options.

Verify before Upload​

Inspect the server certificate's names and validity, then compare its public key with the private key's public key:

openssl x509 -in certificate-chain.pem -noout -subject -issuer -dates -ext subjectAltName
openssl x509 -in certificate-chain.pem -pubkey -noout | openssl pkey -pubin -outform DER | openssl dgst -sha256
openssl pkey -in private-key.pem -pubout -outform DER | openssl dgst -sha256

The two SHA-256 values must match. Configure an SNI covered by the certificate, then use the certificate chain and private key in the import example below. After import, send an HTTPS request with that hostname to verify the certificate chain and routing.

Import the Certificate​

Use jq to encode the PEM files as JSON strings, preserving their line breaks. Replace the SNI and gateway group ID with your deployment's values:

jq -n \
--arg sni 'api.example.com' \
--rawfile cert certificate-chain.pem \
--rawfile key private-key.pem \
'{snis: [$sni], cert: $cert, key: $key}' > ssl.json

curl -k "https://localhost:7443/apisix/admin/ssls/example-com-ssl?gateway_group_id={gateway_group_id}" -X PUT \
-H "X-API-KEY: ${API_KEY}" \
-H "Content-Type: application/json" \
--data-binary @ssl.json

The generated ssl.json contains the private key. Keep it in the private working directory used above and remove it after import. In production, verify the control plane's certificate instead of using -k.

Replace {gateway_group_id} with the gateway group ID from the Gateway Groups page in the Dashboard (use default for the gateway group created by the quickstart). Replace $CERTIFICATE_CONTENT and $PRIVATE_KEY_CONTENT with the PEM-encoded certificate and private key (newlines escaped as \n).

External Secret Managers​

For enterprise-grade secret management, use an external secret manager. API7 Gateway can fetch sensitive values directly from these systems at runtime.

HashiCorp Vault Integration​

API7 Gateway can fetch sensitive values from HashiCorp Vault at runtime. The end-to-end flow is:

  1. Create a Vault Secret Provider in your gateway group via the Admin API. See Configure a HashiCorp Vault Secret Provider for the request shape, required fields (uri, prefix, token, optional kv_version and authentication_method), and a walkthrough for standing up a Vault instance and scoping a token.
  2. Reference secrets in your resource or plugin configuration using the $secret://{provider_type}/{provider_id}/{secret_key} URI scheme.
# In your plugin configuration
plugins:
key-auth:
key: "$secret://vault/my-vault/api-key-path/my-key"

Credential Encryption at Rest​

The API7 Control Plane encrypts selected sensitive fields, such as plugin passwords and keys, before storing them in the configuration database. This reduces plaintext exposure during normal database inspection, but does not protect credentials if the database or a backup is compromised. Encrypt and strictly control access to database backups. If sensitive values and their encryption keys must remain outside the Control Plane, store those values in an external secret manager and reference them from API7 Gateway.

Next Steps​