# Manage Secrets in GCP Secret Manager

[GCP Secret Manager](https://cloud.google.com/security/products/secret-manager?hl=en) is a fully managed service for storing, managing, and accessing sensitive information such as API keys, passwords, and certificates. It allows you to store secrets centrally with encryption, automate versioning, and control access using Google Cloud’s IAM policies.

This guide will show you how to use GCP Secret Manager to manage user credentials for authentication plugin [`key-auth`](https://docs.api7.ai/hub/key-auth.md) and how to retrieve the secret in APISIX.

## Prerequisite(s)[​](#prerequisites "Direct link to Prerequisite(s)")

* Install [Docker](https://docs.docker.com/get-docker/).
* Install [cURL](https://curl.se/) to send requests to the services for validation.
* Follow the [Getting Started tutorial](https://docs.api7.ai/apisix/getting-started.md) to start a new APISIX instance in Docker.
* Have a [GCP account](https://cloud.google.com/?hl=en) and enable Secret Manager.
* If you use the Admin API examples and authentication is enabled, export an admin-role key as `ADMIN_API_KEY`. Configure the key under `deployment.admin.admin_key`. The Docker quickstart disables authentication for local evaluation; keep it enabled in production.

## Create a Secret in GCP Secret Manager[​](#create-a-secret-in-gcp-secret-manager "Direct link to Create a Secret in GCP Secret Manager")

In this section, you will be creating a secret to store the key-auth authentication key for consumer `john`.

Navigate to GCP Secret Manager in the console and create a secret. Fill in the name `apisix-john-key-auth` and the secret `john-key`:

![create a secret for john in GCP secret manager](https://static.api7.ai/uploads/2024/10/16/JNC60Z8O_gcp-step-1.png)

Review the rest of the information and finish secret creation. You should see the secret listed in GCP Secret Manager:

![see the secret listed in the GCP secret manager](https://static.api7.ai/uploads/2024/10/16/WFQQaug5_gcp-step-2.png)

## Obtain GCP Access Credentials[​](#obtain-gcp-access-credentials "Direct link to Obtain GCP Access Credentials")

Follow the [service account credentials](https://developers.google.com/workspace/guides/create-credentials#service-account) doc to create a service account in GCP, assign the account with the **Secret Manager Secret Accessor** role, and create credentials for the account.

You should see a JSON file containing the credentials generated and downloaded to your machine, similar to the following:

```
{

  "type": "service_account",

  "project_id": "apisix-project",

  "private_key_id": "f039bb20b2xxxxxxxxxb43cb7132axxxxxx1f165",

  "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",

  "client_email": "apisix-secret-manager@apisix-project.iam.gserviceaccount.com",

  "client_id": "115458xxxxxxx68702206",

  "auth_uri": "https://accounts.google.com/o/oauth2/auth",

  "token_uri": "https://oauth2.googleapis.com/token",

  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",

  "client_x509_cert_url": "https://www.googleapis.com/robot/v1/metadata/x509/apisix-secret-manager%40apisix-project.iam.gserviceaccount.com",

  "universe_domain": "googleapis.com"

}
```

## Configure a Private CA if Required[​](#configure-a-private-ca-if-required "Direct link to Configure a Private CA if Required")

APISIX uses the system trust store by default, so public Google Cloud Secret Manager endpoints require no trust-store change. Keep `ssl_verify` enabled.

If the gateway must trust a private CA, copy the active configuration and the Docker image's system CA bundle to the host. Keep a configuration backup:

```
docker cp apisix-quickstart:/usr/local/apisix/conf/config.yaml ./config.yaml

docker cp apisix-quickstart:/etc/ssl/certs/ca-certificates.crt \

  ./system-ca-bundle.pem

cp ./config.yaml ./config.yaml.backup
```

Append the private CA to the system roots so APISIX continues to trust public endpoints:

```
cat ./system-ca-bundle.pem ./private-ca.pem > ./combined-ca-bundle.pem
```

Add the combined bundle path under the existing `apisix.ssl` mapping. Merge this fragment into the complete configuration rather than replacing `config.yaml`:

```
apisix:

  ssl:

    ssl_trusted_certificate: /usr/local/apisix/conf/combined-ca-bundle.pem
```

Copy the CA bundle and candidate configuration into the container. Validate the complete configuration before reloading APISIX:

```
docker cp ./combined-ca-bundle.pem \

  apisix-quickstart:/usr/local/apisix/conf/combined-ca-bundle.pem

docker cp ./config.yaml apisix-quickstart:/usr/local/apisix/conf/config.yaml

docker exec apisix-quickstart apisix test && \

  docker exec apisix-quickstart apisix reload
```

If validation fails, restore the backup before making another change:

```
docker cp ./config.yaml.backup apisix-quickstart:/usr/local/apisix/conf/config.yaml
```

## Configure Secret in APISIX[​](#configure-secret-in-apisix "Direct link to Configure Secret in APISIX")

Configure GCP Secret Manager to be a secret provider for john with the access credentials obtained in the [last step](#obtain-gcp-access-credentials):

```
curl "http://127.0.0.1:9180/apisix/admin/secrets/gcp/john" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

  "auth_config": {

    "client_email": "apisix-secret-manager@apisix-project.iam.gserviceaccount.com",

    "private_key": "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----\n",

    "project_id": "apisix-project"

  },

  "ssl_verify": true

}'
```

❶ Replace with your client email.

❷ Replace with your private key.

❸ Replace with your project ID.

❹ Enable SSL verification (default).

## Create a Consumer and its Credential[​](#create-a-consumer-and-its-credential "Direct link to Create a Consumer and its Credential")

Create a consumer `john`:

```
curl "http://127.0.0.1:9180/apisix/admin/consumers" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

  "username": "john"

}'
```

Configure the `key-auth` credential for consumer `john` to fetch the key from secret provider:

```
curl "http://127.0.0.1:9180/apisix/admin/consumers/john/credentials" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

  "id": "cred-john-key-auth",

  "plugins": {

    "key-auth": {

      "key": "$secret://gcp/john/apisix-john-key-auth"

    }

  }

}'
```

## Create a Route with Authentication[​](#create-a-route-with-authentication "Direct link to Create a Route with Authentication")

Create a sample route and enable the `key-auth` plugin:

```
curl "http://127.0.0.1:9180/apisix/admin/routes" -X PUT \

  -H "X-API-KEY: ${ADMIN_API_KEY}" \

  -d '{

  "id": "key-auth-route",

  "uri": "/anything",

  "plugins": {

    "key-auth": {}

  },

  "upstream" : {

    "nodes": {

    "httpbin.org": 1

    }

  }

}'
```

## Verify[​](#verify "Direct link to Verify")

Send a request to the route with the valid credential:

```
curl -i "http://127.0.0.1:9080/anything" -H 'apikey: john-key'
```

You should receive an `HTTP/1.1 200 OK` response.

Send a request to the route with an invalid credential:

```
curl -i "http://127.0.0.1:9080/anything" -H 'apikey: wrong-key'
```

You should receive an `HTTP/1.1 401 Unauthorized` response.

## Next Steps[​](#next-steps "Direct link to Next Steps")

You have now learned how to configure APISIX to fetch secrets from GCP Secret Manager.

In addition to GCP Secret Manager, APISIX also supports the integration with [HashiCorp Vault](https://docs.api7.ai/apisix/how-to-guide/security/secrets-management/manage-secrets-in-hashicorp-vault.md) and [AWS Secrets Manager](https://docs.api7.ai/apisix/how-to-guide/security/secrets-management/manage-secrets-in-aws.md) for secret management.
