Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: user-manager支持周期性地同步token到redis #3333

Merged
merged 4 commits into from
Aug 12, 2024
Merged
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
1 change: 1 addition & 0 deletions bcs-services/bcs-user-manager/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func parseConfig(op *options.UserManagerOptions) (*config.UserMgrConfig, error)
userMgrConfig.BcsAPI = &op.BcsAPI
userMgrConfig.Encrypt = op.Encrypt
userMgrConfig.Activity = op.Activity
userMgrConfig.EnableTokenSync = op.EnableTokenSync

config.Tke = op.TKE
secretID, err := encrypt.DesDecryptFromBase([]byte(config.Tke.SecretID))
Expand Down
18 changes: 18 additions & 0 deletions bcs-services/bcs-user-manager/app/user-manager/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,24 @@ func createBootstrapUsers(users []options.BootStrapUser) error {
// syncTokenToRedis will fetch user token from bcs_tokens, and store it to redis
func syncTokenToRedis() {
tokenStore := sqlstore.NewTokenStore(sqlstore.GCoreDB, config.GlobalCryptor)
ticker := time.NewTicker(10 * time.Minute)
defer ticker.Stop()

if !config.GetGlobalConfig().EnableTokenSync {
syncToken(tokenStore)
return
}

for {
syncToken(tokenStore)
// nolint
select {
case <-ticker.C:
}
}
}

func syncToken(tokenStore sqlstore.TokenStore) {
tokens := tokenStore.GetAllNotExpiredTokens()
blog.Infof("sync token to redis, total %d", len(tokens))
done := 0
Expand Down
11 changes: 6 additions & 5 deletions bcs-services/bcs-user-manager/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,12 @@ type UserMgrConfig struct {

VerifyClientTLS bool

DSN string
RedisDSN string
BootStrapUsers []options.BootStrapUser
TKE options.TKEOptions
PeerToken string
DSN string
RedisDSN string
EnableTokenSync bool
BootStrapUsers []options.BootStrapUser
TKE options.TKEOptions
PeerToken string

IAMConfig options.IAMConfig
EtcdConfig registry.CMDOptions
Expand Down
1 change: 1 addition & 0 deletions bcs-services/bcs-user-manager/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type UserManagerOptions struct {
JWTKeyConfig

VerifyClientTLS bool `json:"verify_client_tls" value:"false" usage:"verify client when brings up a tls server" mapstructure:"verify_client_tls"`
EnableTokenSync bool `json:"enable_token_sync" value:"" usage:"enable sync token to redis periodically"`
RedisDSN string `json:"redis_dsn" value:"" usage:"dsn for connect to redis"`
DSN string `json:"mysql_dsn" value:"" usage:"dsn for connect to mysql"`
BootStrapUsers []BootStrapUser `json:"bootstrap_users"`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"verify_client_tls": false,
"mysql_dsn": "${coreDatabaseDsn}",
"redis_dsn": "${redisDsn}",
"enable_token_sync": ${enableTokenSync},
"bootstrap_users": [
{
"name": "${adminUser}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ bcsTokenNotifyTitle="${bcsTokenNotifyTitle:-TKEx(蓝鲸容器平台) API 密钥
bcsTokenNotifyContent="${bcsTokenNotifyContent:-你好,{{ .Username \}\}:<br>您的 API 密钥过期时间为: {{ .ExpiredAt \}\},如有需要请前往 API 密钥页面及时续期。}" \
bcsTokenNotifyESBEmailPath="${bcsTokenNotifyESBEmailPath:-/api/c/compapi/v2/cmsi/send_mail/}" \
bcsTokenNotifyESBRtxPath="${bcsTokenNotifyESBRtxPath:-/api/c/compapi/v2/cmsi/send_rtx/}" \
enableTokenSync="${enableTokenSync:-false}" \
envsubst | tee ${module}.json
fi

Expand Down
Loading