File: credentials.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 (200 lines) | stat: -rw-r--r-- 3,964 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
package state

import (
	"fmt"

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

const (
	byConsumerID = "byConsumerID"
)

// credentialsCollection stores and indexes key-auth credentials.
type credentialsCollection struct {
	collection
	CredType string
}

func (k *credentialsCollection) TableName() string {
	return k.CredType
}

func (k *credentialsCollection) Schema() *memdb.TableSchema {
	return &memdb.TableSchema{
		Name: k.CredType,
		Indexes: map[string]*memdb.IndexSchema{
			"id": {
				Name:    "id",
				Unique:  true,
				Indexer: &memdb.StringFieldIndex{Field: "ID"},
			},
			byConsumerID: {
				Name: byConsumerID,
				Indexer: &indexers.MethodIndexer{
					Method: "GetConsumer",
				},
			},
			"id2": {
				Name: "id2",
				// TODO configurable
				Unique: true,
				Indexer: &indexers.MethodIndexer{
					Method: "GetID2",
				},
			},
			all: allIndex,
		},
	}
}

func (k *credentialsCollection) getCred(txn *memdb.Txn, IDs ...string) (entity, error) {
	for _, id := range IDs {
		res, err := multiIndexLookupUsingTxn(txn, k.CredType,
			[]string{"id", "id2"}, id)
		if err == ErrNotFound {
			continue
		}
		if err != nil {
			return nil, err
		}
		cred, ok := res.(entity)
		if !ok {
			panic(unexpectedType)
		}
		return cred, nil
	}
	return nil, ErrNotFound
}

// Add adds a key-auth credential to credentialsCollection.
func (k *credentialsCollection) Add(cred entity) error {
	if cred.GetID() == "" {
		return errIDRequired
	}
	txn := k.db.Txn(true)
	defer txn.Abort()

	// TODO detect unique constraint violation for ID2

	_, err := k.getCred(txn, cred.GetID(), cred.GetID2())
	if err == nil {
		return fmt.Errorf("inserting credential %v: %w", cred.GetID(), ErrAlreadyExists)
	} else if err != ErrNotFound {
		return err
	}

	err = txn.Insert(k.CredType, cred)
	if err != nil {
		return err
	}
	txn.Commit()
	return nil
}

// Get gets a credential by ID or endpoint key.
func (k *credentialsCollection) Get(id string) (entity, error) {
	if id == "" {
		return nil, errIDRequired
	}

	txn := k.db.Txn(false)
	defer txn.Abort()
	return k.getCred(txn, id)
}

// Update updates an existing key-auth credential.
func (k *credentialsCollection) Update(cred entity) error {
	// TODO abstract this check in the go-memdb library itself
	if cred.GetID() == "" {
		return errIDRequired
	}

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

	err := k.deleteCred(txn, cred.GetID())
	if err != nil {
		return err
	}
	err = txn.Insert(k.CredType, cred)
	if err != nil {
		return err
	}

	txn.Commit()
	return nil
}

func (k *credentialsCollection) deleteCred(txn *memdb.Txn, nameOrID string) error {
	cred, err := k.getCred(txn, nameOrID)
	if err != nil {
		return err
	}

	err = txn.Delete(k.CredType, cred)
	if err != nil {
		return err
	}
	return nil
}

// Delete deletes a key-auth credential by key or ID.
func (k *credentialsCollection) Delete(id string) error {
	if id == "" {
		return errIDRequired
	}

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

	err := k.deleteCred(txn, id)
	if err != nil {
		return err
	}

	txn.Commit()
	return nil
}

// GetAll gets all key-auth credentials.
func (k *credentialsCollection) GetAll() ([]entity, error) {
	txn := k.db.Txn(false)
	defer txn.Abort()

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

	var res []entity
	for el := iter.Next(); el != nil; el = iter.Next() {
		r, ok := el.(entity)
		if !ok {
			panic(unexpectedType)
		}
		res = append(res, r)
	}
	return res, nil
}

// GetAllByConsumerID returns all key-auth credentials
// belong to a Consumer with id.
func (k *credentialsCollection) GetAllByConsumerID(id string) ([]entity,
	error) {
	txn := k.db.Txn(false)
	iter, err := txn.Get(k.CredType, byConsumerID, id)
	if err != nil {
		return nil, err
	}
	var res []entity
	for el := iter.Next(); el != nil; el = iter.Next() {
		r, ok := el.(entity)
		if !ok {
			panic(unexpectedType)
		}
		res = append(res, r)
	}
	return res, nil
}