PageRenderTime 73ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/boto-2.5.2/boto/ec2/keypair.py

#
Python | 113 lines | 42 code | 12 blank | 59 comment | 8 complexity | a0eac4a25450537a2c509bfde1008576 MD5 | raw file
  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. Represents an EC2 Keypair
  23. """
  24. import os
  25. from boto.ec2.ec2object import EC2Object
  26. from boto.exception import BotoClientError
  27. class KeyPair(EC2Object):
  28. def __init__(self, connection=None):
  29. EC2Object.__init__(self, connection)
  30. self.name = None
  31. self.fingerprint = None
  32. self.material = None
  33. def __repr__(self):
  34. return 'KeyPair:%s' % self.name
  35. def endElement(self, name, value, connection):
  36. if name == 'keyName':
  37. self.name = value
  38. elif name == 'keyFingerprint':
  39. self.fingerprint = value
  40. elif name == 'keyMaterial':
  41. self.material = value
  42. else:
  43. setattr(self, name, value)
  44. def delete(self):
  45. """
  46. Delete the KeyPair.
  47. :rtype: bool
  48. :return: True if successful, otherwise False.
  49. """
  50. return self.connection.delete_key_pair(self.name)
  51. def save(self, directory_path):
  52. """
  53. Save the material (the unencrypted PEM encoded RSA private key)
  54. of a newly created KeyPair to a local file.
  55. :type directory_path: string
  56. :param directory_path: The fully qualified path to the directory
  57. in which the keypair will be saved. The
  58. keypair file will be named using the name
  59. of the keypair as the base name and .pem
  60. for the file extension. If a file of that
  61. name already exists in the directory, an
  62. exception will be raised and the old file
  63. will not be overwritten.
  64. :rtype: bool
  65. :return: True if successful.
  66. """
  67. if self.material:
  68. directory_path = os.path.expanduser(directory_path)
  69. file_path = os.path.join(directory_path, '%s.pem' % self.name)
  70. if os.path.exists(file_path):
  71. raise BotoClientError('%s already exists, it will not be overwritten' % file_path)
  72. fp = open(file_path, 'wb')
  73. fp.write(self.material)
  74. fp.close()
  75. os.chmod(file_path, 0600)
  76. return True
  77. else:
  78. raise BotoClientError('KeyPair contains no material')
  79. def copy_to_region(self, region):
  80. """
  81. Create a new key pair of the same new in another region.
  82. Note that the new key pair will use a different ssh
  83. cert than the this key pair. After doing the copy,
  84. you will need to save the material associated with the
  85. new key pair (use the save method) to a local file.
  86. :type region: :class:`boto.ec2.regioninfo.RegionInfo`
  87. :param region: The region to which this security group will be copied.
  88. :rtype: :class:`boto.ec2.keypair.KeyPair`
  89. :return: The new key pair
  90. """
  91. if region.name == self.region:
  92. raise BotoClientError('Unable to copy to the same Region')
  93. conn_params = self.connection.get_params()
  94. rconn = region.connect(**conn_params)
  95. kp = rconn.create_key_pair(self.name)
  96. return kp