Skip to content

Commit

Permalink
Merge pull request #106 from pitabwire/freature/GH-105
Browse files Browse the repository at this point in the history
enable cors policies on services
  • Loading branch information
pitabwire authored Dec 12, 2023
2 parents 5d095b6 + de62c2a commit 79180d2
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
26 changes: 25 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ type ConfigurationDefault struct {
HttpServerPort string `default:":8080" envconfig:"HTTP_PORT"`
GrpcServerPort string `default:":50051" envconfig:"GRPC_PORT"`

AllowedOrigins string `default:"*" envconfig:"ALLOWED_ORIGINS"`
CORSEnabled bool `default:"false" envconfig:"CORS_ENABLED"`
CORSAllowedHeaders []string `default:"Authorization" envconfig:"CORS_ALLOWED_HEADERS"`
CORSAllowedOrigins []string `default:"*" envconfig:"CORS_ALLOWED_ORIGINS"`
CORSAllowedMethods []string `default:"GET,HEAD,POST,PUT,OPTIONS" envconfig:"CORS_ALLOWED_METHODS"`

TLSCertificatePath string `envconfig:"TLS_CERTIFICATE_PATH"`
TLSCertificateKeyPath string `envconfig:"TLS_CERTIFICATE_KEY_PATH"`
Expand Down Expand Up @@ -92,6 +95,27 @@ func (c *ConfigurationDefault) GrpcPort() string {
return c.Port()
}

type ConfigurationCORS interface {
IsCORSEnabled() bool
GetCORSAllowedHeaders() []string
GetCORSAllowedOrigins() []string
GetCORSAllowedMethods() []string
}

func (c *ConfigurationDefault) IsCORSEnabled() bool {
return c.CORSEnabled
}

func (c *ConfigurationDefault) GetCORSAllowedHeaders() []string {
return c.CORSAllowedHeaders
}
func (c *ConfigurationDefault) GetCORSAllowedOrigins() []string {
return c.CORSAllowedOrigins
}
func (c *ConfigurationDefault) GetCORSAllowedMethods() []string {
return c.CORSAllowedMethods
}

type ConfigurationOAUTH2 interface {
GetOauthWellKnownJwk() string
GetOauth2ServiceURI() string
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/BurntSushi/toml v1.3.2
github.com/alitto/pond v1.8.3
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/gorilla/handlers v1.5.2
github.com/improbable-eng/grpc-web v0.15.0
github.com/jackc/pgx/v5 v5.5.1
github.com/kelseyhightower/envconfig v1.4.0
Expand All @@ -19,7 +20,7 @@ require (
golang.org/x/net v0.19.0
golang.org/x/text v0.14.0
google.golang.org/grpc v1.60.0
google.golang.org/grpc/examples v0.0.0-20230828201338-8b1a67102205
google.golang.org/grpc/examples v0.0.0-20231208223803-52baf161f30f
gorm.io/datatypes v1.2.0
gorm.io/driver/postgres v1.5.4
gorm.io/gorm v1.25.5
Expand All @@ -29,6 +30,7 @@ require (
require (
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/desertbit/timer v0.0.0-20180107155436-c41aec40b27f // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/go-logr/logr v1.3.0 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
Expand Down
10 changes: 6 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ github.com/envoyproxy/go-control-plane v0.9.1-0.20191026205805-5f8ba28d4473/go.m
github.com/envoyproxy/go-control-plane v0.9.4/go.mod h1:6rpuAdCZL397s3pYoYcLgu1mIlRU8Am5FuJP05cCM98=
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/felixge/httpsnoop v1.0.3 h1:s/nj+GCswXYzN5v2DpNMuMQYe+0DDwt5WVCU6CWBdXk=
github.com/felixge/httpsnoop v1.0.3/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db/go.mod h1:7dvUGVsVBjqR7JHJk0brhHOZYGmfBYOrK0ZhYMEtBr4=
github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8/go.mod h1:ZhphrRTfi2rbfLwlschooIH4+wKKDR4Pdxhh+TRoA20=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
Expand Down Expand Up @@ -170,6 +172,8 @@ github.com/googleapis/gax-go/v2 v2.12.0 h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56
github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/handlers v1.5.2 h1:cLTUSsNkgcwhgRqvCNmdbRWG0A3N4F+M2nWKdScwyEE=
github.com/gorilla/handlers v1.5.2/go.mod h1:dX+xVpaxdSw+q0Qek8SSsl3dfMk3jNddUkMzo0GtH0w=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
Expand Down Expand Up @@ -581,12 +585,10 @@ google.golang.org/grpc v1.27.0/go.mod h1:qbnxyOmOxrQa7FizSgH+ReBfzJrCY1pSN7KXBS8
google.golang.org/grpc v1.29.1/go.mod h1:itym6AZVZYACWQqET3MqgPpjcuV5QH3BxFS3IjizoKk=
google.golang.org/grpc v1.32.0/go.mod h1:N36X2cJ7JwdamYAgDz+s+rVMFjt3numwzf/HckM8pak=
google.golang.org/grpc v1.33.2/go.mod h1:JMHMWHQWaTccqQQlmk3MJZS+GWXOdAesneDmEnv2fbc=
google.golang.org/grpc v1.59.0 h1:Z5Iec2pjwb+LEOqzpB2MR12/eKFhDPhuqW91O+4bwUk=
google.golang.org/grpc v1.59.0/go.mod h1:aUPDwccQo6OTjy7Hct4AfBPD1GptF4fyUjIkQ9YtF98=
google.golang.org/grpc v1.60.0 h1:6FQAR0kM31P6MRdeluor2w2gPaS4SVNrD/DNTxrQ15k=
google.golang.org/grpc v1.60.0/go.mod h1:OlCHIeLYqSSsLi6i49B5QGdzaMZK9+M7LXN2FKz4eGM=
google.golang.org/grpc/examples v0.0.0-20230828201338-8b1a67102205 h1:U67x3F2YW9vMau2S65iu+7W2idbdLe/yqw2LlEYiu/c=
google.golang.org/grpc/examples v0.0.0-20230828201338-8b1a67102205/go.mod h1:Tv7vK6WISirChRuVsnMR6m9Quxyd9k2vPozP/nyYv/0=
google.golang.org/grpc/examples v0.0.0-20231208223803-52baf161f30f h1:szXwOVVgjYaQFDCjBEqymUAAJXJ1frFEXu7bXWOgl58=
google.golang.org/grpc/examples v0.0.0-20231208223803-52baf161f30f/go.mod h1:j5uROIAAgi3YmtiETMt1LW0d/lHqQ7wwrIY4uGRXLQ4=
google.golang.org/protobuf v0.0.0-20200109180630-ec00e32a8dfd/go.mod h1:DFci5gLYBciE7Vtevhsrf46CRTquxDuWsQurQQe4oz8=
google.golang.org/protobuf v0.0.0-20200221191635-4d8936d0db64/go.mod h1:kwYJMbMJ01Woi6D6+Kah6886xMZcty6N08ah7+eCXa0=
google.golang.org/protobuf v0.0.0-20200228230310-ab0ca4ff8a60/go.mod h1:cfTl7dwQJ+fmap5saPgwCLgHXTUD7jkjRqWcaiX5VyM=
Expand Down
12 changes: 11 additions & 1 deletion service.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"
"github.com/alitto/pond"
ghandler "github.com/gorilla/handlers"
"github.com/nicksnyder/go-i18n/v2/i18n"
"github.com/pitabwire/frame/internal"
"github.com/sirupsen/logrus"
Expand Down Expand Up @@ -324,7 +325,16 @@ func (s *Service) initServer(ctx context.Context, httpPort string) error {

mux.Handle("/", applicationHandler)

s.handler = mux
config, ok := s.Config().(ConfigurationCORS)
if ok && config.IsCORSEnabled() {
headersOpt := ghandler.AllowedHeaders(config.GetCORSAllowedHeaders())
originsOpt := ghandler.AllowedOrigins(config.GetCORSAllowedOrigins())
methodsOpt := ghandler.AllowedMethods(config.GetCORSAllowedMethods())

s.handler = ghandler.CORS(headersOpt, originsOpt, methodsOpt)(mux)
} else {
s.handler = mux
}

defaultServer := defaultDriver{
ctx: ctx,
Expand Down

0 comments on commit 79180d2

Please sign in to comment.