1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
# Copyright (c) 2016 Cable Television Laboratories, Inc. ("CableLabs")
# and others. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at:
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import logging
import os
from Crypto.PublicKey import RSA
from novaclient.exceptions import NotFound
from snaps.openstack.utils import nova_utils
__author__ = 'spisarski'
logger = logging.getLogger('OpenStackKeypair')
class OpenStackKeypair:
"""
Class responsible for creating a keypair in OpenStack
"""
def __init__(self, os_creds, keypair_settings):
"""
Constructor - all parameters are required
:param os_creds: The credentials to connect with OpenStack
:param keypair_settings: The settings used to create a keypair
"""
self.__os_creds = os_creds
self.keypair_settings = keypair_settings
self.__nova = nova_utils.nova_client(os_creds)
# Attributes instantiated on create()
self.__keypair = None
def create(self, cleanup=False):
"""
Responsible for creating the keypair object.
:param cleanup: Denotes whether or not this is being called for cleanup or not
"""
logger.info('Creating keypair %s...' % self.keypair_settings.name)
try:
self.__keypair = nova_utils.get_keypair_by_name(self.__nova, self.keypair_settings.name)
if not self.__keypair and not cleanup:
if self.keypair_settings.public_filepath and os.path.isfile(self.keypair_settings.public_filepath):
logger.info("Uploading existing keypair")
self.__keypair = nova_utils.upload_keypair_file(self.__nova, self.keypair_settings.name,
self.keypair_settings.public_filepath)
else:
logger.info("Creating new keypair")
# TODO - Make this value configurable
keys = RSA.generate(1024)
self.__keypair = nova_utils.upload_keypair(self.__nova, self.keypair_settings.name,
keys.publickey().exportKey('OpenSSH'))
nova_utils.save_keys_to_files(keys, self.keypair_settings.public_filepath,
self.keypair_settings.private_filepath)
return self.__keypair
except Exception as e:
logger.error('Unexpected error creating keypair named - ' + self.keypair_settings.name)
self.clean()
raise Exception(e.message)
def clean(self):
"""
Removes and deletes the keypair.
"""
if self.__keypair:
try:
nova_utils.delete_keypair(self.__nova, self.__keypair)
except NotFound:
pass
self.__keypair = None
def get_keypair(self):
"""
Returns the OpenStack keypair object
:return:
"""
return self.__keypair
class KeypairSettings:
"""
Class representing a keypair configuration
"""
def __init__(self, config=None, name=None, public_filepath=None, private_filepath=None):
"""
Constructor - all parameters are optional
:param config: Should be a dict object containing the configuration settings using the attribute names below
as each member's the key and overrides any of the other parameters.
:param name: The keypair name.
:param public_filepath: The path to/from the filesystem where the public key file is or will be stored
:param private_filepath: The path where the generated private key file will be stored
:return:
"""
if config:
self.name = config.get('name')
self.public_filepath = config.get('public_filepath')
self.private_filepath = config.get('private_filepath')
else:
self.name = name
self.public_filepath = public_filepath
self.private_filepath = private_filepath
if not self.name:
raise Exception('The attributes name, public_filepath, and private_filepath are required')
|