Skip to content

Commit

Permalink
Merge pull request #24 from krakend/update_dependecies
Browse files Browse the repository at this point in the history
Update dependencies
  • Loading branch information
kpacha committed Jul 12, 2024
2 parents 06f054a + 85562b7 commit 705666f
Show file tree
Hide file tree
Showing 8 changed files with 276 additions and 143 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.17
go-version: '1.20'

- name: Test
run: go test -v ./...
36 changes: 27 additions & 9 deletions cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ const Namespace = "github_com/devopsfaith/krakend-cors"

// Config holds the configuration of CORS
type Config struct {
AllowOrigins []string
AllowMethods []string
AllowHeaders []string
ExposeHeaders []string
AllowCredentials bool
MaxAge time.Duration
Debug bool
AllowOrigins []string
AllowMethods []string
AllowHeaders []string
ExposeHeaders []string
AllowCredentials bool
AllowPrivateNetwork bool
OptionsPassthrough bool
OptionsSuccessStatus int
MaxAge time.Duration
Debug bool
}

// ConfigGetter implements the config.ConfigGetter interface. It parses the extra config an allowed
Expand All @@ -35,7 +38,6 @@ func ConfigGetter(e config.ExtraConfig) interface{} {

cfg := Config{}
cfg.AllowOrigins = getList(tmp, "allow_origins")

cfg.AllowMethods = getList(tmp, "allow_methods")
cfg.AllowHeaders = getList(tmp, "allow_headers")
cfg.ExposeHeaders = getList(tmp, "expose_headers")
Expand All @@ -51,6 +53,22 @@ func ConfigGetter(e config.ExtraConfig) interface{} {
cfg.Debug = ok && v
}

if allowPrivateNetwork, ok := tmp["allow_private_network"]; ok {
v, ok := allowPrivateNetwork.(bool)
cfg.AllowPrivateNetwork = ok && v
}

if optionsPassthrough, ok := tmp["options_passthrough"]; ok {
v, ok := optionsPassthrough.(bool)
cfg.OptionsPassthrough = ok && v
}

if optionsSuccessStatus, ok := tmp["options_success_status"]; ok {
if v, ok := optionsSuccessStatus.(float64); ok {
cfg.OptionsSuccessStatus = int(v)
}
}

if maxAge, ok := tmp["max_age"]; ok {
if d, err := time.ParseDuration(maxAge.(string)); err == nil {
cfg.MaxAge = d
Expand All @@ -60,7 +78,7 @@ func ConfigGetter(e config.ExtraConfig) interface{} {
}

func getList(data map[string]interface{}, name string) []string {
out := []string{}
var out []string
if vs, ok := data[name]; ok {
if v, ok := vs.([]interface{}); ok {
for _, s := range v {
Expand Down
28 changes: 22 additions & 6 deletions gin/cors.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,29 @@ func New(e config.ExtraConfig) gin.HandlerFunc {
return nil
}

if len(cfg.AllowOrigins) == 0 {
cfg.AllowOrigins = []string{"*"}
}
if len(cfg.AllowHeaders) == 0 {
cfg.AllowHeaders = []string{"*"}
}
// Maintain the old default value to not change behaviour
// the rs/cors new default is to return a 204
if cfg.OptionsSuccessStatus == 0 {
cfg.OptionsSuccessStatus = 200
}

return wrapper.New(cors.Options{
AllowedOrigins: cfg.AllowOrigins,
AllowedMethods: cfg.AllowMethods,
AllowedHeaders: cfg.AllowHeaders,
ExposedHeaders: cfg.ExposeHeaders,
AllowCredentials: cfg.AllowCredentials,
MaxAge: int(cfg.MaxAge.Seconds()),
AllowedOrigins: cfg.AllowOrigins,
AllowedMethods: cfg.AllowMethods,
AllowedHeaders: cfg.AllowHeaders,
ExposedHeaders: cfg.ExposeHeaders,
AllowCredentials: cfg.AllowCredentials,
AllowPrivateNetwork: cfg.AllowPrivateNetwork,
OptionsPassthrough: cfg.OptionsPassthrough,
OptionsSuccessStatus: cfg.OptionsSuccessStatus,
MaxAge: int(cfg.MaxAge.Seconds()),
Debug: cfg.Debug,
})
}

Expand Down
15 changes: 9 additions & 6 deletions gin/cors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ func TestNew(t *testing.T) {
}
}`)
json.Unmarshal(serialized, &sampleCfg)
e := gin.Default()
gin.SetMode(gin.TestMode)
e := gin.New()
corsMw := New(sampleCfg)
if corsMw == nil {
t.Error("The cors middleware should not be nil.\n")
Expand All @@ -48,7 +49,7 @@ func TestNew(t *testing.T) {
"Vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
"Access-Control-Allow-Origin": "http://foobar.com",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Origin",
"Access-Control-Allow-Headers": "origin",
"Access-Control-Max-Age": "7200",
})
}
Expand All @@ -60,7 +61,8 @@ func TestAllowOriginWildcard(t *testing.T) {
}
}`)
json.Unmarshal(serialized, &sampleCfg)
e := gin.Default()
gin.SetMode(gin.TestMode)
e := gin.New()
corsMw := New(sampleCfg)
if corsMw == nil {
t.Error("The cors middleware should not be nil.\n")
Expand All @@ -81,7 +83,7 @@ func TestAllowOriginWildcard(t *testing.T) {
"Vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Origin",
"Access-Control-Allow-Headers": "origin",
})
}

Expand All @@ -91,7 +93,8 @@ func TestAllowOriginEmpty(t *testing.T) {
}
}`)
json.Unmarshal(serialized, &sampleCfg)
e := gin.Default()
gin.SetMode(gin.TestMode)
e := gin.New()
corsMw := New(sampleCfg)
if corsMw == nil {
t.Error("The cors middleware should not be nil.\n")
Expand All @@ -112,7 +115,7 @@ func TestAllowOriginEmpty(t *testing.T) {
"Vary": "Origin, Access-Control-Request-Method, Access-Control-Request-Headers",
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET",
"Access-Control-Allow-Headers": "Origin",
"Access-Control-Allow-Headers": "origin",
})
}

Expand Down
41 changes: 25 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
module github.com/krakendio/krakend-cors/v2

require (
github.com/gin-gonic/gin v1.7.7
github.com/luraproject/lura/v2 v2.0.5
github.com/rs/cors v1.6.0
github.com/gin-gonic/gin v1.9.1
github.com/luraproject/lura/v2 v2.6.3
github.com/rs/cors v1.11.0
)

require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.0 // indirect
github.com/go-playground/universal-translator v0.18.0 // indirect
github.com/go-playground/validator/v10 v10.9.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/krakendio/flatmap v1.1.1 // indirect
github.com/leodido/go-urn v1.2.1 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/ugorji/go/codec v1.2.6 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/rs/cors/wrapper/gin v0.0.0-20240515105523-1562b1715b35 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/valyala/fastrand v1.1.0 // indirect
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519 // indirect
golang.org/x/sys v0.0.0-20211004093028-2c5d950f24ef // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

go 1.17
go 1.20
Loading

0 comments on commit 705666f

Please sign in to comment.