Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit 1c89d5c

Browse files
authored
Remove CORS configuration from subscription (#458)
1 parent 8cb5ea9 commit 1c89d5c

File tree

12 files changed

+76
-236
lines changed

12 files changed

+76
-236
lines changed

docs/api.md

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,6 @@ HTTP subscription response depends on [response object](#respond-to-an-http-requ
9898
* `404 Not Found` if there is no backing function registered for requested HTTP endpoint
9999
* `500 Internal Server Error` if the function invocation failed or the backing function didn't return [HTTP response object](#respond-to-an-http-request-event)
100100

101-
##### CORS
102-
103-
By default cross-origin resource sharing (CORS) is disabled for `sync` subscriptions. It can be enabled and configured
104-
per-subscription basis.
105-
106-
Event Gateway handles preflight `OPTIONS` requests for you. You don't need to setup subscription for `OPTIONS` method
107-
because the Event Gateway will respond with all appropriate headers.
108-
109101
#### Path parameters
110102

111103
The Event Gateway allows creating HTTP subscription with parameterized paths. Every path segment prefixed with `:` is
@@ -134,11 +126,6 @@ To respond to an HTTP event a function needs to return object with following fie
134126

135127
Currently, the event gateway supports only string responses.
136128

137-
### CORS
138-
139-
Events API supports CORS requests which means that any origin can emit a custom event. In case of `sync` subscriptions CORS is
140-
configured per-subscription basis.
141-
142129
## Configuration API
143130

144131
[OpenAPI spec](./openapi/openapi-config-api.yaml)
@@ -309,11 +296,6 @@ JSON object:
309296
* `functionId` - `string` - ID of function to receive events
310297
* `path` - `string` - optional, URL path under which events (HTTP requests) are accepted, default: `/`
311298
* `method` - `string` - optional, HTTP method that accepts requests, default: `POST`
312-
* `cors` - `object` - optional, by default CORS is disabled for `sync` subscriptions. When set to empty object CORS configuration will use default values for all fields below. Available fields:
313-
* `origins` - `array` of `string` - list of allowed origins. An origin may contain a wildcard (\*) to replace 0 or more characters (i.e.: http://\*.domain.com), default: `*`
314-
* `methods` - `array` of `string` - list of allowed methods, default: `HEAD`, `GET`, `POST`
315-
* `headers` - `array` of `string` - list of allowed headers, default: `Origin`, `Accept`, `Content-Type`
316-
* `allowCredentials` - `bool` - default: false
317299

318300
**Response**
319301

@@ -331,7 +313,6 @@ JSON object:
331313
* `functionId` - function ID
332314
* `method` - `string` - HTTP method that accepts requests
333315
* `path` - `string` - path that accepts requests, starts with `/`
334-
* `cors` - `object` - optional, CORS configuration
335316

336317
---
337318

@@ -350,11 +331,6 @@ _Note that `type`, `eventType`, `functionId`, `path`, and `method` may not be up
350331
* `functionId` - `string` - ID of function to receive events
351332
* `path` - `string` - optional, URL path under which events (HTTP requests) are accepted, default: `/`
352333
* `method` - `string` - optional, HTTP method that accepts requests, default: `POST`
353-
* `cors` - `object` - optional, by default CORS is disabled for `sync` subscriptions. When set to empty object CORS configuration will use default values for all fields below. Available fields:
354-
* `origins` - `array` of `string` - list of allowed origins. An origin may contain a wildcard (\*) to replace 0 or more characters (i.e.: http://\*.domain.com), default: `*`
355-
* `methods` - `array` of `string` - list of allowed methods, default: `HEAD`, `GET`, `POST`
356-
* `headers` - `array` of `string` - list of allowed headers, default: `Origin`, `Accept`, `Content-Type`
357-
* `allowCredentials` - `bool` - default: false
358334

359335
**Response**
360336

@@ -373,7 +349,6 @@ JSON object:
373349
* `functionId` - function ID
374350
* `method` - `string` - HTTP method that accepts requests
375351
* `path` - `string` - path that accepts requests, starts with `/`
376-
* `cors` - `object` - optional, CORS configuration
377352

378353
---
379354

@@ -414,7 +389,6 @@ JSON object:
414389
* `functionId` - function ID
415390
* `method` - `string` - HTTP method that accepts requests
416391
* `path` - `string` - path that accepts requests, starts with `/`
417-
* `cors` - `object` - optional, CORS configuration
418392

419393
#### Get Subscription
420394

@@ -438,7 +412,6 @@ JSON object:
438412
* `functionId` - function ID
439413
* `method` - `string` - HTTP method that accepts requests
440414
* `path` - `string` - path that accepts requests, starts with `/`
441-
* `cors` - `object` - optional, CORS configuration
442415

443416
### Prometheus Metrics
444417

docs/openapi/openapi-config-api.yaml

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -290,8 +290,6 @@ components:
290290
$ref: '#/components/schemas/Path'
291291
method:
292292
$ref: '#/components/schemas/Method'
293-
cors:
294-
$ref: '#/components/schemas/CORS'
295293
Subscriptions:
296294
type: object
297295
properties:
@@ -400,32 +398,6 @@ components:
400398
properties:
401399
message:
402400
type: string
403-
CORS:
404-
type: object
405-
description: "(only for HTTP event) CORS configuration for HTTP event subscription"
406-
default: null
407-
properties:
408-
origins:
409-
type: array
410-
description: "list of allowed origins. An origin may contain a wildcard (*) to replace 0 or more characters (i.e.: http://*.domain.com)"
411-
items:
412-
type: string
413-
default: ["*"]
414-
methods:
415-
type: array
416-
description: "list of allowed methods"
417-
items:
418-
type: string
419-
default: ["HEAD", "GET", "POST"]
420-
headers:
421-
type: array
422-
description: "list of allowed headers"
423-
items:
424-
type: string
425-
default: ["Origin", "Accept", "Content-Type"]
426-
allowCredentials:
427-
type: boolean
428-
default: false
429401
Errors:
430402
type: object
431403
description: "error response object"
@@ -498,8 +470,6 @@ components:
498470
$ref: '#/components/schemas/Path'
499471
method:
500472
$ref: '#/components/schemas/Method'
501-
cors:
502-
$ref: '#/components/schemas/CORS'
503473
UpdateSubscription:
504474
description: "subscription update request body"
505475
content:
@@ -517,8 +487,6 @@ components:
517487
$ref: '#/components/schemas/Path'
518488
method:
519489
$ref: '#/components/schemas/Method'
520-
cors:
521-
$ref: '#/components/schemas/CORS'
522490
responses:
523491
Error:
524492
description: "internal server error"

httpapi/httpapi_test.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -514,17 +514,9 @@ func TestUpdateSubscription(t *testing.T) {
514514
FunctionID: "func",
515515
Method: "GET",
516516
Path: "/",
517-
CORS: &subscription.CORS{
518-
Origins: []string{"*"},
519-
Methods: []string{"HEAD", "GET", "POST"},
520-
Headers: []string{"Origin", "Accept", "Content-Type"},
521-
AllowCredentials: false,
522-
},
523517
}
524518
updatedValue := []byte(`{"space":"default","subscriptionId":"testid","type":"sync",` +
525-
`"eventType":"http.request","functionId":"func","method":"GET","path":"/",` +
526-
`"cors":{"origins":["*"],"methods":["HEAD","GET","POST"],` +
527-
`"headers":["Origin","Accept","Content-Type"],"allowCredentials":false}}`)
519+
`"eventType":"http.request","functionId":"func","method":"GET","path":"/"}`)
528520

529521
t.Run("subscription updated", func(t *testing.T) {
530522
subscriptions.EXPECT().UpdateSubscription(subscription.ID("testid"), updateSub).Return(updateSub, nil)

internal/cache/subscription_cache.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func (c *subscriptionCache) Modified(k string, v []byte) {
4949
root = pathtree.NewNode()
5050
c.endpoints[s.Method] = root
5151
}
52-
err := root.AddRoute(s.Path, s.Space, s.FunctionID, s.CORS)
52+
err := root.AddRoute(s.Path, s.Space, s.FunctionID)
5353
if err != nil {
5454
c.log.Error("Could not add path to the tree.", zap.Error(err), zap.String("path", s.Path), zap.String("method", s.Method))
5555
}

internal/cache/subscription_cache_test.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/serverless/event-gateway/function"
77
"github.com/serverless/event-gateway/libkv"
8-
"github.com/serverless/event-gateway/subscription"
98
"github.com/stretchr/testify/assert"
109

1110
"go.uber.org/zap"
@@ -59,29 +58,11 @@ func TestSubscriptionCacheModifiedSyncSubscription(t *testing.T) {
5958
"path": "/b",
6059
"method": "GET"}`))
6160

62-
space, id, _, _ := scache.endpoints["GET"].Resolve("/a")
61+
space, id, _ := scache.endpoints["GET"].Resolve("/a")
6362
assert.Equal(t, function.ID("testfunc1"), *id)
6463
assert.Equal(t, "default", space)
6564
}
6665

67-
func TestSubscriptionCacheModifiedCORSConfiguration(t *testing.T) {
68-
scache := newSubscriptionCache(zap.NewNop())
69-
70-
scache.Modified("testsub1", []byte(`{
71-
"subscriptionId":"testsub1",
72-
"type": "sync",
73-
"eventType": "http.request",
74-
"functionId": "testfunc1",
75-
"path": "/a",
76-
"method": "GET",
77-
"cors": {
78-
"origins": ["http://example.com"]
79-
}}`))
80-
81-
_, _, _, corsConfig := scache.endpoints["GET"].Resolve("/a")
82-
assert.Equal(t, &subscription.CORS{Origins: []string{"http://example.com"}}, corsConfig)
83-
}
84-
8566
func TestSubscriptionCacheModifiedEventsWrongPayload(t *testing.T) {
8667
scache := newSubscriptionCache(zap.NewNop())
8768

@@ -139,7 +120,7 @@ func TestSubscriptionCacheModifiedHTTPSubscriptionDeleted(t *testing.T) {
139120
"path": "/",
140121
"method": "GET"}`))
141122

142-
space, id, _, _ := scache.endpoints["GET"].Resolve("/")
123+
space, id, _ := scache.endpoints["GET"].Resolve("/")
143124
assert.Nil(t, id)
144125
assert.Equal(t, "", space)
145126
}

internal/cache/target.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ func (tc *Target) SyncSubscriber(method, path string, eventType eventpkg.TypeNam
4040
return nil
4141
}
4242

43-
space, functionID, params, cors := root.Resolve(path)
43+
space, functionID, params := root.Resolve(path)
4444
if functionID == nil {
4545
return nil
4646
}
@@ -49,7 +49,6 @@ func (tc *Target) SyncSubscriber(method, path string, eventType eventpkg.TypeNam
4949
Space: space,
5050
FunctionID: *functionID,
5151
Params: params,
52-
CORS: cors,
5352
}
5453
}
5554

internal/pathtree/tree.go

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"strings"
77

88
"github.com/serverless/event-gateway/function"
9-
"github.com/serverless/event-gateway/subscription"
109
)
1110

