From c22f22189836c6605932073fb3fdc466596fd32e Mon Sep 17 00:00:00 2001 From: rainzm Date: Wed, 13 Jan 2021 19:45:35 +0800 Subject: [PATCH] feat(keystone): add GetDefaultAdminSession --- pkg/keystone/models/default_admin.go | 10 +++-- pkg/keystone/util/doc.go | 15 +++++++ pkg/keystone/util/session.go | 62 ++++++++++++++++++++++++++++ 3 files changed, 84 insertions(+), 3 deletions(-) create mode 100644 pkg/keystone/util/doc.go create mode 100644 pkg/keystone/util/session.go diff --git a/pkg/keystone/models/default_admin.go b/pkg/keystone/models/default_admin.go index f632122c25..deade48e11 100644 --- a/pkg/keystone/models/default_admin.go +++ b/pkg/keystone/models/default_admin.go @@ -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) } diff --git a/pkg/keystone/util/doc.go b/pkg/keystone/util/doc.go new file mode 100644 index 0000000000..b13afa6815 --- /dev/null +++ b/pkg/keystone/util/doc.go @@ -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" diff --git a/pkg/keystone/util/session.go b/pkg/keystone/util/session.go new file mode 100644 index 0000000000..c216df3b83 --- /dev/null +++ b/pkg/keystone/util/session.go @@ -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 +}