Merge pull request #3593 from ioito/hotfix/qx-keypair-password

fix: 避免公有云绑定秘钥后解密密码为空
This commit is contained in:
yunion-ci-robot
2019-11-15 17:16:20 +08:00
committed by GitHub
4 changed files with 28 additions and 6 deletions
+1
View File
@@ -224,6 +224,7 @@ const (
VM_METADATA_CREATE_PARAMS = "create_params"
VM_METADATA_LOGIN_ACCOUNT = "login_account"
VM_METADATA_LOGIN_KEY = "login_key"
VM_METADATA_LAST_LOGIN_KEY = "last_login_key"
VM_METADATA_LOGIN_KEY_TIMESTAMP = "login_key_timestamp"
VM_METADATA_OS_ARCH = "os_arch"
VM_METADATA_OS_DISTRO = "os_distribution"
+2 -6
View File
@@ -36,7 +36,6 @@ import (
"yunion.io/x/onecloud/pkg/mcclient"
"yunion.io/x/onecloud/pkg/util/billing"
"yunion.io/x/onecloud/pkg/util/cloudinit"
"yunion.io/x/onecloud/pkg/util/seclib2"
)
type SManagedVirtualizedGuestDriver struct {
@@ -492,11 +491,8 @@ func (self *SManagedVirtualizedGuestDriver) RemoteDeployGuestForDeploy(ctx conte
return e
}
//解绑秘钥后需要重置密码
if deleteKeypair {
desc.Password = seclib2.RandomPassword2(12)
return iVM.DeployVM(ctx, desc.Name, desc.Account, desc.Password, desc.PublicKey, false, desc.Description)
}
//可以从秘钥解密旧密码
desc.Password = guest.GetOldPassword(ctx, task.GetUserCred())
return nil
}()
if err != nil {
+24
View File
@@ -23,6 +23,7 @@ import (
"strconv"
"strings"
"time"
"unicode"
"yunion.io/x/jsonutils"
"yunion.io/x/log"
@@ -521,12 +522,35 @@ func (self *SGuest) AllowPerformDeploy(ctx context.Context, userCred mcclient.To
return self.IsOwner(userCred) || db.IsAdminAllowPerform(userCred, self, "deploy")
}
func (self *SGuest) saveOldPassword(ctx context.Context, userCred mcclient.TokenCredential) {
loginKey := self.GetMetadata(api.VM_METADATA_LOGIN_KEY, userCred)
if len(loginKey) > 0 {
password, err := utils.DescryptAESBase64(self.Id, loginKey)
if err == nil && len(password) <= 30 {
for _, r := range password {
if !unicode.IsPrint(r) {
return
}
}
self.SetMetadata(ctx, api.VM_METADATA_LAST_LOGIN_KEY, loginKey, userCred)
}
}
}
func (self *SGuest) GetOldPassword(ctx context.Context, userCred mcclient.TokenCredential) string {
loginSecret := self.GetMetadata(api.VM_METADATA_LAST_LOGIN_KEY, userCred)
password, _ := utils.DescryptAESBase64(self.Id, loginSecret)
return password
}
func (self *SGuest) PerformDeploy(ctx context.Context, userCred mcclient.TokenCredential, query jsonutils.JSONObject, data jsonutils.JSONObject) (jsonutils.JSONObject, error) {
kwargs, ok := data.(*jsonutils.JSONDict)
if !ok {
return nil, fmt.Errorf("Parse query body error")
}
self.saveOldPassword(ctx, userCred)
if kwargs.Contains("__delete_keypair__") || kwargs.Contains("keypair") {
var kpId string
+1
View File
@@ -3957,6 +3957,7 @@ func (self *SGuest) SaveDeployInfo(ctx context.Context, userCred mcclient.TokenC
info["os_language"] = lang
}
self.SetAllMetadata(ctx, info, userCred)
self.saveOldPassword(ctx, userCred)
}
func (self *SGuest) isAllDisksReady() bool {