add(approve request)
This commit is contained in:
1 parent
e3c3b86b04
commit
1a8ed4e8ea
15 files changed
+446
-44
No files matched your search
Generated
+60
-33
@@ -17,6 +17,14 @@
|
||||
revision = "ade4e2031af3aed7fffd241084aad80a58faf421"
|
||||
version = "v0.1.1"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:ffe9824d294da03b391f44e1ae8281281b4afc1bdaa9588c9097785e3af10cec"
|
||||
name = "github.com/davecgh/go-spew"
|
||||
packages = ["spew"]
|
||||
pruneopts = "UT"
|
||||
revision = "8991bc29aa16c548c550c7ff78260e27b9ab7c73"
|
||||
version = "v1.1.1"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:76dc72490af7174349349838f2fe118996381b31ea83243812a97e5a0fd5ed55"
|
||||
name = "github.com/dgrijalva/jwt-go"
|
||||
@@ -155,7 +163,6 @@
|
||||
version = "v1.3.0"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
digest = "1:a1ebd1a46e87e9343cbabec1ca99c34a7a2e0572c0160fcf266175073b2adf5f"
|
||||
name = "github.com/integration-system/isp-lib"
|
||||
packages = [
|
||||
@@ -174,6 +181,7 @@
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "5060d1348219abd8fad3628668b229779a5314c7"
|
||||
version = "v1.9.0"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
@@ -206,7 +214,7 @@
|
||||
version = "1.1.8"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:caa1da085cc12cd39d3ddb267434902f621050e1b46f7e9477b2b933df680193"
|
||||
digest = "1:acfc228fb5fbfb82645c121e3224bd546ed68aef6dfc87f63e2d8de099a3822b"
|
||||
name = "github.com/klauspost/compress"
|
||||
packages = [
|
||||
"flate",
|
||||
@@ -214,16 +222,8 @@
|
||||
"zlib",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "da9470192e5d0ad619defde5ded583e5e907d3e8"
|
||||
version = "v1.8.6"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:923c4d7194b42e054b2eb8a6c62824ac55e23ececc1c7e48d4da69c971c55954"
|
||||
name = "github.com/klauspost/cpuid"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "05a8198c0f5a27739aec358908d7e12c64ce6eb7"
|
||||
version = "v1.2.1"
|
||||
revision = "16a4d3d7137cdefd94d420f22b5c20260674b95c"
|
||||
version = "v1.9.1"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:31e761d97c76151dde79e9d28964a812c46efc5baee4085b86f68f0c654450de"
|
||||
@@ -298,12 +298,12 @@
|
||||
revision = "c48cc78d482608239f6c4c92a4abd87eb8761c90"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:bbd3997f0121200f72b64d7a3826eb8a0b910d6a4c19894c9fe2852b9e5eaf3b"
|
||||
digest = "1:6eea828983c70075ca297bb915ffbcfd3e34c5a50affd94428a65df955c0ff9c"
|
||||
name = "github.com/pelletier/go-toml"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "8fe62057ea2d46ce44254c98e84e810044dbe197"
|
||||
version = "v1.5.0"
|
||||
revision = "903d9455db9ff1d7ac1ab199062eca7266dd11a3"
|
||||
version = "v1.6.0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:cf31692c14422fa27c83a05292eb5cbe0fb2775972e8f1f8446a71549bd8980b"
|
||||
@@ -313,6 +313,14 @@
|
||||
revision = "ba968bfe8b2f7e042a574c888954fccecfa385b4"
|
||||
version = "v0.8.1"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:0028cb19b2e4c3112225cd871870f2d9cf49b9b4276531f03438a88e94be86fe"
|
||||
name = "github.com/pmezard/go-difflib"
|
||||
packages = ["difflib"]
|
||||
pruneopts = "UT"
|
||||
revision = "792786c7400a136282c1664665ae0a8db921c6c2"
|
||||
version = "v1.0.0"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
digest = "1:5bbebe8ac19ecb6c87790a89faa20566e38ed0d6494a1d14c4f5b05d9ce2436c"
|
||||
@@ -365,13 +373,29 @@
|
||||
version = "v1.0.5"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:11118bd196646c6515fea3d6c43f66162833c6ae4939bfb229b9956d91c6cf17"
|
||||
digest = "1:0b60fc944fb6a7b6c985832bd341bdb7ed8fe894fea330414e7774bb24652962"
|
||||
name = "github.com/spf13/viper"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "b5bf975e5823809fb22c7644d008757f78a4259e"
|
||||
revision = "72b022eb357a56469725dcd03918449e2278d02e"
|
||||
version = "v1.5.0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:8548c309c65a85933a625be5e7d52b6ac927ca30c56869fae58123b8a77a75e1"
|
||||
name = "github.com/stretchr/testify"
|
||||
packages = ["assert"]
|
||||
pruneopts = "UT"
|
||||
revision = "221dbe5ed46703ee255b1da0dec05086f5035f62"
|
||||
version = "v1.4.0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:f4b32291cad5efac2bfdba89ccde6aa04618b62ce06c1a571da2dc4f3f2677fb"
|
||||
name = "github.com/subosito/gotenv"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "2ef7124db659d49edac6aa459693a15ae36c671a"
|
||||
version = "v1.2.0"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
digest = "1:d32b64a7a82dd36b674fb73ff767e949eb7155b6b574ff276f49d4edc8af105f"
|
||||
@@ -390,7 +414,7 @@
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
digest = "1:8df917c05bc5b359af4a95a39049919ec141d6224f48bb7b92a8f44c91c0f5ba"
|
||||
digest = "1:35cbf67ac1c92eb7bd6fe70d17b75b60bd38fb2222fa71ac82d44d3813897d22"
|
||||
name = "github.com/valyala/fasthttp"
|
||||
packages = [
|
||||
".",
|
||||
@@ -398,7 +422,7 @@
|
||||
"stackless",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "707b1c1917f38038d0d81e856df73853513100b3"
|
||||
revision = "87c4546c7b1268a1c29c46e8f54def6f0e7596eb"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:39a425f98fb19427061a693fe6bf0683c9bddec4b25b17067e34fdb465e39cb9"
|
||||
@@ -414,11 +438,11 @@
|
||||
name = "golang.org/x/crypto"
|
||||
packages = ["ssh/terminal"]
|
||||
pruneopts = "UT"
|
||||
revision = "87dc89f01550277dc22b74ffcf4cd89fa2f40f4c"
|
||||
revision = "c7e5f84aec591254278750bee18f39e5dd19cdb5"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
digest = "1:c124e945335a9c62ede3a565f3198c5ae9a5ecc39fddd85871138362d128fb74"
|
||||
digest = "1:d639fcedf8f14de3eefa8f26740091ca737108a944314a4700d482bd11a18301"
|
||||
name = "golang.org/x/net"
|
||||
packages = [
|
||||
"context",
|
||||
@@ -430,18 +454,18 @@
|
||||
"trace",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "ec77196f6094c3492a8b61f2c11cf937f78992ae"
|
||||
revision = "a882066a44e04a21d46e51451c71e131344e830e"
|
||||
|
||||
[[projects]]
|
||||
branch = "master"
|
||||
digest = "1:f8c0a298987fa1c452d209f37d2ed9bcf874f2c6eec817607e8ebbba88e6331f"
|
||||
digest = "1:1017834f19e0d987e8b1b928f016b0a1186a202ec1867fee7112efd4b4475bd1"
|
||||
name = "golang.org/x/sys"
|
||||
packages = [
|
||||
"unix",
|
||||
"windows",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "e66fe6eb8e0cbfacade8b490cf55cd2494f4db62"
|
||||
revision = "c1f44814a5cd81a6d1cb589ef1e528bc5d305e07"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:8d8faad6b12a3a4c819a3f9618cb6ee1fa1cfc33253abeeea8b55336721e3405"
|
||||
@@ -477,13 +501,14 @@
|
||||
"googleapis/rpc/status",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "548a555dbc03994223efbaba0090152849259498"
|
||||
revision = "919d9bdd9fe6f1a5dd95ce5d5e4cdb8fd3c516d0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:6cd77d0b616d2dcebd363dfecba593f27b0151fc82cdb5fbfb96c5a7cfbc95b5"
|
||||
digest = "1:0c0ea0f0ccec8a112245dd3ca0c3b304224ede9afd4b7a2b482a7b48471ef894"
|
||||
name = "google.golang.org/grpc"
|
||||
packages = [
|
||||
".",
|
||||
"backoff",
|
||||
"balancer",
|
||||
"balancer/base",
|
||||
"balancer/roundrobin",
|
||||
@@ -499,10 +524,13 @@
|
||||
"internal/backoff",
|
||||
"internal/balancerload",
|
||||
"internal/binarylog",
|
||||
"internal/buffer",
|
||||
"internal/channelz",
|
||||
"internal/envconfig",
|
||||
"internal/grpcrand",
|
||||
"internal/grpcsync",
|
||||
"internal/resolver/dns",
|
||||
"internal/resolver/passthrough",
|
||||
"internal/syscall",
|
||||
"internal/transport",
|
||||
"keepalive",
|
||||
@@ -510,24 +538,22 @@
|
||||
"naming",
|
||||
"peer",
|
||||
"resolver",
|
||||
"resolver/dns",
|
||||
"resolver/passthrough",
|
||||
"serviceconfig",
|
||||
"stats",
|
||||
"status",
|
||||
"tap",
|
||||
]
|
||||
pruneopts = "UT"
|
||||
revision = "f6d0f9ee430895e87ef1ceb5ac8f39725bafceef"
|
||||
version = "v1.24.0"
|
||||
revision = "9d331e2b02dd47daeecae02790f61cc88dc75a64"
|
||||
version = "v1.25.0"
|
||||
|
||||
[[projects]]
|
||||
digest = "1:59f10c1537d2199d9115d946927fe31165959a95190849c82ff11e05803528b0"
|
||||
digest = "1:f26a5d382387e03a40d1471dddfba85dfff9bf05352d7e42d37612677c4d3c5c"
|
||||
name = "gopkg.in/yaml.v2"
|
||||
packages = ["."]
|
||||
pruneopts = "UT"
|
||||
revision = "f221b8435cfb71e54062f6c6e99e9ade30b124d5"
|
||||
version = "v2.2.4"
|
||||
revision = "f90ceb4f409096b60e2e9076b38b304b8246e5fa"
|
||||
version = "v2.2.5"
|
||||
|
||||
[solve-meta]
|
||||
analyzer-name = "dep"
|
||||
@@ -553,6 +579,7 @@
|
||||
"github.com/json-iterator/go",
|
||||
"github.com/pkg/errors",
|
||||
"github.com/rcrowley/go-metrics",
|
||||
"github.com/stretchr/testify/assert",
|
||||
"github.com/valyala/fasthttp",
|
||||
"golang.org/x/net/context",
|
||||
"google.golang.org/grpc",
|
||||
|
||||
+1
-1
@@ -34,7 +34,7 @@
|
||||
|
||||
[[override]]
|
||||
name = "github.com/integration-system/isp-lib"
|
||||
branch = "master"
|
||||
version = "1.9.0"
|
||||
|
||||
[prune]
|
||||
go-tests = true
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
package approve
|
||||
|
||||
import (
|
||||
log "github.com/integration-system/isp-log"
|
||||
"isp-gate-service/approve/matcher"
|
||||
"isp-gate-service/approve/state"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/log_code"
|
||||
)
|
||||
|
||||
var approvingByAppId = make(map[int64]approve)
|
||||
|
||||
type approve struct {
|
||||
matcher matcher.Matcher
|
||||
limitStates map[string]state.LimitState
|
||||
}
|
||||
|
||||
func ReceiveConfiguration(setting []conf.ApproveSetting) {
|
||||
for _, s := range setting {
|
||||
limitState, patternArray, err := state.InitLimitState(s.Limits)
|
||||
if err != nil {
|
||||
log.Fatal(log_code.FatalConfigApproveSetting, err)
|
||||
}
|
||||
|
||||
approvingByAppId[s.ApplicationId] = approve{
|
||||
matcher: matcher.NewCacheableMatcher(patternArray),
|
||||
limitStates: limitState,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func Complete(appId int64, method string) bool {
|
||||
approve, ok := approvingByAppId[appId]
|
||||
if !ok {
|
||||
return true
|
||||
} else {
|
||||
stateStorage := make([]state.LimitState, 0)
|
||||
patternArray := approve.matcher.Match(method)
|
||||
for _, pattern := range patternArray {
|
||||
stateStorage = append(stateStorage, approve.limitStates[pattern])
|
||||
}
|
||||
return state.Update(stateStorage)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
package approve
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"isp-gate-service/conf"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
approveSetting = []conf.ApproveSetting{
|
||||
{ApplicationId: 1, Limits: []conf.LimitSetting{
|
||||
{Pattern: "mdm-master/group/method", MaxCount: 1, Lifetime: "290ms"},
|
||||
{Pattern: "mdm-master/group3/method", MaxCount: 10, Lifetime: "10s"},
|
||||
{Pattern: "mdm-master/group3/*", MaxCount: 5, Lifetime: "10s"},
|
||||
{Pattern: "mdm-master/group4/method", MaxCount: 2, Lifetime: "0s"},
|
||||
}},
|
||||
{ApplicationId: 2, Limits: []conf.LimitSetting{
|
||||
{Pattern: "mdm-master/group/method", MaxCount: 0, Lifetime: "10s"},
|
||||
}},
|
||||
}
|
||||
|
||||
reqExample = []struct {
|
||||
appId int64
|
||||
path []string
|
||||
}{
|
||||
0: {appId: 1, path: []string{
|
||||
"mdm-master/group/method", "mdm-master/group/method", "mdm-master/group/method",
|
||||
"mdm-master/group/method", "mdm-master/group/method", "mdm-master/group/method",
|
||||
}},
|
||||
|
||||
1: {appId: 2, path: []string{
|
||||
"mdm-master/group3/method", "mdm-master/group/method", "mdm-master/group3/method",
|
||||
"mdm-master/group/method", "mdm-master/group3/method", "mdm-master/group/method",
|
||||
}},
|
||||
|
||||
2: {appId: 1, path: []string{
|
||||
"mdm-master/group3/method", "mdm-master/group3/method", "mdm-master/group3/method",
|
||||
"mdm-master/group3/method", "mdm-master/group3/method", "mdm-master/group3/method",
|
||||
}},
|
||||
3: {appId: 1, path: []string{
|
||||
"mdm-master/group4/method", "mdm-master/group4/method", "mdm-master/group4/method",
|
||||
"mdm-master/group4/method", "mdm-master/group4/method", "mdm-master/group4/method",
|
||||
}},
|
||||
}
|
||||
)
|
||||
|
||||
func TestApprove(t *testing.T) {
|
||||
a := assert.New(t)
|
||||
ReceiveConfiguration(approveSetting)
|
||||
|
||||
expected := true
|
||||
req := reqExample[0]
|
||||
for _, path := range req.path {
|
||||
a.Equal(expected, Complete(req.appId, path))
|
||||
expected = !expected
|
||||
time.Sleep(150 * time.Millisecond)
|
||||
}
|
||||
|
||||
//expected == true
|
||||
req = reqExample[1]
|
||||
for _, path := range req.path {
|
||||
a.Equal(expected, Complete(req.appId, path))
|
||||
expected = !expected
|
||||
}
|
||||
|
||||
req = reqExample[2]
|
||||
expectedArray := []bool{true, true, true, true, true, false}
|
||||
for key, path := range req.path {
|
||||
a.Equal(expectedArray[key], Complete(req.appId, path))
|
||||
}
|
||||
|
||||
//expected == true
|
||||
req = reqExample[3]
|
||||
for _, path := range req.path {
|
||||
a.Equal(expected, Complete(req.appId, path))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package matcher
|
||||
|
||||
import "sync"
|
||||
|
||||
type cacheableMatcher struct {
|
||||
cache map[string][]string
|
||||
lock sync.RWMutex
|
||||
rm runtimeMatcher
|
||||
}
|
||||
|
||||
func (mm *cacheableMatcher) Match(method string) []string {
|
||||
if len(mm.rm.patterns) == 0 {
|
||||
return []string{}
|
||||
}
|
||||
|
||||
mm.lock.RLock()
|
||||
arrayPattern, ok := mm.cache[method]
|
||||
mm.lock.RUnlock()
|
||||
if ok {
|
||||
return arrayPattern
|
||||
}
|
||||
|
||||
mm.lock.Lock()
|
||||
defer mm.lock.Unlock()
|
||||
if arrayPattern, ok = mm.cache[method]; ok {
|
||||
return arrayPattern
|
||||
} else if arrayPattern = mm.rm.Match(method); len(arrayPattern) > 0 {
|
||||
mm.cache[method] = arrayPattern
|
||||
}
|
||||
|
||||
return arrayPattern
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package matcher
|
||||
|
||||
type Matcher interface {
|
||||
Match(method string) []string
|
||||
}
|
||||
|
||||
func NewRuntimeMatcher(patterns []string) Matcher {
|
||||
return &runtimeMatcher{
|
||||
patterns: patterns,
|
||||
}
|
||||
}
|
||||
|
||||
func NewCacheableMatcher(patterns []string) Matcher {
|
||||
return &cacheableMatcher{
|
||||
cache: make(map[string][]string, 0),
|
||||
rm: runtimeMatcher{patterns: patterns},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
package matcher
|
||||
|
||||
import "testing"
|
||||
|
||||
var (
|
||||
patterns = []string{
|
||||
"mdm-master/group/method",
|
||||
"mdm-master/group/*",
|
||||
"mdm-master/group2/method",
|
||||
"mdm-master/group3/*",
|
||||
}
|
||||
cases = []struct {
|
||||
method string
|
||||
length int
|
||||
}{
|
||||
{method: "mdm-master/group/method", length: 2},
|
||||
{method: "mdm-master/group/method2", length: 1},
|
||||
{method: "mdm-master/group2/method3", length: 0},
|
||||
{method: "mdm-master/group2/method", length: 1},
|
||||
{method: "mdm-master/group3/some", length: 1},
|
||||
{method: "mdm-master/group3/", length: 1},
|
||||
}
|
||||
)
|
||||
|
||||
func TestCacheableMatcher_Match(t *testing.T) {
|
||||
matcher := NewCacheableMatcher(patterns)
|
||||
for _, c := range cases {
|
||||
res := matcher.Match(c.method)
|
||||
if c.length != len(res) {
|
||||
t.Error(c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCacheableMatcher_Match(b *testing.B) {
|
||||
matcher := NewCacheableMatcher(patterns)
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, c := range cases {
|
||||
_ = matcher.Match(c.method)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkRuntimeMatcher_Match(b *testing.B) {
|
||||
matcher := NewRuntimeMatcher(patterns)
|
||||
for i := 0; i < b.N; i++ {
|
||||
for _, c := range cases {
|
||||
_ = matcher.Match(c.method)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package matcher
|
||||
|
||||
import "path"
|
||||
|
||||
type runtimeMatcher struct {
|
||||
patterns []string
|
||||
}
|
||||
|
||||
func (mm runtimeMatcher) Match(method string) []string {
|
||||
resp := make([]string, 0)
|
||||
for _, pattern := range mm.patterns {
|
||||
if matched, _ := path.Match(pattern, method); matched {
|
||||
resp = append(resp, pattern)
|
||||
}
|
||||
}
|
||||
return resp
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"isp-gate-service/conf"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type (
|
||||
LimitState interface {
|
||||
check() (bool, int, time.Time)
|
||||
update(int, time.Time)
|
||||
}
|
||||
|
||||
updateRequest struct {
|
||||
pointer int
|
||||
datetime time.Time
|
||||
}
|
||||
)
|
||||
|
||||
func InitLimitState(limits []conf.LimitSetting) (map[string]LimitState, []string, error) {
|
||||
limitStates := make(map[string]LimitState)
|
||||
patternArray := make([]string, len(limits))
|
||||
|
||||
for i, limit := range limits {
|
||||
lifetime, err := time.ParseDuration(limit.Lifetime)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
if lifetime == 0 && limit.MaxCount != 0 {
|
||||
limit.MaxCount = 1
|
||||
}
|
||||
|
||||
patternArray[i] = limit.Pattern
|
||||
limitStates[limit.Pattern] = &limiter{
|
||||
lifetime: lifetime,
|
||||
datetime: make([]time.Time, limit.MaxCount),
|
||||
pattern: limit.Pattern,
|
||||
point: -1,
|
||||
mx: sync.RWMutex{},
|
||||
}
|
||||
}
|
||||
return limitStates, patternArray, nil
|
||||
}
|
||||
|
||||
func Update(states []LimitState) bool {
|
||||
update := make([]updateRequest, len(states))
|
||||
for i, st := range states {
|
||||
if ok, point, datetime := st.check(); ok {
|
||||
update[i] = updateRequest{pointer: point, datetime: datetime}
|
||||
} else {
|
||||
return false
|
||||
}
|
||||
}
|
||||
for i, st := range states {
|
||||
u := update[i]
|
||||
st.update(u.pointer, u.datetime)
|
||||
}
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package state
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
type limiter struct {
|
||||
lifetime time.Duration
|
||||
datetime []time.Time
|
||||
pattern string
|
||||
point int
|
||||
mx sync.RWMutex
|
||||
}
|
||||
|
||||
func (lim *limiter) check() (bool, int, time.Time) {
|
||||
if len(lim.datetime) == 0 {
|
||||
return false, 0, time.Time{}
|
||||
}
|
||||
if lim.lifetime == 0 {
|
||||
return true, 0, time.Now()
|
||||
}
|
||||
|
||||
lim.mx.RLock()
|
||||
point := lim.point + 1
|
||||
if point >= len(lim.datetime) {
|
||||
point = 0
|
||||
}
|
||||
date := lim.datetime[point]
|
||||
now := time.Now()
|
||||
approve := now.Sub(date) > lim.lifetime
|
||||
|
||||
lim.mx.RUnlock()
|
||||
return approve, point, now
|
||||
}
|
||||
|
||||
func (lim *limiter) update(point int, time time.Time) {
|
||||
lim.mx.Lock()
|
||||
|
||||
lim.point = point
|
||||
lim.datetime[point] = time
|
||||
|
||||
lim.mx.Unlock()
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import (
|
||||
"google.golang.org/grpc/codes"
|
||||
"isp-gate-service/log_code"
|
||||
"isp-gate-service/routing"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -19,23 +20,23 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
func Do(ctx *fasthttp.RequestCtx) error {
|
||||
func Do(ctx *fasthttp.RequestCtx) (int64, error) {
|
||||
for _, notExpectedHeader := range verifiableKeyHeaderKeyMap {
|
||||
ctx.Request.Header.Del(notExpectedHeader)
|
||||
}
|
||||
|
||||
appToken := ctx.Request.Header.Peek(utils.ApplicationTokenHeader)
|
||||
if len(appToken) == 0 {
|
||||
return createError(codes.Unauthenticated)
|
||||
return 0, createError(codes.Unauthenticated)
|
||||
}
|
||||
|
||||
keys, err := verification.appToken(string(appToken))
|
||||
if err != nil {
|
||||
log.Error(log_code.ErrorAuthenticate, err)
|
||||
return createError(codes.Internal)
|
||||
return 0, createError(codes.Internal)
|
||||
}
|
||||
if len(keys) != 4 {
|
||||
return createError(codes.Unauthenticated)
|
||||
return 0, createError(codes.Unauthenticated)
|
||||
}
|
||||
|
||||
verifiableKeys := make(map[string]string)
|
||||
@@ -51,14 +52,20 @@ func Do(ctx *fasthttp.RequestCtx) error {
|
||||
verifiableKeys, err = verification.keys(verifiableKeys, uri)
|
||||
if err != nil {
|
||||
log.Error(log_code.ErrorAuthenticate, err)
|
||||
return createError(codes.Internal)
|
||||
return 0, createError(codes.Internal)
|
||||
}
|
||||
if verifiableKeys[permittedToCallInfo] == "0" {
|
||||
return createError(codes.PermissionDenied)
|
||||
return 0, createError(codes.PermissionDenied)
|
||||
} else {
|
||||
delete(verifiableKeys, permittedToCallInfo)
|
||||
}
|
||||
|
||||
applicationId, err := strconv.Atoi(verifiableKeys[utils.ApplicationIdHeader])
|
||||
if err != nil {
|
||||
log.Error(log_code.ErrorAuthenticate, err)
|
||||
return 0, createError(codes.Internal)
|
||||
}
|
||||
|
||||
for key, value := range verifiableKeys {
|
||||
if value != "" {
|
||||
ctx.Request.Header.Set(key, value)
|
||||
@@ -69,10 +76,11 @@ func Do(ctx *fasthttp.RequestCtx) error {
|
||||
if _, ok := routing.InnerAddressMap[string(path)]; ok {
|
||||
adminToken := ctx.Request.Header.Peek("x-auth-admin") //todo const key
|
||||
if token.Check(string(adminToken)) != nil {
|
||||
return createError(codes.PermissionDenied)
|
||||
return 0, createError(codes.PermissionDenied)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
|
||||
return int64(applicationId), nil
|
||||
}
|
||||
|
||||
func getPathWithoutPrefix(path []byte) []byte {
|
||||
|
||||
@@ -27,6 +27,18 @@ type (
|
||||
Journal rx.Config `schema:"Настройка логирования"`
|
||||
JournalingMethodsPatterns []string `schema:"Список методов для логирования,список строк вида: 'module/group/method'(* - для частичного совпадения). При обработке запроса, если вызываемый метод совпадает со строкой из списка, тела запроса и ответа записываются в лог"`
|
||||
Redis structure.RedisConfiguration `schema:"Настрока Redis" valid:"required~Required"`
|
||||
ApproveSetting []ApproveSetting
|
||||
}
|
||||
|
||||
ApproveSetting struct {
|
||||
ApplicationId int64
|
||||
Limits []LimitSetting
|
||||
}
|
||||
|
||||
LimitSetting struct {
|
||||
Pattern string
|
||||
MaxCount int
|
||||
Lifetime string
|
||||
}
|
||||
|
||||
ServerSetting struct {
|
||||
|
||||
+9
-1
@@ -3,13 +3,15 @@ package handler
|
||||
import (
|
||||
"github.com/valyala/fasthttp"
|
||||
"google.golang.org/grpc/codes"
|
||||
"isp-gate-service/approve"
|
||||
"isp-gate-service/authenticate"
|
||||
"isp-gate-service/proxy"
|
||||
"isp-gate-service/utils"
|
||||
)
|
||||
|
||||
func Complete(ctx *fasthttp.RequestCtx) {
|
||||
if err := authenticate.Do(ctx); err != nil {
|
||||
applicationId, err := authenticate.Do(ctx)
|
||||
if err != nil {
|
||||
status := codes.Unknown
|
||||
switch e := err.(type) {
|
||||
case authenticate.ErrorDescription:
|
||||
@@ -20,6 +22,12 @@ func Complete(ctx *fasthttp.RequestCtx) {
|
||||
}
|
||||
|
||||
path := string(ctx.Path())
|
||||
|
||||
if !approve.Complete(applicationId, path) {
|
||||
utils.SendError("approve error", codes.PermissionDenied, nil, ctx)
|
||||
return
|
||||
}
|
||||
|
||||
p := proxy.Find(path)
|
||||
if p != nil {
|
||||
p.ProxyRequest(ctx)
|
||||
|
||||
+2
-1
@@ -15,5 +15,6 @@ const (
|
||||
ErrorClientJournal = 608
|
||||
ErrorClientRedis = 609
|
||||
|
||||
ErrorAuthenticate = 612
|
||||
ErrorAuthenticate = 612
|
||||
FatalConfigApproveSetting = 613
|
||||
)
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
"github.com/integration-system/isp-lib/metric"
|
||||
"github.com/integration-system/isp-lib/structure"
|
||||
log "github.com/integration-system/isp-log"
|
||||
"isp-gate-service/approve"
|
||||
"isp-gate-service/conf"
|
||||
"isp-gate-service/journal"
|
||||
"isp-gate-service/log_code"
|
||||
@@ -59,6 +60,7 @@ func onRemoteConfigReceive(remoteConfig, oldRemoteConfig *conf.RemoteConfig) {
|
||||
journal.Client.ReceiveConfiguration(remoteConfig.Journal, localCfg.ModuleName)
|
||||
redis.Client.ReceiveConfiguration(remoteConfig.Redis)
|
||||
server.Http.Init(remoteConfig.ServerSetting.MaxRequestBodySizeBytes)
|
||||
approve.ReceiveConfiguration(remoteConfig.ApproveSetting)
|
||||
|
||||
service.JournalMethodsMatcher = service.NewCacheableMethodMatcher(remoteConfig.JournalingMethodsPatterns)
|
||||
|
||||
|
||||
Reference in new issue
Block a user