From 6a150402d27407ed72ce1fe4625a2e3fc1b85a3c Mon Sep 17 00:00:00 2001 From: DeniskaRediska Date: Wed, 11 May 2022 15:19:54 +0300 Subject: [PATCH] MSP-625 remove auth cache --- authenticate/authenticate.go | 38 ++++--------- conf/remote_config.go | 6 --- main.go | 2 - service/veritification/cacheable_verify.go | 62 ---------------------- service/veritification/verify.go | 12 ----- 5 files changed, 9 insertions(+), 111 deletions(-) delete mode 100644 service/veritification/cacheable_verify.go diff --git a/authenticate/authenticate.go b/authenticate/authenticate.go index c70e809..0643a20 100644 --- a/authenticate/authenticate.go +++ b/authenticate/authenticate.go @@ -2,12 +2,10 @@ package authenticate import ( "strconv" - "time" "github.com/integration-system/isp-lib/v2/config" "github.com/integration-system/isp-lib/v2/utils" log "github.com/integration-system/isp-log" - "github.com/integration-system/isp-log/stdcodes" "github.com/pkg/errors" "github.com/valyala/fasthttp" "google.golang.org/grpc/codes" @@ -33,32 +31,14 @@ var notExpectedHeaders = []string{ utils.DeviceIdHeader, } -var auth = authenticate{} +var verify = veritification.NewRuntimeVerify() -type ( - authenticate struct { - verify veritification.Verify - } - - verifiable struct { - appId int32 - adminId int64 - ctx *fasthttp.RequestCtx - path string - headers map[string]string - } -) - -func ReceiveConfiguration(conf conf.Cache) { - if conf.EnableCache { - if timeout, err := time.ParseDuration(conf.EvictTimeout); err != nil { - log.Fatalf(stdcodes.ModuleInvalidRemoteConfig, "invalid timeout '%s'", timeout) - } else { - auth.verify = veritification.NewCacheablesVerify(timeout) - } - } else { - auth.verify = veritification.NewRuntimeVerify() - } +type verifiable struct { + appId int32 + adminId int64 + ctx *fasthttp.RequestCtx + path string + headers map[string]string } func Do(ctx *fasthttp.RequestCtx, path string) (int32, int64, error) { @@ -114,7 +94,7 @@ func (v *verifiable) verifyAppToken() error { } } - verifiableHeaders, err := auth.verify.ApplicationToken(string(appToken)) + verifiableHeaders, err := verify.ApplicationToken(string(appToken)) if err != nil { log.Error(log_code.ErrorAuthenticate, err) return createError("internal Server error", codes.Internal) @@ -152,7 +132,7 @@ func (v *verifiable) verifyUserToken() error { v.headers[utils.DeviceTokenHeader] = string(v.getParam(utils.DeviceTokenHeader)) var err error - v.headers, err = auth.verify.Identity(v.headers, v.path) + v.headers, err = verify.Identity(v.headers, v.path) if err != nil { switch err { case veritification.ErrorInvalidUserId: diff --git a/conf/remote_config.go b/conf/remote_config.go index 6c77c70..5211a90 100644 --- a/conf/remote_config.go +++ b/conf/remote_config.go @@ -28,12 +28,6 @@ type ( JournalSetting Journal `schema:"Настройка журалирования"` Redis structure.RedisConfiguration `schema:"Настройка Redis" valid:"required~Required"` AccountingSetting Accounting `schema:"Настройка учета запросов"` - AuthCacheSetting Cache `schema:"Настройка кеширования данных аутентификации приложений" valid:"required~Required"` - } - - Cache struct { - EnableCache bool `schema:"Кеширование,включает кеширования для токена приложения"` - EvictTimeout string `schema:"Время жизни записи в кеше"` } Journal struct { diff --git a/main.go b/main.go index 1f70048..2c74959 100644 --- a/main.go +++ b/main.go @@ -16,7 +16,6 @@ import ( "github.com/integration-system/isp-log/stdcodes" "github.com/pkg/errors" "isp-gate-service/accounting" - "isp-gate-service/authenticate" "isp-gate-service/conf" "isp-gate-service/proxy" "isp-gate-service/redis" @@ -67,7 +66,6 @@ func onRemoteConfigReceive(remoteConfig, oldRemoteConfig *conf.RemoteConfig) { matcher.JournalMethods = matcher.NewAtLeastOneMatcher(remoteConfig.JournalSetting.MethodsPatterns) redis.Client.ReceiveConfiguration(remoteConfig.Redis) - authenticate.ReceiveConfiguration(remoteConfig.AuthCacheSetting) accounting.ReceiveConfiguration(remoteConfig.AccountingSetting) metric.InitCollectors(remoteConfig.Metrics, oldRemoteConfig.Metrics) diff --git a/service/veritification/cacheable_verify.go b/service/veritification/cacheable_verify.go deleted file mode 100644 index 0b5b1b2..0000000 --- a/service/veritification/cacheable_verify.go +++ /dev/null @@ -1,62 +0,0 @@ -package veritification - -import ( - "sync" - "time" -) - -const identitiesCount = 4 - -type ( - cacheablesVerify struct { - cache map[string]cacheInfo - timeout time.Duration - rv *runtimeVerify - lock sync.RWMutex - } - - cacheInfo struct { - created time.Time - identity map[string]string - } -) - -func (cv *cacheablesVerify) ApplicationToken(token string) (map[string]string, error) { - cv.lock.RLock() - cache, ok := cv.cache[token] - cv.lock.RUnlock() - if ok { - if time.Since(cache.created) < cv.timeout { - return cv.copyCache(cache.identity), nil - } - } - - cv.lock.Lock() - defer cv.lock.Unlock() - cache, ok = cv.cache[token] - if ok { - if time.Since(cache.created) < cv.timeout { - return cv.copyCache(cache.identity), nil - } - delete(cv.cache, token) - } - - identity, err := cv.rv.ApplicationToken(token) - if err != nil { - return nil, err - } - cv.cache[token] = cacheInfo{created: time.Now(), identity: identity} - return cv.copyCache(identity), nil -} - -func (cv *cacheablesVerify) Identity(identity map[string]string, uri string) (map[string]string, error) { - return cv.rv.Identity(identity, uri) -} - -func (cv *cacheablesVerify) copyCache(cache map[string]string) map[string]string { - resp := make(map[string]string, identitiesCount) - for key, value := range cache { - resp[key] = value - } - return resp -} diff --git a/service/veritification/verify.go b/service/veritification/verify.go index df99862..63c9089 100644 --- a/service/veritification/verify.go +++ b/service/veritification/verify.go @@ -1,9 +1,5 @@ package veritification -import ( - "time" -) - type Verify interface { ApplicationToken(string) (map[string]string, error) Identity(map[string]string, string) (map[string]string, error) @@ -12,11 +8,3 @@ type Verify interface { func NewRuntimeVerify() Verify { return &runtimeVerify{} } - -func NewCacheablesVerify(timeout time.Duration) Verify { - return &cacheablesVerify{ - cache: make(map[string]cacheInfo), - timeout: timeout, - rv: &runtimeVerify{}, - } -}