1# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/ 2# 3# Permission is hereby granted, free of charge, to any person obtaining a 4# copy of this software and associated documentation files (the 5# "Software"), to deal in the Software without restriction, including 6# without limitation the rights to use, copy, modify, merge, publish, dis- 7# tribute, sublicense, and/or sell copies of the Software, and to permit 8# persons to whom the Software is furnished to do so, subject to the fol- 9# lowing conditions: 10# 11# The above copyright notice and this permission notice shall be included 12# in all copies or substantial portions of the Software. 13# 14# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- 16# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT 17# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 18# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 19# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 20# IN THE SOFTWARE. 21 22""" 23Represents an EC2 Keypair 24""" 25 26import os 27from boto.ec2.ec2object import EC2Object 28from boto.exception import BotoClientError 29 30 31class KeyPair(EC2Object): 32 33 def __init__(self, connection=None): 34 super(KeyPair, self).__init__(connection) 35 self.name = None 36 self.fingerprint = None 37 self.material = None 38 39 def __repr__(self): 40 return 'KeyPair:%s' % self.name 41 42 def endElement(self, name, value, connection): 43 if name == 'keyName': 44 self.name = value 45 elif name == 'keyFingerprint': 46 self.fingerprint = value 47 elif name == 'keyMaterial': 48 self.material = value 49 else: 50 setattr(self, name, value) 51 52 def delete(self, dry_run=False): 53 """ 54 Delete the KeyPair. 55 56 :rtype: bool 57 :return: True if successful, otherwise False. 58 """ 59 return self.connection.delete_key_pair(self.name, dry_run=dry_run) 60 61 def save(self, directory_path): 62 """ 63 Save the material (the unencrypted PEM encoded RSA private key) 64 of a newly created KeyPair to a local file. 65 66 :type directory_path: string 67 :param directory_path: The fully qualified path to the directory 68 in which the keypair will be saved. The 69 keypair file will be named using the name 70 of the keypair as the base name and .pem 71 for the file extension. If a file of that 72 name already exists in the directory, an 73 exception will be raised and the old file 74 will not be overwritten. 75 76 :rtype: bool 77 :return: True if successful. 78 """ 79 if self.material: 80 directory_path = os.path.expanduser(directory_path) 81 file_path = os.path.join(directory_path, '%s.pem' % self.name) 82 if os.path.exists(file_path): 83 raise BotoClientError('%s already exists, it will not be overwritten' % file_path) 84 fp = open(file_path, 'wb') 85 fp.write(self.material) 86 fp.close() 87 os.chmod(file_path, 0o600) 88 return True 89 else: 90 raise BotoClientError('KeyPair contains no material') 91 92 def copy_to_region(self, region, dry_run=False): 93 """ 94 Create a new key pair of the same new in another region. 95 Note that the new key pair will use a different ssh 96 cert than the this key pair. After doing the copy, 97 you will need to save the material associated with the 98 new key pair (use the save method) to a local file. 99 100 :type region: :class:`boto.ec2.regioninfo.RegionInfo` 101 :param region: The region to which this security group will be copied. 102 103 :rtype: :class:`boto.ec2.keypair.KeyPair` 104 :return: The new key pair 105 """ 106 if region.name == self.region: 107 raise BotoClientError('Unable to copy to the same Region') 108 conn_params = self.connection.get_params() 109 rconn = region.connect(**conn_params) 110 kp = rconn.create_key_pair(self.name, dry_run=dry_run) 111 return kp 112