-
Notifications
You must be signed in to change notification settings - Fork 19
/
wal.go
181 lines (155 loc) · 5 KB
/
wal.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package azuresecrets
import (
"context"
"errors"
"fmt"
"net/http"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/sdk/azcore"
multierror "github.com/hashicorp/go-multierror"
"github.com/hashicorp/vault/sdk/logical"
"github.com/mitchellh/mapstructure"
)
const (
walAppKey = "appCreate"
walRotateRootCreds = "rotateRootCreds"
walAppRoleAssignment = "appRoleAssign"
)
// Eventually expire the WAL if for some reason the rollback operation consistently fails
var maxWALAge = 24 * time.Hour
func (b *azureSecretBackend) walRollback(ctx context.Context, req *logical.Request, kind string, data interface{}) error {
switch kind {
case walAppKey:
return b.rollbackAppWAL(ctx, req, data)
case walRotateRootCreds:
return b.rollbackRootWAL(ctx, req, data)
case walAppRoleAssignment:
return b.rollbackRoleAssignWAL(ctx, req, data)
default:
return fmt.Errorf("unknown rollback type %q", kind)
}
}
type walApp struct {
AppID string
AppObjID string
Expiration time.Time
}
func (b *azureSecretBackend) rollbackAppWAL(ctx context.Context, req *logical.Request, data interface{}) error {
// Decode the WAL data
var entry walApp
d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: mapstructure.StringToTimeHookFunc(time.RFC3339),
Result: &entry,
})
if err != nil {
return err
}
err = d.Decode(data)
if err != nil {
return err
}
client, err := b.getClient(ctx, req.Storage)
if err != nil {
return err
}
b.Logger().Debug("rolling back SP", "appID", entry.AppID, "appObjID", entry.AppObjID)
// Attempt to delete the App. deleteApp doesn't return an error if the app isn't
// found, so no special handling is needed for that case. If we don't succeed within
// maxWALAge (e.g. client creds have changed and the delete will never succeed),
// unconditionally remove the WAL.
if err := client.deleteApp(ctx, entry.AppObjID, true); err != nil {
// Prevent noisy logs for non-existent or deleted out-of-band errors
respErr := new(azcore.ResponseError)
if errors.As(err, &respErr) && (respErr.StatusCode == http.StatusNoContent || respErr.StatusCode == http.StatusNotFound) {
b.Logger().Trace("app already deleted or does not exist", "err", err.Error())
} else {
b.Logger().Warn("rollback error deleting App", "err", err)
}
if time.Now().After(entry.Expiration) {
b.Logger().Warn("app WAL expired prior to rollback; resources may still exist")
return nil
}
return err
}
return nil
}
type walRotateRoot struct{}
func (b *azureSecretBackend) rollbackRootWAL(ctx context.Context, req *logical.Request, data interface{}) error {
b.Logger().Debug("rolling back config")
config, err := b.getConfig(ctx, req.Storage)
if err != nil {
return err
}
config.NewClientSecret = ""
config.NewClientSecretCreated = time.Time{}
config.NewClientSecretExpirationDate = time.Time{}
config.NewClientSecretKeyID = ""
err = b.saveConfig(ctx, config, req.Storage)
if err != nil {
return err
}
b.updatePassword = false
return nil
}
type walAppRoleAssign struct {
SpID string
AssignmentIDs []string
AzureRoles []*AzureRole
Expiration time.Time
}
func (b *azureSecretBackend) rollbackRoleAssignWAL(ctx context.Context, req *logical.Request, data interface{}) error {
// Decode the WAL data
var entry walAppRoleAssign
d, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
DecodeHook: mapstructure.StringToTimeHookFunc(time.RFC3339),
Result: &entry,
})
if err != nil {
return err
}
err = d.Decode(data)
if err != nil {
return err
}
client, err := b.getClient(ctx, req.Storage)
if err != nil {
return err
}
b.Logger().Debug("rolling back role assignments for service principal", "ID", entry.SpID)
// Return if there aren't any roles to unassign
if entry.AzureRoles == nil {
b.Logger().Error("no azure roles associated with role")
return nil
}
// Assemble all App Role Assignment IDs
var roleAssignments []string
for i, assignmentID := range entry.AssignmentIDs {
if entry.AzureRoles[i] == nil {
return fmt.Errorf("azure role was nil")
}
roleAssignments = append(roleAssignments, fmt.Sprintf("%s/providers/Microsoft.Authorization/roleAssignments/%s",
entry.AzureRoles[i].Scope,
assignmentID))
}
// Check any errors to filter out expected responses. Azure will return
// a 204 when trying to delete a role assignment that has already been
// deleted, or does not exist. We may hit this case during rollback.
if err := client.unassignRoles(ctx, roleAssignments); err != nil {
for _, e := range err.(*multierror.Error).Errors {
switch {
case strings.Contains(e.Error(), "StatusCode=204"):
b.Logger().Trace("role assignment already deleted or does not exist", "err", e.Error())
default:
return fmt.Errorf("rollback error unassinging role: %w", e)
}
}
if time.Now().After(entry.Expiration) {
b.Logger().Warn("role assignment WAL expired prior to rollback; resources may still exist")
return nil
}
}
return nil
}