Sample code for 30+ languages & platforms
Unicode C

AWS Secrets Manager - List Secrets

See more AWS Secrets Manager Examples

Lists the secrets that are stored by Secrets Manager in the AWS account. Lists the secrets that are stored by Secrets Manager in the AWS account.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkRestW.h>
#include <C_CkAuthAwsW.h>
#include <C_CkJsonObjectW.h>
#include <C_CkJsonArrayW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkRestW rest;
    BOOL bTls;
    int port;
    BOOL bAutoReconnect;
    HCkAuthAwsW authAws;
    const wchar_t *strResponse;
    int respStatusCode;
    HCkJsonObjectW jResp;
    const wchar_t *ARN;
    const wchar_t *Description;
    const wchar_t *LastChangedDate;
    const wchar_t *Name;
    const wchar_t *strVal;
    HCkJsonObjectW json2;
    HCkJsonArrayW jarr;
    int i;
    int count_i;
    int count;
    int j;
    const wchar_t *versionName;
    const wchar_t *stage;

    success = FALSE;

    // This example requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // Sends the following sample request.
    // Note: Chilkat will automatically add Content-Length, X-Amz-Date, Accept-Encoding, and Authorization

    // POST / HTTP/1.1
    // Host: secretsmanager.region.domain
    // Accept-Encoding: identity
    // X-Amz-Target: secretsmanager.ListSecrets
    // Content-Type: application/x-amz-json-1.1
    // X-Amz-Date: <date>
    // Authorization: AWS4-HMAC-SHA256 Credential=<credentials>,SignedHeaders=<headers>, Signature=<signature>
    // Content-Length: <payload-size-bytes>
    // 
    // {}

    rest = CkRestW_Create();

    // Connect to the Amazon AWS REST server.
    // such as https://secretsmanager.us-west-2.amazonaws.com/
    bTls = TRUE;
    port = 443;
    bAutoReconnect = TRUE;
    success = CkRestW_Connect(rest,L"secretsmanager.us-west-2.amazonaws.com",port,bTls,bAutoReconnect);

    // Provide AWS credentials for the REST call.
    authAws = CkAuthAwsW_Create();
    CkAuthAwsW_putAccessKey(authAws,L"AWS_ACCESS_KEY");
    CkAuthAwsW_putSecretKey(authAws,L"AWS_SECRET_KEY");
    // the region should match our URL above..
    CkAuthAwsW_putRegion(authAws,L"us-west-2");
    CkAuthAwsW_putServiceName(authAws,L"secretsmanager");

    CkRestW_SetAuthAws(rest,authAws);

    CkRestW_AddHeader(rest,L"Content-Type",L"application/x-amz-json-1.1");
    CkRestW_AddHeader(rest,L"X-Amz-Target",L"secretsmanager.ListSecrets");

    strResponse = CkRestW_fullRequestString(rest,L"POST",L"/",L"{}");
    if (CkRestW_getLastMethodSuccess(rest) == FALSE) {
        wprintf(L"%s\n",CkRestW_lastErrorText(rest));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        return;
    }

    respStatusCode = CkRestW_getResponseStatusCode(rest);
    wprintf(L"response status code = %d\n",respStatusCode);

    jResp = CkJsonObjectW_Create();
    CkJsonObjectW_putEmitCompact(jResp,FALSE);
    CkJsonObjectW_Load(jResp,strResponse);

    if (respStatusCode >= 400) {
        wprintf(L"Response Status Code = %d\n",respStatusCode);
        wprintf(L"Response Header:\n");
        wprintf(L"%s\n",CkRestW_responseHeader(rest));
        wprintf(L"Response Body:\n");
        wprintf(L"%s\n",CkJsonObjectW_emit(jResp));
        CkRestW_Dispose(rest);
        CkAuthAwsW_Dispose(authAws);
        CkJsonObjectW_Dispose(jResp);
        return;
    }

    wprintf(L"Response Body:\n");
    wprintf(L"%s\n",CkJsonObjectW_emit(jResp));

    // Sample response body:

    // {
    //   "SecretList":[
    //     {
    //       "ARN":"arn:aws:secretsmanager:us-west-2:123456789012:secret:MyTestDatabaseSecret-a1b2c3",
    //       "Description":"My test database secret",
    //       "LastChangedDate":1.523477145729E9,
    //       "Name":"MyTestDatabaseSecret",
    //       "SecretVersionsToStages":{
    //         "EXAMPLE2-90ab-cdef-fedc-ba987EXAMPLE":["AWSCURRENT"]
    //       }
    //     },
    //     {
    //       "ARN":"arn:aws:secretsmanager:us-west-2:123456789012:secret:AnotherDatabaseSecret-d4e5f6",
    //       "Description":"Another secret created for a different database",
    //       "LastChangedDate":1.523482025685E9,
    //       "Name":"AnotherDatabaseSecret",
    //       "SecretVersionsToStages":{
    //         "EXAMPLE3-90ab-cdef-fedc-ba987EXAMPLE":["AWSCURRENT"]
    //       }
    //     }
    //   ]
    // }

    json2 = CkJsonObjectW_Create();
    jarr = CkJsonArrayW_Create();

    i = 0;
    count_i = CkJsonObjectW_SizeOfArray(jResp,L"SecretList");
    while (i < count_i) {
        CkJsonObjectW_putI(jResp,i);
        ARN = CkJsonObjectW_stringOf(jResp,L"SecretList[i].ARN");
        Description = CkJsonObjectW_stringOf(jResp,L"SecretList[i].Description");
        LastChangedDate = CkJsonObjectW_stringOf(jResp,L"SecretList[i].LastChangedDate");
        Name = CkJsonObjectW_stringOf(jResp,L"SecretList[i].Name");

        CkJsonObjectW_ObjectOf2(jResp,L"SecretList[i].SecretVersionsToStages",json2);
        count = CkJsonObjectW_getSize(json2);
        j = 0;
        while (j < count) {
            versionName = CkJsonObjectW_nameAt(json2,j);
            CkJsonObjectW_ArrayOf2(json2,versionName,jarr);
            stage = CkJsonArrayW_stringAt(jarr,0);
            wprintf(L"versionName = %s\n",versionName);
            wprintf(L"stage = %s\n",stage);
            j = j + 1;
        }

        i = i + 1;
    }



    CkRestW_Dispose(rest);
    CkAuthAwsW_Dispose(authAws);
    CkJsonObjectW_Dispose(jResp);
    CkJsonObjectW_Dispose(json2);
    CkJsonArrayW_Dispose(jarr);

    }