Merge pull request #2970 from zexi/feature/host-use-public-endpoint

auth: add session-endpoint-type to common options
This commit is contained in:
yunion-ci-robot
2019-09-18 17:20:23 +08:00
committed by GitHub
3 changed files with 36 additions and 7 deletions
+11
View File
@@ -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
+2
View File
@@ -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
}
+23 -7
View File
@@ -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 {