diff --git a/Content/liquibase-pro/policy-checks/custom-policy-checks/home.htm b/Content/liquibase-pro/policy-checks/custom-policy-checks/home.htm index 6f8f4d644..a4f6d6846 100644 --- a/Content/liquibase-pro/policy-checks/custom-policy-checks/home.htm +++ b/Content/liquibase-pro/policy-checks/custom-policy-checks/home.htm @@ -8,134 +8,8 @@

Custom Policy Checks

-

Custom Policy Checks allow you to enforce compliance for a wide array of security, code standards, data quality, and other policies using Python scripts. In addition to Policy Checks built into , you can use Custom Policy Cheks to write Python scripts to create nearly any check tailored for your workflow.

-

This is a feature, so you need a License Key to use it.

-

Uses

-

provides a Library of Policy Checks, but you may find that they don't fully serve the nuanced conditions and contexts you need to evaluate all your policy enforcement goals. Custom policy checks add the flexibility and power of Python to greatly expand your ability to parse and inspect your s and databases for compliance.

-

Prerequisites

-

prerequisites

- -

Python prerequisites

-

Before creating a custom policy check with Python, we recommend being familiar with:

- -

If you're new to Python, it is a best practice to read the official Python tutorials before making custom checks.

-

Downloading Python itself is not required to create custom checks in , but it may be useful to test them. It is a best practice to test custom checks with Python 3.10.14+.

- - - - - - - - - - - - - - - - - - - - -
Compatible Versions
ToolVersion
Python3.10.14
GraalPy24.0.0
-

Create a checks settings file

-
    -
  1. Create a checks settings file if you don't already have one. In the CLI, run this command:
  2. liquibase checks show
    -
  3. If you don't have a checks settings file, a prompt appears that allows you to create the configuration file. Confirm the file creation in the prompt. By default, the settings file is named liquibase.checks-settings.conf.
  4. -
-

Create a new custom policy check

-
    -
  1. Create a new file in your working directory. This file will contain the Python script that is your custom policy check. In this example, we title our new file custom-check-no-tables.py.
  2. -
  3. Open the new custom-check-no-tables.py file and add the following custom policy check to it:
  4. - - - # import Liquibase helper scripts containing useful functions -import liquibase_utilities as lb -import sys - -# define reusable variables -obj = lb.get_database_object() # database object to examine -status = lb.get_status() # Status object of the check - -# write check logic -if lb.is_table(obj): # checks if the current object is a table - status.fired = True # indicates that the custom check has been triggered - status.message = "No tables allowed!" # message for Liquibase to return when check is triggered - sys.exit(1) # halt execution of the script - -

    The purpose of this sample check is to ensure that there are no tables in the database.

    -

    will run the check against every object in the database, so this script doesn't need a Python looping mechanism to iterate through database objects.

    -
-

Configure and customize your new check in the CLI

-
    -
  1. Initiate the customization process. In the CLI, run this command:
  2. liquibase checks customize --check-name=CustomCheckTemplate
    -

    The CLI prompts you to finish configuring your file. A message displays:

    This check cannot be customized directly because one or more fields does not have a default value.
    -

    will then create a copy of this check and initiate the customization workflow.

    -
  3. Give your check a short name for easier identification. In this example, we will name the check CustomCheckNoTables.
    Now you have successfully created the check CustomCheckNoTables from CustomCheckTemplate.
  4. -

    The new check short name CustomCheckNoTables and all of its associated information comes from the Python script you created. Your company may have their own coding standards that these scripts must adhere to.

    -
  5. Set the severity to return a code of 0-4 when triggered. In this example, we will set the severity to 1. Options:
  6. - -
  7. Set the script description for the custom check. In this example we will set the description to:
  8. This script looks to see if any tables exist and notifies you if one is detected.
    -
  9. Set the script scope for the custom check to database. The Python sample provided in this tutorial requires it.
  10. -

    In general, you should set the scope to changelog or database depending on what your custom script does:

    -
      -
    • changelog: for example, if your check looks for syntax patterns or attributes in your  . With this value, the check runs once per .
    • -
    • database: for example, if your check looks for the presence of keys, indexes, or table name patterns in your database schema. With this value, the check runs once for each database object.
    • -
    -

    It is a best practice for your custom checks to have only one scope, not both scopes.

    -
  11. -

    Set the script message. This message will display when the check is triggered. In this example we will leave this blank, as we are handling the message in the script.

    -

    Option for advanced users: You can create Script Message Variables for Custom Policy Checks which are identified in your Python script.

    -
  12. -
  13. Set the script type. In 4.29.0, Python is the only available script type.
  14. -
  15. Set the script path. This is the relative path where your script is stored, whether it is stored locally or in a repository.
  16. -

    In this example, we will set the path to Scripts/custom-check-no-tables.py.

    -
  17. Set the script argument. This allows you to pass dynamic information into the custom policy check without modifying the Python code. The script arguments ensure that your Python scripts remain reusable with different variables.
  18. -
  19. Set whether the check requires a snapshot:
  20. REQUIRES_SNAPSHOT (options: true, false) [false]:
    -

    If your check requires a snapshot, it may need to query the database, which can impact performance. The larger your database, the more performance impact this causes.

    -
