guest_logininfo.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package models
  15. import (
  16. "context"
  17. "yunion.io/x/jsonutils"
  18. "yunion.io/x/pkg/errors"
  19. "yunion.io/x/pkg/utils"
  20. api "yunion.io/x/onecloud/pkg/apis/compute"
  21. "yunion.io/x/onecloud/pkg/httperrors"
  22. "yunion.io/x/onecloud/pkg/mcclient"
  23. "yunion.io/x/onecloud/pkg/util/seclib2"
  24. )
  25. func (guest *SGuest) PerformLoginInfo(
  26. ctx context.Context,
  27. userCred mcclient.TokenCredential,
  28. query jsonutils.JSONObject,
  29. input api.ServerLoginInfoInput,
  30. ) (*api.ServerLoginInfoOutput, error) {
  31. metadata, err := guest.GetAllMetadata(ctx, userCred)
  32. if err != nil {
  33. return nil, errors.Wrap(err, "GetAllMetadata")
  34. }
  35. output := &api.ServerLoginInfoOutput{}
  36. output.Username = metadata["login_account"]
  37. output.Updated = metadata["login_key_timestamp"]
  38. output.LoginKey = metadata["login_key"]
  39. if len(output.LoginKey) > 0 {
  40. var passwd string
  41. keypair := guest.getKeypair()
  42. if keypair != nil {
  43. if len(input.PrivateKey) > 0 {
  44. passwd, err = seclib2.DecryptBase64(input.PrivateKey, output.LoginKey)
  45. if err != nil {
  46. return nil, errors.Wrap(err, "DecryptBase64")
  47. }
  48. } else {
  49. return nil, errors.Wrap(httperrors.ErrInputParameter, "empty private key")
  50. }
  51. } else {
  52. passwd, err = utils.DescryptAESBase64(guest.Id, output.LoginKey)
  53. if err != nil {
  54. return nil, errors.Wrap(err, "DescryptAESBase64")
  55. }
  56. }
  57. output.Password = passwd
  58. }
  59. return output, nil
  60. }
  61. func (host *SHost) PerformLoginInfo(
  62. ctx context.Context,
  63. userCred mcclient.TokenCredential,
  64. query jsonutils.JSONObject,
  65. input api.HostLoginInfoInput,
  66. ) (*api.HostLoginInfoOutput, error) {
  67. metadata, err := host.GetAllMetadata(ctx, userCred)
  68. if err != nil {
  69. return nil, errors.Wrap(err, "GetAllMetadata")
  70. }
  71. login_key := metadata["password"]
  72. // decrypt twice
  73. passwd, err := utils.DescryptAESBase64(host.Id, login_key)
  74. if err != nil {
  75. return nil, errors.Wrap(err, "DescryptAESBase64")
  76. }
  77. passwd, err = utils.DescryptAESBase64(host.Id, passwd)
  78. if err != nil {
  79. return nil, errors.Wrap(err, "DescryptAESBase64 twice")
  80. }
  81. ret := &api.HostLoginInfoOutput{}
  82. ret.Password = passwd
  83. ret.Username = metadata["username"]
  84. ret.Ip = metadata["ip"]
  85. return ret, nil
  86. }