-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
26 changed files
with
902 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
devices | ||
-------------- | ||
|
||
The following methods allow for interaction with the ZCC | ||
Devices API endpoints. | ||
|
||
Methods are accessible via ``zcc.devices`` | ||
|
||
.. _zcc-devices: | ||
|
||
.. automodule:: pyzscaler.zcc.devices | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
ZCC | ||
========== | ||
This package covers the ZCC interface. | ||
|
||
Retrieving the ZCC Company ID. | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
The ZCC Company ID can be obtained by following these instructions: | ||
1. Navigate to the Zscaler Mobile Admin Portal in a web browser. | ||
2. Open the Browser console (typically ``F12``) and click on **Network**. | ||
3. From the top navigation, click on **Enrolled Devices**. | ||
4. Look for the API call ``mobileadmin.zscaler.net/webservice/api/web/usersByCompany`` in the 'Networks' tab | ||
of the Browser Console. Click on this entry. | ||
5. Click on either **Preview** or **Response** to see the data that was returned by the Mobile Admin Portal. | ||
6. The Company ID is represented as an ``int`` and can be found under the ``companyId`` key in the object returned | ||
for each user. | ||
|
||
.. toctree:: | ||
:maxdepth: 1 | ||
:glob: | ||
:hidden: | ||
|
||
* | ||
|
||
.. automodule:: pyzscaler.zcc | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
secrets | ||
-------------- | ||
|
||
The following methods allow for interaction with the ZCC API endpoints for managing secrets. | ||
|
||
Methods are accessible via ``zcc.secrets`` | ||
|
||
.. _zcc-secrets: | ||
|
||
.. automodule:: pyzscaler.zcc.secrets | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
session | ||
-------------- | ||
|
||
The following methods allow for interaction with the ZCC | ||
Session API endpoints. | ||
|
||
Methods are accessible via ``zcc.session`` | ||
|
||
.. _zcc-session: | ||
|
||
.. automodule:: pyzscaler.zcc.session | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ This package covers the ZIA interface. | |
dlp | ||
firewall | ||
locations | ||
rule_labels | ||
sandbox | ||
security | ||
session | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
labels | ||
------------- | ||
|
||
The following methods allow for interaction with the ZIA | ||
Rule Labels API endpoints. | ||
|
||
Methods are accessible via ``zia.labels`` | ||
|
||
.. _zia-labels: | ||
|
||
.. automodule:: pyzscaler.zia.labels | ||
:members: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import os | ||
|
||
from box import Box | ||
from restfly.session import APISession | ||
|
||
from pyzscaler import __version__ | ||
|
||
from .devices import DevicesAPI | ||
from .secrets import SecretsAPI | ||
from .session import AuthenticatedSessionAPI | ||
|
||
|
||
class ZCC(APISession): | ||
""" | ||
A Controller to access Endpoints in the Zscaler Mobile Admin Portal API. | ||
The ZCC object stores the session token and simplifies access to CRUD options within the ZCC Portal. | ||
Attributes: | ||
client_id (str): The ZCC Client ID generated from the ZCC Portal. | ||
client_secret (str): The ZCC Client Secret generated from the ZCC Portal. | ||
company_id (str): | ||
The ZCC Company ID. There seems to be no easy way to obtain this at present. See the note | ||
at the top of this page for information on how to retrieve the Company ID. | ||
""" | ||
|
||
_vendor = "Zscaler" | ||
_product = "Zscaler Mobile Admin Portal" | ||
_backoff = 3 | ||
_build = __version__ | ||
_box = True | ||
_box_attrs = {"camel_killer_box": True} | ||
_env_base = "ZCC" | ||
_env_cloud = "zscaler" | ||
_url = "https://api-mobile.zscaler.net/papi" | ||
|
||
def __init__(self, **kw): | ||
self._client_id = kw.get("client_id", os.getenv(f"{self._env_base}_CLIENT_ID")) | ||
self._client_secret = kw.get("client_secret", os.getenv(f"{self._env_base}_CLIENT_SECRET")) | ||
self.company_id = kw.get("company_id", os.getenv(f"{self._env_base}_COMPANY_ID")) | ||
self.conv_box = True | ||
super(ZCC, self).__init__(**kw) | ||
|
||
def _build_session(self, **kwargs) -> Box: | ||
"""Creates a ZCC API session.""" | ||
super(ZCC, self)._build_session(**kwargs) | ||
self._auth_token = self.session.create_token(client_id=self._client_id, client_secret=self._client_secret) | ||
return self._session.headers.update({"auth-token": f"{self._auth_token}"}) | ||
|
||
@property | ||
def devices(self): | ||
"""The interface object for the :ref:`ZCC Devices interface <zcc-devices>`.""" | ||
return DevicesAPI(self) | ||
|
||
@property | ||
def secrets(self): | ||
"""The interface object for the :ref:`ZCC Secrets interface <zcc-secrets>`.""" | ||
return SecretsAPI(self) | ||
|
||
@property | ||
def session(self): | ||
"""The interface object for the :ref:`ZCC Authenticated Session interface <zcc-session>`.""" | ||
return AuthenticatedSessionAPI(self) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from box import BoxList | ||
from restfly import APISession | ||
from restfly.endpoint import APIEndpoint | ||
|
||
|
||
class DevicesAPI(APIEndpoint): | ||
def __init__(self, api: APISession): | ||
super().__init__(api) | ||
self.company_id = api.company_id | ||
|
||
def list_devices(self) -> BoxList: | ||
""" | ||
Returns the list of devices enrolled in the Mobile Admin Portal. | ||
Returns: | ||
:obj:`BoxList`: A list containing devices using ZCC enrolled in the Mobile Admin Portal. | ||
Examples: | ||
Prints all devices in the Mobile Admin Portal to the console: | ||
>>> for device in zcc.devices.list_devices(): | ||
... print(device) | ||
""" | ||
payload = {"companyId": self.company_id} | ||
|
||
return self._get("public/v1/getDevices", json=payload) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
from restfly import APISession | ||
from restfly.endpoint import APIEndpoint | ||
|
||
|
||
class SecretsAPI(APIEndpoint): | ||
os_map = { | ||
"ios": 1, | ||
"android": 2, | ||
"windows": 3, | ||
"macos": 4, | ||
"linux": 5, | ||
} | ||
|
||
def __init__(self, api: APISession): | ||
super().__init__(api) | ||
self.company_id = api.company_id | ||
|
||
def get_otp(self, device_id: str): | ||
""" | ||
Returns the OTP code for the specified device id. | ||
Args: | ||
device_id (str): The unique id for the enrolled device that the OTP will be obtained for. | ||
Returns: | ||
:obj:`Box`: A dictionary containing the requested OTP code for the specified device id. | ||
Examples: | ||
Obtain the OTP code for a device and print it to console: | ||
>>> otp_code = zcc.secrets.get_otp('System-Serial-Number:1234ABCDEF') | ||
... print(otp_code.otp) | ||
""" | ||
|
||
payload = {"udid": device_id} | ||
|
||
return self._get("public/v1/getOtp", params=payload) | ||
|
||
def get_passwords(self, username: str, os_type: str = "windows"): | ||
""" | ||
Return passwords for the specified username and device OS type. | ||
Args: | ||
username (str): The username that the device belongs to. | ||
os_type (str): The OS Type for the device, defaults to `windows`. Valid options are: | ||
- ios | ||
- android | ||
- windows | ||
- macos | ||
- linux | ||
Returns: | ||
:obj:`Box`: Dictionary containing passwords for the specified username's device. | ||
Examples: | ||
Print macos device passwords for username test@example.com: | ||
>>> print(zcc.secrets.get_passwords(username='test@example.com', | ||
... os_type='macos')) | ||
""" | ||
|
||
payload = { | ||
"companyId": self.company_id, | ||
} | ||
|
||
# Simplify the os_type argument, raise an error if the user supplies the wrong one. | ||
os_type = self.os_map.get(os_type, None) | ||
if not os_type: | ||
raise ValueError("Invalid os_type specified. Check the pyZscaler documentation for valid os_type options.") | ||
|
||
params = { | ||
"username": username, | ||
"osType": os_type, | ||
} | ||
|
||
return self._get("public/v1/getPasswords", data=payload, params=params) |
Oops, something went wrong.