File: plugin.go

package info (click to toggle)
deck 1.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,080 kB
  • sloc: makefile: 17; sh: 3
file content (348 lines) | stat: -rw-r--r-- 7,607 bytes parent folder | download | duplicates (2)
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
package state

import (
	"fmt"

	memdb "github.com/hashicorp/go-memdb"
	"github.com/kong/deck/state/indexers"
	"github.com/kong/deck/utils"
	"github.com/pkg/errors"
)

var (
	errPluginNameRequired = errors.New("name of plugin required")
)

const (
	pluginTableName     = "plugin"
	pluginsByServiceID  = "pluginsByServiceID"
	pluginsByRouteID    = "pluginsByRouteID"
	pluginsByConsumerID = "pluginsByConsumerID"
)

var pluginTableSchema = &memdb.TableSchema{
	Name: pluginTableName,
	Indexes: map[string]*memdb.IndexSchema{
		"id": {
			Name:    "id",
			Unique:  true,
			Indexer: &memdb.StringFieldIndex{Field: "ID"},
		},
		"name": {
			Name:    "name",
			Indexer: &memdb.StringFieldIndex{Field: "Name"},
		},
		all: allIndex,
		// foreign
		pluginsByServiceID: {
			Name: pluginsByServiceID,
			Indexer: &indexers.SubFieldIndexer{
				Fields: []indexers.Field{
					{
						Struct: "Service",
						Sub:    "ID",
					},
				},
			},
			AllowMissing: true,
		},
		pluginsByRouteID: {
			Name: pluginsByRouteID,
			Indexer: &indexers.SubFieldIndexer{
				Fields: []indexers.Field{
					{
						Struct: "Route",
						Sub:    "ID",
					},
				},
			},
			AllowMissing: true,
		},
		pluginsByConsumerID: {
			Name: pluginsByConsumerID,
			Indexer: &indexers.SubFieldIndexer{
				Fields: []indexers.Field{
					{
						Struct: "Consumer",
						Sub:    "ID",
					},
				},
			},
			AllowMissing: true,
		},
		// combined foreign fields
		// FIXME bug: collision if svc/route/consumer has the same ID
		// and same type of plugin is created. Consider the case when only
		// of the association is present
		"fields": {
			Name: "fields",
			Indexer: &indexers.SubFieldIndexer{
				Fields: []indexers.Field{
					{
						Struct: "Plugin",
						Sub:    "Name",
					},
					{
						Struct: "Service",
						Sub:    "ID",
					},
					{
						Struct: "Route",
						Sub:    "ID",
					},
					{
						Struct: "Consumer",
						Sub:    "ID",
					},
				},
			},
		},
	},
}

// PluginsCollection stores and indexes Kong Services.
type PluginsCollection collection

// Add adds a plugin to PluginsCollection
func (k *PluginsCollection) Add(plugin Plugin) error {
	txn := k.db.Txn(true)
	defer txn.Abort()

	err := insertPlugin(txn, plugin)
	if err != nil {
		return err
	}

	txn.Commit()
	return nil
}

func insertPlugin(txn *memdb.Txn, plugin Plugin) error {
	// TODO abstract this check in the go-memdb library itself
	if utils.Empty(plugin.ID) {
		return errIDRequired
	}
	if utils.Empty(plugin.Name) {
		return errPluginNameRequired
	}

	// err out if plugin with same ID is present
	_, err := getPluginByID(txn, *plugin.ID)
	if err == nil {
		return fmt.Errorf("inserting plugin %v: %w", plugin.Console(), ErrAlreadyExists)
	} else if err != ErrNotFound {
		return err
	}

	// err out if another plugin with exact same combination is present
	sID, rID, cID := "", "", ""
	if plugin.Service != nil && !utils.Empty(plugin.Service.ID) {
		sID = *plugin.Service.ID
	}
	if plugin.Route != nil && !utils.Empty(plugin.Route.ID) {
		rID = *plugin.Route.ID
	}
	if plugin.Consumer != nil && !utils.Empty(plugin.Consumer.ID) {
		cID = *plugin.Consumer.ID
	}
	_, err = getPluginBy(txn, *plugin.Name, sID, rID, cID)
	if err == nil {
		return fmt.Errorf("inserting plugin %v: %w", plugin.Console(), ErrAlreadyExists)
	} else if err != ErrNotFound {
		return err
	}

	// all good
	err = txn.Insert(pluginTableName, &plugin)
	if err != nil {
		return err
	}
	return nil
}

func getPluginByID(txn *memdb.Txn, id string) (*Plugin, error) {
	res, err := multiIndexLookupUsingTxn(txn, pluginTableName,
		[]string{"id"}, id)
	if err != nil {
		return nil, err
	}

	plugin, ok := res.(*Plugin)
	if !ok {
		panic(unexpectedType)
	}
	return &Plugin{Plugin: *plugin.DeepCopy()}, nil
}

// Get gets a plugin by id.
func (k *PluginsCollection) Get(id string) (*Plugin, error) {
	if id == "" {
		return nil, errIDRequired
	}

	txn := k.db.Txn(false)
	defer txn.Abort()

	plugin, err := getPluginByID(txn, id)
	if err != nil {
		return nil, err
	}
	return plugin, nil
}

// GetAllByName returns all plugins of a specific type
// (key-auth, ratelimiting, etc).
func (k *PluginsCollection) GetAllByName(name string) ([]*Plugin, error) {
	return k.getAllPluginsBy("name", name)
}

func getPluginBy(txn *memdb.Txn, name, svcID, routeID, consumerID string) (
	*Plugin, error) {
	if name == "" {
		return nil, errPluginNameRequired
	}

	res, err := txn.First(pluginTableName, "fields",
		name, svcID, routeID, consumerID)
	if err != nil {
		return nil, err
	}
	if res == nil {
		return nil, ErrNotFound
	}
	p, ok := res.(*Plugin)
	if !ok {
		panic(unexpectedType)
	}
	return &Plugin{Plugin: *p.DeepCopy()}, nil
}

// GetByProp returns a plugin which matches all the properties passed in
// the arguments. If serviceID, routeID and consumerID are empty strings, then
// a global plugin is searched.
// Otherwise, a plugin with name and the supplied foreign references is
// searched.
// name is required.
func (k *PluginsCollection) GetByProp(name, serviceID,
	routeID string, consumerID string) (*Plugin, error) {
	txn := k.db.Txn(false)
	defer txn.Abort()

	return getPluginBy(txn, name, serviceID, routeID, consumerID)
}

func (k *PluginsCollection) getAllPluginsBy(index, identifier string) (
	[]*Plugin, error) {
	if identifier == "" {
		return nil, errIDRequired
	}

	txn := k.db.Txn(false)
	defer txn.Abort()

	iter, err := txn.Get(pluginTableName, index, identifier)
	if err != nil {
		return nil, err
	}
	var res []*Plugin
	for el := iter.Next(); el != nil; el = iter.Next() {
		p, ok := el.(*Plugin)
		if !ok {
			panic(unexpectedType)
		}
		res = append(res, &Plugin{Plugin: *p.DeepCopy()})
	}
	return res, nil
}

// GetAllByServiceID returns all plugins referencing a service
// by its id.
func (k *PluginsCollection) GetAllByServiceID(id string) ([]*Plugin,
	error) {
	return k.getAllPluginsBy(pluginsByServiceID, id)
}

// GetAllByRouteID returns all plugins referencing a service
// by its id.
func (k *PluginsCollection) GetAllByRouteID(id string) ([]*Plugin,
	error) {
	return k.getAllPluginsBy(pluginsByRouteID, id)
}

// GetAllByConsumerID returns all plugins referencing a consumer
// by its id.
func (k *PluginsCollection) GetAllByConsumerID(id string) ([]*Plugin,
	error) {
	return k.getAllPluginsBy(pluginsByConsumerID, id)
}

// Update updates a plugin
func (k *PluginsCollection) Update(plugin Plugin) error {
	// TODO abstract this check in the go-memdb library itself
	if utils.Empty(plugin.ID) {
		return errIDRequired
	}

	txn := k.db.Txn(true)
	defer txn.Abort()

	err := deletePlugin(txn, *plugin.ID)
	if err != nil {
		return err
	}

	err = insertPlugin(txn, plugin)
	if err != nil {
		return err
	}

	txn.Commit()
	return nil
}

func deletePlugin(txn *memdb.Txn, id string) error {
	plugin, err := getPluginByID(txn, id)
	if err != nil {
		return err
	}
	return txn.Delete(pluginTableName, plugin)
}

// Delete deletes a plugin by ID.
func (k *PluginsCollection) Delete(id string) error {
	if id == "" {
		return errIDRequired
	}

	txn := k.db.Txn(true)
	defer txn.Abort()

	err := deletePlugin(txn, id)
	if err != nil {
		return err
	}

	txn.Commit()
	return nil
}

// GetAll gets a plugin by name or ID.
func (k *PluginsCollection) GetAll() ([]*Plugin, error) {
	txn := k.db.Txn(false)
	defer txn.Abort()

	iter, err := txn.Get(pluginTableName, all, true)
	if err != nil {
		return nil, err
	}

	var res []*Plugin
	for el := iter.Next(); el != nil; el = iter.Next() {
		p, ok := el.(*Plugin)
		if !ok {
			panic(unexpectedType)
		}
		res = append(res, &Plugin{Plugin: *p.DeepCopy()})
	}
	return res, nil
}