Default OSCreds cacert attribute to False.
[snaps.git] / snaps / openstack / os_credentials.py
1 # Copyright (c) 2017 Cable Television Laboratories, Inc. ("CableLabs")
2 #                    and others.  All rights reserved.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at:
7 #
8 #     http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 from neutronclient.common.utils import str2bool
16
17 from snaps import file_utils
18 from snaps.openstack.utils import glance_utils, keystone_utils
19
20 __author__ = 'spisarski'
21
22
23 class OSCreds:
24     """
25     Represents the credentials required to connect with OpenStack servers
26     """
27
28     def __init__(self, **kwargs):
29         """
30         Constructor
31         :param username: The user (required)
32         :param password: The user's password (required)
33         :param auth_url: The OpenStack cloud's authorization URL (required)
34         :param project_name: The project/tenant name
35         :param identity_api_version: The OpenStack's API version to use for
36                                      Keystone clients
37         :param image_api_version: The OpenStack's API version to use for Glance
38                                   clients
39         :param network_api_version: The OpenStack's API version to use for
40                                     Neutron clients
41         :param compute_api_version: The OpenStack's API version to use for Nova
42                                     clients
43         :param heat_api_version: The OpenStack's API version to use for Heat
44                                     clients
45         :param user_domain_id: Used for v3 APIs
46         :param project_domain_id: Used for v3 APIs
47         :param interface: Used to specify the endpoint type for keystone as
48                           public, admin, internal
49         :param proxy_settings: instance of os_credentials.ProxySettings class
50         :param cacert: Default to be True for http, or the certification file
51                        is specified for https verification, or set to be False
52                        to disable server certificate verification without cert
53                        file
54         """
55         self.username = kwargs.get('username')
56         self.password = kwargs.get('password')
57         self.auth_url = kwargs.get('auth_url')
58         self.project_name = kwargs.get('project_name')
59
60         if kwargs.get('identity_api_version') is None:
61             self.identity_api_version = keystone_utils.V2_VERSION_NUM
62         else:
63             self.identity_api_version = float(kwargs['identity_api_version'])
64
65         if kwargs.get('image_api_version') is None:
66             self.image_api_version = glance_utils.VERSION_2
67         else:
68             self.image_api_version = float(kwargs['image_api_version'])
69
70         if kwargs.get('network_api_version') is None:
71             self.network_api_version = 2
72         else:
73             self.network_api_version = float(kwargs['network_api_version'])
74
75         if kwargs.get('compute_api_version') is None:
76             self.compute_api_version = 2
77         else:
78             self.compute_api_version = float(kwargs['compute_api_version'])
79
80         if kwargs.get('heat_api_version') is None:
81             self.heat_api_version = 1
82         else:
83             self.heat_api_version = float(kwargs['heat_api_version'])
84
85         if kwargs.get('user_domain_id') is None:
86             self.user_domain_id = 'default'
87         else:
88             self.user_domain_id = kwargs['user_domain_id']
89
90         if kwargs.get('project_domain_id') is None:
91             self.project_domain_id = 'default'
92         else:
93             self.project_domain_id = kwargs['project_domain_id']
94
95         if kwargs.get('interface') is None:
96             self.interface = 'admin'
97         else:
98             self.interface = kwargs['interface']
99
100         self.cacert = False
101         if kwargs.get('cacert') is not None:
102             if isinstance(kwargs.get('cacert'), str):
103                 if file_utils.file_exists(kwargs['cacert']):
104                     self.cacert = kwargs['cacert']
105                 else:
106                     self.cacert = str2bool(kwargs['cacert'])
107             else:
108                 self.cacert = kwargs['cacert']
109
110         if isinstance(kwargs.get('proxy_settings'), ProxySettings):
111             self.proxy_settings = kwargs.get('proxy_settings')
112         elif isinstance(kwargs.get('proxy_settings'), dict):
113             self.proxy_settings = ProxySettings(**kwargs.get('proxy_settings'))
114         else:
115             self.proxy_settings = None
116
117         if (not self.username or not self.password or not self.auth_url
118                 or not self.project_name):
119             raise OSCredsError('username, password, auth_url, and project_name'
120                                ' are required')
121
122         auth_url_tokens = self.auth_url.split('/')
123         last_token = auth_url_tokens[len(auth_url_tokens) - 1]
124         if len(last_token) == 0:
125             last_token = auth_url_tokens[len(auth_url_tokens) - 2]
126
127         if not last_token.startswith('v'):
128             raise OSCredsError('auth_url last toke must start with \'v\'')
129
130     @property
131     def __str__(self):
132         """Converts object to a string"""
133         return ('OSCreds - username=' + str(self.username) +
134                 ', password=' + str(self.password) +
135                 ', auth_url=' + str(self.auth_url) +
136                 ', project_name=' + str(self.project_name) +
137                 ', identity_api_version=' + str(self.identity_api_version) +
138                 ', image_api_version=' + str(self.image_api_version) +
139                 ', network_api_version=' + str(self.network_api_version) +
140                 ', compute_api_version=' + str(self.compute_api_version) +
141                 ', user_domain_id=' + str(self.user_domain_id) +
142                 ', interface=' + str(self.interface) +
143                 ', proxy_settings=' + str(self.proxy_settings) +
144                 ', cacert=' + str(self.cacert))
145
146
147 class ProxySettings:
148     """
149     Represents the information required for sending traffic (HTTP & SSH)
150     through a proxy
151     """
152
153     def __init__(self, **kwargs):
154         """
155         Constructor
156         :param host: the HTTP proxy host
157         :param port: the HTTP proxy port
158         :param https_host: the HTTPS proxy host (defaults to host)
159         :param https_port: the HTTPS proxy port (defaults to port)
160         :param port: the HTTP proxy port
161         :param ssh_proxy_cmd: the SSH proxy command string (optional)
162         """
163         self.host = kwargs.get('host')
164         self.port = kwargs.get('port')
165
166         self.https_host = kwargs.get('https_host', self.host)
167         self.https_port = kwargs.get('https_port', self.port)
168
169         self.ssh_proxy_cmd = kwargs.get('ssh_proxy_cmd')
170
171         if not self.host or not self.port:
172             raise ProxySettingsError('host & port are required')
173
174     def __str__(self):
175         """Converts object to a string"""
176         return 'ProxySettings - host=' + str(self.host) + \
177                ', port=' + str(self.port) + \
178                ', ssh_proxy_cmd=' + str(self.ssh_proxy_cmd)
179
180
181 class ProxySettingsError(Exception):
182     """
183     Exception to be thrown when an OSCred are invalid
184     """
185
186
187 class OSCredsError(Exception):
188     """
189     Exception to be thrown when an OSCred are invalid
190     """