consulfs

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package consulfs provides an interface to Hashicorp Consul which allows you to interact with the Consul K/V store as a standard filesystem.

This filesystem's behaviour complies with testing/fstest.TestFS.

Usage

To use this filesystem, call New with a base URL. All reads from the filesystem are relative to this base URL. The schemes "consul", "consul+https", "https", "consul+http", and "http" are supported, though the http schemes should only be used in development/test environments. If no authority part (host:port) is present in the URL, the CONSUL_HTTP_ADDR environment variable will be used as the Consul server's address.

To scope the filesystem to a specific path, use that path on the URL. For example, for a filesystem that can only read from keys prefixed with "foo", you could use a URL like "consul://consulserver.local/foo/".

Note: when scoping URLs to specific paths, the URL must end in "/".

In general, data is read from Consul in the same way as with the Consul CLI - that is, the "/kv" prefix is not needed.

See the Consul KV Store docs for more details.

Authentication

To authenticate with Consul, an ACL Token will need to be set. You can set the token in a few ways: with the CONSUL_HTTP_TOKEN environment variable, by providing a custom Consul client configuration with the WithConfigFS extension, by providing custom Consul query options with the WithQueryOptionsFS extension, or by providing a token directly with the WithTokenFS extension.

Usually, you'll want to use the WithTokenFS extension.

Extensions

The filesystem may be configured with a few standard and consulfs-specific extensions. See the documentation for each extension for more details:

Example
base, _ := url.Parse("consul://my.consulserver.local:8500")

fsys, _ := New(base)

b, _ := fs.ReadFile(fsys, "mykey")

fmt.Printf("the secret is %s\n", string(b))
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var FS = fsimpl.FSProviderFunc(New, "consul", "consul+http", "consul+https")

FS is used to register this filesystem with an fsimpl.FSMux

Functions

func New

func New(u *url.URL) (fs.FS, error)

New creates a filesystem for the Consul KV endpoint rooted at u.

func WithConfigFS

func WithConfigFS(config *api.Config, fsys fs.FS) fs.FS

WithConfigFS configures the Consul client used by fsys, if the filesystem supports it (i.e. has a WithConfig method). This can be used to set a custom HTTP client, TLS configuration, etc... This can not be used to set the address of the Consul server, as that is set by the base URL or the CONSUL_HTTP_ADDR environment variable.

If you only want to set the token, use WithTokenFS instead.

Note that calling WithConfigFS will reset the underlying client, which may introduce a performance penalty.

func WithQueryOptionsFS

func WithQueryOptionsFS(opts *api.QueryOptions, fsys fs.FS) fs.FS

WithQueryOptionsFS sets Consul KV query options used by fsys, if the filesystem supports it (i.e. has a WithQueryOptions method). This can not be used to set the request context (use fsimpl.WithContextFS instead).

For options that can also be set in the config, those will be overridden temporarily by the options set here. The underlying configuration and client will be preserved on the filesystem.

func WithTokenFS

func WithTokenFS(token string, fsys fs.FS) fs.FS

WithTokenFS sets the Consul token used by fsys, if the filesystem supports it (i.e. has a WithToken method).

Note that calling WithTokenFS will reset the underlying client, which may introduce a performance penalty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL