|
13 | 13 | description:
|
14 | 14 | - This HttpApi plugin provides methods to connect to Checkpoint
|
15 | 15 | devices over a HTTP(S)-based api.
|
16 |
| -version_added: "1.0.0" |
| 16 | +version_added: "2.8.0" |
17 | 17 | options:
|
| 18 | + cptarget: |
| 19 | + type: str |
| 20 | + description: |
| 21 | + - target gateway |
| 22 | + vars: |
| 23 | + - name: ansible_checkpoint_target |
18 | 24 | domain:
|
19 | 25 | type: str
|
20 | 26 | description:
|
21 | 27 | - Specifies the domain of the Check Point device
|
22 | 28 | vars:
|
23 | 29 | - name: ansible_checkpoint_domain
|
| 30 | + api_key: |
| 31 | + type: str |
| 32 | + description: |
| 33 | + - Login with api-key instead of user & password |
| 34 | + vars: |
| 35 | + - name: ansible_api_key |
| 36 | + cloud_mgmt_id: |
| 37 | + type: str |
| 38 | + description: |
| 39 | + - The Cloud Management ID |
| 40 | + vars: |
| 41 | + - name: ansible_cloud_mgmt_id |
24 | 42 | """
|
25 | 43 |
|
26 | 44 | import json
|
|
30 | 48 | from ansible.module_utils.six.moves.urllib.error import HTTPError
|
31 | 49 | from ansible.plugins.httpapi import HttpApiBase
|
32 | 50 | from ansible.module_utils.connection import ConnectionError
|
| 51 | +from ansible.parsing.dataloader import DataLoader |
| 52 | +from ansible.inventory.manager import InventoryManager |
33 | 53 |
|
34 | 54 | BASE_HEADERS = {
|
35 | 55 | 'Content-Type': 'application/json',
|
|
38 | 58 |
|
39 | 59 |
|
40 | 60 | class HttpApi(HttpApiBase):
|
| 61 | + def __init__(self, connection): |
| 62 | + super(HttpApi, self).__init__(connection) |
| 63 | + self.connection = connection |
| 64 | + self.mgmt_proxy_enabled = False |
| 65 | + |
| 66 | + loader = DataLoader() |
| 67 | + # Initialize InventoryManager |
| 68 | + inventory = InventoryManager(loader=loader, sources=['/etc/ansible/hosts']) |
| 69 | + # Get host |
| 70 | + host = inventory.get_host('mgmt_proxy') |
| 71 | + # Get variable |
| 72 | + try: |
| 73 | + proxy_enabled = host.vars['enabled'] |
| 74 | + if proxy_enabled == True: |
| 75 | + self.mgmt_proxy_enabled = True |
| 76 | + except Exception as e: |
| 77 | + pass |
| 78 | + |
| 79 | + |
41 | 80 | def login(self, username, password):
|
| 81 | + payload = {} |
| 82 | + url = '/gaia_api/login' |
42 | 83 | if username and password:
|
43 | 84 | payload = {'user': username, 'password': password}
|
44 |
| - url = '/gaia_api/login' |
45 |
| - response, response_data = self.send_request(url, payload) |
46 | 85 | else:
|
47 | 86 | raise AnsibleConnectionFailure('Username and password are required for login')
|
| 87 | + if self.mgmt_proxy_enabled == True: |
| 88 | + url = '/web_api/login' |
| 89 | + response, response_data = self.send_request(url, payload) |
48 | 90 |
|
49 | 91 | try:
|
50 | 92 | self.connection._auth = {'X-chkp-sid': response_data['sid']}
|
51 | 93 | except KeyError:
|
52 | 94 | raise ConnectionError(
|
53 |
| - 'Server returned response without token info during connection authentication: %s' % response) |
| 95 | + 'Server returned response without token info during connection authentication: %s' % response_data) |
54 | 96 |
|
55 | 97 | def logout(self):
|
56 | 98 | url = '/gaia_api/logout'
|
57 |
| - |
| 99 | + if self.mgmt_proxy_enabled == True: |
| 100 | + url = '/web_api/logout' |
58 | 101 | response, dummy = self.send_request(url, None)
|
59 | 102 |
|
60 | 103 | def get_session_uid(self):
|
61 | 104 | return self.connection._session_uid
|
62 | 105 |
|
63 | 106 | def send_request(self, path, body_params):
|
| 107 | + # we only replace gaia_ip/ with web_api/gaia-api/ if target is set and path contains for gaia_ip/ |
| 108 | + cp_api_target = self.get_option('cptarget') |
| 109 | + if 'gaia_api/' in path: # Avoid login/logut requests in case of web_api |
| 110 | + if self.mgmt_proxy_enabled == True: |
| 111 | + if cp_api_target != None: |
| 112 | + body_params['target'] = cp_api_target |
| 113 | + path = path.replace("gaia_api/", "web_api/gaia-api/") |
64 | 114 | data = json.dumps(body_params) if body_params else '{}'
|
65 | 115 |
|
66 | 116 | try:
|
|
0 commit comments