1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
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-
11
# The above copyright notice and this permission notice shall be included
12
# in all copies or substantial portions of the Software.
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
23
Represents an EC2 Keypair
27
from boto.ec2.ec2object import EC2Object
28
from boto.exception import BotoClientError
30
class KeyPair(EC2Object):
32
def __init__(self, connection=None):
33
EC2Object.__init__(self, connection)
35
self.fingerprint = None
39
return 'KeyPair:%s' % self.name
41
def endElement(self, name, value, connection):
44
elif name == 'keyFingerprint':
45
self.fingerprint = value
46
elif name == 'keyMaterial':
49
setattr(self, name, value)
56
:return: True if successful, otherwise False.
58
return self.connection.delete_key_pair(self.name)
60
def save(self, directory_path):
62
Save the material (the unencrypted PEM encoded RSA private key)
63
of a newly created KeyPair to a local file.
65
:type directory_path: string
66
:param directory_path: The fully qualified path to the directory
67
in which the keypair will be saved. The
68
keypair file will be named using the name
69
of the keypair as the base name and .pem
70
for the file extension. If a file of that
71
name already exists in the directory, an
72
exception will be raised and the old file
73
will not be overwritten.
76
:return: True if successful.
79
file_path = os.path.join(directory_path, '%s.pem' % self.name)
80
if os.path.exists(file_path):
81
raise BotoClientError('%s already exists, it will not be overwritten' % file_path)
82
fp = open(file_path, 'wb')
83
fp.write(self.material)
87
raise BotoClientError('KeyPair contains no material')
89
def copy_to_region(self, region):
91
Create a new key pair of the same new in another region.
92
Note that the new key pair will use a different ssh
93
cert than the this key pair. After doing the copy,
94
you will need to save the material associated with the
95
new key pair (use the save method) to a local file.
97
:type region: :class:`boto.ec2.regioninfo.RegionInfo`
98
:param region: The region to which this security group will be copied.
100
:rtype: :class:`boto.ec2.keypair.KeyPair`
101
:return: The new key pair
103
if region.name == self.region:
104
raise BotoClientError('Unable to copy to the same Region')
105
conn_params = self.connection.get_params()
106
rconn = region.connect(**conn_params)
107
kp = rconn.create_key_pair(self.name)