Enable https for Openstack in Snaps
[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 __author__ = 'spisarski'
16
17
18 class OSCreds:
19     """
20     Represents the credentials required to connect with OpenStack servers
21     """
22
23     def __init__(self, username, password, auth_url, project_name, identity_api_version=2, image_api_version=2,
24                  network_api_version=2, compute_api_version=2, user_domain_id='default', project_domain_id='default',
25                  proxy_settings=None, cacert=True):
26         """
27         Constructor
28         :param username: The user (required)
29         :param password: The user's password (required)
30         :param auth_url: The OpenStack cloud's authorization URL (required)
31         :param project_name: The project/tenant name
32         :param identity_api_version: The OpenStack's API version to use for Keystone clients
33         :param image_api_version: The OpenStack's API version to use for Glance clients
34         :param network_api_version: The OpenStack's API version to use for Neutron clients
35         :param compute_api_version: The OpenStack's API version to use for Nova clients
36         :param user_domain_id: Used for v3 APIs
37         :param project_domain_id: Used for v3 APIs
38         :param proxy_settings: instance of os_credentials.ProxySettings class
39         :param cacert: Default to be True for http, or the certification file is specified for https verification,
40                        or set to be False to disable server certificate verification without cert file
41         """
42         self.username = username
43         self.password = password
44         self.auth_url = auth_url
45         self.project_name = project_name
46         self.identity_api_version = identity_api_version
47         self.image_api_version = image_api_version
48         self.network_api_version = network_api_version
49         self.compute_api_version = compute_api_version
50         self.user_domain_id = user_domain_id
51         self.project_domain_id = project_domain_id
52         self.proxy_settings = proxy_settings
53         self.cacert = cacert
54
55         if self.proxy_settings and not isinstance(self.proxy_settings, ProxySettings):
56             raise Exception('proxy_settings must be an instance of the class ProxySettings')
57
58         if self.auth_url:
59             auth_url_tokens = self.auth_url.split('/')
60             last_token = auth_url_tokens[len(auth_url_tokens) - 1]
61             if len(last_token) == 0:
62                 last_token = auth_url_tokens[len(auth_url_tokens) - 2]
63
64             if not last_token.startswith('v'):
65                 raise Exception('auth_url last toke must start with \'v\'')
66
67     def __str__(self):
68         """Converts object to a string"""
69         return 'OSCreds - username=' + str(self.username) + \
70                ', password=' + str(self.password) + \
71                ', auth_url=' + str(self.auth_url) + \
72                ', project_name=' + str(self.project_name) + \
73                ', identity_api_version=' + str(self.identity_api_version) + \
74                ', image_api_version=' + str(self.image_api_version) + \
75                ', network_api_version=' + str(self.network_api_version) + \
76                ', compute_api_version=' + str(self.compute_api_version) + \
77                ', user_domain_id=' + str(self.user_domain_id) + \
78                ', proxy_settings=' + str(self.proxy_settings) + \
79                ', cacert=' + str(self.cacert)
80
81
82 class ProxySettings:
83     """
84     Represents the information required for sending traffic (HTTP & SSH) through a proxy
85     """
86
87     def __init__(self, host, port, ssh_proxy_cmd=None):
88         """
89         Constructor
90         :param host: the HTTP proxy host
91         :param port: the HTTP proxy port
92         :param ssh_proxy_cmd: the SSH proxy command string (optional)
93         """
94         # TODO - Add necessary fields here when adding support for secure proxies
95
96         self.host = host
97         self.port = port
98         self.ssh_proxy_cmd = ssh_proxy_cmd
99
100         if not self.host and not self.port:
101             raise Exception('host & port are required')
102
103     def __str__(self):
104         """Converts object to a string"""
105         return 'ProxySettings - host=' + str(self.host) + \
106                ', port=' + str(self.port) + \
107                ', ssh_proxy_cmd=' + str(self.ssh_proxy_cmd)