-

You have now successfully created and customized a policy check!

-

Run your new check

-

To run your custom check, you must use the checks run command. provides additional security configuration parameters for this command to ensure you do not accidentally execute Python code on your database:

- -

For example, if you enable custom checks via the CLI and want to run all policy checks, including your new check:

liquibase checks run --checks-scripts-enabled=true
-

If you instead only want to run policy checks with the scope database (such as this check), you must set the --checks-scope parameter to database:

liquibase checks run --checks-scope=database --checks-scripts-enabled=true
-

If you instead only want to run this specific check, you must specify the check name with --check-name parameter:

liquibase checks run --check-name=CustomCheckNoTables --checks-scripts-enabled=true
-

Troubleshooting

-

If the scope you specify in the CLI while creating your check is mismatched with what your Python code actually does, you may receive an error like this:

Error while executing script 'custom-check-no-tables.py': AttributeError: 'NoneType' object has no attribute 'getObjectTypeName' line: 7
-

The Python code provided in this tutorial calls on database objects. This means you necessarily have to set the scope to database while you create the check. Conversely, if you are creating a check that calls on objects, you necessarily have to set the scope to changelog.

+

Custom Policy Checks are Python scripts that allow you run advanced policies using the  Policy Checks framework. Custom policy checks allow you to enforce compliance for a wide array of security, code standards, data quality, and more.

+

While it's possible to configure the behavior of many built-in policy checks, those checks have a limited scope. In contrast, you can use custom policy checks to create unique checks for any situation in your workflow.

+

This is a feature, so you need a License Key to use it.

\ No newline at end of file diff --git a/Content/liquibase-pro/policy-checks/custom-policy-checks/tutorial.htm b/Content/liquibase-pro/policy-checks/custom-policy-checks/tutorial.htm new file mode 100644 index 000000000..77dd41600 --- /dev/null +++ b/Content/liquibase-pro/policy-checks/custom-policy-checks/tutorial.htm @@ -0,0 +1,139 @@ + + + <MadCap:variable name="Heading.Level1" /> + + + + + + +

Create a Custom Policy Check

+

This tutorial shows you how to create a custom policy check. For a conceptual overview of this feature, see Custom Policy Checks.

+

This is a feature, so you need a License Key to use it.

+

Prerequisites

+

prerequisites

+ +

Python prerequisites

+

Before creating a custom policy check with Python, we recommend being familiar with:

+ +

If you're new to Python, it is a best practice to read the official Python tutorials before making custom checks.

+

Downloading Python itself is not required to create custom checks in , but it may be useful to test them. It is a best practice to test custom checks with Python 3.10.14+.

+ + + + + + + + + + + + + + + + + + + + +
Compatible Versions
ToolVersion
Python3.10.14
GraalPy24.0.0
+

Create a checks settings file

+
    +
  1. Create a checks settings file if you don't already have one. In the CLI, run this command:
  2. liquibase checks show
    +
  3. If you don't have a checks settings file, a prompt appears that allows you to create the configuration file. Confirm the file creation in the prompt. By default, the settings file is named liquibase.checks-settings.conf.
  4. +
+

Create a new custom policy check

