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 DataFlex Downloads
Use ChilkatAx-win32.pkg
Procedure Test
Boolean iSuccess
Handle hoStringCompare
Handle hoCredentials
Boolean iStringMatch1
Boolean iStringMatch2
String sRegion
Handle hoEndpoint
Variant vAws
Handle hoAws
Handle hoRest
Boolean iStringMatch3
Variant vConnection
Handle hoConnection
Handle hoBody
Handle hoResult
String sResponseJson
Handle hoSeenTokens
Variant vSecrets
Handle hoSecrets
Variant vSecretEntry
Handle hoSecretEntry
Variant vVersions
Handle hoVersions
Variant vStages
Handle hoStages
String sNextToken
Integer iPage
Integer i
Integer j
Integer k
Boolean iMore
Boolean iStringMatch4
Boolean iStringMatch5
Boolean iStringMatch6
String sVersionId
Boolean iStringMatch7
String sTemp1
Integer iTemp1
Boolean bTemp1
Move False To iSuccess
Get Create (RefClass(cComChilkatStringBuilder)) To hoStringCompare
If (Not(IsComObjectCreated(hoStringCompare))) Begin
Send CreateComObject of hoStringCompare
End
// Assumes Chilkat is unlocked. See Global Unlock Sample.
// Keep this credentials file outside public web content and source control.
Get Create (RefClass(cComChilkatJsonObject)) To hoCredentials
If (Not(IsComObjectCreated(hoCredentials))) Begin
Send CreateComObject of hoCredentials
End
Set ComStrict Of hoCredentials To True
Get ComLoadFile Of hoCredentials "qa_data/aws/secrets-manager.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private Secrets Manager configuration."
Procedure_Return
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch1
Get ComJsonTypeOf Of hoCredentials "access_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch1 = True)) Begin
Showln "Missing or invalid access_key."
Procedure_Return
End
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch2
Get ComJsonTypeOf Of hoCredentials "secret_key" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch2 = True)) Begin
Showln "Missing or invalid secret_key."
Procedure_Return
End
Move "us-west-2" To sRegion
Get Create (RefClass(cComChilkatStringBuilder)) To hoEndpoint
If (Not(IsComObjectCreated(hoEndpoint))) Begin
Send CreateComObject of hoEndpoint
End
Get ComAppend Of hoEndpoint "secretsmanager." To iSuccess
Get ComAppend Of hoEndpoint sRegion To iSuccess
Get ComAppend Of hoEndpoint ".amazonaws.com" To iSuccess
Get Create (RefClass(cComChilkatAuthAws)) To hoAws
If (Not(IsComObjectCreated(hoAws))) Begin
Send CreateComObject of hoAws
End
Get ComStringOf Of hoCredentials "access_key" To sTemp1
Set ComAccessKey Of hoAws To sTemp1
Get ComStringOf Of hoCredentials "secret_key" To sTemp1
Set ComSecretKey Of hoAws To sTemp1
Set ComRegion Of hoAws To sRegion
Set ComServiceName Of hoAws To "secretsmanager"
Set ComSignatureVersion Of hoAws To 4
Get Create (RefClass(cComChilkatRest)) To hoRest
If (Not(IsComObjectCreated(hoRest))) Begin
Send CreateComObject of hoRest
End
Set ComConnectTimeoutMs Of hoRest To 20000
Set ComIdleTimeoutMs Of hoRest To 60000
Set ComVerboseLogging Of hoRest To False
Get pvComObject of hoAws to vAws
Get ComSetAuthAws Of hoRest vAws To iSuccess
If (iSuccess <> True) Begin
Showln "Could not configure AWS signing."
Procedure_Return
End
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch3
Get ComJsonTypeOf Of hoCredentials "session_token" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch3 = True)) Begin
Showln "Missing or invalid session_token."
Procedure_Return
End
End
Get Create (RefClass(cComChilkatSocket)) To hoConnection
If (Not(IsComObjectCreated(hoConnection))) Begin
Send CreateComObject of hoConnection
End
Set ComRequireSslCertVerify Of hoConnection To True
Set ComMaxReadIdleMs Of hoConnection To 60000
Set ComMaxSendIdleMs Of hoConnection To 60000
Get ComGetAsString Of hoEndpoint To sTemp1
Get ComConnect Of hoConnection sTemp1 443 True 20000 To iSuccess
If (iSuccess <> True) Begin
Showln "TLS connection failed; inspect diagnostics privately."
Procedure_Return
End
Get pvComObject of hoConnection to vConnection
Get ComUseConnection Of hoRest vConnection True To iSuccess
If (iSuccess <> True) Begin
Showln "Could not attach TLS connection."
Procedure_Return
End
// Keep connection alive while Rest uses it.
Get Create (RefClass(cComChilkatJsonObject)) To hoBody
If (Not(IsComObjectCreated(hoBody))) Begin
Send CreateComObject of hoBody
End
Get Create (RefClass(cComChilkatJsonObject)) To hoResult
If (Not(IsComObjectCreated(hoResult))) Begin
Send CreateComObject of hoResult
End
Set ComStrict Of hoResult To True
Get Create (RefClass(cComChilkatHashtable)) To hoSeenTokens
If (Not(IsComObjectCreated(hoSeenTokens))) Begin
Send CreateComObject of hoSeenTokens
End
Get Create (RefClass(cComChilkatJsonArray)) To hoSecrets
If (Not(IsComObjectCreated(hoSecrets))) Begin
Send CreateComObject of hoSecrets
End
Get Create (RefClass(cComChilkatJsonObject)) To hoSecretEntry
If (Not(IsComObjectCreated(hoSecretEntry))) Begin
Send CreateComObject of hoSecretEntry
End
Get Create (RefClass(cComChilkatJsonObject)) To hoVersions
If (Not(IsComObjectCreated(hoVersions))) Begin
Send CreateComObject of hoVersions
End
Get Create (RefClass(cComChilkatJsonArray)) To hoStages
If (Not(IsComObjectCreated(hoStages))) Begin
Send CreateComObject of hoStages
End
Move "" To sNextToken
Move 0 To iPage
Move True To iMore
While (iMore = True)
If (iPage = 10000) Begin
Showln "Page bound reached; secret enumeration is incomplete."
Procedure_Return
End
Move (iPage + 1) To iPage
Send ComClear To hoBody
Get ComUpdateInt Of hoBody "MaxResults" 100 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set page size."
Procedure_Return
End
// 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.
Get ComSetString Of hoStringCompare sNextToken To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch4
If (iStringMatch4 <> True) Begin
Get ComUpdateString Of hoBody "NextToken" sNextToken To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set NextToken."
Procedure_Return
End
End
Get ComClearAllQueryParams Of hoRest To iSuccess
If (iSuccess <> True) Begin
Showln "Could not clear query parameters."
Procedure_Return
End
// Temporary role credentials require the session token as well as both keys.
// Include it in the signed query; keep request URLs and diagnostics private.
Get ComHasMember Of hoCredentials "session_token" To bTemp1
If (bTemp1 = True) Begin
Get ComStringOf Of hoCredentials "session_token" To sTemp1
Get ComAddQueryParam Of hoRest "X-Amz-Security-Token" sTemp1 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set session token."
Procedure_Return
End
End
Get ComAddHeader Of hoRest "Content-Type" "application/x-amz-json-1.1" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set content type."
Procedure_Return
End
Get ComAddHeader Of hoRest "X-Amz-Target" "secretsmanager.ListSecrets" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not select operation."
Procedure_Return
End
Get ComEmit Of hoBody To sTemp1
Get ComFullRequestString Of hoRest "POST" "/" sTemp1 To sResponseJson
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Showln "Secrets Manager request did not complete; inspect diagnostics privately. A write may have succeeded."
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "Secrets Manager HTTP status: " iTemp1
Get ComLoad Of hoResult sResponseJson To iSuccess
If (iSuccess = True) Begin
Get ComJsonTypeOf Of hoResult "__type" To iTemp1
If (iTemp1 = 1) Begin
Get ComStringOf Of hoResult "__type" To sTemp1
Showln "AWS error code: " sTemp1
End
End
Get ComResponseHdrByName Of hoRest "x-amzn-RequestId" To sTemp1
Showln "AWS request ID: " sTemp1
// Do not print response bodies, request bodies, credentials, or secret values.
Procedure_Return
End
Get ComLoad Of hoResult sResponseJson To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a well-formed JSON response."
Procedure_Return
End
Get ComJsonTypeOf Of hoResult "SecretList" To iTemp1
If (iTemp1 <> 4) Begin
Showln "Expected a SecretList array."
Procedure_Return
End
Get pvComObject of hoSecrets to vSecrets
Get ComArrayOf2 Of hoResult "SecretList" vSecrets To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read secret entries."
Procedure_Return
End
Move 0 To i
While (i < (ComSize(hoSecrets)))
Get pvComObject of hoSecretEntry to vSecretEntry
Get ComObjectAt2 Of hoSecrets i vSecretEntry To iSuccess
If (iSuccess <> True) Begin
Showln "Invalid secret entry."
Procedure_Return
End
Get ComStringOf Of hoSecretEntry "Name" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch5
Get ComJsonTypeOf Of hoSecretEntry "Name" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch5 = True)) Begin
Showln "Missing or invalid Name."
Procedure_Return
End
Get ComStringOf Of hoSecretEntry "ARN" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch6
Get ComJsonTypeOf Of hoSecretEntry "ARN" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch6 = True)) Begin
Showln "Missing or invalid ARN."
Procedure_Return
End
Get ComStringOf Of hoSecretEntry "Name" To sTemp1
Showln "Secret name: " sTemp1
Get ComStringOf Of hoSecretEntry "ARN" To sTemp1
Showln "Secret ARN: " sTemp1
// Metadata only: ListSecrets never returns the decrypted value.
// A secret without an initial value may have no version-to-stage mapping.
Get ComHasMember Of hoSecretEntry "SecretVersionsToStages" To bTemp1
If (bTemp1 = True) Begin
Get ComJsonTypeOf Of hoSecretEntry "SecretVersionsToStages" To iTemp1
If (iTemp1 <> 3) Begin
Showln "Expected a version-to-stage object."
Procedure_Return
End
Get pvComObject of hoVersions to vVersions
Get ComObjectOf2 Of hoSecretEntry "SecretVersionsToStages" vVersions To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read version mapping."
Procedure_Return
End
Move 0 To j
While (j < (ComSize(hoVersions)))
// Version IDs are opaque strings, including when used as JSON member names.
Get ComNameAt Of hoVersions j To sVersionId
Get pvComObject of hoStages to vStages
Get ComArrayAt2 Of hoVersions j vStages To iSuccess
If (iSuccess <> True) Begin
Showln "Expected a staging-label array."
Procedure_Return
End
Move 0 To k
While (k < (ComSize(hoStages)))
Get ComTypeAt Of hoStages k To iTemp1
If (iTemp1 <> 1) Begin
Showln "Expected a string staging label."
Procedure_Return
End
Get ComStringAt Of hoStages k To sTemp1
Showln " Version " sVersionId " label: " sTemp1
Move (k + 1) To k
Loop
Move (j + 1) To j
Loop
End
Move (i + 1) To i
Loop
// An empty page can still have NextToken. Stop only when there is no next token.
Get ComHasMember Of hoResult "NextToken" To iMore
If (iMore = True) Begin
Get ComStringOf Of hoResult "NextToken" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch7
Get ComJsonTypeOf Of hoResult "NextToken" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch7 = True)) Begin
Showln "Missing or invalid NextToken."
Procedure_Return
End
Get ComStringOf Of hoResult "NextToken" To sNextToken
Get ComContains Of hoSeenTokens sNextToken To bTemp1
If (bTemp1 = True) Begin
Showln "Repeated pagination token; secret enumeration is incomplete."
Procedure_Return
End
Get ComAddStr Of hoSeenTokens sNextToken "seen" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not track pagination."
Procedure_Return
End
End
Loop
Showln "Secret enumeration complete."
End_Procedure