/
key.go
162 lines (135 loc) · 4.29 KB
/
key.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
package cassandra
import (
"fmt"
"github.com/prometheus/client_golang/prometheus"
"github.com/erikbos/gatekeeper/pkg/types"
)
const (
// Prometheus label for metrics of db interactions
keysMetricLabel = "keys"
// List of key columns we use
keysColumn = `consumer_key,
consumer_secret,
scopes,
api_products,
attributes,
app_id,
status,
issued_at,
expires_at`
)
// KeyStore holds our database config
type KeyStore struct {
db *Database
}
// NewKeyStore creates key instance
func NewKeyStore(database *Database) *KeyStore {
return &KeyStore{
db: database,
}
}
// GetByKey returns details of a single apikey
func (s *KeyStore) GetByKey(organization, key *string) (*types.Key, types.Error) {
var keys types.Keys
var err error
query := "SELECT " + keysColumn + " FROM keys WHERE consumer_key = ? LIMIT 1"
keys, err = s.runGetKeyQuery(query, key)
if err != nil {
s.db.metrics.QueryFailed(keysMetricLabel)
return nil, types.NewDatabaseError(err)
}
if len(keys) == 0 {
s.db.metrics.QueryNotFound(keysMetricLabel)
return nil, types.NewItemNotFoundError(
fmt.Errorf("cannot find apikey '%s'", *key))
}
s.db.metrics.QuerySuccessful(keysMetricLabel)
return &keys[0], nil
}
// GetByDeveloperAppID returns an array with apikey details of a developer app
func (s *KeyStore) GetByDeveloperAppID(organization, developerAppID string) (types.Keys, types.Error) {
query := "SELECT " + keysColumn + " FROM keys WHERE app_id = ?"
keys, err := s.runGetKeyQuery(query, developerAppID)
if err != nil {
s.db.metrics.QueryFailed(keysMetricLabel)
return nil, types.NewDatabaseError(err)
}
s.db.metrics.QuerySuccessful(keysMetricLabel)
return keys, nil
}
// GetCountByAPIProductName counts the number of times an apiproduct has been assigned to keys
func (s *KeyStore) GetCountByAPIProductName(organization, apiProductName string) (int, types.Error) {
query := "SELECT api_products FROM keys"
keys, err := s.runGetKeyQuery(query)
if err != nil {
s.db.metrics.QueryFailed(keysMetricLabel)
return 0, types.NewDatabaseError(err)
}
var count int
for _, key := range keys {
for _, product := range key.APIProducts {
if product.Apiproduct == apiProductName {
count++
}
}
}
return count, nil
}
// runGetKeyQuery executes CQL query and returns resultset
func (s *KeyStore) runGetKeyQuery(query string, queryParameters ...interface{}) (types.Keys, error) {
var keys types.Keys
timer := prometheus.NewTimer(s.db.metrics.queryHistogram)
defer timer.ObserveDuration()
iterable := s.db.CassandraSession.Query(query, queryParameters...).Iter()
m := make(map[string]interface{})
for iterable.MapScan(m) {
keys = append(keys, types.Key{
ConsumerKey: columnToString(m, "consumer_key"),
ConsumerSecret: columnToString(m, "consumer_secret"),
Scopes: columnToStringSlice(m, "scopes"),
APIProducts: KeyAPIProductStatusesUnmarshal(m["api_products"].(string)),
Attributes: columnToAttributes(m, "attributes"),
AppID: columnToString(m, "app_id"),
Status: columnToString(m, "status"),
IssuedAt: columnToInt64(m, "issued_at"),
ExpiresAt: columnToInt64(m, "expires_at"),
})
m = map[string]interface{}{}
}
// In case query failed we return query error
if err := iterable.Close(); err != nil {
return nil, err
}
return keys, nil
}
// UpdateByKey UPSERTs keys in database
func (s *KeyStore) UpdateByKey(organization string, k *types.Key) types.Error {
query := "INSERT INTO keys (" + keysColumn + ") VALUES(?,?,?,?,?,?,?,?,?)"
if err := s.db.CassandraSession.Query(query,
k.ConsumerKey,
k.ConsumerSecret,
k.Scopes,
KeyAPIProductStatusesMarshal(k.APIProducts),
attributesToColumn(k.Attributes),
k.AppID,
k.Status,
k.IssuedAt,
k.ExpiresAt).Exec(); err != nil {
s.db.metrics.QueryFailed(keysMetricLabel)
return types.NewDatabaseError(
fmt.Errorf("cannot update key '%s' (%s)", k.ConsumerKey, err))
}
return nil
}
// DeleteByKey deletes keys
func (s *KeyStore) DeleteByKey(organization, consumerKey string) types.Error {
query := "DELETE FROM keys WHERE consumer_key = ?"
if err := s.db.CassandraSession.Query(query, consumerKey).Exec(); err != nil {
s.db.metrics.QueryFailed(keysMetricLabel)
return types.NewDatabaseError(err)
}
return nil
}
func (s *KeyStore) Metric(what string) {
s.db.metrics.QueryFailed(keysMetricLabel)
}