+
    +
  1. Create a new file in your working directory. This file will contain the Python script that is your custom policy check. In this example, we title our new file custom-check-no-tables.py.
  2. +
  3. Open the new custom-check-no-tables.py file and add the following custom policy check to it:
  4. + + + # import Liquibase helper scripts containing useful functions +import liquibase_utilities as lb +import sys + +# define reusable variables +obj = lb.get_database_object() # database object to examine +status = lb.get_status() # Status object of the check + +# write check logic +if lb.is_table(obj): # checks if the current object is a table + status.fired = True # indicates that the custom check has been triggered + status.message = "No tables allowed!" # message for Liquibase to return when check is triggered + sys.exit(1) # halt execution of the script + +

    The purpose of this sample check is to ensure that there are no tables in the database.

    +

    will run the check against every object in the database, so this script doesn't need a Python looping mechanism to iterate through database objects.

    +
+

Configure and customize your new check in the CLI

+
    +
  1. Initiate the customization process. In the CLI, run this command:
  2. liquibase checks customize --check-name=CustomCheckTemplate
    +

    The CLI prompts you to finish configuring your file. A message displays:

    This check cannot be customized directly because one or more fields does not have a default value.
    +

    will then create a copy of this check and initiate the customization workflow.

    +
  3. Give your check a short name for easier identification. In this example, we will name the check CustomCheckNoTables.
    Now you have successfully created the check CustomCheckNoTables from CustomCheckTemplate.
  4. +

    The new check short name CustomCheckNoTables and all of its associated information comes from the Python script you created. Your company may have their own coding standards that these scripts must adhere to.

    +
  5. Set the severity to return a code of 0-4 when triggered. In this example, we will set the severity to 1. Options:
  6. + +
  7. Set the script description for the custom check. In this example we will set the description to:
  8. This script looks to see if any tables exist and notifies you if one is detected.
    +
  9. Set the script scope for the custom check to database. The Python sample provided in this tutorial requires it.
  10. +

    In general, you should set the scope to changelog or database depending on what your custom script does:

    +
      +
    • changelog: for example, if your check looks for syntax patterns or attributes in your  . With this value, the check runs once per .
    • +
    • database: for example, if your check looks for the presence of keys, indexes, or table name patterns in your database schema. With this value, the check runs once for each database object.
    • +
    +

    It is a best practice for your custom checks to have only one scope, not both scopes.

    +
  11. +

    Set the script message. This message will display when the check is triggered. In this example we will leave this blank, as we are handling the message in the script.

    +

    Option for advanced users: You can create Status Message Variables for Custom Policy Checks which are identified in your Python script.

    +
  12. +
  13. Set the script type. In 4.29.0, Python is the only available script type.
  14. +
  15. Set the script path. This is the relative path where your script is stored, whether it is stored locally or in a repository.
  16. +

    In this example, we will set the path to Scripts/custom-check-no-tables.py.

    +
  17. Set the script argument. This allows you to pass dynamic information into the custom policy check without modifying the Python code. The script arguments ensure that your Python scripts remain reusable with different variables.
  18. +
  19. Set whether the check requires a snapshot:
  20. REQUIRES_SNAPSHOT (options: true, false) [false]:
    +

    If your check requires a snapshot, it may need to query the database, which can impact performance. The larger your database, the more performance impact this causes.

    +
+

You have now successfully created and customized a policy check!

+

Run your new check

+

To run your custom check, you must use the checks run command. provides additional security configuration parameters for this command to ensure you do not accidentally execute Python code on your database:

+ +

For example, if you enable custom checks via the CLI and want to run all policy checks, including your new check:

liquibase checks run --checks-scripts-enabled=true
+

If you instead only want to run policy checks with the scope database (such as this check), you must set the --checks-scope parameter to database:

liquibase checks run --checks-scope=database --checks-scripts-enabled=true
+

If you instead only want to run this specific check, you must specify the check name with --check-name parameter:

liquibase checks run --check-name=CustomCheckNoTables --checks-scripts-enabled=true
+

Troubleshooting

+

If the scope you specify in the CLI while creating your check is mismatched with what your Python code actually does, you may receive an error like this:

Error while executing script 'custom-check-no-tables.py': AttributeError: 'NoneType' object has no attribute 'getObjectTypeName' line: 7
+

The Python code provided in this tutorial calls on database objects. This means you necessarily have to set the scope to database while you create the check. Conversely, if you are creating a check that calls on objects, you necessarily have to set the scope to changelog.

+ + \ No newline at end of file diff --git a/Project/TOCs/TOC.fltoc b/Project/TOCs/TOC.fltoc index b64ec18ff..da511f71c 100644 --- a/Project/TOCs/TOC.fltoc +++ b/Project/TOCs/TOC.fltoc @@ -2047,6 +2047,9 @@ +