-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpath_config.go
287 lines (252 loc) · 8.21 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package openldap
import (
"context"
"errors"
"fmt"
"time"
"github.com/hashicorp/vault-plugin-secrets-openldap/client"
"github.com/hashicorp/vault/sdk/framework"
"github.com/hashicorp/vault/sdk/helper/ldaputil"
"github.com/hashicorp/vault/sdk/logical"
)
const (
configPath = "config"
defaultPasswordLength = 64
defaultSchema = client.SchemaOpenLDAP
defaultTLSVersion = "tls12"
defaultCtxTimeout = 1 * time.Minute
)
func (b *backend) pathConfig() []*framework.Path {
return []*framework.Path{
{
Pattern: configPath,
DisplayAttrs: &framework.DisplayAttributes{
OperationPrefix: operationPrefixLDAP,
},
Fields: b.configFields(),
Operations: map[logical.Operation]framework.OperationHandler{
logical.CreateOperation: &framework.PathOperation{
Callback: b.configCreateUpdateOperation,
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "configure",
},
},
logical.UpdateOperation: &framework.PathOperation{
Callback: b.configCreateUpdateOperation,
DisplayAttrs: &framework.DisplayAttributes{
OperationVerb: "configure",
},
},
logical.ReadOperation: &framework.PathOperation{
Callback: b.configReadOperation,
DisplayAttrs: &framework.DisplayAttributes{
OperationSuffix: "configuration",
},
},
logical.DeleteOperation: &framework.PathOperation{
Callback: b.configDeleteOperation,
DisplayAttrs: &framework.DisplayAttributes{
OperationSuffix: "configuration",
},
},
},
ExistenceCheck: b.pathConfigExistenceCheck,
HelpSynopsis: "Configure the LDAP secrets engine plugin.",
HelpDescription: "This path configures the LDAP secrets engine plugin. See the " +
"documentation for the plugin for a full list of accepted parameters.",
},
}
}
func (b *backend) pathConfigExistenceCheck(ctx context.Context, req *logical.Request, _ *framework.FieldData) (bool, error) {
entry, err := readConfig(ctx, req.Storage)
if err != nil {
return false, err
}
return entry != nil, nil
}
func (b *backend) configFields() map[string]*framework.FieldSchema {
fields := ldaputil.ConfigFields()
fields["ttl"] = &framework.FieldSchema{
Type: framework.TypeDurationSecond,
Description: "The default password time-to-live.",
}
fields["max_ttl"] = &framework.FieldSchema{
Type: framework.TypeDurationSecond,
Description: "The maximum password time-to-live.",
}
fields["schema"] = &framework.FieldSchema{
Type: framework.TypeString,
Default: defaultSchema,
Description: "The desired LDAP schema used when modifying user account passwords.",
}
fields["password_policy"] = &framework.FieldSchema{
Type: framework.TypeString,
Description: "Password policy to use to generate passwords",
}
fields["skip_static_role_import_rotation"] = &framework.FieldSchema{
Type: framework.TypeBool,
Description: "Whether to skip the 'import' rotation.",
}
// Deprecated
fields["length"] = &framework.FieldSchema{
Type: framework.TypeInt,
Description: "The desired length of passwords that Vault generates.",
Deprecated: true,
}
return fields
}
func (b *backend) configCreateUpdateOperation(ctx context.Context, req *logical.Request, fieldData *framework.FieldData) (*logical.Response, error) {
conf, err := readConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
if conf == nil {
conf = new(config)
conf.LDAP = new(client.Config)
}
// Use the existing ldap client config if it is set
var existing *ldaputil.ConfigEntry
if conf.LDAP != nil && conf.LDAP.ConfigEntry != nil {
existing = conf.LDAP.ConfigEntry
}
// Build and validate the ldap conf.
ldapConf, err := ldaputil.NewConfigEntry(existing, fieldData)
if err != nil {
return nil, err
}
if err := ldapConf.Validate(); err != nil {
return nil, err
}
rawPassLength, hasPassLen := fieldData.GetOk("length")
if rawPassLength == nil {
rawPassLength = 0 // Don't set to the default but keep this as the zero value so we know it hasn't been set
}
passLength := rawPassLength.(int)
schema := fieldData.Get("schema").(string)
_, schemaChanged := fieldData.Raw["schema"]
// if update operation and schema not updated in raw payload, keep existing schema
if existing != nil && !schemaChanged {
schema = conf.LDAP.Schema
}
if schema == "" {
return nil, errors.New("schema is required")
}
if !client.ValidSchema(schema) {
return nil, fmt.Errorf("the configured schema %s is not valid. Supported schemas: %s",
schema, client.SupportedSchemas())
}
// Set the userattr if given. Otherwise, set the default for creates.
if userAttrRaw, ok := fieldData.GetOk("userattr"); ok {
ldapConf.UserAttr = userAttrRaw.(string)
} else if req.Operation == logical.CreateOperation {
ldapConf.UserAttr = defaultUserAttr(schema)
}
passPolicy := fieldData.Get("password_policy").(string)
_, passPolicyChanged := fieldData.Raw["password_policy"]
// if update operation and password_policy not updated in raw payload, keep existing password_policy
if existing != nil && !passPolicyChanged {
passPolicy = conf.PasswordPolicy
}
if passPolicy != "" && hasPassLen {
// If both a password policy and a password length are set, we can't figure out what to do
return nil, fmt.Errorf("cannot set both 'password_policy' and 'length'")
}
staticSkip := fieldData.Get("skip_static_role_import_rotation").(bool)
if _, set := fieldData.Raw["skip_static_role_import_rotation"]; existing != nil && !set {
staticSkip = conf.SkipStaticRoleImportRotation // use existing value if not set
}
// Update config field values
conf.PasswordPolicy = passPolicy
conf.PasswordLength = passLength
conf.SkipStaticRoleImportRotation = staticSkip
conf.LDAP.ConfigEntry = ldapConf
conf.LDAP.Schema = schema
err = writeConfig(ctx, req.Storage, *conf)
if err != nil {
return nil, err
}
// Respond with a 204.
return nil, nil
}
// defaultUserAttr returns the default user attribute for the given
// schema or an empty string if the schema is unknown.
func defaultUserAttr(schema string) string {
switch schema {
case client.SchemaAD:
return "userPrincipalName"
case client.SchemaRACF:
return "racfid"
case client.SchemaOpenLDAP:
return "cn"
default:
return ""
}
}
func readConfig(ctx context.Context, storage logical.Storage) (*config, error) {
entry, err := storage.Get(ctx, configPath)
if err != nil {
return nil, err
}
if entry == nil {
return nil, nil
}
config := &config{}
if err := entry.DecodeJSON(config); err != nil {
return nil, err
}
return config, nil
}
func writeConfig(ctx context.Context, storage logical.Storage, config config) (err error) {
entry, err := logical.StorageEntryJSON(configPath, config)
if err != nil {
return err
}
err = storage.Put(ctx, entry)
if err != nil {
return err
}
return nil
}
func (b *backend) configReadOperation(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
config, err := readConfig(ctx, req.Storage)
if err != nil {
return nil, err
}
if config == nil {
return nil, nil
}
// "password" is intentionally not returned by this endpoint
configMap := config.LDAP.PasswordlessMap()
if config.PasswordLength > 0 {
configMap["length"] = config.PasswordLength
}
if config.PasswordPolicy != "" {
configMap["password_policy"] = config.PasswordPolicy
}
configMap["skip_static_role_import_rotation"] = config.SkipStaticRoleImportRotation
if !config.LDAP.LastBindPasswordRotation.IsZero() {
configMap["last_bind_password_rotation"] = config.LDAP.LastBindPasswordRotation
}
if config.LDAP.Schema != "" {
configMap["schema"] = config.LDAP.Schema
}
resp := &logical.Response{
Data: configMap,
}
return resp, nil
}
func (b *backend) configDeleteOperation(ctx context.Context, req *logical.Request, _ *framework.FieldData) (*logical.Response, error) {
if err := req.Storage.Delete(ctx, configPath); err != nil {
return nil, err
}
return nil, nil
}
type config struct {
LDAP *client.Config
PasswordPolicy string `json:"password_policy,omitempty"`
SkipStaticRoleImportRotation bool `json:"skip_static_role_import_rotation"`
// Deprecated
PasswordLength int `json:"length,omitempty"`
}