policies

The following methods allow for interaction with the Zscaler AI Guard Detection Policies API endpoints. Includes listing, creating, updating, and deleting detection policies, and retrieving a policy by ID or name.

Methods are accessible via aiguard.policies

Copyright (c) 2023, Zscaler Inc.

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

class PoliciesAPI

Bases: APIClient

A Client object for the AI Guard Detection Policies resource.

add_policy(**kwargs)

Creates a new detection policy.

Parameters:
  • name (str) – The name of the detection policy.

  • **kwargs – Optional keyword args.

Keyword Arguments:
  • version (str) – The version for this detection policy.

  • description (str) – Additional information about the detection policy.

  • input_detector_policies (str) – The input detector policies for this detection policy.

  • output_detector_policies (str) – The output detector policies for this detection policy.

Returns:

A tuple containing the newly added Policies instance, response, and error.

Return type:

tuple

Examples

Add a new detection policy with input and output detectors:

>>> added_policy, _, error = client.aiguard.policies.add_policy(
...     name="PolicyRule01",
...     inputDetectorPolicies=[
...         {
...             "detector": "toxicity",
...             "enabled": True,
...             "severity": "HIGH",
...             "configuration": {"action": "BLOCK", "threshold": 0.87},
...         },
...         {
...             "detector": "prompt_injection",
...             "enabled": True,
...             "severity": "CRITICAL",
...             "configuration": {"action": "BLOCK", "threshold": 0.75},
...         },
...     ],
...     outputDetectorPolicies=[
...         {
...             "detector": "pii",
...             "enabled": False,
...             "severity": "CRITICAL",
...             "configuration": {
...                 "entities": [
...                     {"action": "BLOCK", "entityType": "CREDIT_CARD"},
...                     {"action": "BLOCK", "entityType": "US_SSN"},
...                     {"action": "DETECT", "entityType": "EMAIL_ADDRESS"},
...                 ],
...                 "threshold": 0.5,
...                 "anonymization": "NONE",
...                 "defaultAction": "BLOCK",
...                 "replaceWithMaskedContent": False,
...             },
...         },
...     ],
... )
>>> if error:
...     print(f"Error adding detection policy: {error}")
...     return
... print(f"Detection policy added successfully: {added_policy.as_dict()}")
delete_policy(policy_id)

Deletes the specified detection policy.

Parameters:

policy_id (int) – The unique identifier for the detection policy.

Returns:

A tuple containing the response object and error (if any).

Return type:

tuple

Examples

Delete a detection policy:

>>> _, _, error = client.aiguard.policies.delete_policy(1013)
>>> if error:
...     print(f"Error deleting detection policy: {error}")
...     return
... print(f"Detection policy deleted successfully.")
get_policy(policy_id)

Fetches a specific detection policy by ID.

Parameters:

policy_id (int) – The unique identifier for the detection policy.

Returns:

A tuple containing (Policies instance, Response, error).

Return type:

tuple

Examples

Print a specific detection policy:

>>> fetched_policy, _, error = client.aiguard.policies.get_policy(1013)
>>> if error:
...     print(f"Error fetching detection policy by ID: {error}")
...     return
... print(f"Fetched detection policy by ID: {fetched_policy.as_dict()}")
get_policy_by_name(name)

Fetches a specific detection policy by name.

Parameters:

name (str) – The name of the detection policy.

Returns:

A tuple containing (Policies instance, Response, error).

Return type:

tuple

Examples

Print a specific detection policy by name:

>>> fetched_policy, _, error = client.aiguard.policies.get_policy_by_name('Policy01')
>>> if error:
...     print(f"Error fetching detection policy by name: {error}")
...     return
... print(f"Fetched detection policy by name: {fetched_policy.as_dict()}")
list_policies(query_params=None)

Lists the detection policies configured in your organization.

Parameters:

{dict} (query_params) – Map of query parameters for the request.

Returns:

A tuple containing (list of Policies instances, Response, error)

Return type:

tuple

Examples

List detection policies:

>>> policy_list, _, error = client.aiguard.policies.list_policies()
>>> if error:
...     print(f"Error listing detection policies: {error}")
...     return
... print(f"Total detection policies found: {len(policy_list)}")
... for policy in policy_list:
...     print(policy.as_dict())

Client-side filtering with JMESPath:

The response object supports client-side filtering and projection via resp.search(expression). See the JMESPath documentation for expression syntax.

update_policy(policy_id, **kwargs)

Updates information for the specified detection policy.

Parameters:

policy_id (int) – The unique identifier for the detection policy.

Keyword Arguments:
  • name (str) – The name of the detection policy.

  • version (str) – The version for this detection policy.

  • description (str) – Additional information about the detection policy.

  • input_detector_policies (str) – The input detector policies for this detection policy.

  • output_detector_policies (str) – The output detector policies for this detection policy.

Returns:

A tuple containing the updated Policies instance, response, and error.

Return type:

tuple

Examples

Update an existing detection policy. The update replaces the policy, so the detector lists are sent in full:

>>> updated_policy, _, error = client.aiguard.policies.update_policy(
...     policy_id=2916,
...     name="PolicyRule01",
...     inputDetectorPolicies=[
...         {
...             "detector": "toxicity",
...             "enabled": True,
...             "severity": "HIGH",
...             "configuration": {"action": "BLOCK", "threshold": 0.87},
...         },
...     ],
...     outputDetectorPolicies=[
...         {
...             "detector": "toxicity",
...             "enabled": True,
...             "severity": "CRITICAL",
...             "configuration": {"action": "BLOCK", "threshold": 0.87},
...         },
...     ],
... )
>>> if error:
...     print(f"Error updating detection policy: {error}")
...     return
... print(f"Detection policy updated successfully: {updated_policy.as_dict()}")