|
| 1 | +// Copyright (c) HashiCorp, Inc. |
| 2 | +// SPDX-License-Identifier: BUSL-2.0 |
| 3 | + |
| 4 | +package ldap |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + |
| 9 | + "github.com/go-ldap/ldap/v3" |
| 10 | + |
| 11 | + "github.com/hashicorp/vault/sdk/helper/base62" |
| 12 | + "github.com/hashicorp/vault/sdk/helper/ldaputil" |
| 13 | + |
| 14 | + "github.com/hashicorp/vault/sdk/framework" |
| 15 | + "github.com/hashicorp/vault/sdk/logical" |
| 16 | +) |
| 17 | + |
| 18 | +func pathConfigRotateRoot(b *backend) *framework.Path { |
| 19 | + return &framework.Path{ |
| 20 | + Pattern: "config/rotate-root", |
| 21 | + |
| 22 | + DisplayAttrs: &framework.DisplayAttributes{ |
| 23 | + OperationPrefix: operationPrefixLDAP, |
| 24 | + OperationVerb: "rotate", |
| 25 | + OperationSuffix: "root-credentials", |
| 26 | + }, |
| 27 | + |
| 28 | + Operations: map[logical.Operation]framework.OperationHandler{ |
| 29 | + logical.UpdateOperation: &framework.PathOperation{ |
| 30 | + Callback: b.pathConfigRotateRootUpdate, |
| 31 | + ForwardPerformanceSecondary: true, |
| 32 | + ForwardPerformanceStandby: true, |
| 33 | + }, |
| 34 | + }, |
| 35 | + |
| 36 | + HelpSynopsis: pathConfigRotateRootHelpSyn, |
| 37 | + HelpDescription: pathConfigRotateRootHelpDesc, |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +func (b *backend) pathConfigRotateRootUpdate(ctx context.Context, req *logical.Request, data *framework.FieldData) (*logical.Response, error) { |
| 42 | + // lock the backend's state - really just the config state - for mutating |
| 43 | + b.mu.Lock() |
| 44 | + defer b.mu.Unlock() |
| 45 | + |
| 46 | + cfg, err := b.Config(ctx, req) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + if cfg == nil { |
| 51 | + return logical.ErrorResponse("attempted to rotate root on an undefined config"), nil |
| 52 | + } |
| 53 | + |
| 54 | + u, p := cfg.BindDN, cfg.BindPassword |
| 55 | + if u == "" || p == "" { |
| 56 | + return logical.ErrorResponse("auth is not using authenticated search, no root to rotate"), nil |
| 57 | + } |
| 58 | + |
| 59 | + // grab our ldap client |
| 60 | + client := ldaputil.Client{ |
| 61 | + Logger: b.Logger(), |
| 62 | + LDAP: ldaputil.NewLDAP(), |
| 63 | + } |
| 64 | + |
| 65 | + conn, err := client.DialLDAP(cfg.ConfigEntry) |
| 66 | + if err != nil { |
| 67 | + return nil, err |
| 68 | + } |
| 69 | + |
| 70 | + err = conn.Bind(u, p) |
| 71 | + if err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + lreq := &ldap.ModifyRequest{ |
| 76 | + DN: cfg.BindDN, |
| 77 | + } |
| 78 | + |
| 79 | + var newPassword string |
| 80 | + if cfg.PasswordPolicy != "" { |
| 81 | + newPassword, err = b.System().GeneratePasswordFromPolicy(ctx, cfg.PasswordPolicy) |
| 82 | + } else { |
| 83 | + newPassword, err = base62.Random(defaultPasswordLength) |
| 84 | + } |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + lreq.Replace("userPassword", []string{newPassword}) |
| 90 | + |
| 91 | + err = conn.Modify(lreq) |
| 92 | + if err != nil { |
| 93 | + return nil, err |
| 94 | + } |
| 95 | + // update config with new password |
| 96 | + cfg.BindPassword = newPassword |
| 97 | + entry, err := logical.StorageEntryJSON("config", cfg) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + if err := req.Storage.Put(ctx, entry); err != nil { |
| 102 | + // we might have to roll-back the password here? |
| 103 | + return nil, err |
| 104 | + } |
| 105 | + |
| 106 | + return nil, nil |
| 107 | +} |
| 108 | + |
| 109 | +const pathConfigRotateRootHelpSyn = ` |
| 110 | +Request to rotate the LDAP credentials used by Vault |
| 111 | +` |
| 112 | + |
| 113 | +const pathConfigRotateRootHelpDesc = ` |
| 114 | +This path attempts to rotate the LDAP bindpass used by Vault for this mount. |
| 115 | +` |
0 commit comments