Sample code for 30+ languages & platforms
Delphi DLL Requires Chilkat v11.6.0+

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 *.

AWS ListKeys reference

Chilkat Delphi DLL Downloads

Delphi DLL
var
success: Boolean;
stringCompare: HCkStringBuilder;
credentials: HCkJsonObject;
stringMatch1: Boolean;
stringMatch2: Boolean;
region: PWideChar;
endpoint: HCkStringBuilder;
aws: HCkAuthAws;
rest: HCkRest;
stringMatch3: Boolean;
connection: HCkSocket;
body: HCkJsonObject;
result: HCkJsonObject;
responseJson: PWideChar;
seenMarkers: HCkHashtable;
keys: HCkJsonArray;
keyEntry: HCkJsonObject;
marker: PWideChar;
page: Integer;
index: Integer;
more: Boolean;
stringMatch4: Boolean;
stringMatch5: Boolean;
stringMatch6: Boolean;
stringMatch7: Boolean;

begin
success := False;

stringCompare := CkStringBuilder_Create();
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
//  Keep this credentials file outside public web content and source control.
credentials := CkJsonObject_Create();
CkJsonObject_putStrict(credentials,True);
success := CkJsonObject_LoadFile(credentials,'qa_data/aws/kms.json');
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not load private KMS configuration.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'access_key'));
stringMatch1 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'access_key') <> 1) or (stringMatch1 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid access_key.');
    Exit;
  end;
CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'secret_key'));
stringMatch2 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
if ((CkJsonObject_JsonTypeOf(credentials,'secret_key') <> 1) or (stringMatch2 = True)) then
  begin
    Memo1.Lines.Add('Missing or invalid secret_key.');
    Exit;
  end;
region := 'us-west-2';
endpoint := CkStringBuilder_Create();
CkStringBuilder_Append(endpoint,'kms.');
CkStringBuilder_Append(endpoint,region);
CkStringBuilder_Append(endpoint,'.amazonaws.com');
aws := CkAuthAws_Create();
CkAuthAws_putAccessKey(aws,CkJsonObject__stringOf(credentials,'access_key'));
CkAuthAws_putSecretKey(aws,CkJsonObject__stringOf(credentials,'secret_key'));
CkAuthAws_putRegion(aws,region);
CkAuthAws_putServiceName(aws,'kms');
CkAuthAws_putSignatureVersion(aws,4);
rest := CkRest_Create();
CkRest_putConnectTimeoutMs(rest,20000);
CkRest_putIdleTimeoutMs(rest,60000);
CkRest_putVerboseLogging(rest,False);
success := CkRest_SetAuthAws(rest,aws);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not configure AWS signing.');
    Exit;
  end;
if (CkJsonObject_HasMember(credentials,'session_token') = True) then
  begin
    CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(credentials,'session_token'));
    stringMatch3 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
    if ((CkJsonObject_JsonTypeOf(credentials,'session_token') <> 1) or (stringMatch3 = True)) then
      begin
        Memo1.Lines.Add('Missing or invalid session_token.');
        Exit;
      end;
  end;
connection := CkSocket_Create();
CkSocket_putRequireSslCertVerify(connection,True);
CkSocket_putMaxReadIdleMs(connection,60000);
CkSocket_putMaxSendIdleMs(connection,60000);
success := CkSocket_Connect(connection,CkStringBuilder__getAsString(endpoint),443,True,20000);
if (success <> True) then
  begin
    Memo1.Lines.Add('TLS connection failed; inspect diagnostics privately.');
    Exit;
  end;
success := CkRest_UseConnection(rest,connection,True);
if (success <> True) then
  begin
    Memo1.Lines.Add('Could not attach TLS connection.');
    Exit;
  end;
//  Keep connection alive while Rest uses it.
body := CkJsonObject_Create();
result := CkJsonObject_Create();
CkJsonObject_putStrict(result,True);

seenMarkers := CkHashtable_Create();
keys := CkJsonArray_Create();
keyEntry := CkJsonObject_Create();
marker := '';
page := 0;

