Documentation
¶
Overview ¶
Package stash provides a Go client for the Stash KV configuration service.
Basic usage:
client, err := stash.New("http://localhost:8080")
if err != nil {
log.Fatal(err)
}
// store a value (default text format)
err = client.Set(ctx, "app/config", `{"debug": true}`)
// store a value with explicit format
err = client.SetWithFormat(ctx, "app/config", `{"debug": true}`, stash.FormatJSON)
// retrieve a value
value, err := client.Get(ctx, "app/config")
// retrieve with default
value, err := client.GetOrDefault(ctx, "app/config", "fallback")
// list all keys
keys, err := client.List(ctx, "")
With authentication:
client, err := stash.New("http://localhost:8080",
stash.WithToken("your-api-token"),
)
With custom options:
client, err := stash.New("http://localhost:8080",
stash.WithToken("your-api-token"),
stash.WithTimeout(10*time.Second),
stash.WithRetry(5, 200*time.Millisecond),
)
Code generated by enum generator; DO NOT EDIT.
Index ¶
- Variables
- func FormatIter() func(yield func(Format) bool)
- func IsValidZKPayload(value []byte) bool
- func IsZKEncrypted(value []byte) bool
- type Client
- func (c *Client) Close()
- func (c *Client) Delete(ctx context.Context, key string) error
- func (c *Client) Get(ctx context.Context, key string) (string, error)
- func (c *Client) GetBytes(ctx context.Context, key string) ([]byte, error)
- func (c *Client) GetOrDefault(ctx context.Context, key, defaultValue string) (string, error)
- func (c *Client) Info(ctx context.Context, key string) (KeyInfo, error)
- func (c *Client) List(ctx context.Context, prefix string) ([]KeyInfo, error)
- func (c *Client) Ping(ctx context.Context) error
- func (c *Client) Set(ctx context.Context, key, value string) error
- func (c *Client) SetWithFormat(ctx context.Context, key, value string, format Format) error
- func (c *Client) Subscribe(ctx context.Context, key string) (*Subscription, error)
- func (c *Client) SubscribeAll(ctx context.Context) (*Subscription, error)
- func (c *Client) SubscribePrefix(ctx context.Context, prefix string) (*Subscription, error)
- type Event
- type Format
- type KeyInfo
- type Option
- type ResponseError
- type Subscription
- type ZKCrypto
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotFound = errors.New("key not found") ErrForbidden = errors.New("forbidden") )
sentinel errors for common API responses
var ( FormatText = Format{/* contains filtered or unexported fields */} FormatJSON = Format{/* contains filtered or unexported fields */} FormatYAML = Format{/* contains filtered or unexported fields */} FormatXML = Format{/* contains filtered or unexported fields */} FormatTOML = Format{/* contains filtered or unexported fields */} FormatINI = Format{/* contains filtered or unexported fields */} FormatHCL = Format{/* contains filtered or unexported fields */} FormatShell = Format{/* contains filtered or unexported fields */} )
Public constants for format values
var ErrZKDecryptionFailed = errors.New("zk decryption failed")
ErrZKDecryptionFailed is returned when ZK decryption fails (wrong key or corrupted data).
var FormatNames = []string{
"text",
"json",
"yaml",
"xml",
"toml",
"ini",
"hcl",
"shell",
}
FormatNames contains all possible enum names
var FormatValues = []Format{ FormatText, FormatJSON, FormatYAML, FormatXML, FormatTOML, FormatINI, FormatHCL, FormatShell, }
FormatValues contains all possible enum values
Functions ¶
func FormatIter ¶
FormatIter returns a function compatible with Go 1.23's range-over-func syntax. It yields all Format values in declaration order. Example:
for v := range FormatIter() {
// use v
}
func IsValidZKPayload ¶ added in v0.18.1
IsValidZKPayload checks if a ZK value has valid format. Returns true if value has $ZK$ prefix followed by valid base64 of sufficient length. This validates format only, not cryptographic correctness (zero-knowledge preserved).
func IsZKEncrypted ¶ added in v0.18.1
IsZKEncrypted checks if a value is ZK-encrypted by looking for the $ZK$ prefix.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client is a Stash KV service client.
func (*Client) Close ¶ added in v0.16.0
func (c *Client) Close()
Close clears sensitive data from memory. Call this when the client is no longer needed.
func (*Client) GetOrDefault ¶
GetOrDefault retrieves a value by key, returning defaultValue if the key doesn't exist.
func (*Client) Info ¶
Info retrieves metadata for a key. Note: this method uses List with prefix filtering, which may be inefficient for large keyspaces with many keys sharing the same prefix.
func (*Client) List ¶
List returns all keys, optionally filtered by prefix. Pass empty string to list all keys.
func (*Client) SetWithFormat ¶
SetWithFormat stores a value with explicit format.
func (*Client) Subscribe ¶ added in v0.19.0
Subscribe creates a subscription for exact key changes. The subscription remains active until context is canceled or Close is called.
func (*Client) SubscribeAll ¶ added in v0.19.0
func (c *Client) SubscribeAll(ctx context.Context) (*Subscription, error)
SubscribeAll creates a subscription for all key changes. The subscription remains active until context is canceled or Close is called.
func (*Client) SubscribePrefix ¶ added in v0.19.0
SubscribePrefix creates a subscription for all keys matching the prefix. The subscription remains active until context is canceled or Close is called.
type Event ¶ added in v0.19.0
type Event struct {
Key string `json:"key"`
Action string `json:"action"` // create, update, delete
Timestamp string `json:"timestamp"`
}
Event represents a key change event from the server.
type Format ¶
type Format struct {
// contains filtered or unexported fields
}
Format is the exported type for the enum
func MustFormat ¶
MustFormat is like ParseFormat but panics if string is invalid
func ParseFormat ¶
ParseFormat converts string to format enum value. Parsing is always case-insensitive.
func (Format) ContentType ¶
ContentType returns the HTTP Content-Type for the format.
func (Format) MarshalText ¶
MarshalText implements encoding.TextMarshaler
func (*Format) UnmarshalText ¶
UnmarshalText implements encoding.TextUnmarshaler
type KeyInfo ¶
type KeyInfo struct {
Key string `json:"key"`
Size int `json:"size"`
Format string `json:"format"`
Secret bool `json:"secret"`
ZKEncrypted bool `json:"zk_encrypted"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
KeyInfo contains metadata about a stored key.
type Option ¶
type Option func(*clientConfig)
Option is a functional option for configuring the client.
func WithHTTPClient ¶
WithHTTPClient sets a custom http.Client. Note: when using WithHTTPClient, the WithTimeout option has no effect since timeout is configured on the http.Client directly.
func WithTimeout ¶
WithTimeout sets the HTTP request timeout.
func WithZKKey ¶ added in v0.16.0
WithZKKey enables client-side zero-knowledge encryption with the given passphrase. When enabled, all values stored via Set/SetWithFormat will be encrypted before sending to the server, and values retrieved via Get/GetBytes will be decrypted if they have the $ZK$ prefix. The server never sees plaintext values. Passphrase must be at least 16 characters.
type ResponseError ¶
type ResponseError struct {
StatusCode int
}
ResponseError represents an HTTP error response from the server.
func (*ResponseError) Error ¶
func (e *ResponseError) Error() string
Error implements the error interface.
type Subscription ¶ added in v0.19.0
type Subscription struct {
// contains filtered or unexported fields
}
Subscription manages an SSE connection for key change events.
func (*Subscription) Close ¶ added in v0.19.0
func (s *Subscription) Close()
Close terminates the subscription and releases resources.
func (*Subscription) Errors ¶ added in v0.19.0
func (s *Subscription) Errors() <-chan error
Errors returns the channel for receiving connection errors.
func (*Subscription) Events ¶ added in v0.19.0
func (s *Subscription) Events() <-chan Event
Events returns the channel for receiving events.
type ZKCrypto ¶ added in v0.18.1
type ZKCrypto struct {
// contains filtered or unexported fields
}
ZKCrypto handles client-side zero-knowledge encryption using AES-256-GCM with Argon2id key derivation.
func NewZKCrypto ¶ added in v0.18.1
NewZKCrypto creates a new ZKCrypto instance with the given passphrase. Passphrase must be at least 16 bytes.
func (*ZKCrypto) Clear ¶ added in v0.18.1
func (z *ZKCrypto) Clear()
Clear securely clears the passphrase from memory. note: this is best-effort; Go's GC may have copied the data and the compiler may optimize away the zeroing if it determines the memory won't be read again.