yunion.io/x/cloudmux@v0.3.10-0-alpha.1/pkg/multicloud/aliyun/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 aliyun
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"strconv"
    21  	"strings"
    22  	"time"
    23  
    24  	"github.com/aokoli/goutils"
    25  	"golang.org/x/crypto/ssh"
    26  
    27  	"yunion.io/x/log"
    28  )
    29  
    30  type SKeypair struct {
    31  	KeyPairFingerPrint string
    32  	KeyPairName        string
    33  }
    34  
    35  func (self *SRegion) GetKeypairs(finger string, name string, offset int, limit int) ([]SKeypair, int, error) {
    36  	if limit > 50 || limit <= 0 {
    37  		limit = 50
    38  	}
    39  	params := make(map[string]string)
    40  	params["RegionId"] = self.RegionId
    41  	params["PageSize"] = fmt.Sprintf("%d", limit)
    42  	params["PageNumber"] = fmt.Sprintf("%d", (offset/limit)+1)
    43  	if len(finger) > 0 {
    44  		params["KeyPairFingerPrint"] = finger
    45  	}
    46  	if len(name) > 0 {
    47  		params["KeyPairName"] = name
    48  	}
    49  
    50  	body, err := self.ecsRequest("DescribeKeyPairs", params)
    51  	if err != nil {
    52  		log.Errorf("GetKeypairs fail %s", err)
    53  		return nil, 0, err
    54  	}
    55  
    56  	keypairs := make([]SKeypair, 0)
    57  	err = body.Unmarshal(&keypairs, "KeyPairs", "KeyPair")
    58  	if err != nil {
    59  		log.Errorf("Unmarshal keypair fail %s", err)
    60  		return nil, 0, err
    61  	}
    62  	total, _ := body.Int("TotalCount")
    63  	return keypairs, int(total), nil
    64  }
    65  
    66  func (self *SRegion) ImportKeypair(name string, pubKey string) (*SKeypair, error) {
    67  	params := make(map[string]string)
    68  	params["RegionId"] = self.RegionId
    69  	params["PublicKeyBody"] = pubKey
    70  	params["KeyPairName"] = name
    71  
    72  	body, err := self.ecsRequest("ImportKeyPair", params)
    73  	if err != nil {
    74  		log.Errorf("ImportKeypair fail %s", err)
    75  		return nil, err
    76  	}
    77  
    78  	log.Debugf("%s", body)
    79  	keypair := SKeypair{}
    80  	err = body.Unmarshal(&keypair)
    81  	if err != nil {
    82  		log.Errorf("Unmarshall keypair fail %s", err)
    83  		return nil, err
    84  	}
    85  	return &keypair, nil
    86  }
    87  
    88  func (self *SRegion) AttachKeypair(instanceId string, name string) error {
    89  	params := make(map[string]string)
    90  	params["RegionId"] = self.RegionId
    91  	params["KeyPairName"] = name
    92  	instances, _ := json.Marshal(&[...]string{instanceId})
    93  	params["InstanceIds"] = string(instances)
    94  	_, err := self.ecsRequest("AttachKeyPair", params)
    95  	if err != nil {
    96  		log.Errorf("AttachKeyPair fail %s", err)
    97  		return err
    98  	}
    99  
   100  	return nil
   101  }
   102  
   103  func (self *SRegion) DetachKeyPair(instanceId string, name string) error {
   104  	params := make(map[string]string)
   105  	params["RegionId"] = self.RegionId
   106  	params["KeyPairName"] = name
   107  	instances, _ := json.Marshal(&[...]string{instanceId})
   108  	params["InstanceIds"] = string(instances)
   109  	_, err := self.ecsRequest("DetachKeyPair", params)
   110  	if err != nil {
   111  		log.Errorf("DetachKeyPair fail %s", err)
   112  		return err
   113  	}
   114  
   115  	return nil
   116  }
   117  
   118  func (self *SRegion) lookUpAliyunKeypair(publicKey string) (string, error) {
   119  	pk, _, _, _, err := ssh.ParseAuthorizedKey([]byte(publicKey))
   120  	if err != nil {
   121  		return "", fmt.Errorf("publicKey error %s", err)
   122  	}
   123  
   124  	fingerprint := strings.Replace(ssh.FingerprintLegacyMD5(pk), ":", "", -1)
   125  	ks, total, err := self.GetKeypairs(fingerprint, "*", 0, 1)
   126  	if total < 1 {
   127  		return "", fmt.Errorf("keypair not found %s", err)
   128  	} else {
   129  		return ks[0].KeyPairName, nil
   130  	}
   131  }
   132  
   133  func (self *SRegion) importAliyunKeypair(publicKey string) (string, error) {
   134  	prefix, e := goutils.RandomAlphabetic(6)
   135  	if e != nil {
   136  		return "", fmt.Errorf("publicKey error %s", e)
   137  	}
   138  
   139  	name := prefix + strconv.FormatInt(time.Now().Unix(), 10)
   140  	if k, e := self.ImportKeypair(name, publicKey); e != nil {
   141  		return "", fmt.Errorf("keypair import error %s", e)
   142  	} else {
   143  		return k.KeyPairName, nil
   144  	}
   145  }
   146  
   147  func (self *SRegion) syncKeypair(publicKey string) (string, error) {
   148  	name, e := self.lookUpAliyunKeypair(publicKey)
   149  	if e == nil {
   150  		return name, nil
   151  	}
   152  	return self.importAliyunKeypair(publicKey)
   153  }