Secrets Zig Reference Documentation

Secrets

Current Version: 11.6.1

Chilkat.Secrets

Manage application secrets through one consistent API.

Chilkat.Secrets provides a unified interface for storing, retrieving, listing, and deleting secrets across local, cloud-based, and in-memory secret stores. It supports string, binary, and JSON secrets, making it useful for passwords, API keys, OAuth credentials, tokens, unlock codes, certificates, and private configuration data.

Local secure storage

  • Windows Credential Manager
  • Apple Keychain

Cloud secret managers

  • AWS Secrets Manager
  • Azure Key Vault
  • IBM Cloud Secrets Manager
  • Oracle Cloud Secrets
  • Doppler Secrets

Temporary secrets

Use in-memory secrets when values should exist only for the lifetime of the running process.

Flexible content types

Store and retrieve secrets as strings, binary data, or structured JSON.

Note: Cloud-based secret managers often require local bootstrap credentials. These can be stored securely in the Windows Credential Manager or Apple Keychain and then used to authenticate to the cloud secret manager.

Object Creation

// Add the package once:
//     zig fetch --save https://chilkatdownload.com/11.6.1/chilkat-zig-11.6.1.tar.gz
// and in build.zig:
//     const chilkat = b.dependency("chilkat", .{ .target = target, .optimize = optimize });
//     exe.root_module.addImport("chilkat", chilkat.module("chilkat"));
const chilkat = @import("chilkat");

// Once per process, before any other Chilkat call:
try chilkat.unlockBundle("Anything for 30-day trial");

const secrets = try chilkat.Secrets.init();
defer secrets.deinit();
pub fn init() Allocator.Error!Secrets

Creates the underlying native Chilkat object. Secrets is a one-pointer struct passed by value; copying it copies the handle (two names for one object). Returns error.OutOfMemory if the library could not allocate the object. Use an object from one thread at a time; it may be handed from one thread to another.

pub fn deinit(self: Secrets) void

Releases the native object. Call it exactly once per object (usually with defer); the handle is invalid afterwards. Objects returned by methods are owned by the caller too and are released the same way.

pub fn fromHandle(h: *chilkat.c.CkSecrets.HCkSecrets) Secrets

Wraps a handle obtained from the C API (chilkat.c.CkSecrets), taking ownership of it. The struct's handle field goes the other way, for anything the Zig API does not cover.

Errors and memory

Methods that can fail return an error union: a method whose only outcome is success or failure returns Error!void; a method producing a string returns (Error || Allocator.Error)![:0]u8; a method producing an object returns Error!T. chilkat.Error is error{ChilkatFailed}; the reason for a failure is in getLastErrorText, and the object remains usable. Properties never fail, and methods that answer a question (hasMember, isUnlocked, ...) return a plain bool.

String arguments are [:0]const u8 (UTF-8; string literals can be passed as is). String results are copies allocated with the allocator argument and owned by the caller, so they stay valid across later calls on the same object.

const text = secrets.someMethod(allocator, ...) catch |err| {
    const why = try secrets.getLastErrorText(allocator);
    defer allocator.free(why);
    std.debug.print("{s}\n", .{why});
    return err;
};
defer allocator.free(text);

Properties

AutoSplit
// read/write
pub fn getAutoSplit(self: Secrets) bool
pub fn setAutoSplit(self: Secrets, value: bool) void
Introduced in version 10.1.0

The AutoSplit property is specific to local_manager secrets on Windows. The Windows Credentials Manager supports a maximum of 2560 bytes per secret. If a secret exceeds this size, it will be automatically compressed and split when created or updated, and automatically recomposed when retrieved.

A secret can be split into a maximum of 10 parts.

The default value is true.

top
Compression
// read/write
pub fn getCompression(self: Secrets) bool
pub fn setCompression(self: Secrets, value: bool) void
Introduced in version 10.1.0

If set to true, then the contents of the secret will be stored compressed. The default value of this property is false.

top
DebugLogFilePath
// read/write
pub fn getDebugLogFilePath(self: Secrets, allocator: Allocator) Allocator.Error![:0]u8
pub fn setDebugLogFilePath(self: Secrets, value: [:0]const u8) void

