-
Notifications
You must be signed in to change notification settings - Fork 2
/
path_config.go
226 lines (196 loc) · 6.25 KB
/
path_config.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package ibmcloudauth
import (
"context"
"errors"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/logical"
)
func pathConfig(b *ibmCloudAuthBackend) *framework.Path {
return &framework.Path{
Pattern: "config",
Fields: map[string]*framework.FieldSchema{
apiKeyField: {
Type: framework.TypeString,
Description: "The administrator API key.",
},
accountIDField: {
Type: framework.TypeString,
Description: "The account ID.",
},
iamEndpointField: {
Type: framework.TypeString,
Description: "The custom or private IAM endpoint.",
},
userManagementEndpointField: {
Type: framework.TypeString,
Description: "The custom or private User Management endpoint.",
},
},
ExistenceCheck: b.pathConfigExistenceCheck,
Operations: map[logical.Operation]framework.OperationHandler{
logical.CreateOperation: &framework.PathOperation{
Callback: b.pathConfigWrite,
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.pathConfigWrite,
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.pathConfigRead,
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.pathConfigDelete,
},
},
HelpSynopsis: confHelpSyn,
HelpDescription: confHelpDesc,
}
}
type ibmCloudConfig struct {
APIKey string `json:"api_key"`
Account string `json:"account"`
IAMEndpoint string `json:"iam_endpoint"`
UserManagementEndpoint string `json:"user_management_endpoint"`
}
func (b *ibmCloudAuthBackend) config(ctx context.Context, s logical.Storage) (*ibmCloudConfig, error) {
entry, err := s.Get(ctx, "config")
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
config := new(ibmCloudConfig)
if err := entry.DecodeJSON(config); err != nil {
return nil, err
}
return config, nil
}
func (b *ibmCloudAuthBackend) pathConfigExistenceCheck(ctx context.Context, req *logical.Request, data *framework.FieldData) (bool, error) {
config, err := b.config(ctx, req.Storage)
if err != nil {
return false, err
}
return config != nil, nil
}
func (b *ibmCloudAuthBackend) pathConfigWrite(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
config, err := b.config(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
config = new(ibmCloudConfig)
}
apiKey, ok := data.GetOk(apiKeyField)
if ok {
config.APIKey = apiKey.(string)
} else {
return logical.ErrorResponse("the required field %s is missing", apiKeyField), nil
}
accountID, ok := data.GetOk(accountIDField)
if ok {
config.Account = accountID.(string)
} else {
return logical.ErrorResponse("the required field %s is missing", accountIDField), nil
}
iamEndpoint, ok := data.GetOk(iamEndpointField)
if ok {
config.IAMEndpoint = iamEndpoint.(string)
} else {
config.IAMEndpoint = iamEndpointFieldDefault
}
userMgmtEndpoint, ok := data.GetOk(userManagementEndpointField)
if ok {
config.UserManagementEndpoint = userMgmtEndpoint.(string)
} else {
config.UserManagementEndpoint = userMgmtEndpointDefault
}
entry, err := logical.StorageEntryJSON("config", config)
if err != nil {
return nil, err
}
if err := req.Storage.Put(ctx, entry); err != nil {
return nil, err
}
// Reset backend
b.reset()
return nil, nil
}
func (b *ibmCloudAuthBackend) pathConfigRead(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
config, err := b.config(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
return nil, nil
}
displayKey := config.APIKey
if displayKey != "" {
displayKey = redacted
}
if config.IAMEndpoint == "" {
config.IAMEndpoint = iamEndpointFieldDefault
}
if config.UserManagementEndpoint == "" {
config.IAMEndpoint = userMgmtEndpointDefault
}
resp := &logical.Response{
Data: map[string]interface{}{
apiKeyField: displayKey,
accountIDField: config.Account,
iamEndpointField: config.IAMEndpoint,
userManagementEndpointField: config.UserManagementEndpoint,
},
}
return resp, nil
}
func (b *ibmCloudAuthBackend) pathConfigDelete(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) {
err := req.Storage.Delete(ctx, "config")
if err == nil {
b.reset()
}
return nil, err
}
func (b *ibmCloudAuthBackend) verifyPluginIsConfigured(ctx context.Context, req *logical.Request) error {
// verify the plugin is configured
config, err := b.config(ctx, req.Storage)
if err != nil {
b.Logger().Error("failed to load configuration", "error", err)
return errors.New("no configuration was found")
}
if config == nil || config.APIKey == "" {
return errors.New("no API key was set in the configuration")
}
if config.Account == "" {
return errors.New("no account ID was set in the configuration")
}
return nil
}
func (b *ibmCloudAuthBackend) getConfig(ctx context.Context, s logical.Storage) (*ibmCloudConfig, *logical.Response) {
// verify the plugin is configured
config, err := b.config(ctx, s)
if err != nil {
b.Logger().Error("failed to load configuration", "error", err)
return nil, logical.ErrorResponse("no configuration was found")
}
if config == nil || config.APIKey == "" {
return nil, logical.ErrorResponse("no API key was set in the configuration")
}
if config.Account == "" {
return nil, logical.ErrorResponse("no account ID was set in the configuration")
}
if config.IAMEndpoint == "" {
config.IAMEndpoint = iamEndpointFieldDefault
}
if config.UserManagementEndpoint == "" {
config.UserManagementEndpoint = userMgmtEndpointDefault
}
return config, nil
}
const confHelpSyn = `Configures credentials and account used to query the IBM Cloud IAM API to verify authenticating accounts`
const confHelpDesc = `
The IBM Cloud auth plugin makes queries to the IBM Cloud IAM API to verify an account
attempting login. The configuration requires an API key and an account ID. The API key
specified should have sufficient permissions to check
access group membership for all access groups specified in the auth plugin's roles.
The account ID is used to scope the users allowed to authenticate to Vault. The users
must have access to the configured account.`