-
Notifications
You must be signed in to change notification settings - Fork 0
/
mtlsauth.go
170 lines (151 loc) · 4.31 KB
/
mtlsauth.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
package types
import (
"context"
"errors"
"fmt"
"github.com/rainest/decklib/pkg/crud"
"github.com/rainest/decklib/pkg/state"
"github.com/rainest/decklib/pkg/utils"
"github.com/kong/go-kong/kong"
)
// mtlsAuthCRUD implements crud.Actions interface.
type mtlsAuthCRUD struct {
client *kong.Client
}
func mtlsAuthFromStruct(arg crud.Event) *state.MTLSAuth {
mtlsAuth, ok := arg.Obj.(*state.MTLSAuth)
if !ok {
panic("unexpected type, expected *state.MTLSAuth")
}
return mtlsAuth
}
// Create creates an mtls-auth credential in Kong.
// The arg should be of type crud.Event, containing the mtlsAuth to be created,
// else the function will panic.
// It returns a the created *state.Route.
func (s *mtlsAuthCRUD) Create(ctx context.Context, arg ...crud.Arg) (crud.Arg, error) {
event := crud.EventFromArg(arg[0])
mtlsAuth := mtlsAuthFromStruct(event)
createdMTLSAuth, err := s.client.MTLSAuths.Create(ctx, mtlsAuth.Consumer.ID,
&mtlsAuth.MTLSAuth)
if err != nil {
return nil, err
}
return &state.MTLSAuth{MTLSAuth: *createdMTLSAuth}, nil
}
// Delete deletes an mtls-auth credential in Kong.
// The arg should be of type crud.Event, containing the mtlsAuth to be deleted,
// else the function will panic.
// It returns a the deleted *state.Route.
func (s *mtlsAuthCRUD) Delete(ctx context.Context, arg ...crud.Arg) (crud.Arg, error) {
event := crud.EventFromArg(arg[0])
mtlsAuth := mtlsAuthFromStruct(event)
cid := ""
if !utils.Empty(mtlsAuth.Consumer.Username) {
cid = *mtlsAuth.Consumer.Username
}
if !utils.Empty(mtlsAuth.Consumer.ID) {
cid = *mtlsAuth.Consumer.ID
}
err := s.client.MTLSAuths.Delete(ctx, &cid, mtlsAuth.ID)
if err != nil {
return nil, err
}
return mtlsAuth, nil
}
// Update updates an mtls-auth credential in Kong.
// The arg should be of type crud.Event, containing the mtlsAuth to be updated,
// else the function will panic.
// It returns a the updated *state.Route.
func (s *mtlsAuthCRUD) Update(ctx context.Context, arg ...crud.Arg) (crud.Arg, error) {
event := crud.EventFromArg(arg[0])
mtlsAuth := mtlsAuthFromStruct(event)
updatedMTLSAuth, err := s.client.MTLSAuths.Create(ctx, mtlsAuth.Consumer.ID,
&mtlsAuth.MTLSAuth)
if err != nil {
return nil, err
}
return &state.MTLSAuth{MTLSAuth: *updatedMTLSAuth}, nil
}
type mtlsAuthDiffer struct {
kind crud.Kind
currentState, targetState *state.KongState
}
func (d *mtlsAuthDiffer) Deletes(handler func(crud.Event) error) error {
currentMTLSAuths, err := d.currentState.MTLSAuths.GetAll()
if err != nil {
return fmt.Errorf("error fetching mtls-auths from state: %w", err)
}
for _, mtlsAuth := range currentMTLSAuths {
n, err := d.deleteMTLSAuth(mtlsAuth)
if err != nil {
return err
}
if n != nil {
err = handler(*n)
if err != nil {
return err
}
}
}
return nil
}
func (d *mtlsAuthDiffer) deleteMTLSAuth(mtlsAuth *state.MTLSAuth) (*crud.Event, error) {
_, err := d.targetState.MTLSAuths.Get(*mtlsAuth.ID)
if errors.Is(err, state.ErrNotFound) {
return &crud.Event{
Op: crud.Delete,
Kind: d.kind,
Obj: mtlsAuth,
}, nil
}
if err != nil {
return nil, fmt.Errorf("looking up mtls-auth %q: %w", *mtlsAuth.ID, err)
}
return nil, nil
}
func (d *mtlsAuthDiffer) CreateAndUpdates(handler func(crud.Event) error) error {
targetMTLSAuths, err := d.targetState.MTLSAuths.GetAll()
if err != nil {
return fmt.Errorf("error fetching mtls-auths from state: %w", err)
}
for _, mtlsAuth := range targetMTLSAuths {
n, err := d.createUpdateMTLSAuth(mtlsAuth)
if err != nil {
return err
}
if n != nil {
err = handler(*n)
if err != nil {
return err
}
}
}
return nil
}
func (d *mtlsAuthDiffer) createUpdateMTLSAuth(mtlsAuth *state.MTLSAuth) (*crud.Event, error) {
mtlsAuth = &state.MTLSAuth{MTLSAuth: *mtlsAuth.DeepCopy()}
currentMTLSAuth, err := d.currentState.MTLSAuths.Get(*mtlsAuth.ID)
if errors.Is(err, state.ErrNotFound) {
// mtlsAuth not present, create it
return &crud.Event{
Op: crud.Create,
Kind: d.kind,
Obj: mtlsAuth,
}, nil
}
if err != nil {
return nil, fmt.Errorf("error looking up mtls-auth %q: %w",
*mtlsAuth.ID, err)
}
// found, check if update needed
if !currentMTLSAuth.EqualWithOpts(mtlsAuth, false, true, false) {
return &crud.Event{
Op: crud.Update,
Kind: d.kind,
Obj: mtlsAuth,
OldObj: currentMTLSAuth,
}, nil
}
return nil, nil
}