-
Notifications
You must be signed in to change notification settings - Fork 10
/
rollback.go
124 lines (102 loc) · 3.21 KB
/
rollback.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package plugin
import (
"context"
"errors"
"fmt"
"time"
"github.com/hashicorp/vault/sdk/logical"
"github.com/mitchellh/mapstructure"
)
const (
rotateCredentialWAL = "rotateCredentialWAL"
)
// rotateCredentialEntry is used to store information in a WAL that can retry a
// credential rotation in the event of partial failure.
type rotateCredentialEntry struct {
LastVaultRotation time.Time `json:"last_vault_rotation"`
LastPassword string `json:"last_password"`
CurrentPassword string `json:"current_password"`
RoleName string `json:"name"`
ServiceAccountName string `json:"service_account_name"`
TTL int `json:"ttl"`
}
func (b *backend) walRollback(ctx context.Context, req *logical.Request, kind string, data interface{}) error {
switch kind {
case rotateCredentialWAL:
return b.handleRotateCredentialRollback(ctx, req.Storage, data)
default:
return fmt.Errorf("unknown WAL entry kind %q", kind)
}
}
func (b *backend) handleRotateCredentialRollback(ctx context.Context, storage logical.Storage, data interface{}) error {
var wal rotateCredentialEntry
if err := mapstructure.WeakDecode(data, &wal); err != nil {
return err
}
if wal.CurrentPassword == "" {
b.Logger().Warn("WAL does not contain a password for service account")
return nil
}
// Check creds for deltas. Exit if creds and WAL are the same.
path := fmt.Sprintf("%s/%s", storageKey, wal.RoleName)
credEntry, err := storage.Get(ctx, path)
if err == nil && credEntry != nil {
cred := make(map[string]interface{})
err := credEntry.DecodeJSON(&cred)
if err == nil && cred != nil {
currentPassword := cred["current_password"]
lastPassword := cred["last_password"]
if currentPassword == wal.CurrentPassword && lastPassword == wal.LastPassword {
return nil
}
}
}
role := &backendRole{
ServiceAccountName: wal.ServiceAccountName,
TTL: wal.TTL,
LastVaultRotation: wal.LastVaultRotation,
}
if err := b.writeRoleToStorage(ctx, storage, wal.RoleName, role); err != nil {
return err
}
// Cache the full role to minimize Vault storage calls.
b.roleCache.SetDefault(wal.RoleName, role)
conf, err := readConfig(ctx, storage)
if err != nil {
return err
}
if conf == nil {
return errors.New("the config is currently unset")
}
if err := b.client.UpdatePassword(conf.ADConf, role.ServiceAccountName, wal.CurrentPassword); err != nil {
return err
}
// Although a service account name is typically my_app@example.com,
// the username it uses is just my_app, or everything before the @.
username, err := getUsername(role.ServiceAccountName)
if err != nil {
return err
}
b.credLock.Lock()
defer b.credLock.Unlock()
cred := map[string]interface{}{
"username": username,
"current_password": wal.CurrentPassword,
}
if wal.LastPassword != "" {
cred["last_password"] = wal.LastPassword
}
// Cache and save the cred.
path = fmt.Sprintf("%s/%s", storageKey, wal.RoleName)
entry, err := logical.StorageEntryJSON(path, cred)
if err != nil {
return err
}
if err := storage.Put(ctx, entry); err != nil {
return err
}
b.credCache.SetDefault(wal.RoleName, cred)
return nil
}