| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
# Copyright (c) 2022, Felix Fontein (@felixfontein) <[email protected]> |
| 4 |
# Copyright (c) 2020, Nikolay Dachev <[email protected]> |
| 5 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 7 |
|
| 8 |
from __future__ import absolute_import, division, print_function |
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
|
| 12 |
from ansible.module_utils.basic import missing_required_lib |
| 13 |
from ansible.module_utils.common.text.converters import to_native |
| 14 |
|
| 15 |
import ssl |
| 16 |
import traceback |
| 17 |
|
| 18 |
LIB_IMP_ERR = None |
| 19 |
try: |
| 20 |
from librouteros import connect |
| 21 |
from librouteros.exceptions import LibRouterosError # noqa: F401, pylint: disable=unused-import |
| 22 |
HAS_LIB = True |
| 23 |
except Exception as e: |
| 24 |
HAS_LIB = False |
| 25 |
LIB_IMP_ERR = traceback.format_exc() |
| 26 |
|
| 27 |
|
| 28 |
def check_has_library(module): |
| 29 |
if not HAS_LIB: |
| 30 |
module.fail_json( |
| 31 |
msg=missing_required_lib('librouteros'), |
| 32 |
exception=LIB_IMP_ERR, |
| 33 |
) |
| 34 |
|
| 35 |
|
| 36 |
def api_argument_spec(): |
| 37 |
return dict( |
| 38 |
username=dict(type='str', required=True), |
| 39 |
password=dict(type='str', required=True, no_log=True), |
| 40 |
hostname=dict(type='str', required=True), |
| 41 |
port=dict(type='int'), |
| 42 |
tls=dict(type='bool', default=False, aliases=['ssl']), |
| 43 |
force_no_cert=dict(type='bool', default=False), |
| 44 |
validate_certs=dict(type='bool', default=True), |
| 45 |
validate_cert_hostname=dict(type='bool', default=False), |
| 46 |
ca_path=dict(type='path'), |
| 47 |
encoding=dict(type='str', default='ASCII'), |
| 48 |
timeout=dict(type='int', default=10), |
| 49 |
) |
| 50 |
|
| 51 |
|
| 52 |
def _ros_api_connect(module, username, password, host, port, use_tls, force_no_cert, validate_certs, validate_cert_hostname, ca_path, encoding, timeout): |
| 53 |
'''Connect to RouterOS API.''' |
| 54 |
if not port: |
| 55 |
if use_tls: |
| 56 |
port = 8729 |
| 57 |
else: |
| 58 |
port = 8728 |
| 59 |
try: |
| 60 |
params = dict( |
| 61 |
username=username, |
| 62 |
password=password, |
| 63 |
host=host, |
| 64 |
port=port, |
| 65 |
encoding=encoding, |
| 66 |
timeout=timeout, |
| 67 |
) |
| 68 |
if use_tls: |
| 69 |
ctx = ssl.create_default_context(cafile=ca_path) |
| 70 |
wrap_context = ctx.wrap_socket |
| 71 |
if force_no_cert: |
| 72 |
ctx.check_hostname = False |
| 73 |
ctx.set_ciphers("ADH:@SECLEVEL=0") |
| 74 |
elif not validate_certs: |
| 75 |
ctx.check_hostname = False |
| 76 |
ctx.verify_mode = ssl.CERT_NONE |
| 77 |
elif not validate_cert_hostname: |
| 78 |
ctx.check_hostname = False |
| 79 |
else: |
| 80 |
# Since librouteros does not pass server_hostname, |
| 81 |
# we have to do this ourselves: |
| 82 |
def wrap_context(*args, **kwargs): |
| 83 |
kwargs.pop('server_hostname', None) |
| 84 |
return ctx.wrap_socket(*args, server_hostname=host, **kwargs) |
| 85 |
params['ssl_wrapper'] = wrap_context |
| 86 |
api = connect(**params) |
| 87 |
except Exception as e: |
| 88 |
connection = { |
| 89 |
'username': username, |
| 90 |
'hostname': host, |
| 91 |
'port': port, |
| 92 |
'ssl': use_tls, |
| 93 |
'status': 'Error while connecting: %s' % to_native(e), |
| 94 |
} |
| 95 |
module.fail_json(msg=connection['status'], connection=connection) |
| 96 |
return api |
| 97 |
|
| 98 |
|
| 99 |
def create_api(module): |
| 100 |
"""Create an API object.""" |
| 101 |
return _ros_api_connect( |
| 102 |
module, |
| 103 |
module.params['username'], |
| 104 |
module.params['password'], |
| 105 |
module.params['hostname'], |
| 106 |
module.params['port'], |
| 107 |
module.params['tls'], |
| 108 |
module.params['force_no_cert'], |
| 109 |
module.params['validate_certs'], |
| 110 |
module.params['validate_cert_hostname'], |
| 111 |
module.params['ca_path'], |
| 112 |
module.params['encoding'], |
| 113 |
module.params['timeout'], |
| 114 |
) |
| 115 |
|
| 116 |
|
| 117 |
def get_api_version(api): |
| 118 |
"""Given an API object, query the system's version.""" |
| 119 |
system_info = list(api.path().join('system', 'resource'))[0] |
| 120 |
return system_info['version'].split(' ', 1)[0] |