Client API¶
The OmniVault client provides a high-level API for secret management.
Creating a Client¶
// With a built-in provider
client, err := omnivault.NewClient(omnivault.Config{
Provider: omnivault.ProviderEnv, // or ProviderFile, ProviderMemory
})
// With a custom provider
client, err := omnivault.NewClient(omnivault.Config{
CustomVault: myCustomProvider,
})
Always Close
Always call client.Close() when done to release resources.
Basic Operations¶
Get¶
Retrieve a secret by path:
secret, err := client.Get(ctx, "path/to/secret")
if err != nil {
if err == vault.ErrSecretNotFound {
// Secret doesn't exist
}
// Handle other errors
}
Set¶
Store a secret:
secret := &omnivault.Secret{
Value: "my-secret-value",
}
err := client.Set(ctx, "path/to/secret", secret)
Delete¶
Remove a secret:
Exists¶
Check if a secret exists:
List¶
List secrets by prefix:
paths, err := client.List(ctx, "database/")
// Returns: ["database/password", "database/username", ...]
Convenience Methods¶
GetValue¶
Get just the string value:
GetField¶
Get a specific field from a multi-field secret:
SetValue¶
Set a simple string value:
Must Variants¶
Variants that panic on error (useful for initialization):
// Panics if secret not found or error occurs
secret := client.MustGet(ctx, "REQUIRED_SECRET")
value := client.MustGetValue(ctx, "REQUIRED_VALUE")
Use with Caution
Must variants should only be used during application startup or in contexts where a missing secret is truly fatal.
Secret Structure¶
type Secret struct {
// Primary value
Value string
// Binary value (alternative to Value)
ValueBytes []byte
// Additional fields (for multi-field secrets)
Fields map[string]string
// Metadata
Metadata Metadata
}
type Metadata struct {
// Custom tags
Tags map[string]string
// Timestamps (set automatically)
CreatedAt *time.Time
ModifiedAt *time.Time
}
Accessing Values¶
secret, _ := client.Get(ctx, "path")
// Primary value as string
value := secret.String()
// Primary value as bytes
bytes := secret.Bytes()
// Specific field
field := secret.GetField("username")
// Check if field exists
if val, ok := secret.Fields["password"]; ok {
// Use val
}
Error Handling¶
secret, err := client.Get(ctx, "path")
if err != nil {
switch err {
case vault.ErrSecretNotFound:
// Secret doesn't exist
case vault.ErrAccessDenied:
// Permission denied
default:
// Other error
}
}
Provider Capabilities¶
Check what operations a provider supports: