helper.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 helper
  15. import (
  16. "context"
  17. "net"
  18. "strconv"
  19. "time"
  20. "golang.org/x/crypto/ssh"
  21. "yunion.io/x/jsonutils"
  22. "yunion.io/x/pkg/errors"
  23. "yunion.io/x/onecloud/pkg/httperrors"
  24. "yunion.io/x/onecloud/pkg/mcclient/auth"
  25. "yunion.io/x/onecloud/pkg/mcclient/modules/compute"
  26. o "yunion.io/x/onecloud/pkg/webconsole/options"
  27. )
  28. func GetValidPrivateKey(host string, port int, username string, projectId string) (string, error) {
  29. errs := []error{}
  30. ctx := context.Background()
  31. admin := auth.GetAdminSession(ctx, o.Options.Region)
  32. for _, gf := range []func() (jsonutils.JSONObject, error){
  33. func() (jsonutils.JSONObject, error) {
  34. if projectId == "" {
  35. return nil, errors.Error("project_id is empty")
  36. }
  37. key, err := compute.Sshkeypairs.GetById(admin, projectId, jsonutils.Marshal(map[string]bool{"admin": true}))
  38. if err != nil {
  39. return nil, errors.Wrapf(err, "Sshkeypairs.GetById(%s)", projectId)
  40. }
  41. return key, nil
  42. },
  43. func() (jsonutils.JSONObject, error) {
  44. query := jsonutils.NewDict()
  45. query.Set("admin", jsonutils.JSONTrue)
  46. ret, err := compute.Sshkeypairs.List(admin, query)
  47. if err != nil {
  48. return nil, errors.Wrap(err, "modules.Sshkeypairs.List")
  49. }
  50. if len(ret.Data) == 0 {
  51. return nil, errors.Wrap(httperrors.ErrNotFound, "Not found admin sshkey")
  52. }
  53. keys := ret.Data[0]
  54. return keys, nil
  55. },
  56. } {
  57. key, err := gf()
  58. if err != nil {
  59. errs = append(errs, err)
  60. continue
  61. }
  62. privKey, err := key.GetString("private_key")
  63. if err != nil {
  64. errs = append(errs, errors.Wrapf(err, "get private_key"))
  65. continue
  66. }
  67. signer, err := ssh.ParsePrivateKey([]byte(privKey))
  68. if err != nil {
  69. errs = append(errs, errors.Wrapf(err, "ParsePrivateKey"))
  70. continue
  71. }
  72. config := &ssh.ClientConfig{
  73. Timeout: time.Second,
  74. User: username,
  75. HostKeyCallback: ssh.InsecureIgnoreHostKey(),
  76. Auth: []ssh.AuthMethod{
  77. ssh.PublicKeys(signer),
  78. },
  79. }
  80. addr := net.JoinHostPort(host, strconv.Itoa(port))
  81. client, err := ssh.Dial("tcp", addr, config)
  82. if err != nil {
  83. errs = append(errs, errors.Wrapf(err, "dial %s by %s", addr, username))
  84. continue
  85. }
  86. defer client.Close()
  87. return privKey, nil
  88. }
  89. return "", errors.NewAggregate(errs)
  90. }