Skip to content

Commit e4923ef

Browse files
authored
Merge pull request #21 from chkp-orso/master
Add functionality and example to login with api-key
2 parents 0b79641 + b07e0eb commit e4923ef

File tree

3 files changed

+119
-18
lines changed

3 files changed

+119
-18
lines changed

cpapi/mgmt_api.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,44 @@ def save_debug_data(self):
139139
out_file = open(self.debug_file, 'w+')
140140
out_file.write(json.dumps(self.api_calls, indent=4, sort_keys=True))
141141

142+
def _common_login_logic(self, credentials, continue_last_session, domain, read_only, payload):
143+
if self.context == "web_api":
144+
credentials.update({"continue-last-session": continue_last_session,
145+
"read-only": read_only})
146+
147+
if domain:
148+
credentials.update({"domain": domain})
149+
if isinstance(payload, dict):
150+
credentials.update(payload)
151+
152+
login_res = self.api_call("login", credentials)
153+
154+
if login_res.success:
155+
self.sid = login_res.data["sid"]
156+
self.domain = domain
157+
if self.api_version is None:
158+
self.api_version = login_res.data["api-server-version"]
159+
return login_res
160+
161+
def login_with_api_key(self, api_key, continue_last_session=False, domain=None, read_only=False,
162+
payload=None):
163+
"""
164+
performs a 'login' API call to the management server
165+
166+
:param api_key: Check Point api-key
167+
:param continue_last_session: [optional] It is possible to continue the last Check Point session
168+
or to create a new one
169+
:param domain: [optional] The name, UID or IP-Address of the domain to login.
170+
:param read_only: [optional] Login with Read Only permissions. This parameter is not considered in case
171+
continue-last-session is true.
172+
:param payload: [optional] More settings for the login command
173+
:returns: APIResponse object
174+
:side-effects: updates the class's uid and server variables
175+
"""
176+
credentials = {"api-key": api_key}
177+
178+
return self._common_login_logic(credentials, continue_last_session, domain, read_only, payload)
179+
142180
def login(self, username, password, continue_last_session=False, domain=None, read_only=False,
143181
payload=None):
144182
"""
@@ -157,23 +195,7 @@ def login(self, username, password, continue_last_session=False, domain=None, re
157195
"""
158196
credentials = {"user": username, "password": password}
159197

160-
if self.context == "web_api":
161-
credentials.update({"continue-last-session": continue_last_session,
162-
"read-only": read_only})
163-
164-
if domain:
165-
credentials.update({"domain": domain})
166-
if isinstance(payload, dict):
167-
credentials.update(payload)
168-
169-
login_res = self.api_call("login", credentials)
170-
171-
if login_res.success:
172-
self.sid = login_res.data["sid"]
173-
self.domain = domain
174-
if self.api_version is None:
175-
self.api_version = login_res.data["api-server-version"]
176-
return login_res
198+
return self._common_login_logic(credentials, continue_last_session, domain, read_only, payload)
177199

178200
def login_as_root(self, domain=None, payload=None):
179201
"""
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#
2+
# add_group_with_api_key.py
3+
# version 1.0
4+
#
5+
#
6+
# This example demonstrates communication with Check Point Management server using Management API Library in Python.
7+
# Login with api-key, and adding a group.
8+
# The demonstrated commands are:
9+
#
10+
# 1. login with api-key
11+
# 2. adding a group
12+
# 3. publishing the changes
13+
#
14+
# Logout command is called automatically after the work with Management API Library is completed.
15+
#
16+
17+
from __future__ import print_function
18+
19+
# A package for reading passwords without displaying them on the console.
20+
import getpass
21+
22+
import sys, os
23+
sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
24+
25+
# cpapi is a library that handles the communication with the Check Point management server.
26+
from cpapi import APIClient, APIClientArgs
27+
28+
29+
def main():
30+
# getting details from the user
31+
api_server = input("Enter server IP address or hostname:")
32+
33+
if sys.stdin.isatty():
34+
api_key = getpass.getpass("Enter api-key: ")
35+
else:
36+
print("Attention! Your api-key will be shown on the screen!")
37+
api_key = input("Enter api-key: ")
38+
39+
client_args = APIClientArgs(server=api_server)
40+
41+
with APIClient(client_args) as client:
42+
43+
group_name = input("Enter the name of the group: ")
44+
45+
#
46+
# The API client, would look for the server's certificate SHA1 fingerprint in a file.
47+
# If the fingerprint is not found on the file, it will ask the user if he accepts the server's fingerprint.
48+
# In case the user does not accept the fingerprint, exit the program.
49+
if client.check_fingerprint() is False:
50+
print("Could not get the server's fingerprint - Check connectivity with the server.")
51+
exit(1)
52+
53+
# login to server:
54+
login_res = client.login_with_api_key(api_key)
55+
56+
if login_res.success is False:
57+
print("Login failed:\n{}".format(login_res.error_message))
58+
exit(1)
59+
60+
# add the group
61+
add_group_response = client.api_call("add-group",
62+
{"name": group_name})
63+
64+
if add_group_response.success:
65+
66+
print("The group: '{}' has been added successfully".format(group_name))
67+
68+
# publish the result
69+
publish_res = client.api_call("publish", {})
70+
if publish_res.success:
71+
print("The changes were published successfully.")
72+
else:
73+
print("Failed to publish the changes.")
74+
else:
75+
print("Failed to add the group: '{}', Error:\n{}".format(group_name, add_group_response.error_message))
76+
77+
78+
if __name__ == "__main__":
79+
main()

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name="cpapi",
6-
version="1.0.4",
6+
version="1.1.0",
77
author="API team",
88
author_email="[email protected]",
99
description="Check Point Management API SDK",

0 commit comments

Comments
 (0)