keypair.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 hcso
  15. import (
  16. "fmt"
  17. "strconv"
  18. "strings"
  19. "time"
  20. "github.com/aokoli/goutils"
  21. "golang.org/x/crypto/ssh"
  22. "yunion.io/x/jsonutils"
  23. )
  24. // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0020212676.html
  25. type SKeypair struct {
  26. Fingerprint string `json:"fingerprint"`
  27. Name string `json:"name"`
  28. PublicKey string `json:"public_key"`
  29. }
  30. func (self *SRegion) getFingerprint(publicKey string) (string, error) {
  31. pk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKey))
  32. if err != nil {
  33. return "", fmt.Errorf("publicKey error %s", err)
  34. }
  35. fingerprint := strings.Replace(ssh.FingerprintLegacyMD5(pk), ":", "", -1)
  36. return fingerprint, nil
  37. }
  38. // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0020212676.html
  39. func (self *SRegion) GetKeypairs() ([]SKeypair, int, error) {
  40. keypairs := make([]SKeypair, 0)
  41. err := doListAll(self.ecsClient.Keypairs.List, nil, &keypairs)
  42. return keypairs, len(keypairs), err
  43. }
  44. func (self *SRegion) lookUpKeypair(publicKey string) (string, error) {
  45. keypairs, _, err := self.GetKeypairs()
  46. if err != nil {
  47. return "", err
  48. }
  49. fingerprint, err := self.getFingerprint(publicKey)
  50. if err != nil {
  51. return "", err
  52. }
  53. for _, keypair := range keypairs {
  54. if keypair.Fingerprint == fingerprint {
  55. return keypair.Name, nil
  56. }
  57. }
  58. return "", fmt.Errorf("keypair not found %s", err)
  59. }
  60. // https://support.huaweicloud.com/api-ecs/zh-cn_topic_0020212678.html
  61. func (self *SRegion) ImportKeypair(name, publicKey string) (*SKeypair, error) {
  62. keypairObj := jsonutils.NewDict()
  63. keypairObj.Add(jsonutils.NewString(name), "name")
  64. keypairObj.Add(jsonutils.NewString(publicKey), "public_key")
  65. params := jsonutils.NewDict()
  66. params.Set("keypair", keypairObj)
  67. ret := SKeypair{}
  68. err := DoCreate(self.ecsClient.Keypairs.Create, params, &ret)
  69. return &ret, err
  70. }
  71. func (self *SRegion) importKeypair(publicKey string) (string, error) {
  72. prefix, e := goutils.RandomAlphabetic(6)
  73. if e != nil {
  74. return "", fmt.Errorf("publicKey error %s", e)
  75. }
  76. name := prefix + strconv.FormatInt(time.Now().Unix(), 10)
  77. if k, e := self.ImportKeypair(name, publicKey); e != nil {
  78. return "", fmt.Errorf("keypair import error %s", e)
  79. } else {
  80. return k.Name, nil
  81. }
  82. }
  83. func (self *SRegion) syncKeypair(publicKey string) (string, error) {
  84. name, e := self.lookUpKeypair(publicKey)
  85. if e == nil {
  86. return name, nil
  87. }
  88. return self.importKeypair(publicKey)
  89. }