mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-01 15:07:17 +08:00
修正:simpletoken没有servicecatalog,导致基于simpletoken的session无法获得service
URL
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
REPO_PREFIX := yunion.io/x/onecloud
|
||||
VENDOR_PATH := $(REPO_PREFIX)/vendor
|
||||
VERSION_PKG := $(VENDOR_PATH)/yunion.io/x/pkg/util/version
|
||||
ROOT_DIR := $(shell readlink -f `pwd`)
|
||||
ROOT_DIR := $(shell pwd -P)
|
||||
BUILD_DIR := $(ROOT_DIR)/_output
|
||||
BIN_DIR := $(BUILD_DIR)/bin
|
||||
BUILD_SCRIPT := $(ROOT_DIR)/build/build.sh
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package mcclient
|
||||
|
||||
type IServiceCatalog interface {
|
||||
GetServiceURL(service, region, zone, endpointType string) (string, error)
|
||||
GetServiceURLs(service, region, zone, endpointType string) ([]string, error)
|
||||
}
|
||||
+23
-28
@@ -10,36 +10,16 @@ import (
|
||||
|
||||
"yunion.io/x/jsonutils"
|
||||
"yunion.io/x/onecloud/pkg/util/httputils"
|
||||
"yunion.io/x/log"
|
||||
)
|
||||
|
||||
/*const (
|
||||
USER_AGENT = "yunioncloud-go/201708"
|
||||
)
|
||||
|
||||
var (
|
||||
red = color.New(color.FgRed, color.Bold).PrintlnFunc()
|
||||
green = color.New(color.FgGreen, color.Bold).PrintlnFunc()
|
||||
yellow = color.New(color.FgYellow, color.Bold).PrintlnFunc()
|
||||
cyan = color.New(color.FgHiCyan, color.Bold).PrintlnFunc()
|
||||
)*/
|
||||
|
||||
/*type JSONClientError struct {
|
||||
Code int
|
||||
Class string
|
||||
Details string
|
||||
}
|
||||
|
||||
func (e *JSONClientError) Error() string {
|
||||
return fmt.Sprintf("JSONClientError: %s %d %s", e.Details, e.Code, e.Class)
|
||||
}*/
|
||||
|
||||
type Client struct {
|
||||
authUrl string
|
||||
timeout int
|
||||
debug bool
|
||||
|
||||
httpconn *http.Client
|
||||
// serviceCatalog *KeystoneServiceCatalog
|
||||
serviceCatalog IServiceCatalog
|
||||
}
|
||||
|
||||
func NewClient(authUrl string, timeout int, debug bool, insecure bool) *Client {
|
||||
@@ -222,7 +202,7 @@ func (this *Client) _authV3(domainName, uname, passwd, projectId, projectName, t
|
||||
if len(tokenId) == 0 {
|
||||
return nil, fmt.Errorf("No X-Subject-Token in header")
|
||||
}
|
||||
ret, err := UnmarshalV3Token(rbody, tokenId)
|
||||
ret, err := this.unmarshalV3Token(rbody, tokenId)
|
||||
return ret, err
|
||||
}
|
||||
|
||||
@@ -245,7 +225,7 @@ func (this *Client) _authV2(uname, passwd, tenantId, tenantName, token string) (
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return UnmarshalV2Token(rbody)
|
||||
return this.unmarshalV2Token(rbody)
|
||||
}
|
||||
|
||||
func (this *Client) Authenticate(uname, passwd, domainName, tenantName string) (TokenCredential, error) {
|
||||
@@ -255,16 +235,21 @@ func (this *Client) Authenticate(uname, passwd, domainName, tenantName string) (
|
||||
return this._authV2(uname, passwd, "", tenantName, "")
|
||||
}
|
||||
|
||||
func UnmarshalV3Token(rbody jsonutils.JSONObject, tokenId string) (cred TokenCredential, err error) {
|
||||
func (this *Client) unmarshalV3Token(rbody jsonutils.JSONObject, tokenId string) (cred TokenCredential, err error) {
|
||||
cred = &TokenCredentialV3{Id: tokenId}
|
||||
err = rbody.Unmarshal(cred)
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Invalid response when unmarshal V3 Token: %v", err)
|
||||
}
|
||||
cata := cred.GetServiceCatalog()
|
||||
if cata == nil {
|
||||
log.Fatalf("No srvice catalog avaiable")
|
||||
}
|
||||
this.serviceCatalog = cata
|
||||
return
|
||||
}
|
||||
|
||||
func UnmarshalV2Token(rbody jsonutils.JSONObject) (cred TokenCredential, err error) {
|
||||
func (this *Client) unmarshalV2Token(rbody jsonutils.JSONObject) (cred TokenCredential, err error) {
|
||||
access, err := rbody.Get("access")
|
||||
if err == nil {
|
||||
cred = &TokenCredentialV2{}
|
||||
@@ -272,6 +257,11 @@ func UnmarshalV2Token(rbody jsonutils.JSONObject) (cred TokenCredential, err err
|
||||
if err != nil {
|
||||
err = fmt.Errorf("Invalid response when unmarshal V2 Token: %s", err)
|
||||
}
|
||||
cata := cred.GetServiceCatalog()
|
||||
if cata == nil {
|
||||
log.Fatalf("No srvice catalog avaiable")
|
||||
}
|
||||
this.serviceCatalog = cata
|
||||
return
|
||||
}
|
||||
err = fmt.Errorf("Invalid response: no access object")
|
||||
@@ -286,7 +276,7 @@ func (this *Client) verifyV3(adminToken, token string) (TokenCredential, error)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return UnmarshalV3Token(rbody, token)
|
||||
return this.unmarshalV3Token(rbody, token)
|
||||
}
|
||||
|
||||
func (this *Client) verifyV2(adminToken, token string) (TokenCredential, error) {
|
||||
@@ -297,7 +287,7 @@ func (this *Client) verifyV2(adminToken, token string) (TokenCredential, error)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return UnmarshalV2Token(rbody)
|
||||
return this.unmarshalV2Token(rbody)
|
||||
}
|
||||
|
||||
func (this *Client) Verify(adminToken, token string) (cred TokenCredential, err error) {
|
||||
@@ -320,6 +310,11 @@ func (this *Client) SetProject(tenantId, tenantName string, token TokenCredentia
|
||||
}
|
||||
|
||||
func (this *Client) NewSession(region, zone, endpointType string, token TokenCredential, apiVersion string) *ClientSession {
|
||||
cata := token.GetServiceCatalog()
|
||||
if cata == nil {
|
||||
log.Fatalf("Missing service catalog in token")
|
||||
}
|
||||
this.serviceCatalog = cata
|
||||
return &ClientSession{client: this, region: region, zone: zone,
|
||||
endpointType: endpointType, token: token,
|
||||
apiVersion: apiVersion,
|
||||
|
||||
+20
-1
@@ -75,7 +75,26 @@ func (this *ClientSession) GetServiceURL(service, endpointType string) (string,
|
||||
if len(this.apiVersion) > 0 && this.apiVersion != DEFAULT_API_VERSION {
|
||||
service = fmt.Sprintf("%s_%s", service, this.apiVersion)
|
||||
}
|
||||
return this.token.GetServiceURL(service, this.region, this.zone, endpointType)
|
||||
url, err := this.token.GetServiceURL(service, this.region, this.zone, endpointType)
|
||||
if err != nil {
|
||||
url, err = this.client.serviceCatalog.GetServiceURL(service, this.region, this.zone, endpointType)
|
||||
}
|
||||
return url, err
|
||||
}
|
||||
|
||||
func (this *ClientSession) GetServiceURLs(service, endpointType string) ([]string, error) {
|
||||
if len(this.endpointType) > 0 {
|
||||
// session specific endpoint type should override the input endpointType, which is supplied by manager
|
||||
endpointType = this.endpointType
|
||||
}
|
||||
if len(this.apiVersion) > 0 && this.apiVersion != DEFAULT_API_VERSION {
|
||||
service = fmt.Sprintf("%s_%s", service, this.apiVersion)
|
||||
}
|
||||
urls, err := this.token.GetServiceURLs(service, this.region, this.zone, endpointType)
|
||||
if err != nil {
|
||||
urls, err = this.client.serviceCatalog.GetServiceURLs(service, this.region, this.zone, endpointType)
|
||||
}
|
||||
return urls, err
|
||||
}
|
||||
|
||||
func (this *ClientSession) getBaseUrl(service, endpointType string) (string, error) {
|
||||
|
||||
@@ -23,6 +23,8 @@ type Endpoint struct {
|
||||
type TokenCredential interface {
|
||||
gotypes.ISerializable
|
||||
|
||||
IServiceCatalog
|
||||
|
||||
GetTokenString() string
|
||||
GetDomainId() string
|
||||
GetDomainName() string
|
||||
@@ -39,7 +41,9 @@ type TokenCredential interface {
|
||||
IsAdmin() bool
|
||||
IsSystemAdmin() bool
|
||||
GetRegions() []string
|
||||
GetServiceURL(service, region, zone, endpointType string) (string, error)
|
||||
|
||||
GetServiceCatalog() IServiceCatalog
|
||||
|
||||
GetInternalServices(region string) []string
|
||||
GetExternalServices(region string) []ExternalService
|
||||
GetEndpoints(region string, endpointType string) []Endpoint
|
||||
|
||||
+18
-2
@@ -133,7 +133,11 @@ func (this *TokenCredentialV2) IsSystemAdmin() bool {
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV2) GetServiceURL(service, region, zone, endpointType string) (string, error) {
|
||||
return this.ServiceCatalog.getServiceURL(service, region, zone, endpointType)
|
||||
return this.ServiceCatalog.GetServiceURL(service, region, zone, endpointType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV2) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) {
|
||||
return this.ServiceCatalog.GetServiceURLs(service, region, zone, endpointType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV2) GetInternalServices(region string) []string {
|
||||
@@ -148,6 +152,10 @@ func (this *TokenCredentialV2) GetEndpoints(region string, endpointType string)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV2) GetServiceCatalog() IServiceCatalog {
|
||||
return this.ServiceCatalog
|
||||
}
|
||||
|
||||
func stringArrayContains(arr []string, needle string) bool {
|
||||
for i := 0; i < len(arr); i++ {
|
||||
if arr[i] == needle {
|
||||
@@ -211,7 +219,7 @@ func (catalog KeystoneServiceCatalogV2) getServiceEndpoint(service, region, zone
|
||||
return selected, fmt.Errorf("No such service %s", service)
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV2) getServiceURL(service, region, zone, endpointType string) (string, error) {
|
||||
func (catalog KeystoneServiceCatalogV2) GetServiceURL(service, region, zone, endpointType string) (string, error) {
|
||||
ep, err := catalog.getServiceEndpoint(service, region, zone)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -219,6 +227,14 @@ func (catalog KeystoneServiceCatalogV2) getServiceURL(service, region, zone, end
|
||||
return ep.getURL(endpointType), nil
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV2) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) {
|
||||
url, err := catalog.GetServiceURL(service, region, zone, endpointType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []string{url}, nil
|
||||
}
|
||||
|
||||
func (ep KeystoneEndpointV2) getURL(epType string) string {
|
||||
switch epType {
|
||||
case "publicURL":
|
||||
|
||||
+18
-2
@@ -141,7 +141,11 @@ func (this *TokenCredentialV3) GetRegions() []string {
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetServiceURL(service, region, zone, endpointType string) (string, error) {
|
||||
return this.Token.Catalog.getServiceURL(service, region, zone, endpointType)
|
||||
return this.Token.Catalog.GetServiceURL(service, region, zone, endpointType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) {
|
||||
return this.Token.Catalog.GetServiceURLs(service, region, zone, endpointType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetInternalServices(region string) []string {
|
||||
@@ -156,6 +160,10 @@ func (this *TokenCredentialV3) GetEndpoints(region string, endpointType string)
|
||||
return this.Token.Catalog.getEndpoints(region, endpointType)
|
||||
}
|
||||
|
||||
func (this *TokenCredentialV3) GetServiceCatalog() IServiceCatalog {
|
||||
return this.Token.Catalog
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV3) getInternalServices(region string) []string {
|
||||
services := make([]string, 0)
|
||||
for i := 0; i < len(catalog); i++ {
|
||||
@@ -242,7 +250,15 @@ func Id2RegionZone(id string) (string, string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV3) getServiceURL(service, region, zone, endpointType string) (string, error) {
|
||||
func (catalog KeystoneServiceCatalogV3) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) {
|
||||
url, err := catalog.GetServiceURL(service, region, zone, endpointType)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []string{url}, nil
|
||||
}
|
||||
|
||||
func (catalog KeystoneServiceCatalogV3) GetServiceURL(service, region, zone, endpointType string) (string, error) {
|
||||
if endpointType == "" {
|
||||
endpointType = "internalURL"
|
||||
}
|
||||
|
||||
@@ -96,6 +96,10 @@ func (self *SSimpleToken) GetServiceURL(service, region, zone, endpointType stri
|
||||
return "", fmt.Errorf("Not available")
|
||||
}
|
||||
|
||||
func (self *SSimpleToken) GetServiceURLs(service, region, zone, endpointType string) ([]string, error) {
|
||||
return nil, fmt.Errorf("Not available")
|
||||
}
|
||||
|
||||
func (self *SSimpleToken) GetInternalServices(region string) []string {
|
||||
return nil
|
||||
}
|
||||
@@ -108,6 +112,10 @@ func (this *SSimpleToken) GetEndpoints(region string, endpointType string) []End
|
||||
return nil
|
||||
}
|
||||
|
||||
func (this *SSimpleToken) GetServiceCatalog() IServiceCatalog {
|
||||
return nil
|
||||
}
|
||||
|
||||
func SimplifyToken(token TokenCredential) TokenCredential {
|
||||
simToken, ok := token.(*SSimpleToken)
|
||||
if ok {
|
||||
|
||||
Reference in New Issue
Block a user