AWS Secrets Manager: List Secrets with Pagination
See more AWS Secrets Manager Examples
Lists secret metadata in the configured account and region, following every NextToken. Requests up to 100 entries per page, continues through empty pages that have a continuation token, and stops with an incomplete-result message on repeated tokens or a defensive page bound. Uses Hashtable to track opaque continuation strings. Processes every staging label attached to each returned version, not just the first label.
Requires secretsmanager:ListSecrets. Results contain metadata, not decrypted secret values; this action does not require permission to read each secret value. Metadata can still be confidential. Secrets scheduled for deletion are excluded by default; IncludePlannedDeletion=true includes them. The commented name-prefix filter illustrates a case-sensitive prefix search. ListSecrets is eventually consistent and may lag recent changes by five minutes; use DescribeSecret for more recent metadata on a known secret.
The version-to-stage mapping is useful for inventory, but ListSecretVersionIds is the dedicated version-history operation, including deprecated versions when requested. Use GetSecretValue or BatchGetSecretValue when the application needs the secret contents. An inventory spanning multiple requests is not an atomic snapshot if secrets change while it runs.
Uses direct Chilkat.Rest requests with AWS Signature Version 4, signing service secretsmanager, and the AWS JSON 1.1 protocol. Configure the region in the code. The illustrated amazonaws.com endpoint is for the commercial AWS partition; use the appropriate endpoint for other partitions. HTTPS certificate verification is enabled.
Supply nonempty string fields access_key and secret_key in private qa_data/aws/secrets-manager.json. For temporary role or STS credentials, also supply the matching nonempty session_token; it is included in the signed query. Prefer short-lived role credentials in production. These samples consume credentials already obtained by the application; they do not implement an SDK credential-provider chain or refresh expired credentials. Protect local credential files and keep them outside source control and public web directories.
The sample distinguishes transport failures, HTTP errors, and invalid JSON. It prints status, AWS error code, and request ID, without printing secret values or full diagnostic bodies. Request URLs and Chilkat diagnostics can contain sensitive data; inspect them privately. Production retry policies should use bounded exponential backoff with jitter for transient failures. Reconcile uncertain writes and preserve their idempotency token and payload before retrying.
Chilkat Unicode C++ Downloads
#include <CkStringBuilderW.h>
#include <CkJsonObjectW.h>
#include <CkAuthAwsW.h>
#include <CkRestW.h>
#include <CkSocketW.h>
#include <CkHashtableW.h>
#include <CkJsonArrayW.h>
void ChilkatSample(void)
{
bool success = false;
CkStringBuilderW stringCompare;
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this credentials file outside public web content and source control.
CkJsonObjectW credentials;
credentials.put_Strict(true);
success = credentials.LoadFile(L"qa_data/aws/secrets-manager.json");
if (success != true) {
wprintf(L"Could not load private Secrets Manager configuration.\n");
return;
}
stringCompare.SetString(credentials.stringOf(L"access_key"));
bool stringMatch1 = stringCompare.ContentsEqual(L"",true);
if ((credentials.JsonTypeOf(L"access_key") != 1) || (stringMatch1 == true)) {
wprintf(L"Missing or invalid access_key.\n");
return;
}
stringCompare.SetString(credentials.stringOf(L"secret_key"));
bool stringMatch2 = stringCompare.ContentsEqual(L"",true);
if ((credentials.JsonTypeOf(L"secret_key") != 1) || (stringMatch2 == true)) {
wprintf(L"Missing or invalid secret_key.\n");
return;
}
const wchar_t *region = L"us-west-2";
CkStringBuilderW endpoint;
endpoint.Append(L"secretsmanager.");
endpoint.Append(region);
endpoint.Append(L".amazonaws.com");
CkAuthAwsW aws;
aws.put_AccessKey(credentials.stringOf(L"access_key"));
aws.put_SecretKey(credentials.stringOf(L"secret_key"));
aws.put_Region(region);
aws.put_ServiceName(L"secretsmanager");
aws.put_SignatureVersion(4);
CkRestW rest;
rest.put_ConnectTimeoutMs(20000);
rest.put_IdleTimeoutMs(60000);
rest.put_VerboseLogging(false);
success = rest.SetAuthAws(aws);
if (success != true) {
wprintf(L"Could not configure AWS signing.\n");
return;
}
if (credentials.HasMember(L"session_token") == true) {
stringCompare.SetString(credentials.stringOf(L"session_token"));
bool stringMatch3 = stringCompare.ContentsEqual(L"",true);
if ((credentials.JsonTypeOf(L"session_token") != 1) || (stringMatch3 == true)) {
wprintf(L"Missing or invalid session_token.\n");
return;
}
}
CkSocketW connection;
connection.put_RequireSslCertVerify(true);
connection.put_MaxReadIdleMs(60000);
connection.put_MaxSendIdleMs(60000);
success = connection.Connect(endpoint.getAsString(),443,true,20000);
if (success != true) {
wprintf(L"TLS connection failed; inspect diagnostics privately.\n");
return;
}
success = rest.UseConnection(connection,true);
if (success != true) {
wprintf(L"Could not attach TLS connection.\n");
return;
}
// Keep connection alive while Rest uses it.
CkJsonObjectW body;
CkJsonObjectW result;
result.put_Strict(true);
const wchar_t *responseJson = 0;
CkHashtableW seenTokens;
CkJsonArrayW secrets;
CkJsonObjectW secretEntry;
CkJsonObjectW versions;
CkJsonArrayW stages;
const wchar_t *nextToken = L"";
int page = 0;
int i;
int j;
int k;
bool more = true;
while ((more == true)) {
if (page == 10000) {
wprintf(L"Page bound reached; secret enumeration is incomplete.\n");
return;
}
page = page + 1;
body.Clear();
success = body.UpdateInt(L"MaxResults",100);
if (success != true) {
wprintf(L"Could not set page size.\n");
return;
}
// Optional filter: name prefixes are case-sensitive.
// body.UpdateString("Filters[0].Key","name");
// body.UpdateString("Filters[0].Values[0]","example/");
// IncludePlannedDeletion defaults to false. Set true to inventory scheduled deletions.
stringCompare.SetString(nextToken);
bool stringMatch4 = stringCompare.ContentsEqual(L"",true);
if (stringMatch4 != true) {
success = body.UpdateString(L"NextToken",nextToken);
if (success != true) {
wprintf(L"Could not set NextToken.\n");
return;
}
}
success = rest.ClearAllQueryParams();
if (success != true) {
wprintf(L"Could not clear query parameters.\n");
return;
}
// Temporary role credentials require the session token as well as both keys.
// Include it in the signed query; keep request URLs and diagnostics private.
if (credentials.HasMember(L"session_token") == true) {
success = rest.AddQueryParam(L"X-Amz-Security-Token",credentials.stringOf(L"session_token"));
if (success != true) {
wprintf(L"Could not set session token.\n");
return;
}
}
success = rest.AddHeader(L"Content-Type",L"application/x-amz-json-1.1");
if (success != true) {
wprintf(L"Could not set content type.\n");
return;
}
success = rest.AddHeader(L"X-Amz-Target",L"secretsmanager.ListSecrets");
if (success != true) {
wprintf(L"Could not select operation.\n");
return;
}
responseJson = rest.fullRequestString(L"POST",L"/",body.emit());
if (rest.get_LastMethodSuccess() != true) {
wprintf(L"Secrets Manager request did not complete; inspect diagnostics privately. A write may have succeeded.\n");
return;
}
if (rest.get_ResponseStatusCode() != 200) {
wprintf(L"Secrets Manager HTTP status: %d\n",rest.get_ResponseStatusCode());
success = result.Load(responseJson);
if (success == true) {
if (result.JsonTypeOf(L"__type") == 1) {
wprintf(L"AWS error code: %s\n",result.stringOf(L"__type"));
}
}
wprintf(L"AWS request ID: %s\n",rest.responseHdrByName(L"x-amzn-RequestId"));
// Do not print response bodies, request bodies, credentials, or secret values.
return;
}
success = result.Load(responseJson);
if (success != true) {
wprintf(L"Expected a well-formed JSON response.\n");
return;
}
if (result.JsonTypeOf(L"SecretList") != 4) {
wprintf(L"Expected a SecretList array.\n");
return;
}
success = result.ArrayOf2(L"SecretList",secrets);
if (success != true) {
wprintf(L"Could not read secret entries.\n");
return;
}
i = 0;
while ((i < secrets.get_Size())) {
success = secrets.ObjectAt2(i,secretEntry);
if (success != true) {
wprintf(L"Invalid secret entry.\n");
return;
}
stringCompare.SetString(secretEntry.stringOf(L"Name"));
bool stringMatch5 = stringCompare.ContentsEqual(L"",true);
if ((secretEntry.JsonTypeOf(L"Name") != 1) || (stringMatch5 == true)) {
wprintf(L"Missing or invalid Name.\n");
return;
}
stringCompare.SetString(secretEntry.stringOf(L"ARN"));
bool stringMatch6 = stringCompare.ContentsEqual(L"",true);
if ((secretEntry.JsonTypeOf(L"ARN") != 1) || (stringMatch6 == true)) {
wprintf(L"Missing or invalid ARN.\n");
return;
}
wprintf(L"Secret name: %s\n",secretEntry.stringOf(L"Name"));
wprintf(L"Secret ARN: %s\n",secretEntry.stringOf(L"ARN"));
// Metadata only: ListSecrets never returns the decrypted value.
// A secret without an initial value may have no version-to-stage mapping.
if (secretEntry.HasMember(L"SecretVersionsToStages") == true) {
if (secretEntry.JsonTypeOf(L"SecretVersionsToStages") != 3) {
wprintf(L"Expected a version-to-stage object.\n");
return;
}
success = secretEntry.ObjectOf2(L"SecretVersionsToStages",versions);
if (success != true) {
wprintf(L"Could not read version mapping.\n");
return;
}
j = 0;
while ((j < versions.get_Size())) {
// Version IDs are opaque strings, including when used as JSON member names.
const wchar_t *versionId = versions.nameAt(j);
success = versions.ArrayAt2(j,stages);
if (success != true) {
wprintf(L"Expected a staging-label array.\n");
return;
}
k = 0;
while ((k < stages.get_Size())) {
if (stages.TypeAt(k) != 1) {
wprintf(L"Expected a string staging label.\n");
return;
}
wprintf(L" Version %s label: %s\n",versionId,stages.stringAt(k));
k = k + 1;
}
j = j + 1;
}
}
i = i + 1;
}
// An empty page can still have NextToken. Stop only when there is no next token.
more = result.HasMember(L"NextToken");
if (more == true) {
stringCompare.SetString(result.stringOf(L"NextToken"));
bool stringMatch7 = stringCompare.ContentsEqual(L"",true);
if ((result.JsonTypeOf(L"NextToken") != 1) || (stringMatch7 == true)) {
wprintf(L"Missing or invalid NextToken.\n");
return;
}
nextToken = result.stringOf(L"NextToken");
if (seenTokens.Contains(nextToken) == true) {
wprintf(L"Repeated pagination token; secret enumeration is incomplete.\n");
return;
}
success = seenTokens.AddStr(nextToken,L"seen");
if (success != true) {
wprintf(L"Could not track pagination.\n");
return;
}
}
}
wprintf(L"Secret enumeration complete.\n");
}