-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
185 lines (158 loc) · 4.75 KB
/
client.go
File metadata and controls
185 lines (158 loc) · 4.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Package omnivault provides a unified interface for secret management across
// multiple providers including password managers (1Password, Bitwarden),
// cloud secret managers (AWS, GCP, Azure), and enterprise vaults (HashiCorp Vault).
//
// Basic usage:
//
// client, err := omnivault.NewClient(omnivault.Config{
// Provider: omnivault.ProviderEnv,
// })
// if err != nil {
// log.Fatal(err)
// }
// defer client.Close()
//
// secret, err := client.Get(ctx, "API_KEY")
//
// Using a custom provider:
//
// customVault := myprovider.New(...)
// client, err := omnivault.NewClient(omnivault.Config{
// CustomVault: customVault,
// })
//
// Using the resolver for URI-based secret references:
//
// resolver := omnivault.NewResolver()
// resolver.Register("op", onepasswordVault)
// resolver.Register("env", envVault)
//
// value, err := resolver.Resolve(ctx, "op://Development/api/token")
package omnivault
import (
"context"
"log/slog"
"net/http"
"github.com/plexusone/omnivault/vault"
)
// Config holds configuration for creating a new Client.
type Config struct {
// Provider is the name of a built-in provider to use.
// Ignored if CustomVault is set.
Provider ProviderName
// CustomVault allows injecting a custom vault implementation.
// When set, this takes precedence over Provider.
CustomVault vault.Vault
// ProviderConfig contains provider-specific configuration.
// The expected type depends on the provider being used.
ProviderConfig any
// HTTPClient is an optional HTTP client for providers that make HTTP requests.
HTTPClient *http.Client
// Logger is an optional structured logger.
Logger *slog.Logger
// Extra contains additional provider-specific options.
Extra map[string]any
}
// Client wraps a vault provider with additional functionality.
type Client struct {
vault vault.Vault
config Config
logger *slog.Logger
}
// NewClient creates a new Client with the given configuration.
func NewClient(config Config) (*Client, error) {
var v vault.Vault
var err error
// Custom vault takes precedence
if config.CustomVault != nil {
v = config.CustomVault
} else {
// Use built-in provider factory
v, err = newProvider(config)
if err != nil {
return nil, err
}
}
logger := config.Logger
if logger == nil {
logger = slog.Default()
}
return &Client{
vault: v,
config: config,
logger: logger,
}, nil
}
// Get retrieves a secret from the vault.
func (c *Client) Get(ctx context.Context, path string) (*vault.Secret, error) {
return c.vault.Get(ctx, path)
}
// GetValue retrieves only the value of a secret (convenience method).
func (c *Client) GetValue(ctx context.Context, path string) (string, error) {
secret, err := c.vault.Get(ctx, path)
if err != nil {
return "", err
}
return secret.String(), nil
}
// GetField retrieves a specific field from a secret.
func (c *Client) GetField(ctx context.Context, path, field string) (string, error) {
secret, err := c.vault.Get(ctx, path)
if err != nil {
return "", err
}
return secret.GetField(field), nil
}
// Set stores a secret in the vault.
func (c *Client) Set(ctx context.Context, path string, secret *vault.Secret) error {
return c.vault.Set(ctx, path, secret)
}
// SetValue stores a simple string value as a secret (convenience method).
func (c *Client) SetValue(ctx context.Context, path, value string) error {
return c.vault.Set(ctx, path, &vault.Secret{Value: value})
}
// Delete removes a secret from the vault.
func (c *Client) Delete(ctx context.Context, path string) error {
return c.vault.Delete(ctx, path)
}
// Exists checks if a secret exists.
func (c *Client) Exists(ctx context.Context, path string) (bool, error) {
return c.vault.Exists(ctx, path)
}
// List returns all secrets matching the given prefix.
func (c *Client) List(ctx context.Context, prefix string) ([]string, error) {
return c.vault.List(ctx, prefix)
}
// Name returns the provider name.
func (c *Client) Name() string {
return c.vault.Name()
}
// Capabilities returns the provider capabilities.
func (c *Client) Capabilities() vault.Capabilities {
return c.vault.Capabilities()
}
// Vault returns the underlying vault provider.
// This can be used to access provider-specific functionality.
func (c *Client) Vault() vault.Vault {
return c.vault
}
// Close releases any resources held by the client.
func (c *Client) Close() error {
return c.vault.Close()
}
// MustGet retrieves a secret or panics if an error occurs.
func (c *Client) MustGet(ctx context.Context, path string) *vault.Secret {
secret, err := c.Get(ctx, path)
if err != nil {
panic(err)
}
return secret
}
// MustGetValue retrieves a secret value or panics if an error occurs.
func (c *Client) MustGetValue(ctx context.Context, path string) string {
value, err := c.GetValue(ctx, path)
if err != nil {
panic(err)
}
return value
}