Skip to content
This repository was archived by the owner on Mar 16, 2021. It is now read-only.

Commit eea3417

Browse files
billimekmxschmitt
authored andcommitted
use redis as a session store backend (#119)
* support for custom github endpoints * implementing requested changes * using redis for session store if it is configured * using non-deprecated sessions lib * hard-coding redis session store private key for multiple instances * re-working GetPrivateKey to return stastic key only when redis is used * making config entries for redis sesion db and shared key
1 parent 890ff87 commit eea3417

File tree

6 files changed

+40
-13
lines changed

6 files changed

+40
-13
lines changed

config/example.yaml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ Proxy: # only relevant when using the proxy authbackend
2222
UserHeader: "X-Goog-Authenticated-User-ID" # pull the unique user ID from this header
2323
DisplayNameHeader: "X-Goog-Authenticated-User-Email" # pull the display naem from this header
2424
Redis:
25-
Host: localhost:6379 # host:port combination; required
26-
Password: replace me # redis connection password; optional; default is none
27-
Db: 0 # redis index (https://redis.io/commands/select); optional; default is 0
28-
MaxRetries: 3 # maximum number of retries for a failed redis command
29-
ReadTimeout: 3s # timeout for read operations; default is 3s. This is a golang time.ParseDuration string
30-
WriteTimeout: 3s # timeout for write operations; default is 3s. This is a golang time.ParseDuration string
25+
Host: localhost:6379 # host:port combination; required
26+
Password: replace me # redis connection password; optional; default is none
27+
Db: 0 # redis index (https://redis.io/commands/select); optional; default is 0
28+
MaxRetries: 3 # maximum number of retries for a failed redis command
29+
ReadTimeout: 3s # timeout for read operations; default is 3s. This is a golang time.ParseDuration string
30+
WriteTimeout: 3s # timeout for write operations; default is 3s. This is a golang time.ParseDuration string
31+
SessionDB: 1 # redis session store index (https://redis.io/commands/select); optional; default is 1
32+
SharedKey: replace me # redis session store shared key; optional; default is "secret"

deployments/cloudfoundry/run.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ fi
1313
if [ "$REDIS" != "" ]; then
1414
export GUS_REDIS_HOST="$(echo $VCAP_SERVICES | jq -r '.["'$REDIS_SERVICE_NAME'"][0].credentials.host'):$(echo $VCAP_SERVICES | jq -r '.["'$REDIS_SERVICE_NAME'"][0].credentials.port')"
1515
export GUS_REDIS_PASSWORD="$(echo $VCAP_SERVICES | jq -r '.["'$REDIS_SERVICE_NAME'"][0].credentials.password')"
16+
export GUS_REDIS_SHARED_KEY=$GUS_REDIS_PASSWORD
1617
fi
1718

1819
echo "#### Starting golang-url-shortener..."

internal/handlers/auth.go

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,22 @@ import (
99
"github.com/sirupsen/logrus"
1010

1111
jwt "github.com/dgrijalva/jwt-go"
12-
"github.com/gin-gonic/contrib/sessions"
12+
"github.com/gin-contrib/sessions"
13+
"github.com/gin-contrib/sessions/cookie"
14+
"github.com/gin-contrib/sessions/redis"
1315
"github.com/gin-gonic/gin"
1416
"github.com/pkg/errors"
1517
)
1618

1719
func (h *Handler) initOAuth() {
18-
h.engine.Use(sessions.Sessions("backend", sessions.NewCookieStore(util.GetPrivateKey())))
19-
20+
switch backend := util.GetConfig().Backend; backend {
21+
// use redis as the session store if it is configured
22+
case "redis":
23+
store, _ := redis.NewStoreWithDB(10, "tcp", util.GetConfig().Redis.Host, util.GetConfig().Redis.Password, util.GetConfig().Redis.SessionDB, util.GetPrivateKey())
24+
h.engine.Use(sessions.Sessions("backend", store))
25+
default:
26+
h.engine.Use(sessions.Sessions("backend", cookie.NewStore(util.GetPrivateKey())))
27+
}
2028
h.providers = []string{}
2129
google := util.GetConfig().Google
2230
if google.Enabled() {
@@ -39,7 +47,14 @@ func (h *Handler) initOAuth() {
3947

4048
// initProxyAuth intializes data structures for proxy authentication mode
4149
func (h *Handler) initProxyAuth() {
42-
h.engine.Use(sessions.Sessions("backend", sessions.NewCookieStore(util.GetPrivateKey())))
50+
switch backend := util.GetConfig().Backend; backend {
51+
// use redis as the session store if it is configured
52+
case "redis":
53+
store, _ := redis.NewStoreWithDB(10, "tcp", util.GetConfig().Redis.Host, util.GetConfig().Redis.Password, util.GetConfig().Redis.SessionDB, util.GetPrivateKey())
54+
h.engine.Use(sessions.Sessions("backend", store))
55+
default:
56+
h.engine.Use(sessions.Sessions("backend", cookie.NewStore(util.GetPrivateKey())))
57+
}
4358
h.providers = []string{}
4459
h.providers = append(h.providers, "proxy")
4560
h.engine.POST("/api/v1/auth/check", h.handleAuthCheck)

internal/handlers/auth/auth.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import (
88
"time"
99

1010
jwt "github.com/dgrijalva/jwt-go"
11-
"github.com/gin-gonic/contrib/sessions"
11+
"github.com/gin-contrib/sessions"
1212
"github.com/gin-gonic/gin"
1313
"github.com/mxschmitt/golang-url-shortener/internal/util"
1414
"github.com/pkg/errors"

internal/util/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type redisConf struct {
3838
MaxRetries int `yaml:"MaxRetries" env:"MAX_RETRIES"`
3939
ReadTimeout string `yaml:"ReadTimeout" env:"READ_TIMEOUT"`
4040
WriteTimeout string `yaml:"WriteTimeout" env:"WRITE_TIMEOUT"`
41+
SessionDB string `yaml:"SessionDB" env:"SESSION_DB"`
42+
SharedKey string `yaml:"SharedKey" env:"SHARED_KEY"`
4143
}
4244

4345
type oAuthConf struct {
@@ -52,7 +54,7 @@ type proxyAuthConf struct {
5254
DisplayNameHeader string `yaml:"DisplayNameHeader" env:"DISPLAY_NAME_HEADER"`
5355
}
5456

55-
// config contains the default values
57+
// Config contains the default values
5658
var Config = Configuration{
5759
ListenAddr: ":8080",
5860
BaseURL: "http://localhost:3000",
@@ -69,6 +71,8 @@ var Config = Configuration{
6971
MaxRetries: 3,
7072
ReadTimeout: "3s",
7173
WriteTimeout: "3s",
74+
SessionDB: "1",
75+
SharedKey: "secret",
7276
},
7377
}
7478

internal/util/private.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,10 @@ func CheckForPrivateKey() error {
3535

3636
// GetPrivateKey returns the private key
3737
func GetPrivateKey() []byte {
38-
return privateKey
38+
switch backend := GetConfig().Backend; backend {
39+
case "redis":
40+
return []byte(GetConfig().Redis.SharedKey)
41+
default:
42+
return privateKey
43+
}
3944
}

0 commit comments

Comments
 (0)