Sample code for 30+ languages & platforms
Zig Requires Chilkat v11.1.0+

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 Zig Downloads

Zig
const std = @import("std");
const chilkat = @import("chilkat");

pub fn main(init: std.process.Init) !void {
    const alloc = init.arena.allocator();

    // 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>
    //
    // {}

    const rest = try chilkat.Rest.init();
    defer rest.deinit();

    // Connect to the Amazon AWS REST server.
    // such as https://secretsmanager.us-west-2.amazonaws.com/
    const b_tls = true;
    const port = 443;
    const b_auto_reconnect = true;
    try rest.connect("secretsmanager.us-west-2.amazonaws.com", port, b_tls, b_auto_reconnect);

    // Provide AWS credentials for the REST call.
    const auth_aws = try chilkat.AuthAws.init();
    defer auth_aws.deinit();
    auth_aws.setAccessKey("AWS_ACCESS_KEY");
    auth_aws.setSecretKey("AWS_SECRET_KEY");
    // the region should match our URL above..
    auth_aws.setRegion("us-west-2");
    auth_aws.setServiceName("secretsmanager");

    rest.setAuthAws(auth_aws) catch {};

    rest.addHeader("Content-Type", "application/x-amz-json-1.1") catch {};
    rest.addHeader("X-Amz-Target", "secretsmanager.ListSecrets") catch {};

    const str_response = rest.fullRequestString(alloc, "POST", "/", "{}") catch {
        std.debug.print("{s}\n", .{try rest.getLastErrorText(alloc)});
        return;
    };

    const resp_status_code = rest.getResponseStatusCode();
    std.debug.print("response status code = {d}\n", .{resp_status_code});

    const j_resp = try chilkat.JsonObject.init();
    defer j_resp.deinit();
    j_resp.setEmitCompact(false);
    j_resp.load(str_response) catch {};

    if (resp_status_code >= 400) {
        std.debug.print("Response Status Code = {d}\n", .{resp_status_code});
        std.debug.print("Response Header:\n", .{});
        std.debug.print("{s}\n", .{try rest.getResponseHeader(alloc)});
        std.debug.print("Response Body:\n", .{});
        std.debug.print("{s}\n", .{try j_resp.emit(alloc)});
        return;
    }

    std.debug.print("Response Body:\n", .{});
    std.debug.print("{s}\n", .{try j_resp.emit(alloc)});

    // 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"]
    //       }
    //     }
    //   ]
    // }

    var arn: [:0]const u8 = "";
    var description: [:0]const u8 = "";
    var last_changed_date: [:0]const u8 = "";
    var name: [:0]const u8 = "";

    const json2 = try chilkat.JsonObject.init();
    defer json2.deinit();
    const jarr = try chilkat.JsonArray.init();
    defer jarr.deinit();

    var i: i32 = 0;
    const count_i = j_resp.sizeOfArray("SecretList");
    while (i < count_i) {
        j_resp.setI(i);
        arn = try j_resp.stringOf(alloc, "SecretList[i].ARN");
        description = try j_resp.stringOf(alloc, "SecretList[i].Description");
        last_changed_date = try j_resp.stringOf(alloc, "SecretList[i].LastChangedDate");
        name = try j_resp.stringOf(alloc, "SecretList[i].Name");

        j_resp.objectOf2("SecretList[i].SecretVersionsToStages", json2) catch {};
        const count = json2.getSize();
        var j: i32 = 0;
        while (j < count) {
            const version_name = try json2.nameAt(alloc, j);
            json2.arrayOf2(version_name, jarr) catch {};
            const stage = try jarr.stringAt(alloc, 0);
            std.debug.print("versionName = {s}\n", .{version_name});
            std.debug.print("stage = {s}\n", .{stage});
            j = j + 1;
        }

        i = i + 1;
    }
}