Skip to content

Commit ed8b9e9

Browse files
authored
Support badger full configuration from Souin configuration file (#101)
* Introduce badger configuration * Update tests and fix caddy dependency * Bump dependencies and fix JWT security issue
1 parent a22d936 commit ed8b9e9

File tree

449 files changed

+17820
-20029
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

449 files changed

+17820
-20029
lines changed

api/auth/jwt.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package auth
22

33
import (
44
"encoding/json"
5-
"github.com/dgrijalva/jwt-go"
5+
"github.com/golang-jwt/jwt"
66
"net/http"
77
"time"
88
)

cache/providers/badgerProvider.go

+20-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package providers
22

33
import (
4+
"encoding/json"
45
"fmt"
56
t "github.com/darkweak/souin/configurationtypes"
67
badger "github.com/dgraph-io/badger/v3"
8+
"github.com/imdario/mergo"
79
"net/http"
810
"regexp"
911
"time"
@@ -15,8 +17,24 @@ type Badger struct {
1517
}
1618

1719
// BadgerConnectionFactory function create new Badger instance
18-
func BadgerConnectionFactory(_ t.AbstractConfigurationInterface) (*Badger, error) {
19-
db, _ := badger.Open(badger.DefaultOptions("").WithInMemory(true))
20+
func BadgerConnectionFactory(c t.AbstractConfigurationInterface) (*Badger, error) {
21+
badgerConfiguration := c.GetDefaultCache().GetBadger()
22+
badgerOptions := badger.DefaultOptions(badgerConfiguration.Path)
23+
if badgerConfiguration.Configuration != nil {
24+
var parsedBadger badger.Options
25+
if b, e := json.Marshal(badgerConfiguration.Configuration); e == nil {
26+
if e = json.Unmarshal(b, &parsedBadger); e != nil {
27+
fmt.Println("Impossible to parse the configuration for the default provider (Badger)")
28+
}
29+
}
30+
31+
if err := mergo.Merge(&badgerOptions, parsedBadger, mergo.WithOverride); err != nil {
32+
fmt.Println("An error occurred during the badgerOptions merge from the default options with your configuration.")
33+
}
34+
} else {
35+
badgerOptions = badgerOptions.WithInMemory(true)
36+
}
37+
db, _ := badger.Open(badgerOptions)
2038

2139
return &Badger{db}, nil
2240
}

cache/providers/badgerProvider_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ func getBadgerClientAndMatchedURL(key string) (types.AbstractProviderInterface,
3030
)
3131
}
3232

33+
// This test ensure that Badger options are override by the Souin configuration
34+
func TestCustomBadgerConnectionFactory(t *testing.T) {
35+
c := tests.MockConfiguration(tests.BadgerConfiguration)
36+
r, err := BadgerConnectionFactory(c)
37+
38+
if nil != err {
39+
errors.GenerateError(t, "Shouldn't have panic")
40+
}
41+
42+
if nil == r {
43+
errors.GenerateError(t, "Badger should be instanciated")
44+
}
45+
}
46+
3347
func TestBadgerConnectionFactory(t *testing.T) {
3448
c := tests.MockConfiguration(tests.BaseConfiguration)
3549
r, err := BadgerConnectionFactory(c)
@@ -46,6 +60,7 @@ func TestBadgerConnectionFactory(t *testing.T) {
4660
func TestIShouldBeAbleToReadAndWriteDataInBadger(t *testing.T) {
4761
client, matchedURL := getBadgerClientAndMatchedURL("Test")
4862

63+
fmt.Println(client, matchedURL)
4964
client.Set("Test", []byte(BADGERVALUE), matchedURL, time.Duration(20)*time.Second)
5065
time.Sleep(1 * time.Second)
5166

configurationtypes/types.go

+7
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ type CacheProvider struct {
6969

7070
//DefaultCache configuration
7171
type DefaultCache struct {
72+
Badger CacheProvider `json:"badger" yaml:"badger"`
7273
Distributed bool `json:"distributed" yaml:"distributed"`
7374
Headers []string `json:"headers" yaml:"headers"`
7475
Olric CacheProvider `json:"olric" yaml:"olric"`
@@ -77,6 +78,11 @@ type DefaultCache struct {
7778
TTL Duration `json:"ttl" yaml:"ttl"`
7879
}
7980

81+
// GetBadger returns the Badger configuration
82+
func (d *DefaultCache) GetBadger() CacheProvider {
83+
return d.Badger
84+
}
85+
8086
// GetDistributed returns if it uses Olric or not as provider
8187
func (d *DefaultCache) GetDistributed() bool {
8288
return d.Distributed
@@ -104,6 +110,7 @@ func (d *DefaultCache) GetTTL() time.Duration {
104110

105111
// DefaultCacheInterface interface
106112
type DefaultCacheInterface interface {
113+
GetBadger() CacheProvider
107114
GetDistributed() bool
108115
GetOlric() CacheProvider
109116
GetHeaders() []string

go.mod

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@ require (
66
github.com/buraksezer/olric v0.3.11
77
github.com/dgraph-io/badger/v3 v3.2103.1
88
github.com/dgraph-io/ristretto v0.1.0
9-
github.com/dgrijalva/jwt-go v3.2.0+incompatible
109
github.com/fsnotify/fsnotify v1.4.9
1110
github.com/go-chi/stampede v0.4.5
11+
github.com/golang-jwt/jwt v3.2.2+incompatible
1212
github.com/google/uuid v1.3.0
13+
github.com/imdario/mergo v0.3.12
1314
github.com/pquerna/cachecontrol v0.1.0
1415
go.uber.org/zap v1.19.0
1516
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b

go.sum

+4-2
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,6 @@ github.com/dgraph-io/badger/v3 v3.2103.1 h1:zaX53IRg7ycxVlkd5pYdCeFp1FynD6qBGQoQ
3333
github.com/dgraph-io/badger/v3 v3.2103.1/go.mod h1:dULbq6ehJ5K0cGW/1TQ9iSfUk0gbSiToDWmWmTsJ53E=
3434
github.com/dgraph-io/ristretto v0.1.0 h1:Jv3CGQHp9OjuMBSne1485aDpUkTKEcUqF+jm/LuerPI=
3535
github.com/dgraph-io/ristretto v0.1.0/go.mod h1:fux0lOrBhrVCJd3lcTHsIJhq1T2rokOu6v9Vcb3Q9ug=
36-
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
37-
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
3836
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 h1:tdlZCpZ/P9DhczCTSixgIKmwPv6+wP5DGjqLYw5SUiA=
3937
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2/go.mod h1:SqUrOPUnsFjfmXRMNPybcSiG0BgUW2AuFH8PAnS2iTw=
4038
github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo=
@@ -47,6 +45,8 @@ github.com/go-chi/stampede v0.4.5 h1:/qEiOLkpBstcdVnlYfg7TEWfL4It3ruXYDKpJkmkZMQ
4745
github.com/go-chi/stampede v0.4.5/go.mod h1:26lupLoxsX2qfPW6QCOEj8Gb1/KWpbH5XysNAoNojsU=
4846
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
4947
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
48+
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
49+
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
5050
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58=
5151
github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q=
5252
github.com/golang/groupcache v0.0.0-20190702054246-869f871628b6 h1:ZgQEtGgCBiWRM39fZuwSd1LwSqqSW0hOdXCYYDX0R3I=
@@ -86,6 +86,8 @@ github.com/hashicorp/logutils v1.0.0 h1:dLEQVugN8vlakKOUE3ihGLTZJRB4j+M2cdTm/ORI
8686
github.com/hashicorp/logutils v1.0.0/go.mod h1:QIAnNjmIWmVIIkWDTG1z5v++HQmx9WQRO+LraFDTW64=
8787
github.com/hashicorp/memberlist v0.1.5 h1:AYBsgJOW9gab/toO5tEB8lWetVgDKZycqkebJ8xxpqM=
8888
github.com/hashicorp/memberlist v0.1.5/go.mod h1:ajVTdAv/9Im8oMAAj5G31PhhMCZJV2pPBoIllUwCN7I=
89+
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
90+
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
8991
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
9092
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
9193
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=

plugins/caddy/configuration.go

+6
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@ import (
88

99
// DefaultCache the struct
1010
type DefaultCache struct {
11+
Badger configurationtypes.CacheProvider
1112
Distributed bool
1213
Headers []string
1314
Olric configurationtypes.CacheProvider
1415
Regex configurationtypes.Regex
1516
TTL time.Duration
1617
}
1718

19+
// GetBadger returns the Badger configuration
20+
func (d *DefaultCache) GetBadger() configurationtypes.CacheProvider {
21+
return d.Badger
22+
}
23+
1824
// GetDistributed returns if it uses Olric or not as provider
1925
func (d *DefaultCache) GetDistributed() bool {
2026
return d.Distributed

plugins/caddy/go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/darkweak/souin/plugins/caddy
33
go 1.15
44

55
require (
6-
github.com/caddyserver/caddy/v2 v2.4.0-beta.2
6+
github.com/caddyserver/caddy/v2 v2.4.3
77
github.com/darkweak/souin v1.5.2
88
go.uber.org/zap v1.19.0
99
)

0 commit comments

Comments
 (0)