Sample code for 30+ languages & platforms
Zig

Azure Key Vault Get Certificates

See more Azure Key Vault Examples

Demonstrates how to list the certificates in an Azure Key Vault.

Note: This example requires Chilkat v9.5.0.96 or later.

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 requires the Chilkat API to have been previously unlocked.
    // See Global Unlock Sample for sample code.

    // We demonstrated how to get an access token for your Azure Key Vault
    // in this example: Azure Key Vault Get OAuth2 Access Token using Client Credentials

    // However.. starting in Chilkat v9.5.0.96, instead of directly providing Chilkat with the OAuth2 access token,
    // you can instead provide the means for Chilkat to automatically get the OAuth2 access token,
    // and in addition, Chilkat will automatically re-fetch a new OAuth2 access token as needed, such as shortly
    // prior to or after expiration.

    // You do this by setting the AuthToken property to a JSON string that contains the required information.

    const json = try chilkat.JsonObject.init();
    defer json.deinit();
    json.updateString("client_id", "APP_ID") catch {};

    // The APP_PASSWORD is the "password" returned by the Azure CLI command: az ad sp create-for-rbac --name http://example.com --role Contributor
    // See Azure Key Vault Get OAuth2 Access Token using Client Credentials
    json.updateString("client_secret", "APP_PASSWORD") catch {};

    // The access token will be for Azure Key Vault operations.
    json.updateString("resource", "https://vault.azure.net") catch {};

    // Specify the token endpoint which includes your tenant ID.
    json.updateString("token_endpoint", "https://login.microsoftonline.com/TENANT_ID/oauth2/token") catch {};

    const http = try chilkat.Http.init();
    defer http.deinit();

    // Instead of providing an actual access token, we give Chilkat the information that allows it to
    // automatically fetch the access token using the OAuth2 client credentials flow.
    http.setAuthToken(try json.emit(alloc));

    // Replace key_vault_name with the name of your Azure Key Vault.
    const sb_response = try chilkat.StringBuilder.init();
    defer sb_response.deinit();
    http.quickGetSb("https://key_vault_name.vault.azure.net/certificates?api-version=7.4", sb_response) catch {
        const status_code = http.getLastStatus();
        if (status_code == 0) {
            // We did not get a response from the server..
            std.debug.print("{s}\n", .{try http.getLastErrorText(alloc)});
        } else {
            // We received a response, but it was an error.
            std.debug.print("Error response status code: {d}\n", .{status_code});
            std.debug.print("Error response:\n", .{});
            std.debug.print("{s}\n", .{try sb_response.getAsString(alloc)});
        }

        return;
    };

    const json_resp = try chilkat.JsonObject.init();
    defer json_resp.deinit();
    json_resp.loadSb(sb_response) catch {};
    json_resp.setEmitCompact(false);

    std.debug.print("{s}\n", .{try json_resp.emit(alloc)});

    // The output looks like this:

    // {
    //   "value": [
    //     {
    //       "id": "https://kvchilkat.vault.azure.net/certificates/BadSSL",
    //       "x5t": "U04xLnb8Ww7BKkW9dD7P1cCHNDY",
    //       "attributes": {
    //         "enabled": true,
    //         "nbf": 1674409014,
    //         "exp": 1737481014,
    //         "created": 1697294224,
    //         "updated": 1697294224
    //       },
    //       "subject": ""
    //     },
    //     {
    //       "id": "https://kvchilkat.vault.azure.net/certificates/Brasil",
    //       "x5t": "ayF5eBtlA35xPMivusE0wpmFjnA",
    //       "attributes": {
    //         "enabled": true,
    //         "nbf": 1667830002,
    //         "exp": 1699366002,
    //         "created": 1697294090,
    //         "updated": 1697294090
    //       },
    //       "subject": ""
    //     }
    //   ],
    //   "nextLink": null
    // }

    // Use this online tool to generate parsing code from sample JSON:
    // Generate Parsing Code from JSON

    var id: [:0]const u8 = "";
    var x5t: [:0]const u8 = "";
    var enabled: bool = false;
    var nbf: i32 = 0;
    var exp: i32 = 0;
    var created: i32 = 0;
    var updated: i32 = 0;
    var subject: [:0]const u8 = "";

    var i: i32 = 0;
    const count_i = json_resp.sizeOfArray("value");
    while (i < count_i) {
        json_resp.setI(i);
        id = try json_resp.stringOf(alloc, "value[i].id");
        x5t = try json_resp.stringOf(alloc, "value[i].x5t");
        enabled = json_resp.boolOf("value[i].attributes.enabled");
        nbf = json_resp.intOf("value[i].attributes.nbf");
        exp = json_resp.intOf("value[i].attributes.exp");
        created = json_resp.intOf("value[i].attributes.created");
        updated = json_resp.intOf("value[i].attributes.updated");
        subject = try json_resp.stringOf(alloc, "value[i].subject");
        i = i + 1;
    }
}