forked from mch1307/vaultlib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
168 lines (145 loc) · 3.82 KB
/
client.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
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
//Package vaultlib is a lightweight Go library for reading Vault KV secrets.
//Interacts with Vault server using HTTP API only.
//
//First create a new *config object using NewConfig().
//
//Then create you Vault client using NewClient(*config).
//
// See Also
//
// https://github.com/aquarapid/vaultlib#vaultlib
package vaultlib
import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net/http"
"net/url"
"sync"
"time"
"github.com/hashicorp/go-cleanhttp"
)
// Client holds the vault client
type Client struct {
sync.RWMutex
address *url.URL
httpClient *http.Client
appRoleCredentials *AppRoleCredentials
token *VaultTokenInfo
status string
isAuthenticated bool
}
// VaultTokenInfo holds the Vault token information
type VaultTokenInfo struct {
Accessor string `json:"accessor"`
CreationTime int `json:"creation_time"`
CreationTTL int `json:"creation_ttl"`
DisplayName string `json:"display_name"`
EntityID string `json:"entity_id"`
ExpireTime interface{} `json:"expire_time"`
ExplicitMaxTTL int `json:"explicit_max_ttl"`
ID string `json:"id"`
IssueTime time.Time `json:"issue_time"`
Meta interface{} `json:"meta"`
NumUses int `json:"num_uses"`
Orphan bool `json:"orphan"`
Path string `json:"path"`
Policies []string `json:"policies"`
Renewable bool `json:"renewable"`
TTL int `json:"ttl"`
Type string `json:"type"`
}
// NewClient returns a new client based on the provided config
func NewClient(c *Config) (*Client, error) {
var caPool *x509.CertPool
// If no config provided, use a new one based on default values and env vars
if c == nil {
c = NewConfig()
}
var cli Client
cli.status = "New"
cli.appRoleCredentials = new(AppRoleCredentials)
cli.appRoleCredentials.RoleID = c.AppRoleCredentials.RoleID
cli.appRoleCredentials.SecretID = c.AppRoleCredentials.SecretID
u, err := url.Parse(c.Address)
if err != nil {
return nil, err
}
cli.address = u
if c.CACert != "" {
caCert, err := ioutil.ReadFile(c.CACert)
if err != nil {
c.CACert = ""
}
caPool = x509.NewCertPool()
caPool.AppendCertsFromPEM(caCert)
}
cli.httpClient = &http.Client{}
tsp := cleanhttp.DefaultPooledTransport()
tsp.TLSClientConfig = &tls.Config{
RootCAs: caPool,
MinVersion: tls.VersionTLS12,
InsecureSkipVerify: c.InsecureSSL,
}
cli.httpClient.Transport = tsp
cli.token = new(VaultTokenInfo)
cli.token.ID = c.Token
if cli.token.ID == "" {
err = cli.setTokenFromAppRole()
if err != nil {
cli.status = "Authentication Error: " + err.Error()
return &cli, err
}
} else {
if err = cli.setTokenInfo(); err != nil {
cli.status = "Authentication Error: " + err.Error()
return &cli, err
}
if cli.token.Renewable {
go cli.renewToken(cli.token.TTL)
}
}
cli.status = "Token ready"
return &cli, nil
}
func (c *Client) getTokenID() string {
var tk string
c.withLockContext(func() {
tk = c.token.ID
})
return tk
}
// GetTokenInfo returns the current token information
func (c *Client) GetTokenInfo() *VaultTokenInfo {
vt := new(VaultTokenInfo)
c.withLockContext(func() {
vt = c.token
})
return vt
}
func (c *Client) setStatus(status string) {
c.withLockContext(func() {
c.status = status
})
}
// GetStatus return the last action status/log
func (c *Client) GetStatus() string {
var status string
c.withLockContext(func() {
status = c.status
})
return status
}
// IsAuthenticated returns bool if last call to vault was ok
func (c *Client) IsAuthenticated() bool {
authOK := false
c.withLockContext(func() {
authOK = c.isAuthenticated
})
return authOK
}
func (c *Client) withLockContext(fn func()) {
c.Lock()
defer c.Unlock()
fn()
}