If set to a file path, this property logs the LastErrorText of each Chilkat method or property call to the specified file. This logging helps identify the context and history of Chilkat calls leading up to any crash or hang, aiding in debugging.

Enabling the VerboseLogging property provides more detailed information. This property is mainly used for debugging rare instances where a Chilkat method call causes a hang or crash, which should generally not happen.

Possible causes of hangs include:

  • A timeout property set to 0, indicating an infinite timeout.
  • A hang occurring within an event callback in the application code.
  • An internal bug in the Chilkat code causing the hang.

More Information and Examples
top
LastErrorHtml
// read-only
pub fn getLastErrorHtml(self: Secrets, allocator: Allocator) Allocator.Error![:0]u8

Provides HTML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorText
// read-only
pub fn getLastErrorText(self: Secrets, allocator: Allocator) Allocator.Error![:0]u8

Provides plain text information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastErrorXml
// read-only
pub fn getLastErrorXml(self: Secrets, allocator: Allocator) Allocator.Error![:0]u8

Provides XML-formatted information about the last called method or property. If a method call fails or behaves unexpectedly, check this property for details. Note that information is available regardless of the method call's success.

top
LastMethodSuccess
// read/write
pub fn getLastMethodSuccess(self: Secrets) bool
pub fn setLastMethodSuccess(self: Secrets, value: bool) void

Indicates the success or failure of the most recent method call: true means success, false means failure. This property remains unchanged by property setters or getters. This method is present to address challenges in checking for null or Nothing returns in certain programming languages. Note: This property does not apply to methods that return integer values or to boolean-returning methods where the boolean does not indicate success or failure.

top
Location
// read/write
pub fn getLocation(self: Secrets, allocator: Allocator) Allocator.Error![:0]u8
pub fn setLocation(self: Secrets, value: [:0]const u8) void
Introduced in version 10.1.0

The location of the secrets. Possible values are:

  • local_manager - On Windows this is the Credentials Manager. On MacOS and iOS it is the Keychain. This is not yet an option on Linux or Android.
  • aws_secrets_manager - AWS Secrets Manager
  • azure_key_vault - Azure Key Vault
  • ibm_cloud - IBM Cloud Secrets Manager
  • oracle_cloud - Oracle Cloud Secrets
  • doppler - Doppler Secrets
  • memory - In-Memory Secrets

The default location is the local_manager. On Linux and Android, local_manager will behave the same as memory.

top
UncommonOptions
// read/write
pub fn getUncommonOptions(self: Secrets, allocator: Allocator) Allocator.Error![:0]u8
pub fn setUncommonOptions(self: Secrets, value: [:0]const u8) void
Introduced in version 10.1.0

This is a catch-all property to be used for uncommon needs. This property defaults to the empty string and should typically remain empty.

top
VerboseLogging
// read/write
pub fn getVerboseLogging(self: Secrets) bool
pub fn setVerboseLogging(self: Secrets, value: bool) void

If set to true, then the contents of LastErrorText (or LastErrorXml, or LastErrorHtml) may contain more verbose information. The default value is false. Verbose logging should only be used for debugging. The potentially large quantity of logged information may adversely affect peformance.

top
Version
// read-only
pub fn getVersion(self: Secrets, allocator: Allocator) Allocator.Error![:0]u8

Version of the component/library, such as "10.1.0"

More Information and Examples
top

Methods

DeleteSecret
pub fn deleteSecret(self: Secrets, json_id: chilkat.JsonObject) Error!void
Introduced in version 10.1.0

Deletes a secret. Cloud secrets managers will typically disable the secret and schedule the secret for deletion after some number of days.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
GetSecretBd
pub fn getSecretBd(self: Secrets, json_id: chilkat.JsonObject, bd: chilkat.BinData) Error!void
Introduced in version 10.1.0

Fetches a binary secret and returns the content in bd.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
GetSecretJson
pub fn getSecretJson(self: Secrets, json_id: chilkat.JsonObject, json_out: chilkat.JsonObject) Error!void
Introduced in version 10.1.0

