-
Notifications
You must be signed in to change notification settings - Fork 0
/
basicauth.go
199 lines (179 loc) · 5.05 KB
/
basicauth.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
package types
import (
"context"
"errors"
"fmt"
"sync"
"github.com/rainest/decklib/pkg/cprint"
"github.com/rainest/decklib/pkg/crud"
"github.com/rainest/decklib/pkg/state"
"github.com/rainest/decklib/pkg/utils"
"github.com/kong/go-kong/kong"
)
// basicAuthCRUD implements crud.Actions interface.
type basicAuthCRUD struct {
client *kong.Client
}
func basicAuthFromStruct(arg crud.Event) *state.BasicAuth {
basicAuth, ok := arg.Obj.(*state.BasicAuth)
if !ok {
panic("unexpected type, expected *state.BasicAuth")
}
return basicAuth
}
// Create creates a Route in Kong.
// The arg should be of type crud.Event, containing the basicAuth to be created,
// else the function will panic.
// It returns a the created *state.Route.
func (s *basicAuthCRUD) Create(ctx context.Context, arg ...crud.Arg) (crud.Arg, error) {
event := crud.EventFromArg(arg[0])
basicAuth := basicAuthFromStruct(event)
cid := ""
if !utils.Empty(basicAuth.Consumer.Username) {
cid = *basicAuth.Consumer.Username
}
if !utils.Empty(basicAuth.Consumer.ID) {
cid = *basicAuth.Consumer.ID
}
createdBasicAuth, err := s.client.BasicAuths.Create(ctx, &cid,
&basicAuth.BasicAuth)
if err != nil {
return nil, err
}
return &state.BasicAuth{BasicAuth: *createdBasicAuth}, nil
}
// Delete deletes a Route in Kong.
// The arg should be of type crud.Event, containing the basicAuth to be deleted,
// else the function will panic.
// It returns a the deleted *state.Route.
func (s *basicAuthCRUD) Delete(ctx context.Context, arg ...crud.Arg) (crud.Arg, error) {
event := crud.EventFromArg(arg[0])
basicAuth := basicAuthFromStruct(event)
cid := ""
if !utils.Empty(basicAuth.Consumer.Username) {
cid = *basicAuth.Consumer.Username
}
if !utils.Empty(basicAuth.Consumer.ID) {
cid = *basicAuth.Consumer.ID
}
err := s.client.BasicAuths.Delete(ctx, &cid, basicAuth.ID)
if err != nil {
return nil, err
}
return basicAuth, nil
}
// Update updates a Route in Kong.
// The arg should be of type crud.Event, containing the basicAuth to be updated,
// else the function will panic.
// It returns a the updated *state.Route.
func (s *basicAuthCRUD) Update(ctx context.Context, arg ...crud.Arg) (crud.Arg, error) {
event := crud.EventFromArg(arg[0])
basicAuth := basicAuthFromStruct(event)
cid := ""
if !utils.Empty(basicAuth.Consumer.Username) {
cid = *basicAuth.Consumer.Username
}
if !utils.Empty(basicAuth.Consumer.ID) {
cid = *basicAuth.Consumer.ID
}
updatedBasicAuth, err := s.client.BasicAuths.Create(ctx, &cid, &basicAuth.BasicAuth)
if err != nil {
return nil, err
}
return &state.BasicAuth{BasicAuth: *updatedBasicAuth}, nil
}
type basicAuthDiffer struct {
kind crud.Kind
once sync.Once
currentState, targetState *state.KongState
}
func (d *basicAuthDiffer) warnBasicAuth() {
const (
basicAuthPasswordWarning = "Warning: import/export of basic-auth" +
"credentials using decK doesn't work due to hashing of passwords in Kong."
)
d.once.Do(func() {
cprint.UpdatePrintln(basicAuthPasswordWarning)
})
}
func (d *basicAuthDiffer) Deletes(handler func(crud.Event) error) error {
currentBasicAuths, err := d.currentState.BasicAuths.GetAll()
if err != nil {
return fmt.Errorf("error fetching basic-auths from state: %w", err)
}
for _, basicAuth := range currentBasicAuths {
n, err := d.deleteBasicAuth(basicAuth)
if err != nil {
return err
}
if n != nil {
err = handler(*n)
if err != nil {
return err
}
}
}
return nil
}
func (d *basicAuthDiffer) deleteBasicAuth(basicAuth *state.BasicAuth) (*crud.Event, error) {
d.warnBasicAuth()
_, err := d.targetState.BasicAuths.Get(*basicAuth.ID)
if errors.Is(err, state.ErrNotFound) {
return &crud.Event{
Op: crud.Delete,
Kind: d.kind,
Obj: basicAuth,
}, nil
}
if err != nil {
return nil, fmt.Errorf("looking up basic-auth %q: %w",
*basicAuth.Username, err)
}
return nil, nil
}
func (d *basicAuthDiffer) CreateAndUpdates(handler func(crud.Event) error) error {
targetBasicAuths, err := d.targetState.BasicAuths.GetAll()
if err != nil {
return fmt.Errorf("error fetching basic-auths from state: %w", err)
}
for _, basicAuth := range targetBasicAuths {
n, err := d.createUpdateBasicAuth(basicAuth)
if err != nil {
return err
}
if n != nil {
err = handler(*n)
if err != nil {
return err
}
}
}
return nil
}
func (d *basicAuthDiffer) createUpdateBasicAuth(basicAuth *state.BasicAuth) (*crud.Event, error) {
d.warnBasicAuth()
basicAuth = &state.BasicAuth{BasicAuth: *basicAuth.DeepCopy()}
currentBasicAuth, err := d.currentState.BasicAuths.Get(*basicAuth.ID)
if errors.Is(err, state.ErrNotFound) {
// basicAuth not present, create it
return &crud.Event{
Op: crud.Create,
Kind: d.kind,
Obj: basicAuth,
}, nil
}
if err != nil {
return nil, fmt.Errorf("error looking up basic-auth %q: %w",
*basicAuth.Username, err)
}
// found, check if update needed
if !currentBasicAuth.EqualWithOpts(basicAuth, false, true, true, false) {
return &crud.Event{
Op: crud.Update,
Kind: d.kind,
Obj: basicAuth,
OldObj: currentBasicAuth,
}, nil
}
return nil, nil
}