-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_opts.go
93 lines (78 loc) · 2.23 KB
/
client_opts.go
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
package vaulty
import (
"context"
"fmt"
"os"
hashiVault "github.com/hashicorp/vault/api"
auth "github.com/hashicorp/vault/api/auth/kubernetes"
)
type ClientOption func(c *client)
func WithContext(ctx context.Context) ClientOption {
return func(c *client) {
c.ctx = ctx
}
}
// WithGeneratedVaultClient creates a vault client with the given address.
//
// Deprecated: Use WithAddr instead for the same effect.
func WithGeneratedVaultClient(vaultAddress string) ClientOption {
return WithAddr(vaultAddress)
}
func WithAddr(addr string) ClientOption {
return func(c *client) {
c.config.Address = addr
}
}
func WithConfig(config *hashiVault.Config) ClientOption {
return func(c *client) {
c.config = config
}
}
func WithAppRoleAuth(roleID, secretID string) ClientOption {
return func(c *client) {
c.auth = func(v *hashiVault.Client) (*hashiVault.Secret, error) {
return appRoleLogin(v, roleID, secretID)
}
}
}
func WithUserPassAuth(username, password string) ClientOption {
return func(c *client) {
c.auth = func(v *hashiVault.Client) (*hashiVault.Secret, error) {
return userPassLogin(v, username, password)
}
}
}
func WithKvv2Mount(mount string) ClientOption {
return func(c *client) {
c.kvv2Mount = mount
}
}
func WithKubernetesAuthDefault() ClientOption {
return func(c *client) {
c.auth = func(v *hashiVault.Client) (*hashiVault.Secret, error) {
role := os.Getenv(envKubernetesRole)
if role == "" {
return nil, fmt.Errorf("%s environment variable not set", envKubernetesRole)
}
return kubernetesLogin(v, role, auth.WithServiceAccountTokenPath(kubernetesServiceAccountTokenPath))
}
}
}
func WithKubernetesAuthFromEnv() ClientOption {
return func(c *client) {
c.auth = func(v *hashiVault.Client) (*hashiVault.Secret, error) {
role := os.Getenv(envKubernetesRole)
if role == "" {
return nil, fmt.Errorf("%s environment variable not set", envKubernetesRole)
}
return kubernetesLogin(v, role, auth.WithServiceAccountTokenEnv(envKubernetesToken))
}
}
}
func WithKubernetesAuth(role, token string) ClientOption {
return func(c *client) {
c.auth = func(v *hashiVault.Client) (*hashiVault.Secret, error) {
return kubernetesLogin(v, role, auth.WithServiceAccountToken(token))
}
}
}