| 1 |
# (c) 2018 Red Hat Inc. |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
from __future__ import absolute_import, division, print_function |
| 5 |
|
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
DOCUMENTATION = """ |
| 9 |
--- |
| 10 |
author: Ansible Networking Team (@rcarrillocruz) |
| 11 |
name: checkpoint |
| 12 |
short_description: HttpApi Plugin for Checkpoint devices |
| 13 |
description: |
| 14 |
- This HttpApi plugin provides methods to connect to Checkpoint |
| 15 |
devices over a HTTP(S)-based api. |
| 16 |
version_added: "2.8.0" |
| 17 |
options: |
| 18 |
domain: |
| 19 |
type: str |
| 20 |
description: |
| 21 |
- Specifies the domain of the Check Point device |
| 22 |
vars: |
| 23 |
- name: ansible_checkpoint_domain |
| 24 |
api_key: |
| 25 |
type: str |
| 26 |
description: |
| 27 |
- Login with api-key instead of user & password |
| 28 |
vars: |
| 29 |
- name: ansible_api_key |
| 30 |
cloud_mgmt_id: |
| 31 |
type: str |
| 32 |
description: |
| 33 |
- The Cloud Management ID |
| 34 |
vars: |
| 35 |
- name: ansible_cloud_mgmt_id |
| 36 |
target: |
| 37 |
type: str |
| 38 |
description: |
| 39 |
- target gateway |
| 40 |
vars: |
| 41 |
- name: ansible_checkpoint_target |
| 42 |
""" |
| 43 |
|
| 44 |
import json |
| 45 |
|
| 46 |
from ansible.module_utils.basic import to_text |
| 47 |
from ansible.errors import AnsibleConnectionFailure |
| 48 |
from ansible.module_utils.six.moves.urllib.error import HTTPError |
| 49 |
from ansible.plugins.httpapi import HttpApiBase |
| 50 |
from ansible.module_utils.connection import ConnectionError |
| 51 |
|
| 52 |
BASE_HEADERS = { |
| 53 |
"Content-Type": "application/json", |
| 54 |
"User-Agent": "Ansible", |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
class HttpApi(HttpApiBase): |
| 59 |
def login(self, username, password): |
| 60 |
payload = {} |
| 61 |
cp_domain = self.get_option("domain") |
| 62 |
cp_api_key = self.get_option("api_key") |
| 63 |
if cp_domain: |
| 64 |
payload["domain"] = cp_domain |
| 65 |
if username and password and not cp_api_key: |
| 66 |
payload["user"] = username |
| 67 |
payload["password"] = password |
| 68 |
elif cp_api_key and not username and not password: |
| 69 |
payload["api-key"] = cp_api_key |
| 70 |
else: |
| 71 |
raise AnsibleConnectionFailure( |
| 72 |
"[Username and password] or api_key are required for login" |
| 73 |
) |
| 74 |
url = "/web_api/login" |
| 75 |
response, response_data = self.send_request(url, payload) |
| 76 |
if response != 200: |
| 77 |
raise ConnectionError("Login to server failed: %s" % response_data) |
| 78 |
try: |
| 79 |
self.connection._auth = {"X-chkp-sid": response_data["sid"]} |
| 80 |
except KeyError: |
| 81 |
raise ConnectionError( |
| 82 |
"Server returned response without token info during connection authentication: %s" |
| 83 |
% response |
| 84 |
) |
| 85 |
# Case of read-only |
| 86 |
if "uid" in response_data.keys(): |
| 87 |
self.connection._session_uid = response_data["uid"] |
| 88 |
|
| 89 |
def logout(self): |
| 90 |
if any([ |
| 91 |
not self.connection._auth, |
| 92 |
(self.connection._auth and "X-chkp-sid" not in self.connection._auth) |
| 93 |
]): |
| 94 |
return |
| 95 |
url = "/web_api/logout" |
| 96 |
|
| 97 |
response, dummy = self.send_request(url, None) |
| 98 |
|
| 99 |
def get_session_uid(self): |
| 100 |
return self.connection._session_uid |
| 101 |
|
| 102 |
def send_request(self, path, body_params): |
| 103 |
cp_cloud_mgmt_id = self.get_option("cloud_mgmt_id") |
| 104 |
if cp_cloud_mgmt_id: |
| 105 |
path = "/" + cp_cloud_mgmt_id + path |
| 106 |
# we only replace gaia_ip/ with web_api/gaia-api/ if target is set and path contains gaia_ip/ |
| 107 |
if 'gaia_api/' in path and self.get_option("target"): |
| 108 |
path = path.replace("gaia_api/", "web_api/gaia-api/") |
| 109 |
body_params['target'] = self.get_option("target") |
| 110 |
data = json.dumps(body_params) if body_params else '{}' |
| 111 |
try: |
| 112 |
self._display_request() |
| 113 |
response, response_data = self.connection.send( |
| 114 |
path, data, method="POST", headers=BASE_HEADERS |
| 115 |
) |
| 116 |
value = self._get_response_value(response_data) |
| 117 |
|
| 118 |
return response.getcode(), self._response_to_json(value) |
| 119 |
except AnsibleConnectionFailure as e: |
| 120 |
return 404, e.message |
| 121 |
except HTTPError as e: |
| 122 |
error = json.loads(e.read()) |
| 123 |
return e.code, error |
| 124 |
|
| 125 |
def _display_request(self): |
| 126 |
self.connection.queue_message( |
| 127 |
"vvvv", "Web Services: %s %s" % ("POST", self.connection._url) |
| 128 |
) |
| 129 |
|
| 130 |
def _get_response_value(self, response_data): |
| 131 |
return to_text(response_data.getvalue()) |
| 132 |
|
| 133 |
def _response_to_json(self, response_text): |
| 134 |
try: |
| 135 |
return json.loads(response_text) if response_text else {} |
| 136 |
# JSONDecodeError only available on Python 3.5+ |
| 137 |
except ValueError: |
| 138 |
raise ConnectionError("Invalid JSON response: %s" % response_text) |