-
Notifications
You must be signed in to change notification settings - Fork 0
/
rbac_endpoint_permission.go
174 lines (153 loc) · 4.95 KB
/
rbac_endpoint_permission.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
package types
import (
"context"
"errors"
"fmt"
"strings"
"github.com/rainest/decklib/pkg/crud"
"github.com/rainest/decklib/pkg/state"
"github.com/kong/go-kong/kong"
)
// rbacEndpointPermissionCRUD implements crud.Actions interface.
type rbacEndpointPermissionCRUD struct {
client *kong.Client
}
func rbacEndpointPermissionFromStruct(arg crud.Event) *state.RBACEndpointPermission {
ep, ok := arg.Obj.(*state.RBACEndpointPermission)
if !ok {
panic("unexpected type, expected *state.RBACEndpointPermission")
}
return ep
}
// Create creates a RBACEndpointPermission in Kong.
// The arg should be of type crud.Event, containing the ep to be created,
// else the function will panic.
// It returns a the created *state.RBACEndpointPermission.
func (s *rbacEndpointPermissionCRUD) Create(ctx context.Context, arg ...crud.Arg) (crud.Arg, error) {
event := crud.EventFromArg(arg[0])
ep := rbacEndpointPermissionFromStruct(event)
createdRBACEndpointPermission, err := s.client.RBACEndpointPermissions.Create(ctx, &ep.RBACEndpointPermission)
if err != nil {
return nil, err
}
return &state.RBACEndpointPermission{RBACEndpointPermission: *createdRBACEndpointPermission}, nil
}
// Delete deletes a RBACEndpointPermission in Kong.
// The arg should be of type crud.Event, containing the ep to be deleted,
// else the function will panic.
// It returns a the deleted *state.RBACEndpointPermission.
func (s *rbacEndpointPermissionCRUD) Delete(ctx context.Context, arg ...crud.Arg) (crud.Arg, error) {
event := crud.EventFromArg(arg[0])
ep := rbacEndpointPermissionFromStruct(event)
// for DELETE calls, the endpoint is passed in the URL only
// including the leading slash results in a URL like
// /rbac/roles/ROLEID/endpoints/workspace//foo/
// Kong expects a URL like
// /rbac/roles/ROLEID/endpoints/workspace/foo/
// so we strip this before passing it to go-kong
trimmed := strings.TrimLeft(*ep.Endpoint, "/")
err := s.client.RBACEndpointPermissions.Delete(ctx, ep.Role.ID, ep.Workspace, &trimmed)
if err != nil {
return nil, err
}
return ep, nil
}
// Update updates a RBACEndpointPermission in Kong.
// The arg should be of type crud.Event, containing the ep to be updated,
// else the function will panic.
// It returns a the updated *state.RBACEndpointPermission.
func (s *rbacEndpointPermissionCRUD) Update(ctx context.Context, arg ...crud.Arg) (crud.Arg, error) {
event := crud.EventFromArg(arg[0])
ep := rbacEndpointPermissionFromStruct(event)
updatedRBACEndpointPermission, err := s.client.RBACEndpointPermissions.Update(ctx, &ep.RBACEndpointPermission)
if err != nil {
return nil, err
}
return &state.RBACEndpointPermission{RBACEndpointPermission: *updatedRBACEndpointPermission}, nil
}
func (d *rbacEndpointPermissionDiffer) Deletes(handler func(crud.Event) error) error {
currentRBACEndpointPermissions, err := d.currentState.RBACEndpointPermissions.GetAll()
if err != nil {
return fmt.Errorf("error fetching eps from state: %w", err)
}
for _, ep := range currentRBACEndpointPermissions {
n, err := d.deleteRBACEndpointPermission(ep)
if err != nil {
return err
}
if n != nil {
err = handler(*n)
if err != nil {
return err
}
}
}
return nil
}
type rbacEndpointPermissionDiffer struct {
kind crud.Kind
currentState, targetState *state.KongState
}
func (d *rbacEndpointPermissionDiffer) deleteRBACEndpointPermission(ep *state.RBACEndpointPermission) (
*crud.Event, error,
) {
_, err := d.targetState.RBACEndpointPermissions.Get(ep.FriendlyName())
if errors.Is(err, state.ErrNotFound) {
return &crud.Event{
Op: crud.Delete,
Kind: d.kind,
Obj: ep,
}, nil
}
if err != nil {
return nil, fmt.Errorf("looking up rbac ep %q: %w",
ep.ID, err)
}
return nil, nil
}
func (d *rbacEndpointPermissionDiffer) CreateAndUpdates(handler func(crud.Event) error) error {
targetRBACEndpointPermissions, err := d.targetState.RBACEndpointPermissions.GetAll()
if err != nil {
return fmt.Errorf("error fetching rbac eps from state: %w", err)
}
for _, ep := range targetRBACEndpointPermissions {
n, err := d.createUpdateRBACEndpointPermission(ep)
if err != nil {
return err
}
if n != nil {
err = handler(*n)
if err != nil {
return err
}
}
}
return nil
}
func (d *rbacEndpointPermissionDiffer) createUpdateRBACEndpointPermission(ep *state.RBACEndpointPermission) (
*crud.Event, error,
) {
epCopy := &state.RBACEndpointPermission{RBACEndpointPermission: *ep.DeepCopy()}
currentEp, err := d.currentState.RBACEndpointPermissions.Get(ep.FriendlyName())
if errors.Is(err, state.ErrNotFound) {
return &crud.Event{
Op: crud.Create,
Kind: d.kind,
Obj: epCopy,
}, nil
}
if err != nil {
return nil, fmt.Errorf("error looking up rbac endpoint permission %q: %w",
ep.FriendlyName(), err)
}
// found, check if update needed
if !currentEp.EqualWithOpts(epCopy, false, true, false) {
return &crud.Event{
Op: crud.Update,
Kind: d.kind,
Obj: epCopy,
OldObj: currentEp,
}, nil
}
return nil, nil
}