-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
369 lines (320 loc) · 12.9 KB
/
main.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
361
362
363
364
365
366
367
368
369
package main
import (
"fmt"
"log"
"os"
"reflect"
"slices"
"github.com/kong/go-kong/kong"
)
var (
version string = "undefined"
)
func slicePointersToValues(slicePointers []*string) []string {
sliceValues := make([]string, len(slicePointers))
for i, s := range slicePointers {
sliceValues[i] = *s
}
slices.Sort(sliceValues)
return sliceValues
}
func main() {
log.Printf("Comparer version: %v \n", version)
if len(os.Args) != 3 {
log.Fatalln("please specify url1 and url2 to compare like: " + os.Args[0] + " http://kong-adm-1 http://kong-adm-2")
}
clientUrl1 := &os.Args[1]
clientUrl2 := &os.Args[2]
client1, err := kong.NewClient(clientUrl1, nil)
if err != nil {
log.Fatalln(err)
}
client2, err := kong.NewClient(clientUrl2, nil)
if err != nil {
log.Fatalln(err)
}
//compare routes and services
allRoutesClient1, err := client1.Routes.ListAll(nil)
if err != nil {
log.Fatalln(err)
}
allRoutesClient2, err := client2.Routes.ListAll(nil)
if err != nil {
log.Fatalln(err)
}
log.Printf("Amount routes in %v: %v \n", *clientUrl1, len(allRoutesClient1))
log.Printf("Amount routes in %v: %v \n", *clientUrl2, len(allRoutesClient2))
for _, client1Route := range allRoutesClient1 {
result := false
client1RoutePaths := slicePointersToValues(client1Route.Paths)
client1RouteHosts := slicePointersToValues(client1Route.Hosts)
strClient1RoutePaths := fmt.Sprint(client1RouteHosts) + fmt.Sprint(client1RoutePaths)
for _, client2Route := range allRoutesClient2 {
if slices.Equal(client1RoutePaths, slicePointersToValues(client2Route.Paths)) && slices.Equal(client1RouteHosts, slicePointersToValues(client2Route.Hosts)) {
result = true
//checking route parameters
if *client1Route.PreserveHost != *client2Route.PreserveHost {
log.Printf("Route %v PreserveHost %v not equals %v \n", strClient1RoutePaths, *client1Route.PreserveHost, *client2Route.PreserveHost)
}
if *client1Route.StripPath != *client2Route.StripPath {
log.Printf("Route %v StripPath %v not equals %v \n", strClient1RoutePaths, *client1Route.StripPath, *client2Route.StripPath)
}
if !slices.Equal(slicePointersToValues(client1Route.Methods), slicePointersToValues(client2Route.Methods)) {
log.Printf("Route %v Methods not equals \n", strClient1RoutePaths)
}
// if !slices.Equal(slicePointersToValues(client1Route.Hosts), slicePointersToValues(client2Route.Hosts)) {
// log.Printf("Route %v Hosts not equals \n", strClient1RoutePaths)
// }
if !slices.Equal(slicePointersToValues(client1Route.Protocols), slicePointersToValues(client2Route.Protocols)) {
log.Printf("Route %v Protocols not equals \n", strClient1RoutePaths)
}
//check route plugins
allPluginsRouteClient1, err := client1.Plugins.ListAllForRoute(nil, client1Route.ID)
if err != nil {
log.Fatalln(err)
}
allPluginsRouteClient2, err := client2.Plugins.ListAllForRoute(nil, client2Route.ID)
if err != nil {
log.Fatalln(err)
}
for _, pluginRouteClient1 := range allPluginsRouteClient1 {
result := false
for _, pluginRouteClient2 := range allPluginsRouteClient2 {
if *pluginRouteClient1.Name == *pluginRouteClient2.Name && *pluginRouteClient1.Enabled == *pluginRouteClient2.Enabled {
result = true
delete(pluginRouteClient1.Config, "anonymous")
delete(pluginRouteClient2.Config, "anonymous")
delete(pluginRouteClient1.Config, "okta_consumer")
delete(pluginRouteClient2.Config, "okta_consumer")
if !reflect.DeepEqual(pluginRouteClient1.Config, pluginRouteClient2.Config) {
//log.Println(pluginRouteClient1.Config)
//log.Println(pluginRouteClient2.Config)
log.Println("Route " + strClient1RoutePaths + " plugin config " + *pluginRouteClient1.Name + " not equals in " + *clientUrl2)
}
break
}
}
if result == false {
log.Println("Route " + strClient1RoutePaths + " plugin " + *pluginRouteClient1.Name + " does not exists in " + *clientUrl2)
}
}
//check route service
serviceRouteClient1, err := client1.Services.GetForRoute(nil, client1Route.ID)
if err != nil {
log.Fatalln(err)
}
serviceRouteClient2, err := client2.Services.GetForRoute(nil, client2Route.ID)
if err != nil {
log.Fatalln(err)
}
if *serviceRouteClient1.Host != *serviceRouteClient2.Host {
log.Printf("Route %v target service Host not equals \n", strClient1RoutePaths)
}
if *serviceRouteClient1.Port != *serviceRouteClient2.Port {
log.Printf("Route %v target service Port not equals \n", strClient1RoutePaths)
}
if *serviceRouteClient1.Path != *serviceRouteClient2.Path {
log.Printf("Route %v target service Path not equals \n", strClient1RoutePaths)
}
//check service plugins
allPluginsServiceClient1, err := client1.Plugins.ListAllForService(nil, serviceRouteClient1.ID)
if err != nil {
log.Fatalln(err)
}
allPluginsServiceClient2, err := client2.Plugins.ListAllForService(nil, serviceRouteClient2.ID)
if err != nil {
log.Fatalln(err)
}
for _, pluginServiceClient1 := range allPluginsServiceClient1 {
result := false
for _, pluginServiceClient2 := range allPluginsServiceClient2 {
if *pluginServiceClient1.Name == *pluginServiceClient2.Name && *pluginServiceClient1.Enabled == *pluginServiceClient2.Enabled {
result = true
delete(pluginServiceClient1.Config, "append")
delete(pluginServiceClient2.Config, "append")
//delete(pluginServiceClient1.Config, "okta_consumer")
//delete(pluginServiceClient2.Config, "okta_consumer")
if !reflect.DeepEqual(pluginServiceClient1.Config, pluginServiceClient2.Config) {
//log.Println(pluginServiceClient1.Config)
//log.Println(pluginServiceClient2.Config)
log.Println("Service " + *serviceRouteClient1.Name + " plugin config " + *pluginServiceClient1.Name + " not equals in " + *clientUrl2)
}
break
}
}
if result == false {
log.Println("Route " + strClient1RoutePaths + " plugin " + *pluginServiceClient1.Name + " does not exists in " + *clientUrl2)
}
}
break
}
}
if result == false {
log.Println("Route " + strClient1RoutePaths + " does not exists in " + *clientUrl2)
}
}
//compare consumers
allConsumersClient1, err := client1.Consumers.ListAll(nil)
if err != nil {
log.Fatalln(err)
}
allConsumersClient2, err := client2.Consumers.ListAll(nil)
if err != nil {
log.Fatalln(err)
}
log.Printf("Amount consumers in %v: %v \n", *clientUrl1, len(allConsumersClient1))
log.Printf("Amount consumers in %v: %v \n", *clientUrl2, len(allConsumersClient2))
log.Printf("Only Consumers plugins and ACLs comparison currently implemented!!! \n")
for _, client1Consumer := range allConsumersClient1 {
result := false
for _, client2Consumer := range allConsumersClient2 {
if *client1Consumer.Username == *client2Consumer.Username && *client1Consumer.CustomID == *client2Consumer.CustomID {
result = true
//check consumer plugins
allPluginsConsumerClient1, err := client1.Plugins.ListAllForConsumer(nil, client1Consumer.ID)
if err != nil {
log.Fatalln(err)
}
allPluginsConsumerClient2, err := client2.Plugins.ListAllForConsumer(nil, client2Consumer.ID)
if err != nil {
log.Fatalln(err)
}
for _, pluginConsumerClient1 := range allPluginsConsumerClient1 {
result := false
for _, pluginConsumerClient2 := range allPluginsConsumerClient2 {
if *pluginConsumerClient1.Name == *pluginConsumerClient2.Name && *pluginConsumerClient1.Enabled == *pluginConsumerClient2.Enabled {
result = true
//log.Println(pluginConsumerClient1.Config)
//delete(pluginConsumerClient1.Config, "append")
//delete(pluginConsumerClient2.Config, "append")
//delete(pluginConsumerClient1.Config, "okta_consumer")
//delete(pluginConsumerClient2.Config, "okta_consumer")
if !reflect.DeepEqual(pluginConsumerClient1.Config, pluginConsumerClient2.Config) {
//log.Println(pluginConsumerClient1.Config)
//log.Println(pluginConsumerClient2.Config)
log.Println("Consumer " + *client1Consumer.Username + " plugin config " + *pluginConsumerClient1.Name + " not equals in " + *clientUrl2)
}
break
}
}
if result == false {
log.Println("Consumer " + *client1Consumer.Username + " plugin " + *pluginConsumerClient1.Name + " does not exists in " + *clientUrl2)
}
}
//check consumer ACLs
allACLsConsumerClient1, _, err := client1.ACLs.ListForConsumer(nil, client1Consumer.ID, nil)
if err != nil {
log.Fatalln(err)
}
allACLsConsumerClient2, _, err := client2.ACLs.ListForConsumer(nil, client2Consumer.ID, nil)
if err != nil {
log.Fatalln(err)
}
for _, aclConsumerClient1 := range allACLsConsumerClient1 {
result := false
for _, aclConsumerClient2 := range allACLsConsumerClient2 {
if *aclConsumerClient1.Group == *aclConsumerClient2.Group {
result = true
break
}
}
if result == false {
log.Println("Consumer " + *client1Consumer.Username + " ACL " + *aclConsumerClient1.Group + " does not exists in " + *clientUrl2)
}
}
break
}
}
if result == false {
log.Println("Consumer " + *client1Consumer.Username + " does not exists in " + *clientUrl2)
}
}
//compare global plugins
allPluginsClient1, err := client1.Plugins.ListAll(nil)
if err != nil {
log.Fatalln(err)
}
var allGlobalPluginsClient1 []*kong.Plugin
for _, PluginClient1 := range allPluginsClient1 {
if PluginClient1.Route == nil && PluginClient1.Service == nil && PluginClient1.Consumer == nil {
allGlobalPluginsClient1 = append(allGlobalPluginsClient1, PluginClient1)
}
}
allPluginsClient2, err := client2.Plugins.ListAll(nil)
if err != nil {
log.Fatalln(err)
}
var allGlobalPluginsClient2 []*kong.Plugin
for _, PluginClient2 := range allPluginsClient2 {
if PluginClient2.Route == nil && PluginClient2.Service == nil && PluginClient2.Consumer == nil {
allGlobalPluginsClient2 = append(allGlobalPluginsClient2, PluginClient2)
}
}
log.Printf("Amount global plugins in %v: %v \n", *clientUrl1, len(allGlobalPluginsClient1))
log.Printf("Amount global plugins in %v: %v \n", *clientUrl2, len(allGlobalPluginsClient2))
for _, globalPluginClient1 := range allGlobalPluginsClient1 {
result := false
for _, globalPluginClient2 := range allGlobalPluginsClient2 {
if *globalPluginClient1.Name == *globalPluginClient2.Name && *globalPluginClient1.Enabled == *globalPluginClient2.Enabled {
result = true
delete(globalPluginClient1.Config, "anonymous")
delete(globalPluginClient2.Config, "anonymous")
delete(globalPluginClient1.Config, "okta_consumer")
delete(globalPluginClient2.Config, "okta_consumer")
if !reflect.DeepEqual(globalPluginClient1.Config, globalPluginClient2.Config) {
//log.Println(pluginRouteClient1.Config)
//log.Println(pluginRouteClient2.Config)
log.Println("Global plugin config " + *globalPluginClient1.Name + " not equals in " + *clientUrl2)
}
break
}
}
if result == false {
log.Println("Global plugin " + *globalPluginClient1.Name + " does not exists in " + *clientUrl2)
}
}
// //compare consumer groups
// allConsGroupsClient1, err := client1.ConsumerGroups.ListAll(nil)
// if err != nil {
// log.Fatalln(err)
// }
// allConsGroupsClient2, err := client2.ConsumerGroups.ListAll(nil)
// if err != nil {
// log.Fatalln(err)
// }
// log.Printf("Amount consumer groups in %v: %v \n", *clientUrl1, len(allConsGroupsClient1))
// log.Printf("Amount consumer groups in %v: %v \n", *clientUrl2, len(allConsGroupsClient2))
// for _, consGroupClient1 := range allConsGroupsClient1 {
// result := false
// for _, consGroupClient2 := range allConsGroupsClient2 {
// if *consGroupClient1.Name == *consGroupClient2.Name && *consGroupClient1.ID == *consGroupClient1.ID {
// result = true
// //check consumer plugins
// consumersGroupClient1, err := client1.ConsumerGroupConsumers.ListAll(nil, consGroupClient1.ID)
// if err != nil {
// log.Fatalln(err)
// }
// consumersGroupClient2, err := client2.ConsumerGroupConsumers.ListAll(nil, consGroupClient2.ID)
// if err != nil {
// log.Fatalln(err)
// }
// for _, groupConsumerClient1 := range consumersGroupClient1.Consumers {
// result := false
// for _, groupConsumerClient2 := range consumersGroupClient2.Consumers {
// if *groupConsumerClient1.Username == *groupConsumerClient2.Username && *groupConsumerClient1.ID == *groupConsumerClient2.ID {
// result = true
// break
// }
// }
// if result == false {
// log.Println("Consumer " + *consGroupClient1.Name + " plugin " + *groupConsumerClient1.Username + " does not exists in " + *clientUrl2)
// }
// }
// break
// }
// }
// if result == false {
// log.Println("Cons Group " + *consGroupClient1.Name + " does not exists in " + *clientUrl2)
// }
// }
}