more := True;
while (more = True) do
  begin
    if (page = 10000) then
      begin
        Memo1.Lines.Add('Page bound reached; key enumeration is incomplete.');
        Exit;
      end;
    page := page + 1;
    CkJsonObject_Clear(body);
    success := CkJsonObject_UpdateInt(body,'Limit',1000);
    if (success <> True) then
      begin
        Memo1.Lines.Add('Could not set page size.');
        Exit;
      end;
    CkStringBuilder_SetString(stringCompare,marker);
    stringMatch4 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
    if (stringMatch4 <> True) then
      begin
        success := CkJsonObject_UpdateString(body,'Marker',marker);
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not set Marker.');
            Exit;
          end;
      end;
    success := CkRest_ClearAllQueryParams(rest);
    if (success <> True) then
      begin
        Memo1.Lines.Add('Could not clear previous query parameters.');
        Exit;
      end;
    //  Keep the JSON operation body separate from authentication parameters.
    //  Include STS credentials in the signed query; never log the request URL.
    if (CkJsonObject_HasMember(credentials,'session_token') = True) then
      begin
        success := CkRest_AddQueryParam(rest,'X-Amz-Security-Token',CkJsonObject__stringOf(credentials,'session_token'));
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not set session token.');
            Exit;
          end;
      end;
    success := CkRest_AddHeader(rest,'X-Amz-Target','TrentService.ListKeys');
    if (success <> True) then
      begin
        Memo1.Lines.Add('Could not set operation.');
        Exit;
      end;
    success := CkRest_AddHeader(rest,'Content-Type','application/x-amz-json-1.1');
    if (success <> True) then
      begin
        Memo1.Lines.Add('Could not set content type.');
        Exit;
      end;
    responseJson := CkRest__fullRequestString(rest,'POST','/',CkJsonObject__emit(body));
    if (CkRest_getLastMethodSuccess(rest) <> True) then
      begin
        Memo1.Lines.Add('KMS request did not complete; inspect diagnostics privately. Reconcile any write before retrying.');
        Exit;
      end;
    if (CkRest_getResponseStatusCode(rest) <> 200) then
      begin
        Memo1.Lines.Add('KMS HTTP status: ' + IntToStr(CkRest_getResponseStatusCode(rest)));
        success := CkJsonObject_Load(result,responseJson);
        if (success = True) then
          begin
            if (CkJsonObject_JsonTypeOf(result,'__type') = 1) then
              begin
                Memo1.Lines.Add('AWS error code: ' + CkJsonObject__stringOf(result,'__type'));
              end;
          end;
        Memo1.Lines.Add('AWS request ID: ' + CkRest__responseHdrByName(rest,'x-amzn-RequestId'));
        //  Do not print full bodies, headers, credentials, key material, or tokens.
        Exit;
      end;
    success := CkJsonObject_Load(result,responseJson);
    if (success <> True) then
      begin
        Memo1.Lines.Add('Expected a well-formed JSON response.');
        Exit;
      end;
    if ((CkJsonObject_JsonTypeOf(result,'Keys') <> 4) or (CkJsonObject_JsonTypeOf(result,'Truncated') <> 5)) then
      begin
        Memo1.Lines.Add('Invalid ListKeys response.');
        Exit;
      end;
    success := CkJsonObject_ArrayOf2(result,'Keys',keys);
    if (success <> True) then
      begin
        Memo1.Lines.Add('Could not read Keys array.');
        Exit;
      end;
    index := 0;
    while (index < CkJsonArray_getSize(keys)) do
      begin
        success := CkJsonArray_ObjectAt2(keys,index,keyEntry);
        if (success <> True) then
          begin
            Memo1.Lines.Add('Invalid key entry.');
            Exit;
          end;
        CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyEntry,'KeyId'));
        stringMatch5 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
        if ((CkJsonObject_JsonTypeOf(keyEntry,'KeyId') <> 1) or (stringMatch5 = True)) then
          begin
            Memo1.Lines.Add('Missing or invalid KeyId.');
            Exit;
          end;
        CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(keyEntry,'KeyArn'));
        stringMatch6 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
        if ((CkJsonObject_JsonTypeOf(keyEntry,'KeyArn') <> 1) or (stringMatch6 = True)) then
          begin
            Memo1.Lines.Add('Missing or invalid KeyArn.');
            Exit;
          end;
        Memo1.Lines.Add('Key ID: ' + CkJsonObject__stringOf(keyEntry,'KeyId'));
        Memo1.Lines.Add('Key ARN: ' + CkJsonObject__stringOf(keyEntry,'KeyArn'));
        index := index + 1;
      end;

    more := CkJsonObject_BoolOf(result,'Truncated');
    if (more = True) then
      begin
        CkStringBuilder_SetString(stringCompare,CkJsonObject__stringOf(result,'NextMarker'));
        stringMatch7 := CkStringBuilder_ContentsEqual(stringCompare,'',True);
        if ((CkJsonObject_JsonTypeOf(result,'NextMarker') <> 1) or (stringMatch7 = True)) then
          begin
            Memo1.Lines.Add('Missing or invalid NextMarker.');
            Exit;
          end;
        marker := CkJsonObject__stringOf(result,'NextMarker');
        if (CkHashtable_Contains(seenMarkers,marker) = True) then
          begin
            Memo1.Lines.Add('Repeated marker; key enumeration is incomplete.');
            Exit;
          end;
        success := CkHashtable_AddStr(seenMarkers,marker,'seen');
        if (success <> True) then
          begin
            Memo1.Lines.Add('Could not track pagination.');
            Exit;
          end;
      end;
  end;

Memo1.Lines.Add('Key enumeration complete.');

CkStringBuilder_Dispose(stringCompare);
CkJsonObject_Dispose(credentials);
CkStringBuilder_Dispose(endpoint);
CkAuthAws_Dispose(aws);
CkRest_Dispose(rest);
CkSocket_Dispose(connection);
CkJsonObject_Dispose(body);
CkJsonObject_Dispose(result);
CkHashtable_Dispose(seenMarkers);
CkJsonArray_Dispose(keys);
CkJsonObject_Dispose(keyEntry);