Fetches a JSON secret and returns the content in json_out.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
GetSecretSb
pub fn getSecretSb(self: Secrets, json_id: chilkat.JsonObject, sb: chilkat.StringBuilder) Error!void
Introduced in version 10.1.0

Fetches a string secret and returns the content in sb.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
GetSecretStr
pub fn getSecretStr(self: Secrets, allocator: Allocator, json_id: chilkat.JsonObject) (Error || Allocator.Error)![:0]u8
Introduced in version 10.1.0

Fetches a string secret and returns the content of the secret.

Returns error.ChilkatFailed on failure; getLastErrorText explains why. The string is allocated with allocator and owned by the caller (defer allocator.free(s)).

top
ListSecrets
pub fn listSecrets(self: Secrets, json: chilkat.JsonObject, json_out: chilkat.JsonObject) Error!void
Introduced in version 10.1.0

Lists secrets matching the wildcarded or exact values passed in json. The results of the query are returned in json_out.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
SecretSpecToJson
pub fn secretSpecToJson(self: Secrets, secret_spec: [:0]const u8, json: chilkat.JsonObject) Error!void
Introduced in version 11.5.0

Converts a secret specification string (secret_spec) to JSON. A secret specification string is formatted as "!!part1|part2|part3|part4" where parts 3 and 4 are optional. json is the output argument.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

More Information and Examples
top
SetBootstrapSecret
pub fn setBootstrapSecret(self: Secrets, json_id: chilkat.JsonObject, secret_content: Secrets) Error!void
Introduced in version 10.1.0

Accessing a cloud-based secrets manager requires authentication credentials, which cannot be stored in the manager itself. Instead, they must be securely stored locally.

One solution is to store the authentication credentials as a secret in the Windows Credentials Manager or Apple Keychain. These credentials serve as the bootstrap secret for authenticating with the cloud-based secrets manager.

json_id contains the name of the bootstrap secret. secret_content contains the bootstrap's secret content.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
UpdateSecretBd
pub fn updateSecretBd(self: Secrets, json_id: chilkat.JsonObject, secret: chilkat.BinData) Error!void
Introduced in version 10.1.0

Creates a binary secret if it does not yet exist, or updates an existing secret. The name of the secret is specified in json_id, and the binary content of the secret in secret.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
UpdateSecretJson
pub fn updateSecretJson(self: Secrets, json_id: chilkat.JsonObject, secret: chilkat.JsonObject) Error!void
Introduced in version 10.1.0

Creates a JSON secret if it does not yet exist, or updates an existing secret. The name of the secret is specified in json_id, and the JSON content of the secret in secret.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
UpdateSecretSb
pub fn updateSecretSb(self: Secrets, json_id: chilkat.JsonObject, sb: chilkat.StringBuilder) Error!void
Introduced in version 10.1.0

Creates a string secret if it does not yet exist, or updates an existing secret. The name of the secret is specified in json_id, and the string content of the secret in sb.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top
UpdateSecretStr
pub fn updateSecretStr(self: Secrets, json_id: chilkat.JsonObject, secret: [:0]const u8) Error!void
Introduced in version 10.1.0

Creates a string secret if it does not yet exist, or updates an existing secret. The name of the secret is specified in json_id, and the string content of the secret in secret.

Note: Starting in Chilkat v11.3.0, if the service = "Chilkat" and the username = "unlock_code", then the secret (which is the Chilkat unlock code) can be stored and retrieved without Chilkat being unlocked. This allows for the Chilkat unlock code itself to be persisted to a secrets manager.

Returns error.ChilkatFailed on failure; getLastErrorText explains why.

top

Events

All Chilkat methods are synchronous: the call returns when the work is done. During a call, Secrets raises three events so your program can show progress and offer a way out. Declare any subset of abortCheck, percentDone and progressInfo in a struct of your own and install a pointer to it with setEventHandler:

const Progress = struct {
    pub fn percentDone(_: *Progress, pct: i32) bool {
        std.debug.print("{d}%\n", .{pct});
        return false; // return true to abort the method in progress
    }
    pub fn progressInfo(_: *Progress, name: [:0]const u8, value: [:0]const u8) void {
        std.debug.print("{s}: {s}\n", .{ name, value });
    }
};

