From dffd27e2bc85ed447d82caf289f07e93a4e03567 Mon Sep 17 00:00:00 2001 From: Jian Qiu Date: Sat, 17 Feb 2024 19:00:12 +0800 Subject: [PATCH] fix: invalid token request too frequent (#19488) Co-authored-by: Qiu Jian --- pkg/mcclient/auth/auth.go | 22 +++++----------------- pkg/mcclient/mcclient.go | 15 +++++++++++++++ 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/pkg/mcclient/auth/auth.go b/pkg/mcclient/auth/auth.go index f6ba4cfd27..25aeb1cb71 100644 --- a/pkg/mcclient/auth/auth.go +++ b/pkg/mcclient/auth/auth.go @@ -24,13 +24,11 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" - "yunion.io/x/pkg/appctx" "yunion.io/x/pkg/errors" "yunion.io/x/pkg/util/cache" "yunion.io/x/pkg/util/httputils" "yunion.io/x/onecloud/pkg/apis/identity" - "yunion.io/x/onecloud/pkg/cloudcommon/consts" "yunion.io/x/onecloud/pkg/cloudcommon/syncman" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/mcclient" @@ -176,14 +174,11 @@ func newAuthManager(cli *mcclient.Client, info *AuthInfo) *authManager { } func (a *authManager) startRefreshRevokeTokens() { - ticker := time.NewTicker(5 * time.Minute) - for range ticker.C { - err := a.refreshRevokeTokens(context.Background()) - if err != nil { - log.Errorf("%s", err) - } + err := a.refreshRevokeTokens(context.Background()) + if err != nil { + log.Errorf("%s", err) } - ticker.Stop() + time.AfterFunc(5*time.Minute, a.startRefreshRevokeTokens) } func (a *authManager) refreshRevokeTokens(ctx context.Context) error { @@ -345,14 +340,7 @@ func (a *authManager) getAdminSession(ctx context.Context, region, zone, endpoin } func getContext(ctx context.Context) context.Context { - if ctx == nil { - ctx = context.Background() - } - srvType := consts.GetServiceType() - if len(srvType) > 0 && len(appctx.AppContextServiceName(ctx)) == 0 { - ctx = context.WithValue(ctx, appctx.APP_CONTEXT_KEY_APPNAME, srvType) - } - return ctx + return mcclient.FixContext(ctx) } func (a *authManager) getSession(ctx context.Context, token mcclient.TokenCredential, region, zone, endpointType string) *mcclient.ClientSession { diff --git a/pkg/mcclient/mcclient.go b/pkg/mcclient/mcclient.go index ca472cf5b5..5a150e848b 100644 --- a/pkg/mcclient/mcclient.go +++ b/pkg/mcclient/mcclient.go @@ -25,6 +25,7 @@ import ( "yunion.io/x/jsonutils" "yunion.io/x/log" + "yunion.io/x/pkg/appctx" "yunion.io/x/pkg/errors" "yunion.io/x/pkg/gotypes" "yunion.io/x/pkg/util/httputils" @@ -33,6 +34,7 @@ import ( "yunion.io/x/onecloud/pkg/apis" api "yunion.io/x/onecloud/pkg/apis/identity" "yunion.io/x/onecloud/pkg/appsrv" + "yunion.io/x/onecloud/pkg/cloudcommon/consts" "yunion.io/x/onecloud/pkg/httperrors" "yunion.io/x/onecloud/pkg/util/rbacutils" "yunion.io/x/onecloud/pkg/util/seclib2" @@ -153,11 +155,24 @@ func joinUrl(baseUrl, path string) string { return fmt.Sprintf("%s%s", baseUrl, path) } +func FixContext(ctx context.Context) context.Context { + if ctx == nil { + ctx = context.Background() + } + srvType := consts.GetServiceType() + if len(srvType) > 0 && len(appctx.AppContextServiceName(ctx)) == 0 { + ctx = context.WithValue(ctx, appctx.APP_CONTEXT_KEY_APPNAME, srvType) + } + return ctx +} + func (client *Client) rawRequest(ctx context.Context, endpoint string, token string, method httputils.THttpMethod, url string, header http.Header, body io.Reader) (*http.Response, error) { + ctx = FixContext(ctx) return httputils.Request(client.httpconn, ctx, method, joinUrl(endpoint, url), getDefaultHeader(header, token), body, client.debug) } func (client *Client) jsonRequest(ctx context.Context, endpoint string, token string, method httputils.THttpMethod, url string, header http.Header, body jsonutils.JSONObject) (http.Header, jsonutils.JSONObject, error) { + ctx = FixContext(ctx) return httputils.JSONRequest(client.httpconn, ctx, method, joinUrl(endpoint, url), getDefaultHeader(header, token), body, client.debug) }