Skip to content

Commit

Permalink
Added support for redis in mongik (#16)
Browse files Browse the repository at this point in the history
Merge Redis support for Mongik in AuthV2
  • Loading branch information
Shubhrajyoti-Dey-FrosTiK authored Jan 25, 2024
1 parent 3dd4305 commit ce868eb
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 20 deletions.
7 changes: 7 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
ATLAS_URI=...
PORT=...
FIREBASE_PROJECT_ID=...

REDIS_URI=...
REDIS_PASSWORD=...
REDIS_USERNAME=...
3 changes: 3 additions & 0 deletions .github/workflows/GCP-Deploy-Staging.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ jobs:
env_vars: |
ATLAS_URI=${{ secrets.MONGO_STAGING_CONNECTION_URL }}
FIREBASE_PROJECT_ID=${{ secrets.FIREBASE_PROJECT_ID }}
REDIS_URI=${{ secrets.REDIS_URI }}
REDIS_PASSWORD=${{ secrets.REDIS_PASSWORD }}
REDIS_USERNAME=${{ secrets.REDIS_USERNAME }}
# If required, use the Cloud Run url output in later steps
- name: Show Output
Expand Down
3 changes: 3 additions & 0 deletions .github/workflows/GCP-Deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ jobs:
env_vars: |
ATLAS_URI=${{ secrets.MONGO_CONNECTION_URL }}
FIREBASE_PROJECT_ID=${{ secrets.FIREBASE_PROJECT_ID }}
REDIS_URI=${{ secrets.REDIS_URI }}
REDIS_PASSWORD=${{ secrets.REDIS_PASSWORD }}
REDIS_USERNAME=${{ secrets.REDIS_USERNAME }}
# If required, use the Cloud Run url output in later steps
- name: Show Output
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.envrc
.envrc
.env
6 changes: 6 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,15 @@ FROM golang:1.21.4

ARG ATLAS_URI
ARG FIREBASE_PROJECT_ID
ARG REDIS_URI
ARG REDIS_PASSWORD
ARG REDIS_USERNAME

ENV APP_HOME /go/src/authv2
ENV ATLAS_URI ${ATLAS_URI}
ENV REDIS_URI ${REDIS_URI}
ENV REDIS_PASSWORD ${REDIS_PASSWORD}
ENV REDIS_USERNAME ${REDIS_USERNAME}
ENV FIREBASE_PROJECT_ID ${FIREBASE_PROJECT_ID}
ENV GIN_MODE=release

Expand Down
6 changes: 6 additions & 0 deletions Dockerfile.production
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ FROM golang:1.21.4

ARG ATLAS_URI
ARG FIREBASE_PROJECT_ID
ARG REDIS_URI
ARG REDIS_PASSWORD
ARG REDIS_USERNAME

ENV APP_HOME /go/src/authv2
ENV ATLAS_URI ${ATLAS_URI}
ENV REDIS_URI ${REDIS_URI}
ENV REDIS_PASSWORD ${REDIS_PASSWORD}
ENV REDIS_USERNAME ${REDIS_USERNAME}
ENV FIREBASE_PROJECT_ID ${FIREBASE_PROJECT_ID}
ENV GIN_MODE=release

Expand Down
5 changes: 4 additions & 1 deletion constants/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import "time"

const DB = "portal"
const CONNECTION_STRING = "ATLAS_URI"
const REDIS_URI = "REDIS_URI"
const REDIS_USERNAME = "REDIS_USERNAME"
const REDIS_PASSWORD = "REDIS_PASSWORD"

const COLLECTION_EXAM = "exams"
const COLLECTION_STUDENT = "students"
Expand All @@ -13,7 +16,7 @@ const COLLECTION_ATTENDANCE = "attendances"

const FIREBASE_PROJECT_ID = "FIREBASE_PROJECT_ID"

const CACHING_DURATION = 20 * time.Minute
const CACHING_DURATION = 20 * time.Hour
const CACHE_CONTROL_HEADER = "Cache-Control"
const NO_CACHE = "no-cache"

Expand Down
14 changes: 0 additions & 14 deletions controller/student.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,6 @@ func getAliasEmailList(email string) []string {
func GetUserByEmail(mongikClient *models.Mongik, email *string, role *string, noCache bool) (*model.StudentPopulated, *string) {
var studentPopulated model.StudentPopulated

// Check if copy is there in the cache
if !noCache {
studentBytes, _ := mongikClient.CacheClient.Get(*email)
if err := json.Unmarshal(studentBytes, &studentPopulated); err == nil {
fmt.Println("Retreiving the student data from the cache")
return &studentPopulated, nil
}
}

// Gets the alias emails
emailList := getAliasEmailList(*email)

Expand All @@ -58,10 +49,5 @@ func GetUserByEmail(mongikClient *models.Mongik, email *string, role *string, no
return nil, &constants.ERROR_NOT_A_STUDENT
}

// Set to bigCache
studentBytes, _ := json.Marshal(studentPopulated)
if err := mongikClient.CacheClient.Set(*email, studentBytes); err == nil {
fmt.Println("Successfully set UserDetails in cache")
}
return &studentPopulated, nil
}
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ module frostik.com/auth
go 1.21.1

require (
github.com/FrosTiK-SD/mongik v0.1.5
github.com/FrosTiK-SD/mongik v0.1.17
github.com/allegro/bigcache/v3 v3.1.0
github.com/gin-gonic/gin v1.9.0
github.com/lestrrat-go/jwx/v2 v2.0.16
go.mongodb.org/mongo-driver v1.13.0
)

require (
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/golang/snappy v0.0.3 // indirect
github.com/klauspost/compress v1.13.6 // indirect
github.com/lestrrat-go/blackmagic v1.0.2 // indirect
Expand All @@ -20,6 +22,7 @@ require (
github.com/lestrrat-go/iter v1.0.2 // indirect
github.com/lestrrat-go/option v1.0.1 // indirect
github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe // indirect
github.com/redis/go-redis/v9 v9.4.0 // indirect
github.com/segmentio/asm v1.2.0 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ github.com/FrosTiK-SD/mongik v0.1.4 h1:7Wa46bWDy7FxhjdVTC6p7kJgpewiIOl8vZBgfLK4V
github.com/FrosTiK-SD/mongik v0.1.4/go.mod h1:OnabLBpqWYNf+8Dn5ZPa7mAqEnnAkyOOEsTDzuvPN1o=
github.com/FrosTiK-SD/mongik v0.1.5 h1:CkredRLbvy6WmV81Ynl5INq8DPvR73gKYbnqoH/WEv8=
github.com/FrosTiK-SD/mongik v0.1.5/go.mod h1:EAFjp0U+R9SQcEe2tCa7In2lc58bhmO+0a08Ss3OaN4=
github.com/FrosTiK-SD/mongik v0.1.17 h1:hCcohfz7rRG5om6+5K84RZ2Nlz4UpZg6DUY+zWmyKCc=
github.com/FrosTiK-SD/mongik v0.1.17/go.mod h1:AjFFmUGUAix1sf24uxh0Wxq5fNIpykx4EEd44AMYhfw=
github.com/allegro/bigcache/v3 v3.1.0 h1:H2Vp8VOvxcrB91o86fUSVJFqeuz8kpyyB02eH3bSzwk=
github.com/allegro/bigcache/v3 v3.1.0/go.mod h1:aPyh7jEvrog9zAwx5N7+JUQX5dZTSGpxF1LAR4dr35I=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
Expand All @@ -10,6 +12,8 @@ github.com/bytedance/sonic v1.8.2/go.mod h1:i736AoUSYt75HyZLoJW9ERYxcy6eaN6h4BZX
github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM=
github.com/bytedance/sonic v1.10.2 h1:GQebETVBxYB7JGWJtLBi07OVzWwt+8dWA00gEVW2ZFE=
github.com/bytedance/sonic v1.10.2/go.mod h1:iZcSUejdk5aukTND/Eu/ivjQuEL0Cu9/rf50Hi0u/g4=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06/go.mod h1:DH46F32mSOjUmXrMHnKwZdA8wcEefY7UVqBKYGjpdQY=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 h1:qSGYFH7+jGhDF8vLC+iwCD4WpbV1EBDSzWkJODFLams=
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311/go.mod h1:b583jCggY9gE99b6G5LEC39OIiVsWj+R97kbl5odCEk=
Expand All @@ -25,6 +29,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0 h1:8UrgZ3GkP4i/CLijOJx79Yu+etlyjdBU4sfcs2WYQMs=
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.2.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
github.com/gin-contrib/cors v1.4.0 h1:oJ6gwtUl3lqV0WEIwM/LxPF1QZ5qe2lGWdY2+bz7y0g=
Expand Down Expand Up @@ -124,6 +130,8 @@ github.com/pelletier/go-toml/v2 v2.1.0/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdU
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.4.0 h1:Yzoz33UZw9I/mFhx4MNrB6Fk+XHO1VukNcCa1+lwyKk=
github.com/redis/go-redis/v9 v9.4.0/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc=
github.com/rogpeppe/go-internal v1.8.0 h1:FCbCCtXNOY3UtUuHUYaghJg4y7Fd14rXifAYUAtL9R8=
github.com/rogpeppe/go-internal v1.8.0/go.mod h1:WmiCO8CzOY8rg0OYDC4/i/2WRWAB6poM+XZ2dLUbcbE=
Expand Down
4 changes: 3 additions & 1 deletion handler/verify.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"frostik.com/auth/constants"
"frostik.com/auth/controller"
models "github.com/FrosTiK-SD/mongik/models"
"github.com/allegro/bigcache/v3"
"github.com/gin-gonic/gin"
"github.com/lestrrat-go/jwx/v2/jwk"
)

type Handler struct {
MongikClient *models.Mongik
JwkSet *jwk.Set
BigCache *bigcache.BigCache
}

func (h *Handler) HandlerVerifyStudentIdToken(ctx *gin.Context) {
Expand All @@ -20,7 +22,7 @@ func (h *Handler) HandlerVerifyStudentIdToken(ctx *gin.Context) {
noCache = true
}

email, exp, err := controller.VerifyToken(h.MongikClient.CacheClient, idToken, h.JwkSet, noCache)
email, exp, err := controller.VerifyToken(h.BigCache, idToken, h.JwkSet, noCache)

if err != nil {
ctx.JSON(200, gin.H{
Expand Down
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
"os"

Expand All @@ -9,17 +10,30 @@ import (
"frostik.com/auth/handler"
"frostik.com/auth/util"
"github.com/FrosTiK-SD/mongik"
mongikConstants "github.com/FrosTiK-SD/mongik/constants"
mongikModels "github.com/FrosTiK-SD/mongik/models"
"github.com/allegro/bigcache/v3"
"github.com/gin-contrib/cors"
"github.com/gin-gonic/gin"
)

func main() {
r := gin.Default()

mongikClient := mongik.NewClient(os.Getenv(constants.CONNECTION_STRING), constants.CACHING_DURATION)
mongikClient := mongik.NewClient(os.Getenv(constants.CONNECTION_STRING), &mongikModels.Config{
Client: mongikConstants.REDIS,
TTL: constants.CACHING_DURATION,
RedisConfig: &mongikModels.RedisConfig{
URI: os.Getenv(constants.REDIS_URI),
Password: os.Getenv(constants.REDIS_PASSWORD),
Username: os.Getenv(constants.REDIS_USERNAME),
},
})

cacheClient, _ := bigcache.New(context.Background(), bigcache.DefaultConfig(constants.CACHING_DURATION))

// Initialie default JWKs
defaultJwkSet, jwkSetRetrieveError := controller.GetJWKs(mongikClient.CacheClient, true)
defaultJwkSet, jwkSetRetrieveError := controller.GetJWKs(cacheClient, true)
if jwkSetRetrieveError != nil {
fmt.Println("Error retrieving JWKs")
}
Expand All @@ -28,6 +42,7 @@ func main() {

handler := &handler.Handler{
MongikClient: mongikClient,
BigCache: cacheClient,
JwkSet: defaultJwkSet,
}

Expand Down
3 changes: 3 additions & 0 deletions makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
run local:
nodemon --exec go run main.go --signal SIGTERM

build:
docker build -t authv2 . -f Dockerfile.production && docker run --env-file .env -dp 8081:8080 authv2

serve:
kubectl port-forward service/authv2 8080:8080

Expand Down

0 comments on commit ce868eb

Please sign in to comment.