feat(keystone): add GetDefaultAdminSession

This commit is contained in:
rainzm
2021-01-13 20:01:12 +08:00
parent afbb4a0470
commit c22f221898
3 changed files with 84 additions and 3 deletions
+7 -3
View File
@@ -30,7 +30,7 @@ var (
defaultClient *mcclient.Client = nil
)
func getDefaultClient() *mcclient.Client {
func GetDefaultClient() *mcclient.Client {
if defaultClient == nil {
defaultClient = mcclient.NewClient("", 300, options.Options.DebugClient, true, "", "")
refreshDefaultClientServiceCatalog()
@@ -53,7 +53,11 @@ func GetDefaultAdminCred() mcclient.TokenCredential {
return defaultAdminCred
}
func getDefaultAdminCred() mcclient.TokenCredential {
func GetDefaultAdminSSimpleToken() *mcclient.SSimpleToken {
return getDefaultAdminCred()
}
func getDefaultAdminCred() *mcclient.SSimpleToken {
token := mcclient.SSimpleToken{}
usr, _ := UserManager.FetchUserExtended("", api.SystemAdminUser, api.DEFAULT_DOMAIN_ID, "")
token.UserId = usr.Id
@@ -72,5 +76,5 @@ func getDefaultAdminCred() mcclient.TokenCredential {
}
func GetDefaultClientSession(ctx context.Context, token mcclient.TokenCredential, region, apiVersion string) *mcclient.ClientSession {
return getDefaultClient().NewSession(ctx, region, "", "", token, apiVersion)
return GetDefaultClient().NewSession(ctx, region, "", "", token, apiVersion)
}
+15
View File
@@ -0,0 +1,15 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util // import "yunion.io/x/onecloud/pkg/keystone/util"
+62
View File
@@ -0,0 +1,62 @@
// Copyright 2019 Yunion
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package util
import (
"context"
"time"
"yunion.io/x/pkg/utils"
api "yunion.io/x/onecloud/pkg/apis/identity"
"yunion.io/x/onecloud/pkg/keystone/models"
"yunion.io/x/onecloud/pkg/keystone/tokens"
"yunion.io/x/onecloud/pkg/mcclient"
)
var (
authToken *tokens.SAuthToken
simpleToken *mcclient.SSimpleToken
)
func getDefaultAdminCredWithToken() (mcclient.TokenCredential, error) {
if simpleToken == nil {
simpleToken = models.GetDefaultAdminSSimpleToken()
}
var err error
if now := time.Now(); authToken == nil || authToken.ExpiresAt.Sub(now) < time.Duration(3600) {
authTokenTmp := &tokens.SAuthToken{
UserId: simpleToken.GetUserId(),
Method: api.AUTH_METHOD_TOKEN,
ProjectId: simpleToken.GetProjectId(),
ExpiresAt: now.Add(24 * time.Hour),
AuditIds: []string{utils.GenRequestId(16)},
}
simpleToken.Token, err = authTokenTmp.EncodeFernetToken()
if err != nil {
return nil, err
}
authToken = authTokenTmp
}
return simpleToken, nil
}
func GetDefaulAdminSession(ctx context.Context, region, apiVersion string) (*mcclient.ClientSession, error) {
cred, err := getDefaultAdminCredWithToken()
if err != nil {
return nil, err
}
return models.GetDefaultClientSession(ctx, cred, region, apiVersion), nil
}