This repository has been archived by the owner on Apr 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathk5_router_create_ssl_vpn.py
480 lines (350 loc) · 14.1 KB
/
k5_router_create_ssl_vpn.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
#!/usr/bin/python
ANSIBLE_METADATA = {'status': ['preview'],
'supported_by': 'community',
'version': '1.0'}
DOCUMENTATION = '''
---
module: k5_router_create_ssl_vpn
short_description: Create an OpenVPN Server
version_added: "1.0"
description:
- Create an OpenVPN Server
options:
name:
description:
- Name of the VPN
required: true
default: None
router_name:
description:
- Name of the router to attach the SSL VPN to
required: true
default: None
subnet_name:
description:
- Name of the subnet that the VON connects to inside K5
required: true
default: None
client_cidr:
description:
- IP subnet of the remote clients. A NAT subnet inside K5. (not the clients actual subnet)
required: true
default: None
availablity_zone:
description:
- Name of the availability zone
required: true
default: None
ca:
description:
- CA key in pem format
required: true
default: None
server_certificate:
description:
- server cert in pem format
required: true
default: None
server_key:
description:
- server key in pem format
required: true
default: None
dh:
description:
- dh in pem format
required: true
default: None
requirements:
- "python >= 2.6"
'''
#TODO
EXAMPLES = '''
'''
#TODO
RETURN = '''
'''
import requests
import os
import json
from ansible.module_utils.basic import *
############## Common debug ###############
k5_debug = False
k5_debug_out = []
def k5_debug_get():
"""Return our debug list"""
return k5_debug_out
def k5_debug_clear():
"""Clear our debug list"""
k5_debug_out = []
def k5_debug_add(s):
"""Add string to debug list if env K5_DEBUG is defined"""
if k5_debug:
k5_debug_out.append(s)
############## functions #############
def k5_get_endpoint(e,name):
"""Pull particular endpoint name from dict"""
return e['endpoints'][name]
def k5_upload_payload(module, payload, payload_name):
"""Upload payload vpn secrets"""
k5_debug_add("def: " + sys._getframe().f_code.co_name)
k5_facts = module.params['k5_auth']
endpoint = k5_facts['endpoints']['keymanagement']
auth_token = k5_facts['auth_token']
vpn_name = module.params['name']
k5_debug_add('auth_token: {0}'.format(auth_token))
session = requests.Session()
project_id = k5_facts['auth_spec']['os_project_id']
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Auth-Token': auth_token }
url = endpoint + "/" + project_id + '/secrets'
query_json = {
"name": vpn_name + "_" + payload_name,
#"name": payload_name,
"payload": payload,
"payload_content_type": "text/plain"
}
k5_debug_add('endpoint: {0}'.format(endpoint))
k5_debug_add('REQ: {0}'.format(url))
k5_debug_add('headers: {0}'.format(headers))
# k5_debug_add('json: {0}'.format(query_json))
try:
response = session.request('POST', url, headers=headers, json=query_json)
except requests.exceptions.RequestException as e:
module.fail_json(msg=e)
# we failed to make a change
if response.status_code not in (201,):
module.fail_json(msg="k5_upload_payload " + payload_name + " RESP: HTTP Code:" + str(response.status_code) + " " + str(response.content), debug=k5_debug_out)
return response.json()['secret_ref']
def k5_create_credentials_container(module, ca_ref, server_certificate_ref, server_key, dh_ref):
"""create the container"""
k5_debug_add("def: " + sys._getframe().f_code.co_name)
k5_facts = module.params['k5_auth']
endpoint = k5_facts['endpoints']['keymanagement']
auth_token = k5_facts['auth_token']
vpn_name = module.params['name']
k5_debug_add('secret_refs:'+ str( [ ca_ref, server_certificate_ref, server_key, dh_ref ]))
session = requests.Session()
project_id = k5_facts['auth_spec']['os_project_id']
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Auth-Token': auth_token }
url = endpoint + '/' + project_id + '/containers'
#Provided object does not match schema 'Container': only 'private_key', 'certificate', or 'intermediates' reference names are allowed for Certificate type
query_json = {
"name": vpn_name + "_container",
"type":"generic", # TODO why this? and not certificate
"secret_refs":[
{
"name": "ca",
"secret_ref": ca_ref
},
{
"name": "server_certificate",
"secret_ref": server_certificate_ref
},
{
"name": "server_key",
"secret_ref": server_key
},
{
"name": "dh",
"secret_ref": dh_ref
}
]
}
k5_debug_add('endpoint: {0}'.format(endpoint))
k5_debug_add('REQ: {0}'.format(url))
k5_debug_add('headers: {0}'.format(headers))
k5_debug_add('json: {0}'.format(query_json))
try:
response = session.request('POST', url, headers=headers, json=query_json)
except requests.exceptions.RequestException as e:
module.fail_json(msg=e)
# we failed to make a change
if response.status_code not in (201,):
module.fail_json(msg="k5_create_credentials_container RESP: HTTP Code:" + str(response.status_code) + " " + str(response.content), debug=k5_debug_out)
return response.json()['container_ref']
def k5_get_subnet_id_from_name(module, k5_facts):
"""Get an id from a subnet_name"""
k5_debug_add("def: " + sys._getframe().f_code.co_name)
endpoint = k5_facts['endpoints']['networking']
auth_token = k5_facts['auth_token']
subnet_name = module.params['subnet_name']
session = requests.Session()
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Auth-Token': auth_token }
url = endpoint + '/v2.0/subnets'
k5_debug_add('endpoint: {0}'.format(endpoint))
k5_debug_add('REQ: {0}'.format(url))
k5_debug_add('headers: {0}'.format(headers))
try:
response = session.request('GET', url, headers=headers)
except requests.exceptions.RequestException as e:
module.fail_json(msg=e)
# we failed to get data
if response.status_code not in (200,):
module.fail_json(msg="k5_get_subnet_id_from_name RESP: HTTP Code:" + str(response.status_code) + " " + str(response.content), debug=k5_debug_out)
#k5_debug_add("RESP: " + str(response.json()))
for n in response.json()['subnets']:
#k5_debug_add("Found subnet name: " + str(n['name']))
if str(n['name']) == subnet_name:
#k5_debug_add("Found it!")
return n['id']
return ''
def k5_get_router_id_from_name(module, k5_facts):
"""Get an id from a router_name"""
k5_debug_add("def: " + sys._getframe().f_code.co_name)
endpoint = k5_facts['endpoints']['networking']
auth_token = k5_facts['auth_token']
router_name = module.params['router_name']
session = requests.Session()
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Auth-Token': auth_token }
url = endpoint + '/v2.0/routers'
k5_debug_add('endpoint: {0}'.format(endpoint))
k5_debug_add('REQ: {0}'.format(url))
k5_debug_add('headers: {0}'.format(headers))
try:
response = session.request('GET', url, headers=headers)
except requests.exceptions.RequestException as e:
module.fail_json(msg=e)
# we failed to get data
if response.status_code not in (200,):
module.fail_json(msg="k5_get_router_id_from_name RESP: HTTP Code:" + str(response.status_code) + " " + str(response.content), debug=k5_debug_out)
#k5_debug_add("RESP: " + str(response.json()))
for n in response.json()['routers']:
#k5_debug_add("Found router name: " + str(n['name']))
if str(n['name']) == router_name:
#k5_debug_add("Found it!")
return n['id']
return ''
def k5_router_attach_ssl_vpn_service(module):
"""create the service"""
k5_debug_add("def: " + sys._getframe().f_code.co_name)
k5_facts = module.params['k5_auth']
endpoint = k5_facts['endpoints']['networking']
auth_token = k5_facts['auth_token']
vpn_name = module.params['name']
az = module.params['availability_zone']
session = requests.Session()
subnet_id = k5_get_subnet_id_from_name(module,k5_facts)
router_id = k5_get_router_id_from_name(module,k5_facts)
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Auth-Token': auth_token }
url = endpoint + '/v2.0/vpn/vpnservices'
query_json = {
"vpnservice": {
"subnet_id": subnet_id,
"router_id": router_id,
"name": vpn_name,
"availability_zone": az
}
}
k5_debug_add('endpoint: {0}'.format(endpoint))
k5_debug_add('REQ: {0}'.format(url))
k5_debug_add('headers: {0}'.format(headers))
k5_debug_add('json: {0}'.format(query_json))
try:
response = session.request('POST', url, headers=headers, json=query_json)
except requests.exceptions.RequestException as e:
module.fail_json(msg=e)
# we failed to make a change
if response.status_code not in (201,):
module.fail_json(msg="k5_router_attach_ssl_vpn_service RESP: HTTP Code:" + str(response.status_code) + " " + str(response.content), debug=k5_debug_out)
return response.json()['vpnservice']['id']
def k5_create_ssl_vpn_connection(module, container_id, vpn_id):
"""create the connection"""
k5_debug_add("def: " + sys._getframe().f_code.co_name)
k5_facts = module.params['k5_auth']
endpoint = k5_facts['endpoints']['networking']
auth_token = k5_facts['auth_token']
vpn_name = module.params['name']
az = module.params['availability_zone']
client_cidr = module.params['client_cidr']
session = requests.Session()
project_id = k5_facts['auth_spec']['os_project_id']
headers = {'Content-Type': 'application/json', 'Accept': 'application/json', 'X-Auth-Token': auth_token }
url = endpoint + '/v2.0/vpn/ssl-vpn-connections'
query_json = {
"ssl_vpn_connection": {
"name": vpn_name,
"client_address_pool_cidr": client_cidr,
"credential_id": container_id,
"vpnservice_id": vpn_id,
"availability_zone": az,
"protocol": "tcp"
}
}
k5_debug_add('endpoint: {0}'.format(endpoint))
k5_debug_add('REQ: {0}'.format(url))
k5_debug_add('headers: {0}'.format(headers))
k5_debug_add('json: {0}'.format(query_json))
try:
response = session.request('POST', url, headers=headers, json=query_json)
except requests.exceptions.RequestException as e:
module.fail_json(msg=e)
# we failed to make a change
if response.status_code not in (201,):
module.fail_json(msg="k5_create_ssl_vpn_connection RESP: HTTP Code:" + str(response.status_code) + " " + str(response.content), debug=k5_debug_out)
# all okay
if k5_debug:
module.exit_json(msg="k5_create_ssl_vpn_connection successful", k5_ssl_vpn=response.json(), debug=k5_debug_out)
else:
module.exit_json(msg="k5_create_ssl_vpn_connection successful", k5_ssl_vpn=response.json() )
def k5_router_create_ssl_vpn(module):
"""Create ssl vpn on a router"""
global k5_debug
k5_debug_clear()
k5_debug_add("def: " + sys._getframe().f_code.co_name)
if 'K5_DEBUG' in os.environ:
k5_debug = True
if 'auth_spec' in module.params['k5_auth']:
k5_facts = module.params['k5_auth']
else:
module.fail_json(msg="k5_auth_facts not found, have you run k5_auth?")
endpoint = k5_facts['endpoints']['networking']
auth_token = k5_facts['auth_token']
vpn_name = module.params['name']
ca = module.params['ca']
server_certificate = module.params['server_certificate']
server_key = module.params['server_key']
dh = module.params['dh']
k5_debug_add('auth_token: {0}'.format(auth_token))
k5_debug_add('vpn_name: {0}'.format(vpn_name))
#
# upload the payloads
#
ca_ref = k5_upload_payload(module, ca, "ca")
server_certificate_ref = k5_upload_payload(module, server_certificate, "server_certificate")
server_key = k5_upload_payload(module, server_key, "server_key")
dh_ref = k5_upload_payload(module, dh, "dh")
#
# create the credentials container
#
container_ref = k5_create_credentials_container(module, ca_ref, server_certificate_ref, server_key, dh_ref)
container_id = container_ref.split('/')[-1]
#
# create vpn service on the router
#
vpn_id = k5_router_attach_ssl_vpn_service(module)
# vpn_id = vpn_ref.split('/')[-1]
#
# create ssl vpn connection
#
k5_create_ssl_vpn_connection(module, container_id, vpn_id)
######################################################################################
def main():
module = AnsibleModule( argument_spec=dict(
name = dict(required=True, default=None, type='str'),
router_name = dict(required=True, default=None, type='str'),
subnet_name = dict(required=True, default=None, type='str'),
client_cidr = dict(required=True, default=None, type='str'),
availability_zone = dict(required=True, default=None, type='str'),
ca = dict(required=True, default=None, type='str'),
server_certificate = dict(required=True, default=None, type='str'),
server_key = dict(required=True, default=None, type='str'),
dh = dict(required=True, default=None, type='str'),
k5_auth = dict(required=True, default=None, type='dict')
) )
# TODO more checks on the data here - are the certs provided
if k5_get_router_id_from_name(module,module.params['k5_auth']) is '':
module.fail_json(msg="Router does not exist")
k5_router_create_ssl_vpn(module)
######################################################################################
if __name__ == '__main__':
main()