Skip to content

Commit a248922

Browse files
authored
Merge pull request #41 from chkp-ameera/master
Check Point Ansible Proxy Integration
2 parents e43040f + 0e04af6 commit a248922

File tree

2 files changed

+92
-5
lines changed

2 files changed

+92
-5
lines changed

README.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,40 @@ See [COPYING](https://www.gnu.org/licenses/gpl-3.0.txt) to see the full text.
153153
## Supported Python versions
154154

155155
- Modules and plugins require Python 2.7 or newer
156+
157+
158+
# Check Point Ansible Proxy Integration
159+
160+
## Overview
161+
162+
This feature integrating the Proxy API for Check Point Security Gateways through the Management Server. The Proxy API enables the Management Server to forward API requests to designated gateways, eliminating the need to address each gateway individually.
163+
164+
165+
![image](https://github.com/nilsujma-dev/Check-Point-Ansible-Proxy-Integration/assets/114651180/0a9dc69f-2a64-4511-bb95-01e28f0049af)
166+
167+
168+
169+
## Integration Process
170+
171+
### Step 1: Options Selected
172+
173+
1. **Source Code:**
174+
175+
The revised code introduces a significant enhancement – the 'target gateway' option. This addition allows the specification of a designated gateway to receive API requests, leveraging the Management Server's Proxy API feature. This modification expands the module's capabilities, aligning with advanced network management requirements and enabling more precise API interactions.
176+
177+
## How to Use
178+
179+
1. Edit the `hosts` so that it will contain a new section similar to this one:
180+
```
181+
[check_point_mgmt]
182+
mgmt_proxy enabled=True
183+
```
184+
2. in the playbook add this var under each task:
185+
```
186+
vars:
187+
ansible_checkpoint_target: <target_gatway>
188+
```
189+
3. in `hosts` change ansible_user and ansible_password to management credintials
190+
3. Follow the standard Ansible playbook execution process with the enhanced Check Point Ansible Collection.
191+
192+

plugins/httpapi/checkpoint.py

Lines changed: 55 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,32 @@
1313
description:
1414
- This HttpApi plugin provides methods to connect to Checkpoint
1515
devices over a HTTP(S)-based api.
16-
version_added: "1.0.0"
16+
version_added: "2.8.0"
1717
options:
18+
cptarget:
19+
type: str
20+
description:
21+
- target gateway
22+
vars:
23+
- name: ansible_checkpoint_target
1824
domain:
1925
type: str
2026
description:
2127
- Specifies the domain of the Check Point device
2228
vars:
2329
- 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
2442
"""
2543

2644
import json
@@ -30,6 +48,8 @@
3048
from ansible.module_utils.six.moves.urllib.error import HTTPError
3149
from ansible.plugins.httpapi import HttpApiBase
3250
from ansible.module_utils.connection import ConnectionError
51+
from ansible.parsing.dataloader import DataLoader
52+
from ansible.inventory.manager import InventoryManager
3353

3454
BASE_HEADERS = {
3555
'Content-Type': 'application/json',
@@ -38,29 +58,59 @@
3858

3959

4060
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+
4180
def login(self, username, password):
81+
payload = {}
82+
url = '/gaia_api/login'
4283
if username and password:
4384
payload = {'user': username, 'password': password}
44-
url = '/gaia_api/login'
45-
response, response_data = self.send_request(url, payload)
4685
else:
4786
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)
4890

4991
try:
5092
self.connection._auth = {'X-chkp-sid': response_data['sid']}
5193
except KeyError:
5294
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)
5496

5597
def logout(self):
5698
url = '/gaia_api/logout'
57-
99+
if self.mgmt_proxy_enabled == True:
100+
url = '/web_api/logout'
58101
response, dummy = self.send_request(url, None)
59102

60103
def get_session_uid(self):
61104
return self.connection._session_uid
62105

63106
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/")
64114
data = json.dumps(body_params) if body_params else '{}'
65115

66116
try:

0 commit comments

Comments
 (0)