From 5c6e0aa3c30a672b964574e25236deb830ee120c Mon Sep 17 00:00:00 2001 From: sredny buitrago Date: Wed, 31 Jul 2024 21:17:58 -0400 Subject: [PATCH 1/6] accept kv storage --- api.go | 11 ++++++----- http_handlers.go | 7 ++++--- initializer/initializer.go | 29 +++++++++++++++++++++++------ main.go | 11 ++--------- 4 files changed, 35 insertions(+), 23 deletions(-) diff --git a/api.go b/api.go index d1badc93..56ffc1b6 100644 --- a/api.go +++ b/api.go @@ -4,6 +4,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/TykTechnologies/tyk-identity-broker/initializer" "io/ioutil" "net/http" @@ -83,7 +84,7 @@ func IsAuthenticated(h http.Handler) http.Handler { // ------ End Middleware methods ------- func HandleGetProfileList(w http.ResponseWriter, r *http.Request) { - profiles := AuthConfigStore.GetAll("") + profiles := initializer.AuthConfigStore.GetAll("") HandleAPIOK(profiles, "", 200, w, r) } @@ -92,7 +93,7 @@ func HandleGetProfile(w http.ResponseWriter, r *http.Request) { key := mux.Vars(r)["id"] thisProfile := tap.Profile{} - keyErr := AuthConfigStore.GetKey(key, thisProfile.OrgID, &thisProfile) + keyErr := initializer.AuthConfigStore.GetKey(key, thisProfile.OrgID, &thisProfile) if keyErr != nil { HandleAPIError(APILogTag, "Profile not found", keyErr, 404, w, r) return @@ -122,7 +123,7 @@ func HandleAddProfile(w http.ResponseWriter, r *http.Request) { return } - httpErr := tap.AddProfile(thisProfile, AuthConfigStore, GlobalDataLoader.Flush) + httpErr := tap.AddProfile(thisProfile, initializer.AuthConfigStore, GlobalDataLoader.Flush) if httpErr != nil { HandleAPIError(APILogTag, httpErr.Message, httpErr.Error, httpErr.Code, w, r) return @@ -147,7 +148,7 @@ func HandleUpdateProfile(w http.ResponseWriter, r *http.Request) { return } - updateErr := tap.UpdateProfile(key, thisProfile, AuthConfigStore, GlobalDataLoader.Flush) + updateErr := tap.UpdateProfile(key, thisProfile, initializer.AuthConfigStore, GlobalDataLoader.Flush) if updateErr != nil { HandleAPIError(APILogTag, updateErr.Message, updateErr.Error, updateErr.Code, w, r) return @@ -158,7 +159,7 @@ func HandleUpdateProfile(w http.ResponseWriter, r *http.Request) { func HandleDeleteProfile(w http.ResponseWriter, r *http.Request) { key := mux.Vars(r)["id"] - err := tap.DeleteProfile(key, "", AuthConfigStore, GlobalDataLoader.Flush) + err := tap.DeleteProfile(key, "", initializer.AuthConfigStore, GlobalDataLoader.Flush) if err != nil { HandleAPIError(APILogTag, err.Message, err.Error, err.Code, w, r) return diff --git a/http_handlers.go b/http_handlers.go index 6423f6e2..2ed06231 100644 --- a/http_handlers.go +++ b/http_handlers.go @@ -2,6 +2,7 @@ package main import ( "errors" + "github.com/TykTechnologies/tyk-identity-broker/initializer" "net/http" "github.com/TykTechnologies/tyk-identity-broker/constants" @@ -33,7 +34,7 @@ func HandleAuth(w http.ResponseWriter, r *http.Request) { return } - thisIdentityProvider, thisProfile, err := providers.GetTapProfile(AuthConfigStore, IdentityKeyStore, thisId, TykAPIHandler) + thisIdentityProvider, thisProfile, err := providers.GetTapProfile(initializer.AuthConfigStore, initializer.IdentityKeyStore, thisId, TykAPIHandler) if err != nil { return } @@ -52,7 +53,7 @@ func HandleAuthCallback(w http.ResponseWriter, r *http.Request) { return } - thisIdentityProvider, thisProfile, err := providers.GetTapProfile(AuthConfigStore, IdentityKeyStore, thisId, TykAPIHandler) + thisIdentityProvider, thisProfile, err := providers.GetTapProfile(initializer.AuthConfigStore, initializer.IdentityKeyStore, thisId, TykAPIHandler) if err != nil { tykerrors.HandleError(constants.HandlerLogTag, err.Message, err.Error, err.Code, w, r) return @@ -72,7 +73,7 @@ func HandleMetadata(w http.ResponseWriter, r *http.Request) { return } - thisIdentityProvider, _, err := providers.GetTapProfile(AuthConfigStore, IdentityKeyStore, thisId, TykAPIHandler) + thisIdentityProvider, _, err := providers.GetTapProfile(initializer.AuthConfigStore, initializer.IdentityKeyStore, thisId, TykAPIHandler) if err != nil { tykerrors.HandleError(constants.HandlerLogTag, err.Message, err.Error, err.Code, w, r) return diff --git a/initializer/initializer.go b/initializer/initializer.go index 4526610a..2944eb27 100644 --- a/initializer/initializer.go +++ b/initializer/initializer.go @@ -16,11 +16,17 @@ import ( var log = logger.Get() var initializerLogger = log.WithField("prefix", "TIB INITIALIZER") +// IdentityKeyStore keeps a record of identities tied to tokens (if needed) +var IdentityKeyStore tap.AuthRegisterBackend + +// AuthConfigStore Is the back end we are storing our configuration files to +var AuthConfigStore tap.AuthRegisterBackend + // initBackend: Get our backend to use from configs files, new back-ends must be registered here func InitBackend(profileBackendConfiguration interface{}, identityBackendConfiguration interface{}) (tap.AuthRegisterBackend, tap.AuthRegisterBackend) { - AuthConfigStore := &backends.InMemoryBackend{} - IdentityKeyStore := &backends.RedisBackend{KeyPrefix: "identity-cache."} + AuthConfigStore = &backends.InMemoryBackend{} + IdentityKeyStore = &backends.RedisBackend{KeyPrefix: "identity-cache."} initializerLogger.Info("Initialising Profile Configuration Store") AuthConfigStore.Init(profileBackendConfiguration) @@ -30,13 +36,14 @@ func InitBackend(profileBackendConfiguration interface{}, identityBackendConfigu return AuthConfigStore, IdentityKeyStore } +func SetKVStore(kv temporal.KeyValue) { + +} + // CreateBackendFromRedisConn: creates a redis backend from an existent redis Connection -func CreateBackendFromRedisConn(kv temporal.KeyValue, keyPrefix string) tap.AuthRegisterBackend { +func createBackendFromRedisConn(kv temporal.KeyValue, keyPrefix string) tap.AuthRegisterBackend { redisBackend := &backends.RedisBackend{KeyPrefix: keyPrefix} - - initializerLogger.Info("Initializing Identity Cache") redisBackend.SetDb(kv) - return redisBackend } @@ -72,3 +79,13 @@ func CreateMongoBackend(store persistent.PersistentStorage) tap.AuthRegisterBack func SetConfigHandler(backend tap.AuthRegisterBackend) { tothic.SetParamsStoreHandler(backend) } + +func SetKVStorage(kv temporal.KeyValue) { + configHandler := createBackendFromRedisConn(kv, "tib-provider-config-") + + initializerLogger.Info("Initializing Config handler") + tothic.SetParamsStoreHandler(configHandler) + + initializerLogger.Info("Initializing Identity Cache") + IdentityKeyStore = createBackendFromRedisConn(kv, "identity-cache") +} diff --git a/main.go b/main.go index ad3c56e7..84f075c6 100644 --- a/main.go +++ b/main.go @@ -14,18 +14,11 @@ import ( errors "github.com/TykTechnologies/tyk-identity-broker/error" logger "github.com/TykTechnologies/tyk-identity-broker/log" - "github.com/TykTechnologies/tyk-identity-broker/tap" "github.com/TykTechnologies/tyk-identity-broker/tothic" "github.com/TykTechnologies/tyk-identity-broker/tyk-api" "github.com/gorilla/mux" ) -// AuthConfigStore Is the back end we are storing our configuration files to -var AuthConfigStore tap.AuthRegisterBackend - -// IdentityKeyStore keeps a record of identities tied to tokens (if needed) -var IdentityKeyStore tap.AuthRegisterBackend - // config is the system-wide configuration var config configuration.Configuration @@ -48,7 +41,7 @@ func init() { flag.Parse() configuration.LoadConfig(confFile, &config) - AuthConfigStore, IdentityKeyStore = initializer.InitBackend(config.BackEnd.ProfileBackendSettings, config.BackEnd.IdentityBackendSettings) + initializer.InitBackend(config.BackEnd.ProfileBackendSettings, config.BackEnd.IdentityBackendSettings) configStore := &backends.RedisBackend{KeyPrefix: "tib-provider-config-"} configStore.Init(config.BackEnd.IdentityBackendSettings) @@ -69,7 +62,7 @@ func init() { if err != nil { return } - err = GlobalDataLoader.LoadIntoStore(AuthConfigStore) + err = GlobalDataLoader.LoadIntoStore(initializer.AuthConfigStore) if err != nil { mainLogger.Errorf("loading into store %v", err) return From 582f0f3962ad3146b3df3a490aba224b1b6a17b4 Mon Sep 17 00:00:00 2001 From: sredny buitrago Date: Wed, 31 Jul 2024 23:31:03 -0400 Subject: [PATCH 2/6] created Start method on initializer --- initializer/initializer.go | 27 +++++++++++++++++++++++++-- tothic/tothic.go | 11 ++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/initializer/initializer.go b/initializer/initializer.go index 2944eb27..1a6700df 100644 --- a/initializer/initializer.go +++ b/initializer/initializer.go @@ -4,6 +4,7 @@ import ( "github.com/TykTechnologies/storage/persistent" temporal "github.com/TykTechnologies/storage/temporal/keyvalue" "github.com/TykTechnologies/tyk-identity-broker/backends" + tykerror "github.com/TykTechnologies/tyk-identity-broker/error" logger "github.com/TykTechnologies/tyk-identity-broker/log" "github.com/TykTechnologies/tyk-identity-broker/providers" @@ -47,7 +48,7 @@ func createBackendFromRedisConn(kv temporal.KeyValue, keyPrefix string) tap.Auth return redisBackend } -func SetLogger(newLogger *logrus.Logger) { +func setLogger(newLogger *logrus.Logger) { logger.SetLogger(newLogger) log = newLogger @@ -80,7 +81,7 @@ func SetConfigHandler(backend tap.AuthRegisterBackend) { tothic.SetParamsStoreHandler(backend) } -func SetKVStorage(kv temporal.KeyValue) { +func setKVStorage(kv temporal.KeyValue) { configHandler := createBackendFromRedisConn(kv, "tib-provider-config-") initializerLogger.Info("Initializing Config handler") @@ -89,3 +90,25 @@ func SetKVStorage(kv temporal.KeyValue) { initializerLogger.Info("Initializing Identity Cache") IdentityKeyStore = createBackendFromRedisConn(kv, "identity-cache") } + +type TIB struct { + Logger *logrus.Logger + KV temporal.KeyValue + CookieSecret string +} + +func (tib *TIB) Start() { + if tib.Logger == nil { + tib.Logger = logrus.New() + } + setLogger(tib.Logger) + setKVStorage(tib.KV) + + tothic.TothErrorHandler = tykerror.HandleError + if tib.CookieSecret != "" { + tothic.SetupSessionStore(tib.CookieSecret) + } else { + // then it will read it from env + tothic.SetupSessionStore() + } +} diff --git a/tothic/tothic.go b/tothic/tothic.go index 31a807c3..00b29fc1 100644 --- a/tothic/tothic.go +++ b/tothic/tothic.go @@ -49,9 +49,14 @@ func (p PathParam) MarshalBinary() ([]byte, error) { return json.Marshal(p) } -func SetupSessionStore() { - key := KeyFromEnv() - Store = sessions.NewCookieStore([]byte(key)) +func SetupSessionStore(secret ...string) { + var key []byte + if len(secret) > 0 && secret[0] != "" { + key = []byte(secret[0]) + } else { + key = []byte(KeyFromEnv()) + } + Store = sessions.NewCookieStore(key) } func KeyFromEnv() (key string) { From 0c5b4f57cb3290698900c1aa10e27056fd6e8824 Mon Sep 17 00:00:00 2001 From: sredny buitrago Date: Wed, 31 Jul 2024 23:37:25 -0400 Subject: [PATCH 3/6] add cert manager as part of TIB --- initializer/initializer.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/initializer/initializer.go b/initializer/initializer.go index 1a6700df..f207138c 100644 --- a/initializer/initializer.go +++ b/initializer/initializer.go @@ -37,10 +37,6 @@ func InitBackend(profileBackendConfiguration interface{}, identityBackendConfigu return AuthConfigStore, IdentityKeyStore } -func SetKVStore(kv temporal.KeyValue) { - -} - // CreateBackendFromRedisConn: creates a redis backend from an existent redis Connection func createBackendFromRedisConn(kv temporal.KeyValue, keyPrefix string) tap.AuthRegisterBackend { redisBackend := &backends.RedisBackend{KeyPrefix: keyPrefix} @@ -94,6 +90,7 @@ func setKVStorage(kv temporal.KeyValue) { type TIB struct { Logger *logrus.Logger KV temporal.KeyValue + CertManager certs.CertificateManager CookieSecret string } @@ -104,6 +101,7 @@ func (tib *TIB) Start() { setLogger(tib.Logger) setKVStorage(tib.KV) + SetCertManager(tib.CertManager) tothic.TothErrorHandler = tykerror.HandleError if tib.CookieSecret != "" { tothic.SetupSessionStore(tib.CookieSecret) From 75daceac1b2d38ebc208edaabd22a9e8f2e7f914 Mon Sep 17 00:00:00 2001 From: sredny buitrago Date: Fri, 2 Aug 2024 12:05:20 -0400 Subject: [PATCH 4/6] goiimports --- api.go | 3 ++- http_handlers.go | 3 ++- initializer/initializer.go | 11 ++++++++++- providers/tapProvider.go | 3 ++- 4 files changed, 16 insertions(+), 4 deletions(-) diff --git a/api.go b/api.go index 56ffc1b6..690d388f 100644 --- a/api.go +++ b/api.go @@ -4,10 +4,11 @@ import ( "encoding/json" "errors" "fmt" - "github.com/TykTechnologies/tyk-identity-broker/initializer" "io/ioutil" "net/http" + "github.com/TykTechnologies/tyk-identity-broker/initializer" + tykerror "github.com/TykTechnologies/tyk-identity-broker/error" "github.com/TykTechnologies/tyk-identity-broker/tap" diff --git a/http_handlers.go b/http_handlers.go index 2ed06231..78d060b9 100644 --- a/http_handlers.go +++ b/http_handlers.go @@ -2,9 +2,10 @@ package main import ( "errors" - "github.com/TykTechnologies/tyk-identity-broker/initializer" "net/http" + "github.com/TykTechnologies/tyk-identity-broker/initializer" + "github.com/TykTechnologies/tyk-identity-broker/constants" "github.com/TykTechnologies/tyk-identity-broker/providers" diff --git a/initializer/initializer.go b/initializer/initializer.go index f207138c..54a26bf4 100644 --- a/initializer/initializer.go +++ b/initializer/initializer.go @@ -1,6 +1,8 @@ package initializer import ( + "errors" + "github.com/TykTechnologies/storage/persistent" temporal "github.com/TykTechnologies/storage/temporal/keyvalue" "github.com/TykTechnologies/tyk-identity-broker/backends" @@ -94,13 +96,20 @@ type TIB struct { CookieSecret string } -func (tib *TIB) Start() { +func (tib *TIB) Start() error { if tib.Logger == nil { tib.Logger = logrus.New() } setLogger(tib.Logger) + + if tib.KV == nil { + return errors.New("kv store cannot be nil") + } setKVStorage(tib.KV) + if tib.CertManager == nil { + return errors.New("certificate manager cannot be nil") + } SetCertManager(tib.CertManager) tothic.TothErrorHandler = tykerror.HandleError if tib.CookieSecret != "" { diff --git a/providers/tapProvider.go b/providers/tapProvider.go index 8eb06407..6e9a8a9e 100644 --- a/providers/tapProvider.go +++ b/providers/tapProvider.go @@ -3,6 +3,7 @@ package providers import ( "encoding/json" "errors" + "github.com/TykTechnologies/tyk-identity-broker/initializer" "github.com/TykTechnologies/tyk-identity-broker/constants" "github.com/TykTechnologies/tyk-identity-broker/tap" @@ -66,7 +67,7 @@ func GetTapProfile(AuthConfigStore, identityKeyStore tap.AuthRegisterBackend, id } } - thisIdentityProvider, providerErr := GetTAProvider(thisProfile, tykHandler, identityKeyStore) + thisIdentityProvider, providerErr := GetTAProvider(thisProfile, tykHandler, initializer.IdentityKeyStore) if providerErr != nil { log.WithError(providerErr).Error("Getting Tap Provider") return nil, thisProfile, &tap.HttpError{ From ddb257d49f1c76be8adaf0c153e0137fa8779b5c Mon Sep 17 00:00:00 2001 From: sredny buitrago Date: Fri, 2 Aug 2024 12:15:03 -0400 Subject: [PATCH 5/6] fix build --- initializer/initializer.go | 1 + providers/tapProvider.go | 3 +-- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/initializer/initializer.go b/initializer/initializer.go index 54a26bf4..f5e9e7f8 100644 --- a/initializer/initializer.go +++ b/initializer/initializer.go @@ -118,4 +118,5 @@ func (tib *TIB) Start() error { // then it will read it from env tothic.SetupSessionStore() } + return nil } diff --git a/providers/tapProvider.go b/providers/tapProvider.go index 6e9a8a9e..8eb06407 100644 --- a/providers/tapProvider.go +++ b/providers/tapProvider.go @@ -3,7 +3,6 @@ package providers import ( "encoding/json" "errors" - "github.com/TykTechnologies/tyk-identity-broker/initializer" "github.com/TykTechnologies/tyk-identity-broker/constants" "github.com/TykTechnologies/tyk-identity-broker/tap" @@ -67,7 +66,7 @@ func GetTapProfile(AuthConfigStore, identityKeyStore tap.AuthRegisterBackend, id } } - thisIdentityProvider, providerErr := GetTAProvider(thisProfile, tykHandler, initializer.IdentityKeyStore) + thisIdentityProvider, providerErr := GetTAProvider(thisProfile, tykHandler, identityKeyStore) if providerErr != nil { log.WithError(providerErr).Error("Getting Tap Provider") return nil, thisProfile, &tap.HttpError{ From 1c64420d0394c47ef8d5fb33e7ac1bb4af69768b Mon Sep 17 00:00:00 2001 From: sredny buitrago Date: Fri, 2 Aug 2024 12:40:49 -0400 Subject: [PATCH 6/6] remove unused file --- initializer/initializer.go | 18 ------------------ initializer/initializer_test.go | 25 ------------------------- 2 files changed, 43 deletions(-) delete mode 100644 initializer/initializer_test.go diff --git a/initializer/initializer.go b/initializer/initializer.go index f5e9e7f8..79e7f4c9 100644 --- a/initializer/initializer.go +++ b/initializer/initializer.go @@ -3,7 +3,6 @@ package initializer import ( "errors" - "github.com/TykTechnologies/storage/persistent" temporal "github.com/TykTechnologies/storage/temporal/keyvalue" "github.com/TykTechnologies/tyk-identity-broker/backends" tykerror "github.com/TykTechnologies/tyk-identity-broker/error" @@ -58,23 +57,6 @@ func SetCertManager(cm certs.CertificateManager) { providers.CertManager = cm } -func CreateInMemoryBackend() tap.AuthRegisterBackend { - inMemoryBackend := &backends.InMemoryBackend{} - var config interface{} - inMemoryBackend.Init(config) - return inMemoryBackend -} - -func CreateMongoBackend(store persistent.PersistentStorage) tap.AuthRegisterBackend { - mongoBackend := &backends.MongoBackend{ - Store: store, - Collection: tap.ProfilesCollectionName, - } - var config interface{} - mongoBackend.Init(config) - return mongoBackend -} - func SetConfigHandler(backend tap.AuthRegisterBackend) { tothic.SetParamsStoreHandler(backend) } diff --git a/initializer/initializer_test.go b/initializer/initializer_test.go deleted file mode 100644 index 856b8fc8..00000000 --- a/initializer/initializer_test.go +++ /dev/null @@ -1,25 +0,0 @@ -package initializer - -import ( - "testing" - - temporal "github.com/TykTechnologies/storage/temporal/keyvalue" - "github.com/TykTechnologies/tyk-identity-broker/backends" - "github.com/stretchr/testify/assert" -) - -func TestCreateBackendFromRedisConn(t *testing.T) { - var kv temporal.KeyValue - keyPrefix := "test-prefix" - - // Call the function - result := CreateBackendFromRedisConn(kv, keyPrefix) - - // Assert that result is not nil - assert.NotNil(t, result) - redisBackend, ok := result.(*backends.RedisBackend) - assert.True(t, ok) - - // Assert that the KeyPrefix is correctly set - assert.Equal(t, keyPrefix, redisBackend.KeyPrefix) -}