Skip to content

Commit

Permalink
Update CORS config
Browse files Browse the repository at this point in the history
  • Loading branch information
levichevdmitry committed Jul 30, 2021
1 parent 7da4db3 commit 932be3b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 14 deletions.
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# <img src="https://uploads-ssl.webflow.com/5ea5d3315186cf5ec60c3ee4/5edf1c94ce4c859f2b188094_logo.svg" alt="Pip.Services Logo" width="200"> <br/> Remote Procedure Calls for Pip.Services in Go Changelog


## <a name="1.4.2"></a> 1.4.2 (2021-07-30)
### Features
* Add configuration parameters for CORS Headers in HttpEndpoint. Use *cors_headers* and *cors_origins*.
Example:
```yml
-cors_headers: "correlation_id, access_token, Accept, Content-Type, Content-Length, X-CSRF-Token"
-cors_origins: "*"
```
## <a name="1.4.1"></a> 1.4.1 (2021-07-26)
### Bug fixing
- Fix route checks in interceptors
Expand Down
17 changes: 10 additions & 7 deletions services/HttpEndpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,17 @@ func (c *HttpEndpoint) Configure(config *cconf.ConfigParams) {
c.fileMaxSize = config.GetAsLongWithDefault("options.file_max_size", c.fileMaxSize)
c.protocolUpgradeEnabled = config.GetAsBooleanWithDefault("options.protocol_upgrade_enabled", c.protocolUpgradeEnabled)

corsParams := config.GetSection("cors-headers")
headers := corsParams.GetSectionNames()
headers := strings.Split(config.GetAsStringWithDefault("cors_headers", ""), ",")
if headers != nil && len(headers) > 0 {
for _, key := range headers {
origin := corsParams.GetAsString(key)
if len(origin) > 0 {
c.AddCorsHeader(key, origin)
}
for _, header := range headers {
c.AddCorsHeader(strings.TrimSpace(header), "")
}
}

origins := strings.Split(config.GetAsStringWithDefault("cors_origins", ""), ",")
if origins != nil && len(origins) > 0 {
for _, origin := range origins {
c.AddCorsHeader("", strings.TrimSpace(origin))
}
}
}
Expand Down
21 changes: 14 additions & 7 deletions test/services/Main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,8 @@ func BuildTestStatusRestService() *services.StatusRestService {
"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", StatusRestServicePort,
"cors-headerscorrelation_id", "*",
"cors_headers", "correlation_id, access_token, Accept, Content-Type, Content-Length, X-CSRF-Token",
"cors_origins", "*",
)

service := services.NewStatusRestService()
Expand All @@ -133,7 +134,8 @@ func BuildTestHttpEndpointService() (*DummyRestService, *services.HttpEndpoint)
"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", HttpEndpointServicePort,
"cors-headerscorrelation_id", "*",
"cors_headers", "correlation_id, access_token, Accept, Content-Type, Content-Length, X-CSRF-Token",
"cors_origins", "*",
)

ctrl := tlogic.NewDummyController()
Expand Down Expand Up @@ -163,7 +165,8 @@ func BuildTestDummyRestService() *DummyRestService {
"connection.port", DummyRestServicePort,
"openapi_content", "swagger yaml or json content",
"swagger.enable", "true",
"cors-headerscorrelation_id", "*",
"cors_headers", "correlation_id, access_token, Accept, Content-Type, Content-Length, X-CSRF-Token",
"cors_origins", "*",
)

var service *DummyRestService
Expand Down Expand Up @@ -204,7 +207,8 @@ func BuildTestDummyOpenAPIFileRestService() (*DummyRestService, string) {
"connection.port", DummyOpenAPIFileRestServicePort,
"openapi_file", filename, // for test only
"swagger.enable", "true",
"cors-headerscorrelation_id", "*",
"cors_headers", "correlation_id, access_token, Accept, Content-Type, Content-Length, X-CSRF-Token",
"cors_origins", "*",
)

var service *DummyRestService
Expand All @@ -228,7 +232,8 @@ func BuildTestDummyCommandableHttpService() *DummyCommandableHttpService {
"connection.host", "localhost",
"connection.port", DummyCommandableHttpServicePort,
"swagger.enable", "true",
"cors-headerscorrelation_id", "*",
"cors_headers", "correlation_id, access_token, Accept, Content-Type, Content-Length, X-CSRF-Token",
"cors_origins", "*",
)

ctrl := tlogic.NewDummyController()
Expand All @@ -253,7 +258,8 @@ func BuildTestDummyCommandableSwaggerHttpService() *DummyCommandableHttpService
"connection.port", DummyCommandableSwaggerHttpServicePort,
"swagger.enable", "true",
"swagger.auto", false,
"cors-headerscorrelation_id", "*",
"cors_headers", "correlation_id, access_token, Accept, Content-Type, Content-Length, X-CSRF-Token",
"cors_origins", "*",
)

ctrl := tlogic.NewDummyController()
Expand All @@ -275,7 +281,8 @@ func BuildTestHeartbeatRestService() *services.HeartbeatRestService {
"connection.protocol", "http",
"connection.host", "localhost",
"connection.port", HeartbeatRestServicePort,
"cors-headerscorrelation_id", "*",
"cors_headers", "correlation_id, access_token, Accept, Content-Type, Content-Length, X-CSRF-Token",
"cors_origins", "*",
)

service := services.NewHeartbeatRestService()
Expand Down

0 comments on commit 932be3b

Please sign in to comment.