forked from TykTechnologies/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoprocess_helpers.go
196 lines (174 loc) · 6.22 KB
/
coprocess_helpers.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
// +build coprocess
package main
import (
"encoding/json"
"github.com/Sirupsen/logrus"
"github.com/TykTechnologies/tyk/coprocess"
"github.com/TykTechnologies/tyk/user"
)
// TykSessionState takes a coprocess.SessionState (as returned by the Protocol Buffer binding), and outputs a standard Tyk SessionState.
func TykSessionState(session *coprocess.SessionState) *user.SessionState {
accessDefinitions := make(map[string]user.AccessDefinition, len(session.AccessRights))
for key, protoAccDef := range session.AccessRights {
allowedUrls := make([]user.AccessSpec, len(protoAccDef.AllowedUrls))
for _, protoAllowedURL := range protoAccDef.AllowedUrls {
allowedUrls = append(allowedUrls, user.AccessSpec{
URL: protoAllowedURL.Url,
Methods: protoAllowedURL.Methods,
})
}
accessDefinitions[key] = user.AccessDefinition{
APIName: protoAccDef.ApiName,
APIID: protoAccDef.ApiId,
Versions: protoAccDef.Versions,
AllowedURLs: allowedUrls,
}
}
var basicAuthData struct {
Password string `json:"password" msg:"password"`
Hash user.HashType `json:"hash_type" msg:"hash_type"`
}
if session.BasicAuthData != nil {
basicAuthData.Password = session.BasicAuthData.Password
basicAuthData.Hash = user.HashType(session.BasicAuthData.Hash)
}
var jwtData struct {
Secret string `json:"secret" msg:"secret"`
}
if session.JwtData != nil {
jwtData.Secret = session.JwtData.Secret
}
var monitor struct {
TriggerLimits []float64 `json:"trigger_limits" msg:"trigger_limits"`
}
if session.Monitor != nil {
monitor.TriggerLimits = session.Monitor.TriggerLimits
}
metadata := make(map[string]interface{})
if session.Metadata != nil {
for k, v := range session.Metadata {
metadata[k] = v
}
}
return &user.SessionState{
LastCheck: session.LastCheck,
Allowance: session.Allowance,
Rate: session.Rate,
Per: session.Per,
Expires: session.Expires,
QuotaMax: session.QuotaMax,
QuotaRenews: session.QuotaRenews,
QuotaRemaining: session.QuotaRemaining,
QuotaRenewalRate: session.QuotaRenewalRate,
AccessRights: accessDefinitions,
OrgID: session.OrgId,
OauthClientID: session.OauthClientId,
OauthKeys: session.OauthKeys,
Certificate: session.Certificate,
BasicAuthData: basicAuthData,
JWTData: jwtData,
HMACEnabled: session.HmacEnabled,
HmacSecret: session.HmacSecret,
IsInactive: session.IsInactive,
ApplyPolicyID: session.ApplyPolicyId,
ApplyPolicies: session.ApplyPolicies,
DataExpires: session.DataExpires,
MetaData: metadata,
Monitor: monitor,
EnableDetailedRecording: session.EnableDetailedRecording,
Tags: session.Tags,
Alias: session.Alias,
LastUpdated: session.LastUpdated,
IdExtractorDeadline: session.IdExtractorDeadline,
SessionLifetime: session.SessionLifetime,
}
}
// ProtoSessionState takes a standard SessionState and outputs a SessionState object compatible with Protocol Buffers.
func ProtoSessionState(session *user.SessionState) *coprocess.SessionState {
accessDefinitions := make(map[string]*coprocess.AccessDefinition, len(session.AccessRights))
for key, accessDefinition := range session.AccessRights {
var allowedUrls []*coprocess.AccessSpec
for _, allowedURL := range accessDefinition.AllowedURLs {
accessSpec := &coprocess.AccessSpec{
Url: allowedURL.URL,
Methods: allowedURL.Methods,
}
allowedUrls = append(allowedUrls, accessSpec)
}
accessDefinitions[key] = &coprocess.AccessDefinition{
ApiName: accessDefinition.APIName,
ApiId: accessDefinition.APIID,
Versions: accessDefinition.Versions,
AllowedUrls: allowedUrls,
}
}
basicAuthData := &coprocess.BasicAuthData{
Password: session.BasicAuthData.Password,
Hash: string(session.BasicAuthData.Hash),
}
jwtData := &coprocess.JWTData{
Secret: session.JWTData.Secret,
}
monitor := &coprocess.Monitor{
TriggerLimits: session.Monitor.TriggerLimits,
}
metadata := make(map[string]string)
if len(session.MetaData) > 0 {
for k, v := range session.MetaData {
switch v.(type) {
case string:
metadata[k] = v.(string)
default:
jsonValue, err := json.Marshal(v)
if err != nil {
log.WithFields(logrus.Fields{
"prefix": "coprocess",
}).WithError(err).Error("Couldn't encode session metadata")
continue
}
metadata[k] = string(jsonValue)
}
}
}
return &coprocess.SessionState{
LastCheck: session.LastCheck,
Allowance: session.Allowance,
Rate: session.Rate,
Per: session.Per,
Expires: session.Expires,
QuotaMax: session.QuotaMax,
QuotaRenews: session.QuotaRenews,
QuotaRemaining: session.QuotaRemaining,
QuotaRenewalRate: session.QuotaRenewalRate,
AccessRights: accessDefinitions,
OrgId: session.OrgID,
OauthClientId: session.OauthClientID,
OauthKeys: session.OauthKeys,
BasicAuthData: basicAuthData,
JwtData: jwtData,
HmacEnabled: session.HMACEnabled,
HmacSecret: session.HmacSecret,
IsInactive: session.IsInactive,
ApplyPolicyId: session.ApplyPolicyID,
ApplyPolicies: session.ApplyPolicies,
DataExpires: session.DataExpires,
Monitor: monitor,
Metadata: metadata,
EnableDetailedRecording: session.EnableDetailedRecording,
Tags: session.Tags,
Alias: session.Alias,
LastUpdated: session.LastUpdated,
IdExtractorDeadline: session.IdExtractorDeadline,
SessionLifetime: session.SessionLifetime,
}
}
// ProtoMap is a helper function for maps with string slice values.
func ProtoMap(inputMap map[string][]string) map[string]string {
newMap := make(map[string]string)
if inputMap != nil {
for k, v := range inputMap {
newMap[k] = v[0]
}
}
return newMap
}