AWS KMS: List All Keys with Pagination
See more AWS KMS Examples
Uses the current AWS KMS API. Configure the region and keep credentials in private qa_data/aws/kms.json, with nonempty string fields access_key and secret_key. The examples do not obtain or refresh credentials. Keep diagnostics private; they may contain credentials or sensitive key data. Create the output directory before running file-writing examples.
Uses the AWS JSON 1.1 protocol with TrentService targets, service kms, HTTPS certificate verification, and Signature Version 4. When using temporary credentials, also provide a nonempty session_token; this Rest example includes it in the signed query on each request. Never log that URL. The endpoint shown is for the commercial AWS partition; use the appropriate endpoint for other partitions. Failures stop the sample; production retry policies should use bounded backoff with jitter and reconcile uncertain writes.
Lists keys in the caller's account and selected region, following Truncated and NextMarker even when an intermediate page is empty. Limit is 1–1000 (default 100); this sample requests 1000. It detects repeated markers and stops at 10,000 pages with an incomplete result message. Results are not an atomic snapshot. ListKeys returns IDs and ARNs, not key status, aliases, or a KeyCount field. Use DescribeKey for metadata. Requires kms:ListKeys on Resource *.
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 hoSeenMarkers
Variant vKeys
Handle hoKeys
Variant vKeyEntry
Handle hoKeyEntry
String sMarker
Integer iPage
Integer iIndex
Boolean iMore
Boolean iStringMatch4
Boolean iStringMatch5
Boolean iStringMatch6
Boolean iStringMatch7
String sTemp1
Integer iTemp1
Integer iTemp2
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/kms.json" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not load private KMS 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 "kms." 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 "kms"
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 hoSeenMarkers
If (Not(IsComObjectCreated(hoSeenMarkers))) Begin
Send CreateComObject of hoSeenMarkers
End
Get Create (RefClass(cComChilkatJsonArray)) To hoKeys
If (Not(IsComObjectCreated(hoKeys))) Begin
Send CreateComObject of hoKeys
End
Get Create (RefClass(cComChilkatJsonObject)) To hoKeyEntry
If (Not(IsComObjectCreated(hoKeyEntry))) Begin
Send CreateComObject of hoKeyEntry
End
Move "" To sMarker
Move 0 To iPage
Move True To iMore
While (iMore = True)
If (iPage = 10000) Begin
Showln "Page bound reached; key enumeration is incomplete."
Procedure_Return
End
Move (iPage + 1) To iPage
Send ComClear To hoBody
Get ComUpdateInt Of hoBody "Limit" 1000 To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set page size."
Procedure_Return
End
Get ComSetString Of hoStringCompare sMarker To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch4
If (iStringMatch4 <> True) Begin
Get ComUpdateString Of hoBody "Marker" sMarker To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set Marker."
Procedure_Return
End
End
Get ComClearAllQueryParams Of hoRest To iSuccess
If (iSuccess <> True) Begin
Showln "Could not clear previous query parameters."
Procedure_Return
End
// Keep the JSON operation body separate from authentication parameters.
// Include STS credentials in the signed query; never log the request URL.
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 "X-Amz-Target" "TrentService.ListKeys" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not set operation."
Procedure_Return
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 ComEmit Of hoBody To sTemp1
Get ComFullRequestString Of hoRest "POST" "/" sTemp1 To sResponseJson
Get ComLastMethodSuccess Of hoRest To bTemp1
If (bTemp1 <> True) Begin
Showln "KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying."
Procedure_Return
End
Get ComResponseStatusCode Of hoRest To iTemp1
If (iTemp1 <> 200) Begin
Get ComResponseStatusCode Of hoRest To iTemp1
Showln "KMS 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 full bodies, headers, credentials, key material, or tokens.
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 "Keys" To iTemp1
Get ComJsonTypeOf Of hoResult "Truncated" To iTemp2
If ((iTemp1 <> 4) Or (iTemp2 <> 5)) Begin
Showln "Invalid ListKeys response."
Procedure_Return
End
Get pvComObject of hoKeys to vKeys
Get ComArrayOf2 Of hoResult "Keys" vKeys To iSuccess
If (iSuccess <> True) Begin
Showln "Could not read Keys array."
Procedure_Return
End
Move 0 To iIndex
While (iIndex < (ComSize(hoKeys)))
Get pvComObject of hoKeyEntry to vKeyEntry
Get ComObjectAt2 Of hoKeys iIndex vKeyEntry To iSuccess
If (iSuccess <> True) Begin
Showln "Invalid key entry."
Procedure_Return
End
Get ComStringOf Of hoKeyEntry "KeyId" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch5
Get ComJsonTypeOf Of hoKeyEntry "KeyId" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch5 = True)) Begin
Showln "Missing or invalid KeyId."
Procedure_Return
End
Get ComStringOf Of hoKeyEntry "KeyArn" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch6
Get ComJsonTypeOf Of hoKeyEntry "KeyArn" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch6 = True)) Begin
Showln "Missing or invalid KeyArn."
Procedure_Return
End
Get ComStringOf Of hoKeyEntry "KeyId" To sTemp1
Showln "Key ID: " sTemp1
Get ComStringOf Of hoKeyEntry "KeyArn" To sTemp1
Showln "Key ARN: " sTemp1
Move (iIndex + 1) To iIndex
Loop
Get ComBoolOf Of hoResult "Truncated" To iMore
If (iMore = True) Begin
Get ComStringOf Of hoResult "NextMarker" To sTemp1
Get ComSetString Of hoStringCompare sTemp1 To iSuccess
Get ComContentsEqual Of hoStringCompare "" True To iStringMatch7
Get ComJsonTypeOf Of hoResult "NextMarker" To iTemp1
If ((iTemp1 <> 1) Or (iStringMatch7 = True)) Begin
Showln "Missing or invalid NextMarker."
Procedure_Return
End
Get ComStringOf Of hoResult "NextMarker" To sMarker
Get ComContains Of hoSeenMarkers sMarker To bTemp1
If (bTemp1 = True) Begin
Showln "Repeated marker; key enumeration is incomplete."
Procedure_Return
End
Get ComAddStr Of hoSeenMarkers sMarker "seen" To iSuccess
If (iSuccess <> True) Begin
Showln "Could not track pagination."
Procedure_Return
End
End
Loop
Showln "Key enumeration complete."
End_Procedure