MSP-625 remove auth cache
This commit is contained in:
1 parent
626eda7990
commit
6a150402d2
5 files changed
+9
-111
No files matched your search
@@ -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:
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
}
|
||||
@@ -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{},
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user