diff --git a/.env.sample b/.env.sample new file mode 100644 index 0000000..9dc4211 --- /dev/null +++ b/.env.sample @@ -0,0 +1,7 @@ +ATLAS_URI=... +PORT=... +FIREBASE_PROJECT_ID=... + +REDIS_URI=... +REDIS_PASSWORD=... +REDIS_USERNAME=... \ No newline at end of file diff --git a/.github/workflows/GCP-Deploy-Staging.yml b/.github/workflows/GCP-Deploy-Staging.yml index 689f9f6..b9db1ba 100644 --- a/.github/workflows/GCP-Deploy-Staging.yml +++ b/.github/workflows/GCP-Deploy-Staging.yml @@ -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 diff --git a/.github/workflows/GCP-Deploy.yml b/.github/workflows/GCP-Deploy.yml index c3f2ada..fee0c83 100644 --- a/.github/workflows/GCP-Deploy.yml +++ b/.github/workflows/GCP-Deploy.yml @@ -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 diff --git a/.gitignore b/.gitignore index 1761c01..c4acebe 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ -.envrc \ No newline at end of file +.envrc +.env \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index aa0f8ec..fb4a3e6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -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 diff --git a/Dockerfile.production b/Dockerfile.production index 747a23d..a23aab7 100644 --- a/Dockerfile.production +++ b/Dockerfile.production @@ -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 diff --git a/constants/db.go b/constants/db.go index f652968..efd4c39 100644 --- a/constants/db.go +++ b/constants/db.go @@ -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" @@ -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" diff --git a/controller/student.go b/controller/student.go index 6e9890a..637d2c3 100644 --- a/controller/student.go +++ b/controller/student.go @@ -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) @@ -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 } diff --git a/go.mod b/go.mod index ced5450..2debfa5 100644 --- a/go.mod +++ b/go.mod @@ -3,7 +3,7 @@ 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 @@ -11,7 +11,9 @@ require ( ) 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 @@ -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 diff --git a/go.sum b/go.sum index 0fab8f1..917302e 100644 --- a/go.sum +++ b/go.sum @@ -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= @@ -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= @@ -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= @@ -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= diff --git a/handler/verify.go b/handler/verify.go index e5076dd..0cbbfdb 100644 --- a/handler/verify.go +++ b/handler/verify.go @@ -4,6 +4,7 @@ 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" ) @@ -11,6 +12,7 @@ import ( type Handler struct { MongikClient *models.Mongik JwkSet *jwk.Set + BigCache *bigcache.BigCache } func (h *Handler) HandlerVerifyStudentIdToken(ctx *gin.Context) { @@ -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{ diff --git a/main.go b/main.go index b30c6cf..27058a2 100644 --- a/main.go +++ b/main.go @@ -1,6 +1,7 @@ package main import ( + "context" "fmt" "os" @@ -9,6 +10,9 @@ 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" ) @@ -16,10 +20,20 @@ import ( 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") } @@ -28,6 +42,7 @@ func main() { handler := &handler.Handler{ MongikClient: mongikClient, + BigCache: cacheClient, JwkSet: defaultJwkSet, } diff --git a/makefile b/makefile index 2ea5591..55e2c4f 100644 --- a/makefile +++ b/makefile @@ -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