Skip to content

feat(cors): Added cross origin support #221

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
May 5, 2020
Merged

feat(cors): Added cross origin support #221

merged 13 commits into from
May 5, 2020

Conversation

msohailhussain
Copy link
Contributor

@msohailhussain msohailhussain commented Apr 30, 2020

Summary

  • Cross origin support added specifically when running from js.

TestPlan

  • Unit tests added that checks headers for preflight requests.

Issues

No Issues

@@ -94,6 +94,15 @@ func assertAPIAuth(t *testing.T, actual config.ServiceAuthConfig) {
assert.Equal(t, 25*time.Second, actual.JwksUpdateInterval)
}

func assertAPICORS(t *testing.T, actual config.CORSConfig) {
assert.Equal(t, []string{"http://test.com"}, actual.AllowedOrigins)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add one more domain.

@codecov
Copy link

codecov bot commented Apr 30, 2020

Codecov Report

Merging #221 into master will increase coverage by 0.17%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #221      +/-   ##
==========================================
+ Coverage   82.38%   82.55%   +0.17%     
==========================================
  Files          27       27              
  Lines        1243     1261      +18     
==========================================
+ Hits         1024     1041      +17     
- Misses        164      165       +1     
  Partials       55       55              
Impacted Files Coverage Δ
pkg/handlers/notification.go 82.81% <ø> (-0.27%) ⬇️
config/config.go 100.00% <100.00%> (ø)
pkg/routers/api.go 97.14% <100.00%> (+0.53%) ⬆️
pkg/server/server.go 90.00% <0.00%> (-1.43%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 73c375d...ce89726. Read the comment docs.

@msohailhussain msohailhussain changed the title Sohail/cors feat(cors): Added cross origin support Apr 30, 2020
@msohailhussain msohailhussain marked this pull request as ready for review April 30, 2020 22:07
@mikeproeng37 mikeproeng37 requested a review from a team May 1, 2020 00:37
@@ -66,6 +78,17 @@ var testAuthMiddleware = func(next http.Handler) http.Handler {
return http.HandlerFunc(fn)
}

var testCorsHandler = cors.Handler(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like we're testing the cors.Handler more than we're testing api.go. I think what we really need to test is that some handler was properly added where appropriate in the request path.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mikecdavis handler is added for all requests. The only way to verify is to check things are coming back in the response or not.
Please suggest if you want unit tests other than this approach.

config/config.go Outdated
}

// ToCorsOptions converts CORSConfig to cors middleware options
func (c *CORSConfig) ToCorsOptions() cors.Options {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit, but I'd prefer we didn't add this here as it feels like we're leaking a dependency. I think having a method in api.go would be a cleaner abstraction.

func CreateCorsHandler(c conf.CORSConfig) func(next http.Handler) http.Handler {
  options := cors.Options{
		AllowedOrigins:   c.AllowedOrigins,
		AllowedMethods:   c.AllowedMethods,
		AllowedHeaders:   c.AllowedHeaders,
		ExposedHeaders:   c.ExposedHeaders,
		AllowCredentials: c.AllowedCredentials,
		MaxAge:           c.MaxAge,
	}

  return cors.Handler(options)
}

config/config.go Outdated
Comment on lines 53 to 54
// If allowedOrigins is nil or empty, value is set to ["*"].
AllowedOrigins: nil,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this is defaulting to wide-open CORS support. We need to default to disabled then enable only if explicitly configured.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agree with this comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Discussed offline, it's OK to comment it out and give an idea to the user, CORS is not enabled. It will work as expected without cors before implementing it how it was working.

@mjc1283
Copy link
Contributor

mjc1283 commented May 4, 2020

@msohailhussain @mikecdavis I suggest this setting be used in the Notifications handler, which is currently hard-coded to Access-Control-Allow-Origin: *.

config.yaml Outdated
##
## admin service configuration
##
admin:
## http listener port
port: "8088"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remove this line.

@msohailhussain
Copy link
Contributor Author

#221 (comment)
I will take care of it @mjc1283

@msohailhussain
Copy link
Contributor Author

@mjc1283 I added this commit, cca1309
AccessControl header is set in parent handler. And it's only useful for OPTION requests, not for all.

Copy link
Contributor

@mikecdavis mikecdavis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One minor nit, else LGTM

config/config.go Outdated
// If AllowedOrigins is nil or empty, value is set to ["*"].
AllowedOrigins: nil,
// If AllowedMethods is nil or empty, value is set to (HEAD, GET and POST).
AllowedMethods: []string{"HEAD", "GET", "PUT", "POST", "DELETE"},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might as well default to nil or empty here rather than to extend the default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will do.

@mikeproeng37 mikeproeng37 merged commit c9751ee into master May 5, 2020
@mikeproeng37 mikeproeng37 deleted the sohail/cors branch May 5, 2020 19:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants