keypairs.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2023 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 volcengine
  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/pkg/errors"
  23. )
  24. type SKeypair struct {
  25. KeyPairFingerPrint string
  26. KeyPairName string
  27. }
  28. func (region *SRegion) GetKeypairs(finger string, name string, limit int, token string) ([]SKeypair, string, error) {
  29. if limit > 500 || limit <= 0 {
  30. limit = 500
  31. }
  32. params := make(map[string]string)
  33. params["MaxResults"] = fmt.Sprintf("%d", limit)
  34. if len(token) > 0 {
  35. params["NextToken"] = token
  36. }
  37. if len(finger) > 0 {
  38. params["FingerPrint"] = finger
  39. }
  40. if len(name) > 0 {
  41. params["KeyPairName"] = name
  42. }
  43. body, err := region.ecsRequest("DescribeKeyPairs", params)
  44. if err != nil {
  45. return nil, "", errors.Wrapf(err, "GetKeypairs fail")
  46. }
  47. keypairs := make([]SKeypair, 0)
  48. err = body.Unmarshal(&keypairs, "KeyPairs")
  49. if err != nil {
  50. return nil, "", errors.Wrapf(err, "Unmarshal keypair fail")
  51. }
  52. nextToken, _ := body.GetString("NextToken")
  53. return keypairs, nextToken, nil
  54. }
  55. func (region *SRegion) ImportKeypair(name string, pubKey string) (*SKeypair, error) {
  56. params := make(map[string]string)
  57. params["PublicKey"] = pubKey
  58. params["KeyPairName"] = name
  59. body, err := region.ecsRequest("ImportKeyPair", params)
  60. if err != nil {
  61. return nil, errors.Wrapf(err, "ImportKeypair fail")
  62. }
  63. keypair := SKeypair{}
  64. err = body.Unmarshal(&keypair)
  65. if err != nil {
  66. return nil, errors.Wrapf(err, "Unmarshal keypair fail")
  67. }
  68. return &keypair, nil
  69. }
  70. func (region *SRegion) AttachKeypair(instanceId string, name string) error {
  71. params := make(map[string]string)
  72. params["KeyPairName"] = name
  73. params["InstanceIds.1"] = instanceId
  74. _, err := region.ecsRequest("AttachKeyPair", params)
  75. if err != nil {
  76. return errors.Wrapf(err, "AttachKeyPair fail")
  77. }
  78. return nil
  79. }
  80. func (region *SRegion) DetachKeyPair(instanceId string, name string) error {
  81. params := make(map[string]string)
  82. params["KeyPairName"] = name
  83. params["InstanceIds.1"] = instanceId
  84. _, err := region.ecsRequest("DetachKeyPair", params)
  85. if err != nil {
  86. return errors.Wrapf(err, "DetachKeyPair fail")
  87. }
  88. return nil
  89. }
  90. func (region *SRegion) lookUpVolcEngineKeypair(publicKey string) (string, error) {
  91. pk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKey))
  92. if err != nil {
  93. return "", fmt.Errorf("publicKey error %s", err)
  94. }
  95. fingerprint := strings.Replace(ssh.FingerprintLegacyMD5(pk), ":", "", -1)
  96. ks, _, err := region.GetKeypairs(fingerprint, "*", 0, "")
  97. if len(ks) < 1 {
  98. return "", fmt.Errorf("keypair not found %s", err)
  99. } else {
  100. return ks[0].KeyPairName, nil
  101. }
  102. }
  103. func (region *SRegion) importVolcEngineKeypair(publicKey string) (string, error) {
  104. prefix, e := goutils.RandomAlphabetic(6)
  105. if e != nil {
  106. return "", fmt.Errorf("publicKey error %s", e)
  107. }
  108. name := prefix + strconv.FormatInt(time.Now().Unix(), 10)
  109. if k, e := region.ImportKeypair(name, publicKey); e != nil {
  110. return "", fmt.Errorf("keypair import error %s", e)
  111. } else {
  112. return k.KeyPairName, nil
  113. }
  114. }
  115. func (region *SRegion) syncKeypair(publicKey string) (string, error) {
  116. name, e := region.lookUpVolcEngineKeypair(publicKey)
  117. if e == nil {
  118. return name, nil
  119. }
  120. return region.importVolcEngineKeypair(publicKey)
  121. }