forked from mch1307/vaultlib
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathauth.go
136 lines (110 loc) · 3.25 KB
/
auth.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
package vaultlib
import (
"encoding/json"
"fmt"
"time"
"github.com/pkg/errors"
)
// vaultAuth holds the Vault Auth response from server
type vaultAuth struct {
ClientToken string `json:"client_token"`
Accessor string `json:"accessor"`
Policies []string `json:"policies"`
Metadata struct {
RoleName string `json:"role_name"`
} `json:"metadata"`
LeaseDuration int `json:"lease_duration"`
Renewable bool `json:"renewable"`
EntityID string `json:"entity_id"`
}
// renew the client's token, launched at client creation time as a go routine
func (c *Client) renewToken(renewDeadline int) {
var vaultData vaultAuth
jsonToken := make(map[string]string)
for {
timeToRenewal := 10 * time.Second
if renewDeadline > 20 {
timeToRenewal = time.Second * time.Duration(renewDeadline / 2)
}
time.Sleep(timeToRenewal)
url := c.address.String() + "/v1/auth/token/renew-self"
req, _ := c.newRequest("POST", url)
// Sending a payload (even empty) is required for vault to respond with the `auth` param
_ = req.setJSONBody(jsonToken)
resp, err := req.execute()
if err != nil {
c.setStatus("Error renewing token " + err.Error())
continue
}
jsonErr := json.Unmarshal([]byte(resp.Auth), &vaultData)
if jsonErr != nil {
c.setStatus("Error renewing token " + jsonErr.Error())
continue
}
if err := c.setTokenInfo(); err != nil {
c.setStatus("Error renewing token " + err.Error())
continue
}
c.setStatus("token renewed")
renewDeadline = vaultData.LeaseDuration
}
}
// setTokenFromAppRole get the token from Vault and set it in the client
func (c *Client) setTokenFromAppRole() error {
var vaultData vaultAuth
mp := "approle"
if c.appRoleCredentials.MountPoint != "" {
mp = c.appRoleCredentials.MountPoint
}
if c.appRoleCredentials.RoleID == "" || c.appRoleCredentials.SecretID == "" {
return errors.New("No credentials provided")
}
url := fmt.Sprintf("%s/v1/auth/%s/login", c.address.String(), mp)
req, _ := c.newRequest("POST", url)
_ = req.setJSONBody(c.appRoleCredentials)
resp, err := req.execute()
if err != nil {
return errors.Wrap(errors.WithStack(err), errInfo())
}
jsonErr := json.Unmarshal([]byte(resp.Auth), &vaultData)
if jsonErr != nil {
return errors.Wrap(errors.WithStack(err), errInfo())
}
c.withLockContext(func() {
c.token.ID = vaultData.ClientToken
})
if err = c.setTokenInfo(); err != nil {
return errors.Wrap(errors.WithStack(err), errInfo())
}
if c.token.Renewable {
go c.renewToken(vaultData.LeaseDuration)
}
return nil
}
// vaultSecretKV2 holds the Vault secret (kv v2)
type vaultSecretKV2 struct {
Data map[string]interface{} `json:"data"`
Metadata struct {
CreatedTime time.Time `json:"created_time"`
DeletionTime string `json:"deletion_time"`
Destroyed bool `json:"destroyed"`
Version int `json:"version"`
} `json:"metadata"`
}
func (c *Client) setTokenInfo() error {
url := c.address.String() + "/v1/auth/token/lookup-self"
var tokenInfo VaultTokenInfo
req, _ := c.newRequest("GET", url)
res, err := req.execute()
if err != nil {
return err
}
if err := json.Unmarshal(res.Data, &tokenInfo); err != nil {
return err
}
c.withLockContext(func() {
c.token = &tokenInfo
c.isAuthenticated = true
})
return nil
}