import random
import string


def _generate_password(length=16):
    """
    Generate password of specified length with at least one digit,
    uppercase letter, and lowercase letter. This is used as the
    IKEv2 PSK on both sides of the tunnel.
    """

    # Need 1 each: uppercase, lowercase, digit
    pw_minimum = [
        random.choice(string.digits),
        random.choice(string.ascii_uppercase),
        random.choice(string.ascii_lowercase),
    ]

    # Fill in the remaining N-3 characters
    pw_rest = [
        random.choice(string.digits + string.ascii_letters)
        for i in range(length - 3)
    ]

    # Randomize the letters and return the password as a string
    pw_list = pw_minimum + pw_rest
    random.shuffle(pw_list)
    return "".join(pw_list)