-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
storage.go
176 lines (150 loc) · 4.65 KB
/
storage.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
package storage
import (
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"errors"
"fmt"
"hash"
"strings"
"github.com/buger/jsonparser"
"github.com/TykTechnologies/murmur3"
logger "github.com/TykTechnologies/tyk/log"
"github.com/TykTechnologies/tyk/internal/uuid"
)
var log = logger.Get()
// ErrKeyNotFound is a standard error for when a key is not found in the storage engine
var ErrKeyNotFound = errors.New("key not found")
// Handler is a standard interface to a storage backend, used by
// AuthorisationManager to read and write key values to the backend
type Handler interface {
GetKey(string) (string, error) // Returned string is expected to be a JSON object (user.SessionState)
GetMultiKey([]string) ([]string, error)
GetRawKey(string) (string, error)
SetKey(string, string, int64) error // Second input string is expected to be a JSON object (user.SessionState)
SetRawKey(string, string, int64) error
SetExp(string, int64) error // Set key expiration
GetExp(string) (int64, error) // Returns expiry of a key
GetKeys(string) []string
DeleteKey(string) bool
DeleteAllKeys() bool
DeleteRawKey(string) bool
Connect() bool
GetKeysAndValues() map[string]string
GetKeysAndValuesWithFilter(string) map[string]string
DeleteKeys([]string) bool
Decrement(string)
IncrememntWithExpire(string, int64) int64
SetRollingWindow(key string, per int64, val string, pipeline bool) (int, []interface{})
GetRollingWindow(key string, per int64, pipeline bool) (int, []interface{})
GetSet(string) (map[string]string, error)
AddToSet(string, string)
GetAndDeleteSet(string) []interface{}
RemoveFromSet(string, string)
DeleteScanMatch(string) bool
GetKeyPrefix() string
AddToSortedSet(string, string, float64)
GetSortedSetRange(string, string, string) ([]string, []float64, error)
RemoveSortedSetRange(string, string, string) error
GetListRange(string, int64, int64) ([]string, error)
RemoveFromList(string, string) error
AppendToSet(string, string)
Exists(string) (bool, error)
}
type AnalyticsHandler interface {
Connect() bool
AppendToSetPipelined(string, [][]byte)
GetAndDeleteSet(string) []interface{}
SetExp(string, int64) error // Set key expiration
GetExp(string) (int64, error) // Returns expiry of a key
}
const defaultHashAlgorithm = "murmur64"
// If hashing algorithm is empty, use legacy key generation
func GenerateToken(orgID, keyID, hashAlgorithm string) (string, error) {
if keyID == "" {
keyID = uuid.NewHex()
}
if hashAlgorithm != "" {
_, err := hashFunction(hashAlgorithm)
if err != nil {
hashAlgorithm = defaultHashAlgorithm
}
jsonToken := fmt.Sprintf(`{"org":"%s","id":"%s","h":"%s"}`, orgID, keyID, hashAlgorithm)
return base64.StdEncoding.EncodeToString([]byte(jsonToken)), err
}
// Legacy keys
return orgID + keyID, nil
}
// `{"` in base64
const B64JSONPrefix = "ey"
func TokenHashAlgo(token string) string {
// Legacy tokens not b64 and not JSON records
if strings.HasPrefix(token, B64JSONPrefix) {
if jsonToken, err := base64.StdEncoding.DecodeString(token); err == nil {
hashAlgo, _ := jsonparser.GetString(jsonToken, "h")
return hashAlgo
}
}
return ""
}
// TODO: add checks
func TokenID(token string) (id string, err error) {
jsonToken, err := base64.StdEncoding.DecodeString(token)
if err != nil {
return "", err
}
return jsonparser.GetString(jsonToken, "id")
}
func TokenOrg(token string) string {
if strings.HasPrefix(token, B64JSONPrefix) {
if jsonToken, err := base64.StdEncoding.DecodeString(token); err == nil {
// Checking error in case if it is a legacy tooken which just by accided has the same b64JSON prefix
if org, err := jsonparser.GetString(jsonToken, "org"); err == nil {
return org
}
}
}
// 24 is mongo bson id length
if len(token) > 24 {
return token[:24]
}
return ""
}
var (
HashSha256 = "sha256"
HashMurmur32 = "murmur32"
HashMurmur64 = "murmur64"
HashMurmur128 = "murmur128"
)
func hashFunction(algorithm string) (hash.Hash, error) {
switch algorithm {
case HashSha256:
return sha256.New(), nil
case HashMurmur64:
return murmur3.New64(), nil
case HashMurmur128:
return murmur3.New128(), nil
case "", HashMurmur32:
return murmur3.New32(), nil
default:
return murmur3.New32(), fmt.Errorf("Unknown key hash function: %s. Falling back to murmur32.", algorithm)
}
}
func HashStr(in string, withAlg ...string) string {
var algo string
if len(withAlg) > 0 && withAlg[0] != "" {
algo = withAlg[0]
} else {
algo = TokenHashAlgo(in)
}
h, _ := hashFunction(algo)
h.Write([]byte(in))
return hex.EncodeToString(h.Sum(nil))
}
func HashKey(in string, hashKey bool) string {
if !hashKey {
// Not hashing? Return the raw key
return in
}
return HashStr(in)
}