-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #85 from wklken/ft_appcode_appsecret_check
verify app_code app_secret from paas only keep cache for 1min if invalid
- Loading branch information
Showing
12 changed files
with
282 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,3 +42,4 @@ init_test.sh | |
.Archive/ | ||
.md_configs.data | ||
.me_configs.data | ||
.codecc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,127 @@ | ||
/* | ||
* TencentBlueKing is pleased to support the open source community by making 蓝鲸智云-权限中心(BlueKing-IAM) available. | ||
* Copyright (C) 2017-2021 THL A29 Limited, a Tencent company. All rights reserved. | ||
* Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at http://opensource.org/licenses/MIT | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package cacheimpls | ||
package cacheimpls_test | ||
|
||
import ( | ||
"errors" | ||
"testing" | ||
"time" | ||
|
||
"github.com/TencentBlueKing/gopkg/cache" | ||
"github.com/TencentBlueKing/gopkg/cache/memory" | ||
"github.com/agiledragon/gomonkey/v2" | ||
"github.com/golang/mock/gomock" | ||
. "github.com/onsi/ginkgo/v2" | ||
gocache "github.com/patrickmn/go-cache" | ||
"github.com/stretchr/testify/assert" | ||
|
||
"iam/pkg/cacheimpls" | ||
"iam/pkg/component" | ||
mock2 "iam/pkg/component/mock" | ||
"iam/pkg/database/edao" | ||
"iam/pkg/database/edao/mock" | ||
) | ||
|
||
func TestAppCodeAppSecretCacheKey_Key(t *testing.T) { | ||
k := AppCodeAppSecretCacheKey{ | ||
AppCode: "hello", | ||
AppSecret: "123", | ||
} | ||
assert.Equal(t, "hello:123", k.Key()) | ||
} | ||
|
||
func TestVerifyAppCodeAppSecret(t *testing.T) { | ||
var ( | ||
expiration = 5 * time.Minute | ||
) | ||
|
||
// valid | ||
retrieveFunc := func(key cache.Key) (interface{}, error) { | ||
return true, nil | ||
} | ||
mockCache := memory.NewCache( | ||
"mockCache", false, retrieveFunc, expiration, nil) | ||
LocalAppCodeAppSecretCache = mockCache | ||
assert.True(t, VerifyAppCodeAppSecret("test", "123")) | ||
|
||
// error | ||
retrieveFunc = func(key cache.Key) (interface{}, error) { | ||
return false, errors.New("error here") | ||
} | ||
mockCache = memory.NewCache( | ||
"mockCache", false, retrieveFunc, expiration, nil) | ||
LocalAppCodeAppSecretCache = mockCache | ||
assert.False(t, VerifyAppCodeAppSecret("test", "123")) | ||
} | ||
var _ = Describe("LocalAppCodeSecret", func() { | ||
|
||
Describe("VerifyAppCodeAppSecret", func() { | ||
var ctl *gomock.Controller | ||
var mockManager *mock.MockAppSecretManager | ||
var patches *gomonkey.Patches | ||
BeforeEach(func() { | ||
cacheimpls.LocalAppCodeAppSecretCache = gocache.New(12*time.Hour, 5*time.Minute) | ||
|
||
ctl = gomock.NewController(GinkgoT()) | ||
mockManager = mock.NewMockAppSecretManager(ctl) | ||
|
||
}) | ||
|
||
AfterEach(func() { | ||
ctl.Finish() | ||
if patches != nil { | ||
patches.Reset() | ||
} | ||
}) | ||
|
||
It("hit", func() { | ||
cacheimpls.LocalAppCodeAppSecretCache.Set("app:123", true, 12*time.Hour) | ||
ok := cacheimpls.VerifyAppCodeAppSecret("app", "123") | ||
assert.True(GinkgoT(), ok) | ||
}) | ||
|
||
It("miss, get from database error", func() { | ||
mockManager.EXPECT().Exists(gomock.Any(), gomock.Any()).Return(false, errors.New("errror happend")).AnyTimes() | ||
patches = gomonkey.ApplyFunc(edao.NewAppSecretManager, | ||
func() edao.AppSecretManager { | ||
return mockManager | ||
}) | ||
|
||
ok := cacheimpls.VerifyAppCodeAppSecret("app", "123") | ||
assert.False(GinkgoT(), ok) | ||
}) | ||
|
||
It("miss, get from database valid", func() { | ||
mockManager.EXPECT().Exists(gomock.Any(), gomock.Any()).Return(false, nil).AnyTimes() | ||
patches = gomonkey.ApplyFunc(edao.NewAppSecretManager, | ||
func() edao.AppSecretManager { | ||
return mockManager | ||
}) | ||
|
||
ok := cacheimpls.VerifyAppCodeAppSecret("app", "123") | ||
assert.False(GinkgoT(), ok) | ||
|
||
}) | ||
|
||
It("miss, get from database invalid", func() { | ||
mockManager.EXPECT().Exists(gomock.Any(), gomock.Any()).Return(true, nil).AnyTimes() | ||
patches = gomonkey.ApplyFunc(edao.NewAppSecretManager, | ||
func() edao.AppSecretManager { | ||
return mockManager | ||
}) | ||
|
||
ok := cacheimpls.VerifyAppCodeAppSecret("app", "123") | ||
assert.True(GinkgoT(), ok) | ||
}) | ||
}) | ||
|
||
Describe("VerifyAppCodeAppSecretFromAuth", func() { | ||
var ctl *gomock.Controller | ||
var mockCli *mock2.MockAuthClient | ||
BeforeEach(func() { | ||
cacheimpls.LocalAuthAppAccessKeyCache = gocache.New(12*time.Hour, 5*time.Minute) | ||
|
||
ctl = gomock.NewController(GinkgoT()) | ||
mockCli = mock2.NewMockAuthClient(ctl) | ||
}) | ||
|
||
AfterEach(func() { | ||
ctl.Finish() | ||
}) | ||
|
||
It("hit", func() { | ||
cacheimpls.LocalAuthAppAccessKeyCache.Set("app:123", true, 12*time.Hour) | ||
ok := cacheimpls.VerifyAppCodeAppSecretFromAuth("app", "123") | ||
assert.True(GinkgoT(), ok) | ||
}) | ||
|
||
It("miss, get from bkauth error", func() { | ||
mockCli.EXPECT().Verify(gomock.Any(), gomock.Any()).Return(false, errors.New("errror happend")).AnyTimes() | ||
component.BkAuth = mockCli | ||
|
||
ok := cacheimpls.VerifyAppCodeAppSecretFromAuth("app", "123") | ||
assert.False(GinkgoT(), ok) | ||
}) | ||
|
||
It("miss, get from bkauth valid", func() { | ||
mockCli.EXPECT().Verify(gomock.Any(), gomock.Any()).Return(false, nil).AnyTimes() | ||
component.BkAuth = mockCli | ||
|
||
ok := cacheimpls.VerifyAppCodeAppSecretFromAuth("app", "123") | ||
assert.False(GinkgoT(), ok) | ||
|
||
}) | ||
|
||
It("miss, get from bkauth invalid", func() { | ||
mockCli.EXPECT().Verify(gomock.Any(), gomock.Any()).Return(true, nil).AnyTimes() | ||
component.BkAuth = mockCli | ||
|
||
ok := cacheimpls.VerifyAppCodeAppSecretFromAuth("app", "123") | ||
assert.True(GinkgoT(), ok) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.