-
Notifications
You must be signed in to change notification settings - Fork 5
/
auth_stack.py
239 lines (191 loc) · 8.46 KB
/
auth_stack.py
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Copyright (c) 2019 Rackspace US, Inc.
#
# 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 contextlib
import datetime
import errno
import json
import logging
import os
import re
import sys
import traceback
from keystoneclient.v2_0 import client as client
from novaclient import client as nova_client
from neutronclient.neutron import client as neutron_client
from glanceclient import Client as glance_client
from cinderclient.client import Client as cinder_client
from cinderclient import exceptions as c_exc
from neutronclient.common import exceptions as n_exc
from novaclient.client import exceptions as nova_exc
from glanceclient import exc as g_exc
from keystoneclient.openstack.common.apiclient import exceptions as k_exc
OPENRC_FROM = './from_auth'
OPENRC_TO = './to_auth'
class AuthStack(object):
def __init__(self):
from_auth = self.get_auth_details('from')
to_auth = self.get_auth_details('to')
self.from_auth_url = from_auth['OS_AUTH_URL']
self.from_auth_ip = from_auth['OS_AUTH_IP']
self.from_username = from_auth['OS_USERNAME']
self.from_password = from_auth['OS_PASSWORD']
self.from_tenant_name = from_auth['OS_TENANT_NAME']
self.to_auth_url = to_auth['OS_AUTH_URL']
self.to_auth_ip = to_auth['OS_AUTH_IP']
self.to_username = to_auth['OS_USERNAME']
self.to_password = to_auth['OS_PASSWORD']
self.to_tenant_name = to_auth['OS_TENANT_NAME']
def get_from_auth_ref(self):
keystone = client.Client(username=self.from_username, password=self.from_password,
tenant_name=self.from_tenant_name, auth_url=self.from_auth_url)
return keystone.auth_ref
def get_to_auth_ref(self):
keystone = client.Client(username=self.to_username, password=self.to_password,
tenant_name=self.to_tenant_name, auth_url=self.to_auth_url)
return keystone.auth_ref
def get_from_keystone_client(self):
auth_ref = self.get_from_auth_ref()
keystone = client.Client(auth_ref=auth_ref, endpoint=self.from_auth_url)
return keystone
def get_to_keystone_client(self):
auth_ref = self.get_to_auth_ref()
keystone = client.Client(auth_ref=auth_ref, endpoint=self.to_auth_url)
return keystone
def get_keystone_client(self, destination):
if destination == 'to':
return self.get_to_keystone_client()
else:
return self.get_from_keystone_client()
def get_from_nova_client(self):
# nova = nova_client.Client('2', self.from_username, self.from_password,
# self.from_tenant_name, self.from_auth_url)
auth_ref = self.get_from_auth_ref()
auth_token = auth_ref['token']['id']
tenant_id = auth_ref['token']['tenant']['id']
bypass_url = 'http://{ip}:8774/v2.1/{tenant_id}' \
.format(ip=self.from_auth_ip, tenant_id=tenant_id)
nova = nova_client.Client('2.1', auth_token=auth_token, bypass_url=bypass_url)
return nova
def get_to_nova_client(self):
# nova = nova_client.Client('2', self.to_username, self.to_password,
# self.to_tenant_name, self.to_auth_url)
auth_ref = self.get_to_auth_ref()
auth_token = auth_ref['token']['id']
tenant_id = auth_ref['token']['tenant']['id']
bypass_url = 'http://{ip}:8774/v2/{tenant_id}' \
.format(ip=self.to_auth_ip, tenant_id=tenant_id)
nova = nova_client.Client('2', auth_token=auth_token, bypass_url=bypass_url)
return nova
def get_nova_client(self, destination):
if destination == 'to':
return self.get_to_nova_client()
else:
return self.get_from_nova_client()
def get_from_neutron_client(self):
auth_ref = self.get_from_auth_ref()
token = auth_ref['token']['id']
endpoint_url = 'http://{ip}:9696'.format(ip=self.from_auth_ip)
neutron = neutron_client.Client('2.0', token=token, endpoint_url=endpoint_url)
return neutron
def get_to_neutron_client(self):
auth_ref = self.get_to_auth_ref()
token = auth_ref['token']['id']
endpoint_url = 'http://{ip}:9696'.format(ip=self.to_auth_ip)
neutron = neutron_client.Client('2.0', token=token, endpoint_url=endpoint_url)
return neutron
def get_neutron_client(self, destination):
if destination == 'to':
return self.get_to_neutron_client()
else:
return self.get_from_neutron_client()
def get_from_glance_client(self):
auth_ref = self.get_from_auth_ref()
token = auth_ref['token']['id']
endpoint_url = 'http://{ip}:9292/v1'.format(ip=self.from_auth_ip)
glance = glance_client('1', endpoint=endpoint_url, token=token)
return glance
def get_to_glance_client(self):
auth_ref = self.get_to_auth_ref()
token = auth_ref['token']['id']
endpoint_url = 'http://{ip}:9292/v1'.format(ip=self.to_auth_ip)
glance = glance_client('1', endpoint=endpoint_url, token=token)
return glance
def get_glance_client(self, destination):
if destination == 'to':
return self.get_to_glance_client()
else:
return self.get_from_glance_client()
def get_from_cinder_client(self):
auth_ref = self.get_from_auth_ref()
token = auth_ref['token']['id']
tenant_id = auth_ref['token']['tenant']['id']
endpoint_url = ('http://{ip}:8776/v1/{tenant}'.format
(ip=self.from_auth_ip, tenant=tenant_id))
cinder = cinder_client('1', self.from_username, token,
project_id=self.from_tenant_name,
auth_url=self.from_auth_url)
cinder.client.auth_token = token
cinder.client.management_url = endpoint_url
return cinder
def get_to_cinder_client(self):
auth_ref = self.get_to_auth_ref()
token = auth_ref['token']['id']
tenant_id = auth_ref['token']['tenant']['id']
endpoint_url = ('http://{ip}:8776/v1/{tenant}'.format
(ip=self.to_auth_ip, tenant=tenant_id))
cinder = cinder_client('1', self.to_username, token,
project_id=self.to_tenant_name,
auth_url=self.to_auth_url)
cinder.client.auth_token = token
cinder.client.management_url = endpoint_url
return cinder
def get_cinder_client(self, destination):
if destination == 'to':
return self.get_to_cinder_client()
else:
return self.get_from_cinder_client()
def get_auth_details(self, destination):
AUTH_DETAILS = {'OS_USERNAME': None,
'OS_PASSWORD': None,
'OS_TENANT_NAME': None,
'OS_AUTH_URL': None,
'OS_AUTH_IP': None}
auth_details = AUTH_DETAILS
pattern = re.compile(
'^(?:export\s)?(?P<key>\w+)(?:\s+)?=(?:\s+)?(?P<value>.*)$'
)
try:
if destination == 'to':
openrc_file = OPENRC_TO
else:
openrc_file = OPENRC_FROM
with open(openrc_file) as openrc:
for line in openrc:
match = pattern.match(line)
if match is None:
continue
k = match.group('key')
v = match.group('value')
if k in auth_details and auth_details[k] is None:
auth_details[k] = v
except IOError as e:
if e.errno != errno.ENOENT:
print str(e)
# no openrc file, so we try the environment
for key in auth_details.keys():
auth_details[key] = os.environ.get(key)
for key in auth_details.keys():
if auth_details[key] is None:
print '%s not set' % key
return auth_details