Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions certs/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (

"github.com/sirupsen/logrus"

"github.com/TykTechnologies/tyk/interfaces"
"github.com/TykTechnologies/tyk/internal/cache"
tykcrypto "github.com/TykTechnologies/tyk/internal/crypto"
"github.com/TykTechnologies/tyk/storage"
"github.com/TykTechnologies/tyk/storage/mdcb"
)

const (
Expand Down Expand Up @@ -52,14 +53,14 @@ type CertificateManager interface {
}

type certificateManager struct {
storage storage.Handler
storage interfaces.Handler
logger *logrus.Entry
cache cache.Repository
secret string
migrateCertList bool
}

func NewCertificateManager(storage storage.Handler, secret string, logger *logrus.Logger, migrateCertList bool) *certificateManager {
func NewCertificateManager(storage interfaces.Handler, secret string, logger *logrus.Logger, migrateCertList bool) *certificateManager {
if logger == nil {
logger = logrus.New()
}
Expand All @@ -79,7 +80,7 @@ func getOrgFromKeyID(key, certID string) string {
return orgId
}

func NewSlaveCertManager(localStorage, rpcStorage storage.Handler, secret string, logger *logrus.Logger, migrateCertList bool) *certificateManager {
func NewSlaveCertManager(localStorage, rpcStorage interfaces.Handler, secret string, logger *logrus.Logger, migrateCertList bool) *certificateManager {
if logger == nil {
logger = logrus.New()
}
Expand All @@ -101,7 +102,7 @@ func NewSlaveCertManager(localStorage, rpcStorage storage.Handler, secret string
return err
}

mdcbStorage := storage.NewMdcbStorage(localStorage, rpcStorage, log)
mdcbStorage := mdcb.NewMdcbStorage(localStorage, rpcStorage, log)
mdcbStorage.CallbackonPullfromRPC = &callbackOnPullCertFromRPC

cm.storage = mdcbStorage
Expand Down
2 changes: 1 addition & 1 deletion certs/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func TestStorageIndex(t *testing.T) {
storage, ok := m.storage.(*storage.DummyStorage)

if !ok {
t.Error("cannot make storage.DummyStorage of type storage.Handler")
t.Error("cannot make storage.DummyStorage of type interfaces.Handler")
}

if len(storage.IndexList) != 0 {
Expand Down
4 changes: 2 additions & 2 deletions ctx/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import (
"net/http"

"github.com/TykTechnologies/tyk/apidef/oas"
"github.com/TykTechnologies/tyk/storage/util"

"github.com/TykTechnologies/tyk/config"

"github.com/TykTechnologies/tyk/apidef"
logger "github.com/TykTechnologies/tyk/log"
"github.com/TykTechnologies/tyk/storage"
"github.com/TykTechnologies/tyk/user"
)

Expand Down Expand Up @@ -69,7 +69,7 @@ func ctxSetSession(r *http.Request, s *user.SessionState, scheduleUpdate bool, h
}

if s.KeyHashEmpty() {
s.SetKeyHash(storage.HashKey(s.KeyID, hashKey))
s.SetKeyHash(util.HashKey(s.KeyID, hashKey))
}

ctx := r.Context()
Expand Down
3 changes: 2 additions & 1 deletion gateway/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/regexp"
"github.com/TykTechnologies/tyk/storage"
"github.com/TykTechnologies/tyk/storage/util"
)

const analyticsKeyName = "tyk-system-analytics"
Expand Down Expand Up @@ -175,7 +176,7 @@ func (r *RedisAnalyticsHandler) recordWorker() {
// we have new record - prepare it and add to buffer

// If we are obfuscating API Keys, store the hashed representation (config check handled in hashing function)
record.APIKey = storage.HashKey(record.APIKey, r.globalConf.HashKeys)
record.APIKey = util.HashKey(record.APIKey, r.globalConf.HashKeys)

if r.globalConf.SlaveOptions.UseRPC {
// Extend tag list to include this data so wecan segment by node if necessary
Expand Down
55 changes: 44 additions & 11 deletions gateway/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ import (
"github.com/getkin/kin-openapi/openapi3"

"github.com/TykTechnologies/tyk/config"
"github.com/TykTechnologies/tyk/storage"
"github.com/TykTechnologies/tyk/storage/util"

"github.com/TykTechnologies/tyk/internal/otel"
"github.com/TykTechnologies/tyk/internal/uuid"
Expand All @@ -64,7 +66,6 @@ import (
"github.com/TykTechnologies/tyk/apidef"
"github.com/TykTechnologies/tyk/ctx"
"github.com/TykTechnologies/tyk/header"
"github.com/TykTechnologies/tyk/storage"
"github.com/TykTechnologies/tyk/user"

gql "github.com/TykTechnologies/graphql-go-tools/pkg/graphql"
Expand Down Expand Up @@ -415,7 +416,7 @@ func (gw *Gateway) setBasicAuthSessionPassword(session *user.SessionState) {
return
}

session.BasicAuthData.Password = storage.HashStr(session.BasicAuthData.Password, basicAuthHashAlgo)
session.BasicAuthData.Password = util.HashStr(session.BasicAuthData.Password, basicAuthHashAlgo)
session.BasicAuthData.Hash = user.HashType(basicAuthHashAlgo)
}

Expand Down Expand Up @@ -538,7 +539,7 @@ func (gw *Gateway) handleAddOrUpdate(keyName string, r *http.Request, isHashed b
newSession.BasicAuthData.Password = originalKey.BasicAuthData.Password
}

if r.Method == http.MethodPost || storage.TokenOrg(keyName) != "" {
if r.Method == http.MethodPost || util.TokenOrg(keyName) != "" {
// use new key format if key gets created or updating key with new format
if err := gw.doAddOrUpdate(keyName, newSession, suppressReset, isHashed); err != nil {
return apiError("Failed to create key, ensure security settings are correct."), http.StatusInternalServerError
Expand Down Expand Up @@ -586,7 +587,7 @@ func (gw *Gateway) handleAddOrUpdate(keyName string, r *http.Request, isHashed b
if isHashed {
response.KeyHash = keyName
} else {
response.KeyHash = storage.HashKey(keyName, gw.GetConfig().HashKeys)
response.KeyHash = util.HashKey(keyName, gw.GetConfig().HashKeys)
}
}

Expand Down Expand Up @@ -614,7 +615,7 @@ func (gw *Gateway) handleGetDetail(sessionKey, apiID, orgID string, byHash bool)
mw.ApplyPolicies(&session)

if session.QuotaMax != -1 {
quotaKey := QuotaKeyPrefix + storage.HashKey(sessionKey, gw.GetConfig().HashKeys)
quotaKey := QuotaKeyPrefix + util.HashKey(sessionKey, gw.GetConfig().HashKeys)
if byHash {
quotaKey = QuotaKeyPrefix + sessionKey
}
Expand Down Expand Up @@ -649,7 +650,7 @@ func (gw *Gateway) handleGetDetail(sessionKey, apiID, orgID string, byHash bool)
quotaScope = access.AllowanceScope + "-"
}

limQuotaKey := QuotaKeyPrefix + quotaScope + storage.HashKey(sessionKey, gw.GetConfig().HashKeys)
limQuotaKey := QuotaKeyPrefix + quotaScope + util.HashKey(sessionKey, gw.GetConfig().HashKeys)
if byHash {
limQuotaKey = QuotaKeyPrefix + quotaScope + sessionKey
}
Expand Down Expand Up @@ -679,7 +680,7 @@ func (gw *Gateway) handleGetDetail(sessionKey, apiID, orgID string, byHash bool)

// If it's a basic auth key and a valid Base64 string, use it as the key ID:
if session.IsBasicAuth() {
if storage.TokenOrg(sessionKey) != "" {
if util.TokenOrg(sessionKey) != "" {
session.KeyID = sessionKey
}
session.BasicAuthData.Password = ""
Expand Down Expand Up @@ -1794,7 +1795,7 @@ func (gw *Gateway) handleOrgAddOrUpdate(orgID string, r *http.Request) (interfac
if r.URL.Query().Get("reset_quota") == "1" {
sessionManager.ResetQuota(orgID, newSession, false)
newSession.QuotaRenews = time.Now().Unix() + newSession.QuotaRenewalRate
rawKey := QuotaKeyPrefix + storage.HashKey(orgID, gw.GetConfig().HashKeys)
rawKey := QuotaKeyPrefix + util.HashKey(orgID, gw.GetConfig().HashKeys)

// manage quotas separately
gw.DefaultQuotaStore.RemoveSession(orgID, rawKey, false)
Expand Down Expand Up @@ -2062,7 +2063,7 @@ func (gw *Gateway) createKeyHandler(w http.ResponseWriter, r *http.Request) {

// add key hash to reply
if gw.GetConfig().HashKeys {
obj.KeyHash = storage.HashKey(newKey, gw.GetConfig().HashKeys)
obj.KeyHash = util.HashKey(newKey, gw.GetConfig().HashKeys)
}

gw.FireSystemEvent(EventTokenCreated, EventTokenMeta{
Expand Down Expand Up @@ -2235,13 +2236,29 @@ func (gw *Gateway) createOauthClient(w http.ResponseWriter, r *http.Request) {
storageManager := gw.getGlobalMDCBStorageHandler(prefix, false)
storageManager.Connect()

store, err := storage.NewStorageHandler(storage.GetStorageForModule(storage.DEFAULT_MODULE),
storage.WithKeyPrefix(prefix),
storage.WithHashKeys(false),
storage.WithConnectionHandler(gw.StorageConnectionHandler))

if err != nil {
log.WithFields(logrus.Fields{
"prefix": "api",
"apiID": apiID,
"status": "fail",
"err": err,
}).Error("Failed to create OAuth client")
doJSONWrite(w, http.StatusInternalServerError, apiError("Failure in storing client data."))
return
}

apiSpec.OAuthManager = &OAuthManager{
OsinServer: gw.TykOsinNewServer(
&osin.ServerConfig{},
&RedisOsinStorageInterface{
storageManager,
gw.GlobalSessionManager,
&storage.RedisCluster{KeyPrefix: prefix, HashKeys: false, ConnectionHandler: gw.StorageConnectionHandler},
store,
apiSpec.OrgID,
gw,
}),
Expand Down Expand Up @@ -2622,12 +2639,28 @@ func (gw *Gateway) getOauthClientDetails(keyName, apiID string) (interface{}, in
prefix := generateOAuthPrefix(apiSpec.APIID)
storageManager := gw.getGlobalMDCBStorageHandler(prefix, false)
storageManager.Connect()

store, err := storage.NewStorageHandler(storage.GetStorageForModule(storage.DEFAULT_MODULE),
storage.WithKeyPrefix(prefix),
storage.WithHashKeys(false),
storage.WithConnectionHandler(gw.StorageConnectionHandler))

if err != nil {
log.WithFields(logrus.Fields{
"prefix": "api",
"apiID": apiID,
"status": "fail",
"err": err,
}).Error("Failed to retrieve OAuth client details")
return apiError("OAuth Client ID not found"), http.StatusNotFound
}

apiSpec.OAuthManager = &OAuthManager{
OsinServer: gw.TykOsinNewServer(&osin.ServerConfig{},
&RedisOsinStorageInterface{
storageManager,
gw.GlobalSessionManager,
&storage.RedisCluster{KeyPrefix: prefix, HashKeys: false, ConnectionHandler: gw.StorageConnectionHandler},
store,
apiSpec.OrgID,
gw,
}),
Expand Down
4 changes: 2 additions & 2 deletions gateway/api_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
texttemplate "text/template"
"time"

"github.com/TykTechnologies/tyk/interfaces"
"github.com/TykTechnologies/tyk/storage/kv"

"github.com/getkin/kin-openapi/routers"
Expand Down Expand Up @@ -49,7 +50,6 @@ import (
"github.com/TykTechnologies/tyk/header"
"github.com/TykTechnologies/tyk/regexp"
"github.com/TykTechnologies/tyk/rpc"
"github.com/TykTechnologies/tyk/storage"
)

// const used by cache middleware
Expand Down Expand Up @@ -1421,7 +1421,7 @@ func (a APIDefinitionLoader) getExtendedPathSpecs(apiVersionDef apidef.VersionIn
return combinedPath, len(whiteListPaths) > 0
}

func (a *APISpec) Init(authStore, sessionStore, healthStore, orgStore storage.Handler) {
func (a *APISpec) Init(authStore, sessionStore, healthStore, orgStore interfaces.Handler) {
a.AuthManager.Init(authStore)
a.Health.Init(healthStore)
a.OrgSessionManager.Init(orgStore)
Expand Down
8 changes: 4 additions & 4 deletions gateway/api_healthcheck.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"time"

"github.com/TykTechnologies/tyk/storage"
"github.com/TykTechnologies/tyk/interfaces"
)

type HealthPrefix string
Expand All @@ -19,7 +19,7 @@ const (
)

type HealthChecker interface {
Init(storage.Handler)
Init(interfaces.Handler)
ApiHealthValues() (HealthCheckValues, error)
StoreCounterVal(HealthPrefix, string)
}
Expand All @@ -34,11 +34,11 @@ type HealthCheckValues struct {

type DefaultHealthChecker struct {
Gw *Gateway `json:"-"`
storage storage.Handler
storage interfaces.Handler
APIID string
}

func (h *DefaultHealthChecker) Init(storeType storage.Handler) {
func (h *DefaultHealthChecker) Init(storeType interfaces.Handler) {
if !h.Gw.GetConfig().HealthCheck.EnableHealthChecks {
return
}
Expand Down
Loading