var progress = Progress{};
secrets.setEventHandler(&progress); // progress must outlive the installation
defer secrets.clearEventHandler();
secrets.setHeartbeatMs(250); // abortCheck 4 times per second during Chilkat calls
pub fn setEventHandler(self: Secrets, handler: anytype) void

Installs handler, a pointer to any struct, as the receiver of this object's events, replacing any handler installed earlier. The dispatch is resolved at compile time: only the methods the struct declares are called, and a struct declaring none of the three is a compile error. The struct must stay alive, at the same address, until clearEventHandler or deinit.

pub fn clearEventHandler(self: Secrets) void

Removes the handler; events are no longer delivered.

AbortCheck fires at regular intervals controlled by the HeartbeatMs property (0, the default, disables it); PercentDone fires when an operation's completion percentage is known; ProgressInfo delivers named progress values. Returning true from abortCheck or percentDone aborts the running method, which then returns error.ChilkatFailed.

Events fire on the thread that called the method, before that method returns. To abort a long operation from another thread, have abortCheck read a std.atomic.Value(bool), or set the object's AbortCurrent property.

AbortCheck
// handler struct method (optional); install with setEventHandler
pub fn abortCheck(self: *T) bool

Enables a method call to be aborted by triggering the AbortCheck event at intervals defined by the HeartbeatMs property. If HeartbeatMs is set to its default value of 0, no events will occur. For instance, set HeartbeatMs to 200 to trigger 5 AbortCheck events per second.

More Information and Examples

Example

const Abort = struct {
    stop: std.atomic.Value(bool) = .init(false),
    pub fn abortCheck(self: *Abort) bool {
        return self.stop.load(.acquire); // another thread may call abort.stop.store(true, .release)
    }
};

var abort = Abort{};
secrets.setHeartbeatMs(250); // call abortCheck 4 times per second
secrets.setEventHandler(&abort);
defer secrets.clearEventHandler();
top
PercentDone
// handler struct method (optional); install with setEventHandler
pub fn percentDone(self: *T, pct: i32) bool

This provides the percentage completion for any method involving network communications or time-consuming processing, assuming the progress can be measured as a percentage. This event is triggered only when it's possible and logical to express the operation's progress as a percentage. The pctDone argument will range from 1 to 100. For methods that finish quickly, the number of PercentDone callbacks may vary, but the final callback will have pctDone equal to 100. For longer operations, callbacks will not exceed one per percentage point (e.g., 1, 2, 3, ..., 98, 99, 100).

The PercentDone callback also acts as an AbortCheck event. For fast methods where PercentDone fires, an AbortCheck event may not trigger since the PercentDone callback already provides an opportunity to abort. For longer operations, where time between PercentDone callbacks is extended, AbortCheck callbacks enable more responsive operation termination.

To abort the operation, set the abort output argument to true. This will cause the method to terminate and return a failure status or corresponding failure value.

More Information and Examples

Example

const Progress = struct {
    pub fn percentDone(_: *Progress, pct: i32) bool {
        // pct ranges from 1 to 100.
        std.debug.print("Percent done: {d}\n", .{pct});
        return false; // return true to abort the method in progress
    }
};

var progress = Progress{};
secrets.setEventHandler(&progress);
defer secrets.clearEventHandler();
top
ProgressInfo
// handler struct method (optional); install with setEventHandler
pub fn progressInfo(self: *T, name: [:0]const u8, value: [:0]const u8) void

This event callback provides tag name/value pairs that detail what occurs during a method call. To discover existing tag names, create code to handle the event, emit the pairs, and review them. Most tag names are self-explanatory.

Note: Some Chilkat methods don't fire any ProgressInfo events.

More Information and Examples

Example

const Info = struct {
    pub fn progressInfo(_: *Info, name: [:0]const u8, value: [:0]const u8) void {
        std.debug.print("{s}: {s}\n", .{ name, value });
    }
};

var info = Info{};
secrets.setEventHandler(&info);
defer secrets.clearEventHandler();
top