-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
approle.go
211 lines (191 loc) · 6.68 KB
/
approle.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package approle
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/hashicorp/errwrap"
hclog "github.com/hashicorp/go-hclog"
"github.com/hashicorp/vault/api"
"github.com/hashicorp/vault/command/agent/auth"
"github.com/hashicorp/vault/sdk/helper/parseutil"
)
type approleMethod struct {
logger hclog.Logger
mountPath string
roleIDFilePath string
secretIDFilePath string
cachedRoleID string
cachedSecretID string
removeSecretIDFileAfterReading bool
secretIDResponseWrappingPath string
}
func NewApproleAuthMethod(conf *auth.AuthConfig) (auth.AuthMethod, error) {
if conf == nil {
return nil, errors.New("empty config")
}
if conf.Config == nil {
return nil, errors.New("empty config data")
}
a := &approleMethod{
logger: conf.Logger,
mountPath: conf.MountPath,
removeSecretIDFileAfterReading: true,
}
roleIDFilePathRaw, ok := conf.Config["role_id_file_path"]
if !ok {
return nil, errors.New("missing 'role_id_file_path' value")
}
a.roleIDFilePath, ok = roleIDFilePathRaw.(string)
if !ok {
return nil, errors.New("could not convert 'role_id_file_path' config value to string")
}
if a.roleIDFilePath == "" {
return nil, errors.New("'role_id_file_path' value is empty")
}
secretIDFilePathRaw, ok := conf.Config["secret_id_file_path"]
if ok {
a.secretIDFilePath, ok = secretIDFilePathRaw.(string)
if !ok {
return nil, errors.New("could not convert 'secret_id_file_path' config value to string")
}
if a.secretIDFilePath == "" {
return a, nil
}
removeSecretIDFileAfterReadingRaw, ok := conf.Config["remove_secret_id_file_after_reading"]
if ok {
removeSecretIDFileAfterReading, err := parseutil.ParseBool(removeSecretIDFileAfterReadingRaw)
if err != nil {
return nil, errwrap.Wrapf("error parsing 'remove_secret_id_file_after_reading' value: {{err}}", err)
}
a.removeSecretIDFileAfterReading = removeSecretIDFileAfterReading
}
secretIDResponseWrappingPathRaw, ok := conf.Config["secret_id_response_wrapping_path"]
if ok {
a.secretIDResponseWrappingPath, ok = secretIDResponseWrappingPathRaw.(string)
if !ok {
return nil, errors.New("could not convert 'secret_id_response_wrapping_path' config value to string")
}
if a.secretIDResponseWrappingPath == "" {
return nil, errors.New("'secret_id_response_wrapping_path' value is empty")
}
}
}
return a, nil
}
func (a *approleMethod) Authenticate(ctx context.Context, client *api.Client) (string, map[string]interface{}, error) {
if _, err := os.Stat(a.roleIDFilePath); err == nil {
roleID, err := ioutil.ReadFile(a.roleIDFilePath)
if err != nil {
if a.cachedRoleID == "" {
return "", nil, errwrap.Wrapf("error reading role ID file and no cached role ID known: {{err}}", err)
}
a.logger.Warn("error reading role ID file", "error", err)
}
if len(roleID) == 0 {
if a.cachedRoleID == "" {
return "", nil, errors.New("role ID file empty and no cached role ID known")
}
a.logger.Warn("role ID file exists but read empty value, re-using cached value")
} else {
a.cachedRoleID = strings.TrimSpace(string(roleID))
}
}
if a.cachedRoleID == "" {
return "", nil, errors.New("no known role ID")
}
if a.secretIDFilePath == "" {
return fmt.Sprintf("%s/login", a.mountPath), map[string]interface{}{
"role_id": a.cachedRoleID,
}, nil
}
if _, err := os.Stat(a.secretIDFilePath); err == nil {
secretID, err := ioutil.ReadFile(a.secretIDFilePath)
if err != nil {
if a.cachedSecretID == "" {
return "", nil, errwrap.Wrapf("error reading secret ID file and no cached secret ID known: {{err}}", err)
}
a.logger.Warn("error reading secret ID file", "error", err)
}
if len(secretID) == 0 {
if a.cachedSecretID == "" {
return "", nil, errors.New("secret ID file empty and no cached secret ID known")
}
a.logger.Warn("secret ID file exists but read empty value, re-using cached value")
} else {
stringSecretID := strings.TrimSpace(string(secretID))
if a.secretIDResponseWrappingPath != "" {
clonedClient, err := client.Clone()
if err != nil {
return "", nil, errwrap.Wrapf("error cloning client to unwrap secret ID: {{err}}", err)
}
clonedClient.SetToken(stringSecretID)
// Validate the creation path
resp, err := clonedClient.Logical().Read("sys/wrapping/lookup")
if err != nil {
return "", nil, errwrap.Wrapf("error looking up wrapped secret ID: {{err}}", err)
}
if resp == nil {
return "", nil, errors.New("response nil when looking up wrapped secret ID")
}
if resp.Data == nil {
return "", nil, errors.New("data in response nil when looking up wrapped secret ID")
}
creationPathRaw, ok := resp.Data["creation_path"]
if !ok {
return "", nil, errors.New("creation_path in response nil when looking up wrapped secret ID")
}
creationPath, ok := creationPathRaw.(string)
if !ok {
return "", nil, errors.New("creation_path in response could not be parsed as string when looking up wrapped secret ID")
}
if creationPath != a.secretIDResponseWrappingPath {
a.logger.Error("SECURITY: unable to validate wrapping token creation path", "expected", a.secretIDResponseWrappingPath, "found", creationPath)
return "", nil, errors.New("unable to validate wrapping token creation path")
}
// Now get the secret ID
resp, err = clonedClient.Logical().Unwrap("")
if err != nil {
return "", nil, errwrap.Wrapf("error unwrapping secret ID: {{err}}", err)
}
if resp == nil {
return "", nil, errors.New("response nil when unwrapping secret ID")
}
if resp.Data == nil {
return "", nil, errors.New("data in response nil when unwrapping secret ID")
}
secretIDRaw, ok := resp.Data["secret_id"]
if !ok {
return "", nil, errors.New("secret_id in response nil when unwrapping secret ID")
}
secretID, ok := secretIDRaw.(string)
if !ok {
return "", nil, errors.New("secret_id in response could not be parsed as string when unwrapping secret ID")
}
stringSecretID = secretID
}
a.cachedSecretID = stringSecretID
if a.removeSecretIDFileAfterReading {
if err := os.Remove(a.secretIDFilePath); err != nil {
a.logger.Error("error removing secret ID file after reading", "error", err)
}
}
}
}
if a.cachedSecretID == "" {
return "", nil, errors.New("no known secret ID")
}
return fmt.Sprintf("%s/login", a.mountPath), map[string]interface{}{
"role_id": a.cachedRoleID,
"secret_id": a.cachedSecretID,
}, nil
}
func (a *approleMethod) NewCreds() chan struct{} {
return nil
}
func (a *approleMethod) CredSuccess() {
}
func (a *approleMethod) Shutdown() {
}