GeneratePassword

This function generates a password and allows various parameters to customize the properties of the password depending on the use case (e.g. password complexity requirements). The default behavior is to generate a password of *random length* including all four character classes (upper, lower, digits, symbols) with at least five and at most ten characters per class. The min_* values all default to 0. This means that if the command is executed in this way: !GeneratePassword max_lcase=10 It is possible that a password of length zero could be generated. It is therefore recommended to always include a min_* parameter that matches. The debug parameter will print certain properties of the command into the WarRoom for easy diagnostics.

python · Common Scripts

Details

IDGeneratePassword
Languagepython
From Version5.0.0
Docker Imagedemisto/python3:3.12.13.10404775
TagsUtility

README

Generates a password and allows various parameters to customize the properties of the password depending on the use case. For example, “password complexity requirements”. The default behavior is to generate a password of random length including all four character classes, upper, lower, digits, and symbols, with at least five and at most ten characters per class.

The min_* values all default to 0.

This means that if the command is executed in this way:
!GeneratePassword max_lcase=10
It is possible that a password of length zero could be generated. It is therefore recommended to always include a min_* parameter that matches.

The debug parameter will print certain properties of the command into the War Room for easy diagnostics.

Script Data


Name Description
Script Type python
Tags Utility

Inputs


Argument Name Description
min_lcase The minimum number of lower case characters to include in password.
max_lcase The maximum number of lower case characters to include in password.
min_ucase The minimum number of upper case characters to include in password.
max_ucase The maximum number of upper case characters to include in password.
min_digits The minimum number of digits to include in password.
max_digits The maximum number of digits to include in password.
min_symbols The minimum number of symbols to include in password.
max_symbols The maximum number of symbols to include in password.
debug Sees various values as they pass through the function if enabled.

Note:
At least one of the following arguments should be above 0: min_uppercase, min_lowercase, min_digits, min_symbols

Outputs


Path Description Type
NEW_PASSWORD The new password generated for the user. Unknown
import pytest
from CommonServerPython import DemistoException
from GeneratePassword import SYMBOLS, generate_password


def does_password_meet_requirement(
    password: str,
    min_lowercase: int,
    max_lowercase: int,
    min_uppercase: int,
    max_uppercase: int,
    min_digits: int,
    max_digits: int,
    min_symbols: int,
    max_symbols: int,
) -> bool:
    lowercase_count = sum(1 for char in password if char.islower())
    uppercase_count = sum(1 for char in password if char.isupper())
    digit_count = sum(1 for char in password if char.isdigit())
    symbol_count = sum(1 for char in password if char in SYMBOLS)

    return all(
        [
            min_lowercase <= lowercase_count <= max_lowercase,
            min_uppercase <= uppercase_count <= max_uppercase,
            min_digits <= digit_count <= max_digits,
            min_symbols <= symbol_count <= max_symbols,
        ]
    )


@pytest.mark.parametrize(
    "min_lowercase, max_lowercase, min_uppercase, max_uppercase, min_digits, max_digits, min_symbols, max_symbols",
    [
        (1, 2, 1, 2, 1, 2, 1, 2),  # Test case with all ranges set to 1-2
        (2, 5, 3, 5, 4, 6, 1, 3),  # Test case with various ranges
        (2, 5, 3, 5, 4, 10, 0, 0),  # Test case with no symbols
    ],
)
def test_generate_password(
    min_lowercase: int,
    max_lowercase: int,
    min_uppercase: int,
    max_uppercase: int,
    min_digits: int,
    max_digits: int,
    min_symbols: int,
    max_symbols: int,
):
    args = {
        "debug": "true",
        "min_lcase": min_lowercase,
        "max_lcase": max_lowercase,
        "min_ucase": min_uppercase,
        "max_ucase": max_uppercase,
        "min_digits": min_digits,
        "max_digits": max_digits,
        "min_symbols": min_symbols,
        "max_symbols": max_symbols,
    }
    result = generate_password(args)
    pwd = result.outputs
    assert isinstance(pwd, str)
    assert does_password_meet_requirement(
        pwd,
        min_lowercase,
        max_lowercase,
        min_uppercase,
        max_uppercase,
        min_digits,
        max_digits,
        min_symbols,
        max_symbols,
    )


@pytest.mark.parametrize(
    "min_lowercase, max_lowercase, min_uppercase, max_uppercase, min_digits, max_digits, min_symbols, max_symbols, exception",
    [
        (
            0,
            5,
            0,
            5,
            0,
            5,
            0,
            5,
            "error: At least one of the following arguments should be above 0",
        ),  # Test case with all ranges set to 0-5
        (-3, 5, 0, 5, 0, 5, 0, 5, "All numeral arguments must be positive."),
    ],
)
def test_generate_password_zero_inputs(
    min_lowercase: int,
    max_lowercase: int,
    min_uppercase: int,
    max_uppercase: int,
    min_digits: int,
    max_digits: int,
    min_symbols: int,
    max_symbols: int,
    exception: str,
):
    args = {
        "debug": "true",
        "min_lcase": min_lowercase,
        "max_lcase": max_lowercase,
        "min_ucase": min_uppercase,
        "max_ucase": max_uppercase,
        "min_digits": min_digits,
        "max_digits": max_digits,
        "min_symbols": min_symbols,
        "max_symbols": max_symbols,
    }
    with pytest.raises(DemistoException, match=exception):
        generate_password(args)