keypair.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 huawei
  15. import (
  16. "fmt"
  17. "net/url"
  18. "strconv"
  19. "strings"
  20. "time"
  21. "github.com/aokoli/goutils"
  22. "golang.org/x/crypto/ssh"
  23. "yunion.io/x/pkg/errors"
  24. )
  25. type SKeypair struct {
  26. Keypair struct {
  27. Fingerprint string `json:"fingerprint"`
  28. Name string `json:"name"`
  29. PublicKey string `json:"public_key"`
  30. }
  31. }
  32. func (self *SRegion) getFingerprint(publicKey string) (string, error) {
  33. pk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKey))
  34. if err != nil {
  35. return "", fmt.Errorf("publicKey error %s", err)
  36. }
  37. fingerprint := strings.Replace(ssh.FingerprintLegacyMD5(pk), ":", "", -1)
  38. return fingerprint, nil
  39. }
  40. // https://console.huaweicloud.com/apiexplorer/#/openapi/ECS/doc?api=NovaListKeypairs
  41. func (self *SRegion) GetKeypairs() ([]SKeypair, error) {
  42. ret := []SKeypair{}
  43. query := url.Values{}
  44. resp, err := self.list(SERVICE_ECS_V2_1, "os-keypairs", query)
  45. if err != nil {
  46. return nil, errors.Wrapf(err, "list os-keypairs")
  47. }
  48. err = resp.Unmarshal(&ret, "keypairs")
  49. if err != nil {
  50. return nil, err
  51. }
  52. return ret, nil
  53. }
  54. func (self *SRegion) lookUpKeypair(publicKey string) (string, error) {
  55. keypairs, err := self.GetKeypairs()
  56. if err != nil {
  57. return "", err
  58. }
  59. fingerprint, err := self.getFingerprint(publicKey)
  60. if err != nil {
  61. return "", err
  62. }
  63. for _, keypair := range keypairs {
  64. if keypair.Keypair.Fingerprint == fingerprint {
  65. return keypair.Keypair.Name, nil
  66. }
  67. }
  68. return "", fmt.Errorf("keypair not found %s", err)
  69. }
  70. // https://console.huaweicloud.com/apiexplorer/#/openapi/ECS/doc?api=NovaCreateKeypair
  71. func (self *SRegion) ImportKeypair(name, publicKey string) (*SKeypair, error) {
  72. params := map[string]interface{}{
  73. "name": name,
  74. "public_key": publicKey,
  75. }
  76. resp, err := self.post(SERVICE_ECS_V2_1, "os-keypairs", map[string]interface{}{"keypair": params})
  77. if err != nil {
  78. return nil, errors.Wrapf(err, "create os-keypairs")
  79. }
  80. ret := &SKeypair{}
  81. err = resp.Unmarshal(ret)
  82. if err != nil {
  83. return nil, errors.Wrapf(err, "Unmarshal")
  84. }
  85. return ret, nil
  86. }
  87. func (self *SRegion) importKeypair(publicKey string) (string, error) {
  88. prefix, e := goutils.RandomAlphabetic(6)
  89. if e != nil {
  90. return "", fmt.Errorf("publicKey error %s", e)
  91. }
  92. name := prefix + strconv.FormatInt(time.Now().Unix(), 10)
  93. if k, e := self.ImportKeypair(name, publicKey); e != nil {
  94. return "", fmt.Errorf("keypair import error %s", e)
  95. } else {
  96. return k.Keypair.Name, nil
  97. }
  98. }
  99. func (self *SRegion) syncKeypair(publicKey string) (string, error) {
  100. name, e := self.lookUpKeypair(publicKey)
  101. if e == nil {
  102. return name, nil
  103. }
  104. return self.importKeypair(publicKey)
  105. }