fix: invalid token request too frequent (#19488)

Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
Jian Qiu
2024-02-17 19:00:12 +08:00
committed by GitHub
parent 579cba8b6f
commit dffd27e2bc
2 changed files with 20 additions and 17 deletions

View File

@@ -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 {

View File

@@ -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)
}