

# Using the Secrets Manager extension in AWS Systems Manager Cloud Extensions

Call the Secrets Manager extension on localhost to read secrets from its cache, by query or by path. The base URL of the extension is `http://localhost:5333/secretsmanager`. You can call the extension in two ways, and both use the same cache.
+ **Query** – `GET /secretsmanager/get?secretId=ID`, with the optional parameters `versionId`, `versionStage`, `refreshNow`, and `roleArn`.
+ **Path** – `GET /secretsmanager/ID`, with the same optional parameters. The extension keeps slashes in the secret name and decodes `%2F`, so `/secretsmanager/prod/db` and `/secretsmanager/prod%2Fdb` both request `prod/db`. A secret named `get` must use the query route.

By default, the extension returns the `AWSCURRENT` version. To get a different version, use `versionStage` or `versionId`.

While the server-side request forgery (SSRF) token check is on, include the SSRF token on each query or path request. Put the token in one of the SSRF token headers, such as `X-Aws-Parameters-Secrets-Token`. The check is on by default. The extension refuses requests that don't have a token header or that have a token that isn't valid. The extension always refuses query and path requests that have an `X-Forwarded-For` header. You can change the header names with the **SSRF token headers** setting.

**Topics**
+ [Using the AWS SDK for Secrets Manager](#cloud-extensions-ext-secrets-manager-using-using-the-aws-sdk-for-secrets-manager)
+ [Example requests](#cloud-extensions-ext-secrets-manager-using-example-requests)

## Using the AWS SDK for Secrets Manager
<a name="cloud-extensions-ext-secrets-manager-using-using-the-aws-sdk-for-secrets-manager"></a>

You can also read secrets with the AWS SDK for Secrets Manager. Set the endpoint of the Secrets Manager client to `http://localhost:5333/secretsmanager`, and call `GetSecretValue` as you do for Secrets Manager. The extension serves the call on its `serviceSdk` interface, from its cache. The interface has no server-side request forgery (SSRF) token check.

The SDK signs each request with the credentials of the client. With the default authentication, runtime credentials, use the credentials of the Lambda function or the container. For more information, see [Authentication](cloud-extensions-security.md#cloud-extensions-security-authentication).

The following Python example reads a secret:

```python
import boto3
secretsmanager = boto3.client(
    "secretsmanager",
    endpoint_url="http://localhost:5333/secretsmanager",
)
secret = secretsmanager.get_secret_value(SecretId="YOUR_SECRET_ID")
print(secret["Name"], secret["VersionStages"])
```

The interface serves only `GetSecretValue`, with `SecretId`, `VersionId`, and `VersionStage`. It doesn't support `refreshNow` or `roleArn`. To use those parameters, use the query route or the path route. For other Secrets Manager operations, use a client with the standard endpoint.

## Example requests
<a name="cloud-extensions-ext-secrets-manager-using-example-requests"></a>

The following examples sign the request with the access key ID and the secret key of the profile. This applies when the profile uses an extension key. When the profile uses runtime credentials, sign with the credentials of the runtime instead. The examples also send the SSRF token. Replace `YOUR_SSRF_TOKEN` with the value of the **SSRF token** setting. The runtime verifies the signature with the AWS Region and the service in the credential scope of the request. The request time must be within 5 seconds of the clock of the runtime. If the SSRF token check is off, omit the token header.

To get a secret with curl, run the following command:

```bash
curl -v \
  --aws-sigv4 "aws:amz:us-west-2:secretsmanager" \
  --user "ACCESS_KEY_ID:SECRET_KEY" \
  -H "X-Aws-Parameters-Secrets-Token: YOUR_SSRF_TOKEN" \
  'http://localhost:5333/secretsmanager/get?secretId=YOUR_SECRET_ID'
```

To get a secret with Python, use the following code:

```python
import requests
from botocore.auth import SigV4Auth
from botocore.awsrequest import AWSRequest
from botocore.credentials import Credentials
credentials = Credentials("ACCESS_KEY_ID", "SECRET_KEY")
def get_secret():
    url = "http://localhost:5333/secretsmanager/get?secretId=YOUR_SECRET_ID"
    token = "YOUR_SSRF_TOKEN"
    request = AWSRequest(method="GET", url=url, headers={"X-Aws-Parameters-Secrets-Token": token})
    SigV4Auth(credentials, "secretsmanager", "us-west-2").add_auth(request)
    response = requests.get(url, headers=dict(request.headers))
    if response.status_code != 200:
        raise Exception(f"Status code {response.status_code} - {response.text}")
    return response.text
```

The response has the same shape as the response of the AWS Secrets Manager Agent. It has the following members:
+ `ARN`
+ `Name`
+ `VersionId`
+ `SecretString` or `SecretBinary`. The extension returns `SecretBinary` as base64.
+ `VersionStages`
+ `CreatedDate`, as a string of epoch seconds. For example, the value is `"1569534789.046"`.