/
identity_groups.go
360 lines (301 loc) · 11.3 KB
/
identity_groups.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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
// Copyright © 2022 Banzai Cloud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package vault
import (
"fmt"
"log/slog"
"strings"
"emperror.dev/errors"
"github.com/hashicorp/vault/api"
"github.com/spf13/cast"
)
type group struct {
Name string `mapstructure:"name"`
Type string `mapstructure:"type"`
Policies []string `mapstructure:"policies"`
Metadata map[string]interface{} `mapstructure:"metadata"`
}
type groupAlias struct {
Name string `mapstructure:"name"`
MountPath string `mapstructure:"mountpath"`
Group string `mapstructure:"group"`
}
//
// Common functions.
func readVaultGroup(group string, client *api.Client) (secret *api.Secret, err error) {
secret, err = client.Logical().Read(fmt.Sprintf("identity/group/name/%s", group))
if err != nil {
return nil, errors.Wrapf(err, "failed to read group %s by name", group)
}
if secret == nil {
// No Data returned, Group does not exist
return nil, nil
}
return secret, nil
}
func readVaultGroupAlias(id string, client *api.Client) (secret *api.Secret, err error) {
secret, err = client.Logical().Read(fmt.Sprintf("identity/group-alias/id/%s", id))
if err != nil {
return nil, errors.Wrapf(err, "failed to read group alias by id %s", id)
}
if secret == nil {
// No Data returned, Group does not exist
return nil, nil
}
return secret, nil
}
func getVaultAuthMountAccessor(path string, client *api.Client) (accessor string, err error) {
path = strings.TrimRight(path, "/") + "/"
mounts, err := client.Sys().ListAuth()
if err != nil {
return "", errors.Wrapf(err, "failed to read auth mounts from vault")
}
if mounts[path] == nil {
return "", errors.Errorf("auth mount path %s does not exist in vault", path)
}
return mounts[path].Accessor, nil
}
func getVaultGroupID(group string, client *api.Client) (id string, err error) {
g, err := readVaultGroup(group, client)
if err != nil {
return "", errors.Wrapf(err, "error reading group %s", group)
}
if g == nil {
return "", errors.Errorf("group %s does not exist", group)
}
return g.Data["id"].(string), nil
}
func getVaultGroupAliasName(aliasID string, client *api.Client) (id string, err error) {
alias, err := readVaultGroupAlias(aliasID, client)
if err != nil {
return "", errors.Wrapf(err, "error reading group alias %s", aliasID)
}
if alias == nil {
return "", errors.Errorf("group alias %s does not exist", aliasID)
}
return alias.Data["name"].(string), nil
}
func getVaultGroupAliasMount(aliasID string, client *api.Client) (id string, err error) {
alias, err := readVaultGroupAlias(aliasID, client)
if err != nil {
return "", errors.Wrapf(err, "error reading group alias %s", aliasID)
}
if alias == nil {
return "", errors.Errorf("group alias %s does not exist", aliasID)
}
return alias.Data["mount_accessor"].(string), nil
}
func findVaultGroupAliasIDFromNameAndMount(name string, accessor string, client *api.Client) (id string, err error) {
aliases, err := client.Logical().List("identity/group-alias/id")
if err != nil {
return "", errors.Wrap(err, "error listing group aliases")
}
if aliases == nil {
return "", nil
}
for _, alias := range aliases.Data["keys"].([]interface{}) {
aliasName, err := getVaultGroupAliasName(cast.ToString(alias), client)
if err != nil {
return "", errors.Wrapf(err, "error fetching name for alias id: %s err", alias)
}
aliasMount, err := getVaultGroupAliasMount(cast.ToString(alias), client)
if err != nil {
return "", errors.Wrapf(err, "error fetching mount for alias id: %s err", alias)
}
if aliasName == name && aliasMount == accessor {
return cast.ToString(alias), nil
}
}
// Did not find any alias matching Name and MountPath
return "", nil
}
//
// Groups.
func (v *vault) getExistingGroups() (map[string]bool, error) {
existinGroups := make(map[string]bool)
existingGroupsList, err := v.cl.Logical().ReadWithData("identity/group/name", map[string][]string{"list": {"true"}})
if err != nil {
return nil, errors.Wrapf(err, "failed to retrieve list of groups")
}
if existingGroupsList == nil {
slog.Debug("vault has no groups")
return nil, nil
}
existingGroupsNames := cast.ToStringSlice(existingGroupsList.Data["keys"])
for _, existingGroupName := range existingGroupsNames {
existinGroups[existingGroupName] = true
}
return existinGroups, nil
}
func getUnmanagedGroups(existingGroups map[string]bool, managedGroups []group) map[string]bool {
for _, managedGroup := range managedGroups {
delete(existingGroups, managedGroup.Name)
}
return existingGroups
}
func (v *vault) addManagedGroups(managedGroups []group) error {
for _, group := range managedGroups {
g, err := readVaultGroup(group.Name, v.cl)
if err != nil {
return errors.Wrap(err, "error reading group")
}
// Currently does not support specifying members directly in the group config
// Use group aliases for that
if group.Type != "external" {
return errors.Errorf("only external groups are supported for now")
}
config := map[string]interface{}{
"name": group.Name,
"type": group.Type,
"policies": group.Policies,
"metadata": group.Metadata,
}
if g == nil {
slog.Info(fmt.Sprintf("adding group %s", group.Name))
_, err = v.writeWithWarningCheck("identity/group", config)
if err != nil {
return errors.Wrapf(err, "failed to create group %s", group.Name)
}
} else {
slog.Info(fmt.Sprintf("tuning already existing group: %s", group.Name))
_, err = v.writeWithWarningCheck(fmt.Sprintf("identity/group/name/%s", group.Name), config)
if err != nil {
return errors.Wrapf(err, "failed to tune group %s", group.Name)
}
}
}
return nil
}
func (v *vault) removeUnmanagedGroups(managedGroups []group) error {
if !v.externalConfig.PurgeUnmanagedConfig.Enabled || v.externalConfig.PurgeUnmanagedConfig.Exclude.Groups {
slog.Debug("purge config is disabled, no unmanaged groups will be removed")
return nil
}
existingGroups, err := v.getExistingGroups()
if err != nil {
return errors.Wrapf(err, "failed to get existing groups from vault")
}
unmanagedGroups := getUnmanagedGroups(existingGroups, managedGroups)
for unmanagedGroupName := range unmanagedGroups {
slog.Info(fmt.Sprintf("removing group %s", unmanagedGroupName))
_, err := v.cl.Logical().Delete("identity/group/name/" + unmanagedGroupName)
if err != nil {
return errors.Wrapf(err, "error removing group %s from vault", unmanagedGroupName)
}
}
return nil
}
//
// Group Aliases.
func (v *vault) addManagedGroupAliases(managedGroupAliases []groupAlias) error {
// Group Aliases for External Groups might require to have the same Name when on different Mount/Path combinations
// external groups can only have ONE alias so we need to make sure not to overwrite any
for _, groupAlias := range managedGroupAliases {
accessor, err := getVaultAuthMountAccessor(groupAlias.MountPath, v.cl)
if err != nil {
return errors.Wrapf(err, "error getting mount accessor for %s", groupAlias.MountPath)
}
id, err := getVaultGroupID(groupAlias.Group, v.cl)
if err != nil {
return errors.Wrapf(err, "error getting canonical_id for group %s", groupAlias.Group)
}
config := map[string]interface{}{
"name": groupAlias.Name,
"mount_accessor": accessor,
"canonical_id": id,
}
// Find a matching alias for NAME and MOUNT
ga, err := findVaultGroupAliasIDFromNameAndMount(groupAlias.Name, accessor, v.cl)
if err != nil {
return errors.Wrapf(err, "error finding group-alias %s", groupAlias.Name)
}
if ga == "" {
slog.Info(fmt.Sprintf("adding group-alias: %s@%s", groupAlias.Name, accessor))
_, err = v.writeWithWarningCheck("identity/group-alias", config)
if err != nil {
return errors.Wrapf(err, "failed to create group-alias %s", groupAlias.Name)
}
} else {
slog.Info(fmt.Sprintf("tuning already existing group-alias: %s@%s - ID: %s", groupAlias.Name, accessor, ga))
_, err = v.writeWithWarningCheck(fmt.Sprintf("identity/group-alias/id/%s", ga), config)
if err != nil {
return errors.Wrapf(err, "failed to tune group-alias %s", ga)
}
}
}
return nil
}
func (v *vault) getExistingGroupAliases() (map[string]string, error) {
existingGroupAliases := make(map[string]string)
existingGroupAliasesRaw, err := v.cl.Logical().ReadWithData(
"identity/group-alias/id", map[string][]string{"list": {"true"}})
if err != nil {
return nil, errors.Wrapf(err, "failed to retrieve list of group-alias")
}
if existingGroupAliasesRaw == nil {
slog.Debug("vault has no group-aliases")
return nil, nil
}
existingGroupAliasesData := cast.ToStringMap(existingGroupAliasesRaw.Data["key_info"])
for existingGroupAliasID, existingGroupAliasRaw := range existingGroupAliasesData {
existingGroupAlias := cast.ToStringMapString(existingGroupAliasRaw)
existingGroupAliases[existingGroupAlias["name"]] = existingGroupAliasID
}
return existingGroupAliases, nil
}
func getUnmanagedGroupAliases(existingGroupAliases map[string]string, managedGroupAliases []groupAlias) map[string]string {
for _, managedGroupAlias := range managedGroupAliases {
delete(existingGroupAliases, managedGroupAlias.Name)
}
return existingGroupAliases
}
func (v *vault) removeUnmanagedGroupAliases(managedGroupAliases []groupAlias) error {
if !v.externalConfig.PurgeUnmanagedConfig.Enabled || v.externalConfig.PurgeUnmanagedConfig.Exclude.GroupAliases {
slog.Debug("purge config is disabled, no unmanaged group-alias will be removed")
return nil
}
existingGroupAliases, err := v.getExistingGroupAliases()
if err != nil {
return errors.Wrapf(err, "failed to get existing group-alias from vault")
}
unmanagedGroupAliases := getUnmanagedGroupAliases(existingGroupAliases, managedGroupAliases)
slog.Info(fmt.Sprintf("removing group-aliases ... %T", unmanagedGroupAliases))
for unmanagedGroupAliasName, unmanagedGroupAliasID := range unmanagedGroupAliases {
_, err := v.cl.Logical().Delete("identity/group-alias/id/" + unmanagedGroupAliasID)
if err != nil {
return errors.Wrapf(err, "error removing group-alias %s with ID %s from vault",
unmanagedGroupAliasName, unmanagedGroupAliasID)
}
}
return nil
}
//
// Configure groups and group-aliases.
func (v *vault) configureIdentityGroups() error {
managedGroups := v.externalConfig.Groups
managedGroupAliases := v.externalConfig.GroupAliases
if err := v.addManagedGroups(managedGroups); err != nil {
return errors.Wrap(err, "error while adding groups")
}
if err := v.addManagedGroupAliases(managedGroupAliases); err != nil {
return errors.Wrap(err, "error while adding groups aliases")
}
if err := v.removeUnmanagedGroups(managedGroups); err != nil {
return errors.Wrap(err, "error while removing groups")
}
if err := v.removeUnmanagedGroupAliases(managedGroupAliases); err != nil {
return errors.Wrap(err, "error while removing group aliases")
}
return nil
}