From 3781ccb427bed61f876af1fbc2da4a1147c3e965 Mon Sep 17 00:00:00 2001 From: Zexi Li Date: Wed, 18 Sep 2019 11:42:35 +0800 Subject: [PATCH] auth: add session-endpoint-type to common options --- pkg/cloudcommon/app/auth.go | 11 +++++++++++ pkg/cloudcommon/options/options.go | 2 ++ pkg/mcclient/auth/auth.go | 30 +++++++++++++++++++++++------- 3 files changed, 36 insertions(+), 7 deletions(-) diff --git a/pkg/cloudcommon/app/auth.go b/pkg/cloudcommon/app/auth.go index c219e8183f..b957f2f092 100644 --- a/pkg/cloudcommon/app/auth.go +++ b/pkg/cloudcommon/app/auth.go @@ -20,6 +20,9 @@ import ( "os" "time" + "yunion.io/x/log" + "yunion.io/x/pkg/utils" + "yunion.io/x/onecloud/pkg/cloudcommon/consts" "yunion.io/x/onecloud/pkg/cloudcommon/notifyclient" common_options "yunion.io/x/onecloud/pkg/cloudcommon/options" @@ -60,6 +63,14 @@ func InitAuth(options *common_options.CommonOptions, authComplete auth.AuthCompl // debug := options.LogLevel == "debug" + if options.SessionEndpointType != "" { + if !utils.IsInStringArray(options.SessionEndpointType, + []string{auth.PublicEndpointType, auth.InternalEndpointType}) { + log.Fatalf("Invalid session endpoint type %s", options.SessionEndpointType) + } + auth.SetEndpointType(options.SessionEndpointType) + } + auth.Init(a, options.DebugClient, true, options.SslCertfile, options.SslKeyfile) // , authComplete) users := options.NotifyAdminUsers diff --git a/pkg/cloudcommon/options/options.go b/pkg/cloudcommon/options/options.go index d75db514b7..af10e3a036 100644 --- a/pkg/cloudcommon/options/options.go +++ b/pkg/cloudcommon/options/options.go @@ -83,6 +83,8 @@ type CommonOptions struct { TenantCacheExpireSeconds int `help:"expire seconds of cached tenant/domain info. defailt 15 minutes" default:"900"` + SessionEndpointType string `help:"Client session end point type"` + BaseOptions } diff --git a/pkg/mcclient/auth/auth.go b/pkg/mcclient/auth/auth.go index 4ad36c7154..7019b84401 100644 --- a/pkg/mcclient/auth/auth.go +++ b/pkg/mcclient/auth/auth.go @@ -17,21 +17,27 @@ package auth import ( "context" "fmt" + "net/http" "time" "yunion.io/x/jsonutils" "yunion.io/x/log" "yunion.io/x/pkg/util/cache" - "net/http" "yunion.io/x/onecloud/pkg/mcclient" ) var ( - manager *authManager - defaultTimeout int = 600 // maybe time.Duration better - defaultCacheCount int64 = 100000 - initCh chan bool = make(chan bool) + manager *authManager + defaultTimeout int = 600 // maybe time.Duration better + defaultCacheCount int64 = 100000 + initCh chan bool = make(chan bool) + globalEndpointType string +) + +const ( + PublicEndpointType string = "public" + InternalEndpointType string = "internal" ) type AuthInfo struct { @@ -49,6 +55,10 @@ func SetTimeout(t time.Duration) { defaultTimeout = int(t) } +func SetEndpointType(epType string) { + globalEndpointType = epType +} + func NewV2AuthInfo(authUrl, user, passwd, tenant string) *AuthInfo { return NewAuthInfo(authUrl, "", user, passwd, tenant, "") } @@ -276,6 +286,9 @@ func AdminSession(ctx context.Context, region, zone, endpointType, apiVersion st if cli == nil { return nil } + if endpointType == "" && globalEndpointType != "" { + endpointType = globalEndpointType + } return cli.NewSession(ctx, region, zone, endpointType, AdminCredential(), apiVersion) } @@ -331,15 +344,18 @@ func GetAdminSessionWithPublic(ctx context.Context, region string, } func GetSession(ctx context.Context, token mcclient.TokenCredential, region string, apiVersion string) *mcclient.ClientSession { + if len(globalEndpointType) != 0 { + return getSessionByType(ctx, token, region, apiVersion, globalEndpointType) + } return GetSessionWithInternal(ctx, token, region, apiVersion) } func GetSessionWithInternal(ctx context.Context, token mcclient.TokenCredential, region string, apiVersion string) *mcclient.ClientSession { - return getSessionByType(ctx, token, region, apiVersion, "internal") + return getSessionByType(ctx, token, region, apiVersion, InternalEndpointType) } func GetSessionWithPublic(ctx context.Context, token mcclient.TokenCredential, region string, apiVersion string) *mcclient.ClientSession { - return getSessionByType(ctx, token, region, apiVersion, "public") + return getSessionByType(ctx, token, region, apiVersion, PublicEndpointType) } func getSessionByType(ctx context.Context, token mcclient.TokenCredential, region string, apiVersion string, epType string) *mcclient.ClientSession {