forked from anexia-it/tyk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupstream.go
413 lines (341 loc) · 14.1 KB
/
upstream.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
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
package oas
import (
"sort"
"strings"
"github.com/TykTechnologies/tyk/apidef"
)
// Upstream holds configuration for an upstream server.
type Upstream struct {
// URL defines the target URL that the request should be proxied to.
// Tyk classic API definition: `proxy.target_url`
URL string `bson:"url" json:"url"` // required
// ServiceDiscovery contains the configuration related to Service Discovery.
// Tyk classic API definition: `proxy.service_discovery`
ServiceDiscovery *ServiceDiscovery `bson:"serviceDiscovery,omitempty" json:"serviceDiscovery,omitempty"`
// Test contains the configuration related to uptime tests.
Test *Test `bson:"test,omitempty" json:"test,omitempty"`
// MutualTLS contains the configuration related to upstream mutual TLS.
MutualTLS *MutualTLS `bson:"mutualTLS,omitempty" json:"mutualTLS,omitempty"`
// CertificatePinning contains the configuration related to certificate pinning.
CertificatePinning *CertificatePinning `bson:"certificatePinning,omitempty" json:"certificatePinning,omitempty"`
}
// Fill fills *Upstream from apidef.APIDefinition.
func (u *Upstream) Fill(api apidef.APIDefinition) {
u.URL = api.Proxy.TargetURL
if u.ServiceDiscovery == nil {
u.ServiceDiscovery = &ServiceDiscovery{}
}
u.ServiceDiscovery.Fill(api.Proxy.ServiceDiscovery)
if ShouldOmit(u.ServiceDiscovery) {
u.ServiceDiscovery = nil
}
if u.Test == nil {
u.Test = &Test{}
}
u.Test.Fill(api.UptimeTests)
if ShouldOmit(u.Test) {
u.Test = nil
}
if u.MutualTLS == nil {
u.MutualTLS = &MutualTLS{}
}
u.MutualTLS.Fill(api)
if ShouldOmit(u.MutualTLS) {
u.MutualTLS = nil
}
if u.CertificatePinning == nil {
u.CertificatePinning = &CertificatePinning{}
}
u.CertificatePinning.Fill(api)
if ShouldOmit(u.CertificatePinning) {
u.CertificatePinning = nil
}
}
// ExtractTo extracts *Upstream into *apidef.APIDefinition.
func (u *Upstream) ExtractTo(api *apidef.APIDefinition) {
api.Proxy.TargetURL = u.URL
if u.ServiceDiscovery != nil {
u.ServiceDiscovery.ExtractTo(&api.Proxy.ServiceDiscovery)
}
if u.Test != nil {
u.Test.ExtractTo(&api.UptimeTests)
}
if u.MutualTLS != nil {
u.MutualTLS.ExtractTo(api)
}
if u.CertificatePinning != nil {
u.CertificatePinning.ExtractTo(api)
}
}
// ServiceDiscovery holds configuration required for service discovery.
type ServiceDiscovery struct {
// Enabled enables Service Discovery.
//
// Tyk classic API definition: `service_discovery.use_discovery_service`
Enabled bool `bson:"enabled" json:"enabled"` // required
// QueryEndpoint is the endpoint to call, this would usually be Consul, etcd or Eureka K/V store.
// Tyk classic API definition: `service_discovery.query_endpoint`
QueryEndpoint string `bson:"queryEndpoint,omitempty" json:"queryEndpoint,omitempty"`
// DataPath is the namespace of the data path - where exactly in your service response the namespace can be found.
// For example, if your service responds with:
//
// ```
// {
// "action": "get",
// "node": {
// "key": "/services/single",
// "value": "http://httpbin.org:6000",
// "modifiedIndex": 6,
// "createdIndex": 6
// }
// }
// ```
//
// then your namespace would be `node.value`.
//
// Tyk classic API definition: `service_discovery.data_path`
DataPath string `bson:"dataPath,omitempty" json:"dataPath,omitempty"`
// UseNestedQuery enables using a combination of `dataPath` and `parentDataPath`.
// It is necessary when the data lives within this string-encoded JSON object.
//
// ```
// {
// "action": "get",
// "node": {
// "key": "/services/single",
// "value": "{"hostname": "http://httpbin.org", "port": "80"}",
// "modifiedIndex": 6,
// "createdIndex": 6
// }
// }
// ```
//
// Tyk classic API definition: `service_discovery.use_nested_query`
UseNestedQuery bool `bson:"useNestedQuery,omitempty" json:"useNestedQuery,omitempty"`
// ParentDataPath is the namespace of the where to find the nested
// value, if `useNestedQuery` is `true`. In the above example, it
// would be `node.value`. You would change the `dataPath` setting
// to be `hostname`, since this is where the host name data
// resides in the JSON string. Tyk automatically assumes that
// `dataPath` in this case is in a string-encoded JSON object and
// will try to deserialize it.
//
// Tyk classic API definition: `service_discovery.parent_data_path`
ParentDataPath string `bson:"parentDataPath,omitempty" json:"parentDataPath,omitempty"`
// PortDataPath is the port of the data path. In the above nested example, we can see that there is a separate `port` value
// for the service in the nested JSON. In this case, you can set the `portDataPath` value and Tyk will treat `dataPath` as
// the hostname and zip them together (this assumes that the hostname element does not end in a slash or resource identifier
// such as `/widgets/`). In the above example, the `portDataPath` would be `port`.
//
// Tyk classic API definition: `service_discovery.port_data_path`
PortDataPath string `bson:"portDataPath,omitempty" json:"portDataPath,omitempty"`
// UseTargetList should be set to `true`, if you are using load balancing. Tyk will treat the data path as a list and
// inject it into the target list of your API definition.
//
// Tyk classic API definition: `service_discovery.use_target_list`
UseTargetList bool `bson:"useTargetList,omitempty" json:"useTargetList,omitempty"`
// CacheTimeout is the timeout of a cache value when a new data is loaded from a discovery service.
// Setting it too low will cause Tyk to call the SD service too often, setting it too high could mean that
// failures are not recovered from quickly enough.
//
// Deprecated: The field is deprecated, usage needs to be updated to configure caching.
//
// Tyk classic API definition: `service_discovery.cache_timeout`
CacheTimeout int64 `bson:"cacheTimeout,omitempty" json:"cacheTimeout,omitempty"`
// Cache holds cache related flags.
//
// Tyk classic API definition:
// - `service_discovery.cache_disabled`
// - `service_discovery.cache_timeout`
Cache *ServiceDiscoveryCache `bson:"cache,omitempty" json:"cache,omitempty"`
// TargetPath is to set a target path to append to the discovered endpoint, since many SD services
// only provide host and port data. It is important to be able to target a specific resource on that host.
// Setting this value will enable that.
//
// Tyk classic API definition: `service_discovery.target_path`
TargetPath string `bson:"targetPath,omitempty" json:"targetPath,omitempty"`
// EndpointReturnsList is set `true` when the response type is a list instead of an object.
//
// Tyk classic API definition: `service_discovery.endpoint_returns_list`
EndpointReturnsList bool `bson:"endpointReturnsList,omitempty" json:"endpointReturnsList,omitempty"`
}
// ServiceDiscoveryCache holds configuration for caching ServiceDiscovery data.
type ServiceDiscoveryCache struct {
// Enabled turns service discovery cache on or off.
//
// Tyk classic API definition: `service_discovery.cache_disabled`
Enabled bool `bson:"enabled" json:"enabled"` // required
// Timeout is the TTL for a cached object in seconds.
//
// Tyk classic API definition: `service_discovery.cache_timeout`
Timeout int64 `bson:"timeout,omitempty" json:"timeout,omitempty"`
}
// CacheOptions returns the timeout value in effect, and a bool if cache is enabled.
func (sd *ServiceDiscovery) CacheOptions() (int64, bool) {
if sd.Cache != nil {
return sd.Cache.Timeout, sd.Cache.Enabled
}
return sd.CacheTimeout, sd.CacheTimeout > 0
}
// Fill fills *ServiceDiscovery from apidef.ServiceDiscoveryConfiguration.
func (sd *ServiceDiscovery) Fill(serviceDiscovery apidef.ServiceDiscoveryConfiguration) {
sd.Enabled = serviceDiscovery.UseDiscoveryService
sd.EndpointReturnsList = serviceDiscovery.EndpointReturnsList
sd.ParentDataPath = serviceDiscovery.ParentDataPath
sd.QueryEndpoint = serviceDiscovery.QueryEndpoint
sd.TargetPath = serviceDiscovery.TargetPath
sd.UseTargetList = serviceDiscovery.UseTargetList
sd.UseNestedQuery = serviceDiscovery.UseNestedQuery
sd.DataPath = serviceDiscovery.DataPath
sd.PortDataPath = serviceDiscovery.PortDataPath
enabled := !serviceDiscovery.CacheDisabled
timeout := serviceDiscovery.CacheTimeout
sd.CacheTimeout = 0
sd.Cache = &ServiceDiscoveryCache{
Enabled: enabled,
Timeout: timeout,
}
if ShouldOmit(sd.Cache) {
sd.Cache = nil
}
}
// ExtractTo extracts *ServiceDiscovery into *apidef.ServiceDiscoveryConfiguration.
func (sd *ServiceDiscovery) ExtractTo(serviceDiscovery *apidef.ServiceDiscoveryConfiguration) {
if sd == nil {
serviceDiscovery.CacheDisabled = true
return
}
serviceDiscovery.UseDiscoveryService = sd.Enabled
serviceDiscovery.EndpointReturnsList = sd.EndpointReturnsList
serviceDiscovery.ParentDataPath = sd.ParentDataPath
serviceDiscovery.QueryEndpoint = sd.QueryEndpoint
serviceDiscovery.TargetPath = sd.TargetPath
serviceDiscovery.UseTargetList = sd.UseTargetList
serviceDiscovery.UseNestedQuery = sd.UseNestedQuery
serviceDiscovery.DataPath = sd.DataPath
serviceDiscovery.PortDataPath = sd.PortDataPath
timeout, enabled := sd.CacheOptions()
serviceDiscovery.CacheTimeout = timeout
serviceDiscovery.CacheDisabled = !enabled
}
// Test holds the test configuration for service discovery.
type Test struct {
// ServiceDiscovery contains the configuration related to test Service Discovery.
// Tyk classic API definition: `proxy.service_discovery`
ServiceDiscovery *ServiceDiscovery `bson:"serviceDiscovery,omitempty" json:"serviceDiscovery,omitempty"`
}
// Fill fills *Test from apidef.UptimeTests.
func (t *Test) Fill(uptimeTests apidef.UptimeTests) {
if t.ServiceDiscovery == nil {
t.ServiceDiscovery = &ServiceDiscovery{}
}
t.ServiceDiscovery.Fill(uptimeTests.Config.ServiceDiscovery)
if ShouldOmit(t.ServiceDiscovery) {
t.ServiceDiscovery = nil
}
}
// ExtractTo extracts *Test into *apidef.UptimeTests.
func (t *Test) ExtractTo(uptimeTests *apidef.UptimeTests) {
t.ServiceDiscovery.ExtractTo(&uptimeTests.Config.ServiceDiscovery)
}
// MutualTLS holds configuration related to mTLS on APIs, domain to certificate mappings.
type MutualTLS struct {
// Enabled enables/disables upstream mutual TLS auth for the API.
// Tyk classic API definition: `upstream_certificates_disabled`
Enabled bool `bson:"enabled" json:"enabled"`
// DomainToCertificates maintains the mapping of domain to certificate.
// Tyk classic API definition: `upstream_certificates`
DomainToCertificates []DomainToCertificate `bson:"domainToCertificateMapping" json:"domainToCertificateMapping"`
}
// DomainToCertificate holds a single mapping of domain name into a certificate.
type DomainToCertificate struct {
// Domain contains the domain name.
Domain string `bson:"domain" json:"domain"`
// Certificate contains the certificate mapped to the domain.
Certificate string `bson:"certificate" json:"certificate"`
}
// Fill fills *MutualTLS from apidef.APIDefinition.
func (m *MutualTLS) Fill(api apidef.APIDefinition) {
m.Enabled = !api.UpstreamCertificatesDisabled
m.DomainToCertificates = make([]DomainToCertificate, len(api.UpstreamCertificates))
i := 0
for domain, cert := range api.UpstreamCertificates {
m.DomainToCertificates[i] = DomainToCertificate{Domain: domain, Certificate: cert}
i++
}
if ShouldOmit(m.DomainToCertificates) {
api.UpstreamCertificates = nil
}
}
// ExtractTo extracts *MutualTLS into *apidef.APIDefinition.
func (m *MutualTLS) ExtractTo(api *apidef.APIDefinition) {
api.UpstreamCertificatesDisabled = !m.Enabled
if len(m.DomainToCertificates) > 0 {
api.UpstreamCertificates = make(map[string]string)
}
for _, domainToCert := range m.DomainToCertificates {
api.UpstreamCertificates[domainToCert.Domain] = domainToCert.Certificate
}
}
// PinnedPublicKey contains a mapping from the domain name into a list of public keys.
type PinnedPublicKey struct {
// Domain contains the domain name.
Domain string `bson:"domain" json:"domain"`
// PublicKeys contains a list of the public keys pinned to the domain name.
PublicKeys []string `bson:"publicKeys" json:"publicKeys"`
}
// PinnedPublicKeys is a list of domains and pinned public keys for them.
type PinnedPublicKeys []PinnedPublicKey
// Fill fills *PinnerPublicKeys (slice) from publicKeys argument.
func (ppk PinnedPublicKeys) Fill(publicKeys map[string]string) {
domains := make([]string, len(publicKeys))
i := 0
for domain := range publicKeys {
domains[i] = domain
i++
}
sort.Slice(domains, func(i, j int) bool {
return domains[i] < domains[j]
})
i = 0
for _, domain := range domains {
ppk[i] = PinnedPublicKey{Domain: domain, PublicKeys: strings.Split(strings.ReplaceAll(publicKeys[domain], " ", ""), ",")}
i++
}
}
// ExtractTo extracts PinnedPublicKeys values into the publicKeys map.
func (ppk PinnedPublicKeys) ExtractTo(publicKeys map[string]string) {
for _, publicKey := range ppk {
publicKeys[publicKey.Domain] = strings.Join(publicKey.PublicKeys, ",")
}
}
// CertificatePinning holds the configuration about mapping of domains to pinned public keys.
type CertificatePinning struct {
// Enabled is a boolean flag, if set to `true`, it enables certificate pinning for the API.
//
// Tyk classic API definition: `certificate_pinning_disabled`
Enabled bool `bson:"enabled" json:"enabled"`
// DomainToPublicKeysMapping maintains the mapping of domain to pinned public keys.
//
// Tyk classic API definition: `pinned_public_keys`
DomainToPublicKeysMapping PinnedPublicKeys `bson:"domainToPublicKeysMapping" json:"domainToPublicKeysMapping"`
}
// Fill fills *CertificatePinning from apidef.APIDefinition.
func (cp *CertificatePinning) Fill(api apidef.APIDefinition) {
cp.Enabled = !api.CertificatePinningDisabled
if cp.DomainToPublicKeysMapping == nil {
cp.DomainToPublicKeysMapping = make(PinnedPublicKeys, len(api.PinnedPublicKeys))
}
cp.DomainToPublicKeysMapping.Fill(api.PinnedPublicKeys)
if ShouldOmit(cp.DomainToPublicKeysMapping) {
cp.DomainToPublicKeysMapping = nil
}
}
// ExtractTo extracts *CertficiatePinning into *apidef.APIDefinition.
func (cp *CertificatePinning) ExtractTo(api *apidef.APIDefinition) {
api.CertificatePinningDisabled = !cp.Enabled
if len(cp.DomainToPublicKeysMapping) > 0 {
api.PinnedPublicKeys = make(map[string]string)
cp.DomainToPublicKeysMapping.ExtractTo(api.PinnedPublicKeys)
}
}