1211
// Node is a data structure, inspired by prefix tree, used for routing HTTP requests in the Event Gateway. It's used for creating tree structure
@@ -16,7 +15,6 @@ type Node struct {
1615
children map[string]*Node
1716
space string
1817
functionID *function.ID
19-
cors *subscription.CORS
2018
parameter string
2119
isStatic bool
2220
isParameter bool
@@ -35,11 +33,10 @@ type Params map[string]string
3533

3634
// AddRoute adds route to the tree. This function will panic in case of adding conflicting parameterized paths.
3735
// nolint: gocyclo
38-
func (n *Node) AddRoute(route string, space string, functionID function.ID, corsConfig *subscription.CORS) error {
36+
func (n *Node) AddRoute(route string, space string, functionID function.ID) error {
3937
if route == "/" {
4038
n.space = space
4139
n.functionID = &functionID
42-
n.cors = corsConfig
4340
return nil
4441
}
4542

@@ -62,7 +59,6 @@ func (n *Node) AddRoute(route string, space string, functionID function.ID, cors
6259
if i == len(segments)-1 {
6360
currentNode.children[segment].space = space
6461
currentNode.children[segment].functionID = &functionID
65-
currentNode.children[segment].cors = corsConfig
6662
return nil
6763
}
6864
currentNode = currentNode.children[segment]
@@ -96,7 +92,6 @@ func (n *Node) AddRoute(route string, space string, functionID function.ID, cors
9692
}
9793
currentNode.children[segment].space = space
9894
currentNode.children[segment].functionID = &functionID
99-
currentNode.children[segment].cors = corsConfig
10095
return nil
10196
}
10297
currentNode = currentNode.children[segment]
@@ -110,7 +105,6 @@ func (n *Node) DeleteRoute(route string) error {
110105
if route == "/" {
111106
n.space = ""
112107
n.functionID = nil
113-
n.cors = nil
114108
return nil
115109
}
116110

@@ -129,7 +123,6 @@ func (n *Node) DeleteRoute(route string) error {
129123
} else {
130124
currentNode.children[segment].space = ""
131125
currentNode.children[segment].functionID = nil
132-
currentNode.children[segment].cors = nil
133126
}
134127

135128
return nil
@@ -143,12 +136,12 @@ func (n *Node) DeleteRoute(route string) error {
143136

144137
// Resolve takes request URL path and traverse the tree trying find corresponding route.
145138
// nolint: gocyclo
146-
func (n *Node) Resolve(path string) (string, *function.ID, Params, *subscription.CORS) {
139+
func (n *Node) Resolve(path string) (string, *function.ID, Params) {
147140
if path == "/" {
148141
if n.functionID != nil {
149-
return n.space, n.functionID, nil, n.cors
142+
return n.space, n.functionID, nil
150143
}
151-
return "", nil, nil, nil
144+
return "", nil, nil
152145
}
153146

154147
segments := toSegments(path)
@@ -162,7 +155,7 @@ func (n *Node) Resolve(path string) (string, *function.ID, Params, *subscription
162155
// look for param
163156
child, exists = first(currentNode.children)
164157
if !exists || !(child.isParameter || child.isWildcard) {
165-
return "", nil, nil, nil
158+
return "", nil, nil
166159
}
167160
}
168161
currentNode = child
@@ -174,15 +167,15 @@ func (n *Node) Resolve(path string) (string, *function.ID, Params, *subscription
174167
if currentNode.isWildcard {
175168
// add missing parts
176169
params[currentNode.parameter] = strings.Join(segments[i:], "/")
177-
return currentNode.space, currentNode.functionID, params, currentNode.cors
170+
return currentNode.space, currentNode.functionID, params
178171
}
179172

180173
if i == len(segments)-1 {
181-
return currentNode.space, currentNode.functionID, params, currentNode.cors
174+
return currentNode.space, currentNode.functionID, params
182175
}
183176
}
184177

185-
return "", nil, nil, nil
178+
return "", nil, nil
186179
}
187180

188181
func toSegments(route string) []string {

0 commit comments

Comments
 (0)