From 78a673e4ad26e88586f0e0cfd0fe8412b2afe6fe Mon Sep 17 00:00:00 2001 From: Lakhan Samani Date: Wed, 8 Jun 2022 09:50:30 +0530 Subject: [PATCH 1/2] fix: fix parallel access of env vars --- server/memorystore/providers/inmemory/envstore.go | 9 --------- server/memorystore/providers/inmemory/store.go | 9 --------- 2 files changed, 18 deletions(-) diff --git a/server/memorystore/providers/inmemory/envstore.go b/server/memorystore/providers/inmemory/envstore.go index ce49d59..6ba4f4c 100644 --- a/server/memorystore/providers/inmemory/envstore.go +++ b/server/memorystore/providers/inmemory/envstore.go @@ -26,20 +26,11 @@ func (e *EnvStore) UpdateStore(store map[string]interface{}) { // GetStore returns the env store func (e *EnvStore) GetStore() map[string]interface{} { - if os.Getenv("ENV") != "test" { - e.mutex.Lock() - defer e.mutex.Unlock() - } - return e.store } // Get returns the value of the key in evn store func (e *EnvStore) Get(key string) interface{} { - if os.Getenv("ENV") != "test" { - e.mutex.Lock() - defer e.mutex.Unlock() - } return e.store[key] } diff --git a/server/memorystore/providers/inmemory/store.go b/server/memorystore/providers/inmemory/store.go index 4d74c2b..420319d 100644 --- a/server/memorystore/providers/inmemory/store.go +++ b/server/memorystore/providers/inmemory/store.go @@ -19,10 +19,6 @@ func (c *provider) ClearStore() error { // GetUserSessions returns all the user session token from the in-memory store. func (c *provider) GetUserSessions(userId string) map[string]string { - if os.Getenv("ENV") != "test" { - c.mutex.Lock() - defer c.mutex.Unlock() - } res := map[string]string{} for k, v := range c.stateStore { split := strings.Split(v, "@") @@ -61,11 +57,6 @@ func (c *provider) SetState(key, state string) error { // GetState gets the state from the in-memory store. func (c *provider) GetState(key string) (string, error) { - if os.Getenv("ENV") != "test" { - c.mutex.Lock() - defer c.mutex.Unlock() - } - state := "" if stateVal, ok := c.stateStore[key]; ok { state = stateVal From 02eb1d66776f3f8f0f33e9494b149dcad0d41231 Mon Sep 17 00:00:00 2001 From: Lakhan Samani Date: Thu, 9 Jun 2022 23:13:22 +0530 Subject: [PATCH 2/2] fix: add const for test env --- server/constants/env.go | 2 ++ server/email/email.go | 2 +- server/memorystore/providers/inmemory/envstore.go | 6 ++++-- server/memorystore/providers/inmemory/store.go | 10 ++++++---- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/server/constants/env.go b/server/constants/env.go index c9ab6fb..b5c3218 100644 --- a/server/constants/env.go +++ b/server/constants/env.go @@ -3,6 +3,8 @@ package constants var VERSION = "0.0.1" const ( + // TestEnv is used for testing + TestEnv = "test" // EnvKeyEnv key for env variable ENV EnvKeyEnv = "ENV" // EnvKeyEnvPath key for cli arg variable ENV_PATH diff --git a/server/email/email.go b/server/email/email.go index 4234eff..99b2c03 100644 --- a/server/email/email.go +++ b/server/email/email.go @@ -37,7 +37,7 @@ func SendMail(to []string, Subject, bodyMessage string) error { if err != nil { return err } - if envKey == "test" { + if envKey == constants.TestEnv { return nil } m := gomail.NewMessage() diff --git a/server/memorystore/providers/inmemory/envstore.go b/server/memorystore/providers/inmemory/envstore.go index 6ba4f4c..25d0712 100644 --- a/server/memorystore/providers/inmemory/envstore.go +++ b/server/memorystore/providers/inmemory/envstore.go @@ -3,6 +3,8 @@ package inmemory import ( "os" "sync" + + "github.com/authorizerdev/authorizer/server/constants" ) // EnvStore struct to store the env variables @@ -13,7 +15,7 @@ type EnvStore struct { // UpdateEnvStore to update the whole env store object func (e *EnvStore) UpdateStore(store map[string]interface{}) { - if os.Getenv("ENV") != "test" { + if os.Getenv("ENV") != constants.TestEnv { e.mutex.Lock() defer e.mutex.Unlock() } @@ -36,7 +38,7 @@ func (e *EnvStore) Get(key string) interface{} { // Set sets the value of the key in env store func (e *EnvStore) Set(key string, value interface{}) { - if os.Getenv("ENV") != "test" { + if os.Getenv("ENV") != constants.TestEnv { e.mutex.Lock() defer e.mutex.Unlock() } diff --git a/server/memorystore/providers/inmemory/store.go b/server/memorystore/providers/inmemory/store.go index 420319d..44de3b7 100644 --- a/server/memorystore/providers/inmemory/store.go +++ b/server/memorystore/providers/inmemory/store.go @@ -4,11 +4,13 @@ import ( "fmt" "os" "strings" + + "github.com/authorizerdev/authorizer/server/constants" ) // ClearStore clears the in-memory store. func (c *provider) ClearStore() error { - if os.Getenv("ENV") != "test" { + if os.Getenv("ENV") != constants.TestEnv { c.mutex.Lock() defer c.mutex.Unlock() } @@ -32,7 +34,7 @@ func (c *provider) GetUserSessions(userId string) map[string]string { // DeleteAllUserSession deletes all the user sessions from in-memory store. func (c *provider) DeleteAllUserSession(userId string) error { - if os.Getenv("ENV") != "test" { + if os.Getenv("ENV") != constants.TestEnv { c.mutex.Lock() defer c.mutex.Unlock() } @@ -46,7 +48,7 @@ func (c *provider) DeleteAllUserSession(userId string) error { // SetState sets the state in the in-memory store. func (c *provider) SetState(key, state string) error { - if os.Getenv("ENV") != "test" { + if os.Getenv("ENV") != constants.TestEnv { c.mutex.Lock() defer c.mutex.Unlock() } @@ -67,7 +69,7 @@ func (c *provider) GetState(key string) (string, error) { // RemoveState removes the state from the in-memory store. func (c *provider) RemoveState(key string) error { - if os.Getenv("ENV") != "test" { + if os.Getenv("ENV") != constants.TestEnv { c.mutex.Lock() defer c.mutex.Unlock() }