-
Notifications
You must be signed in to change notification settings - Fork 2
/
role_update.go
98 lines (77 loc) · 3.08 KB
/
role_update.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
package kubernetesauth
import (
"errors"
"path/filepath"
"reflect"
"github.com/youniqx/heist/pkg/httpclient"
"github.com/youniqx/heist/pkg/vault/core"
)
type createRoleRequest struct {
BoundServiceAccountNames []string `json:"bound_service_account_names"`
BoundServiceAccountNamespaces []string `json:"bound_service_account_namespaces"`
Policies []core.PolicyName `json:"policies"`
}
//nolint:cyclop
func (k *kubernetesAuthAPI) UpdateKubernetesAuthRole(method core.MountPathEntity, role RoleEntity) error {
log := k.Core.Log().WithValues("method", "UpdateKubernetesAuthRole")
path, err := method.GetMountPath()
if err != nil {
log.Info("failed to get mount path", "error", err)
return core.ErrAPIError.WithDetails("failed to get auth mount path").WithCause(err)
}
log = log.WithValues("path", path)
roleName, err := role.GetRoleName()
if err != nil {
log.Info("failed to get k8s role name", "error", err)
return core.ErrAPIError.WithDetails("failed to get k8s role name").WithCause(err)
}
log = log.WithValues("role", roleName)
rolePolicies, err := role.GetRolePolicies()
if err != nil {
log.Info("failed to get k8s role policies", "error", err)
return core.ErrAPIError.WithDetails("failed to get k8s role policies").WithCause(err)
}
log = log.WithValues("policies", rolePolicies)
boundNamespaces, err := role.GetBoundNamespaces()
if err != nil {
log.Info("failed to get k8s role bound namespaces", "error", err)
return core.ErrAPIError.WithDetails("failed to list bound namespaces").WithCause(err)
}
log = log.WithValues("boundNamespaces", boundNamespaces)
boundServiceAccounts, err := role.GetBoundServiceAccounts()
if err != nil {
log.Info("failed to get k8s role bound service accounts", "error", err)
return core.ErrAPIError.WithDetails("failed to list bound service accounts").WithCause(err)
}
log = log.WithValues("boundServiceAccounts", boundServiceAccounts)
desiredRole := &Role{
Name: roleName,
Policies: rolePolicies,
BoundNamespaces: boundNamespaces,
BoundServiceAccounts: boundServiceAccounts,
}
var updateRequired bool
switch currentRole, err := k.ReadKubernetesAuthRole(method, role); {
case errors.Is(err, core.ErrDoesNotExist):
updateRequired = true
case err == nil:
updateRequired = !reflect.DeepEqual(currentRole, desiredRole)
default:
log.Info("failed to check current state of the k8s role", "error", err)
return core.ErrAPIError.WithDetails("failed to check current state of k8s role").WithCause(err)
}
if !updateRequired {
return nil
}
rolePath := filepath.Join("/v1/auth", path, "role", roleName)
request := &createRoleRequest{
BoundServiceAccountNames: boundServiceAccounts,
BoundServiceAccountNamespaces: boundNamespaces,
Policies: rolePolicies,
}
if err := k.Core.MakeRequest(core.MethodPost, rolePath, httpclient.JSON(request), nil); err != nil {
log.Info("failed to create k8s auth role", "error", err)
return core.ErrAPIError.WithDetails("failed to create k8s auth role").WithCause(err)
}
return nil
}