From 2c673a116990b91ad121bf38979f6483a36d4851 Mon Sep 17 00:00:00 2001 From: Qiu Jian Date: Wed, 24 Jul 2019 13:13:35 +0800 Subject: [PATCH] fix: server-logininfo rpc include login_key --- pkg/mcclient/modules/mod_servers.go | 65 ++++++++++++++++++----------- pkg/util/huawei/vpc.go | 2 +- 2 files changed, 42 insertions(+), 25 deletions(-) diff --git a/pkg/mcclient/modules/mod_servers.go b/pkg/mcclient/modules/mod_servers.go index 40fd2cef55..d4bec5050e 100644 --- a/pkg/mcclient/modules/mod_servers.go +++ b/pkg/mcclient/modules/mod_servers.go @@ -16,6 +16,7 @@ package modules import ( "fmt" + "strings" "yunion.io/x/jsonutils" "yunion.io/x/pkg/gotypes" @@ -30,38 +31,54 @@ type ServerManager struct { } func (this *ServerManager) GetLoginInfo(s *mcclient.ClientSession, id string, params jsonutils.JSONObject) (jsonutils.JSONObject, error) { - data, e := this.GetMetadata(s, id, nil) + data, e := this.Get(s, id, nil) if e != nil { return nil, e } + ret := jsonutils.NewDict() - loginKey, e := data.GetString("login_key") + + v, e := data.Get("metadata", "login_account") + if e == nil { + ret.Add(v, "username") + } + v, e = data.Get("metadata", "login_key_timestamp") + if e == nil { + ret.Add(v, "updated") + } + + loginKey, _ := data.GetString("metadata", "login_key") if e != nil { return nil, fmt.Errorf("No login key: %s", e) } - var privateKey string - if params != nil && !gotypes.IsNil(params) { - privateKey, _ = params.GetString("private_key") - } - - var passwd string - if len(privateKey) > 0 { - passwd, e = seclib2.DecryptBase64(privateKey, loginKey) - } else { - passwd, e = utils.DescryptAESBase64(id, loginKey) - } - if e != nil { - return nil, e - } - ret.Add(jsonutils.NewString(passwd), "password") - v, e := data.Get("login_account") - if e == nil { - ret.Add(v, "username") - } - v, e = data.Get("login_key_timestamp") - if e == nil { - ret.Add(v, "updated") + if len(loginKey) > 0 { + ret.Add(jsonutils.NewString(loginKey), "login_key") + var passwd string + keypairId, _ := data.GetString("keypair_id") + if len(keypairId) > 0 && !strings.EqualFold(keypairId, "none") { + keypair, e := data.Get("keypair") + if e == nil { + ret.Add(keypair, "keypair") + } + if params != nil && !gotypes.IsNil(params) { + privateKey, _ := params.GetString("private_key") + if len(privateKey) > 0 { + passwd, e = seclib2.DecryptBase64(privateKey, loginKey) + if e != nil { + return nil, e + } + } + } + } else { + passwd, e = utils.DescryptAESBase64(id, loginKey) + if e != nil { + return nil, e + } + } + if len(passwd) > 0 { + ret.Add(jsonutils.NewString(passwd), "password") + } } return ret, nil diff --git a/pkg/util/huawei/vpc.go b/pkg/util/huawei/vpc.go index 962a5f2bdd..b86133d297 100644 --- a/pkg/util/huawei/vpc.go +++ b/pkg/util/huawei/vpc.go @@ -17,8 +17,8 @@ package huawei import ( "strings" - "yunion.io/x/pkg/errors" "yunion.io/x/jsonutils" + "yunion.io/x/pkg/errors" api "yunion.io/x/onecloud/pkg/apis/compute" "yunion.io/x/onecloud/pkg/cloudprovider"