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