Code refactoring (#889)

* Add __all__ to all module and plugin utils.

* Convert quite a few positional args to keyword args.

* Avoid Python 3.8+ syntax.
This commit is contained in:
Felix Fontein
2025-05-16 06:55:57 +02:00
committed by GitHub
parent a5a4e022ba
commit 44bcc8cebc
101 changed files with 1510 additions and 748 deletions

View File

@@ -53,7 +53,7 @@ def test_eckeyparse_cryptography(
fn = tmpdir / "test.pem"
fn.write(pem)
module = MagicMock()
backend = CryptographyBackend(module)
backend = CryptographyBackend(module=module)
key = backend.parse_key(key_file=str(fn))
key.pop("key_obj")
assert key == result
@@ -69,7 +69,7 @@ def test_csridentifiers_cryptography(
fn = tmpdir / "test.csr"
fn.write(csr)
module = MagicMock()
backend = CryptographyBackend(module)
backend = CryptographyBackend(module=module)
identifiers = backend.get_csr_identifiers(csr_filename=str(fn))
assert identifiers == result
identifiers = backend.get_csr_identifiers(csr_content=csr)
@@ -84,7 +84,7 @@ def test_certdays_cryptography(
fn = tmpdir / "test-cert.pem"
fn.write(TEST_CERT)
module = MagicMock()
backend = CryptographyBackend(module)
backend = CryptographyBackend(module=module)
days = backend.get_cert_days(cert_filename=str(fn), now=now)
assert days == expected_days
days = backend.get_cert_days(cert_content=TEST_CERT, now=now)
@@ -103,7 +103,7 @@ def test_get_cert_information(
fn = tmpdir / "test-cert.pem"
fn.write(cert_content)
module = MagicMock()
backend = CryptographyBackend(module)
backend = CryptographyBackend(module=module)
if CRYPTOGRAPHY_TIMEZONE:
expected_cert_info = expected_cert_info._replace(
@@ -126,7 +126,7 @@ def test_get_cert_information(
def test_now(timezone: datetime.timedelta) -> None:
with freeze_time("2024-02-03 04:05:06", tz_offset=timezone):
module = MagicMock()
backend = CryptographyBackend(module)
backend = CryptographyBackend(module=module)
now = backend.get_now()
if CRYPTOGRAPHY_TIMEZONE:
assert now.tzinfo is not None
@@ -142,7 +142,7 @@ def test_parse_acme_timestamp(
) -> None:
with freeze_time("2024-02-03 04:05:06 +00:00", tz_offset=timezone):
module = MagicMock()
backend = CryptographyBackend(module)
backend = CryptographyBackend(module=module)
ts_expected = backend.get_utc_datetime(**expected)
timestamp = backend.parse_acme_timestamp(input)
assert ts_expected == timestamp
@@ -160,9 +160,11 @@ def test_interpolate_timestamp(
) -> None:
with freeze_time("2024-02-03 04:05:06", tz_offset=timezone):
module = MagicMock()
backend = CryptographyBackend(module)
backend = CryptographyBackend(module=module)
ts_start = backend.get_utc_datetime(**start)
ts_end = backend.get_utc_datetime(**end)
ts_expected = backend.get_utc_datetime(**expected)
timestamp = backend.interpolate_timestamp(ts_start, ts_end, percentage)
timestamp = backend.interpolate_timestamp(
ts_start, ts_end, percentage=percentage
)
assert ts_expected == timestamp

View File

@@ -61,7 +61,7 @@ def test_eckeyparse_openssl(
fn.write(pem)
module = MagicMock()
module.run_command = MagicMock(return_value=(0, openssl_output, 0))
backend = OpenSSLCLIBackend(module, openssl_binary="openssl")
backend = OpenSSLCLIBackend(module=module, openssl_binary="openssl")
key = backend.parse_key(key_file=str(fn))
key.pop("key_file")
assert key == result
@@ -75,15 +75,15 @@ def test_csridentifiers_openssl(
fn.write(csr)
module = MagicMock()
module.run_command = MagicMock(return_value=(0, openssl_output, 0))
backend = OpenSSLCLIBackend(module, openssl_binary="openssl")
identifiers = backend.get_csr_identifiers(str(fn))
backend = OpenSSLCLIBackend(module=module, openssl_binary="openssl")
identifiers = backend.get_csr_identifiers(csr_filename=str(fn))
assert identifiers == result
@pytest.mark.parametrize("ip, result", TEST_IPS)
def test_normalize_ip(ip: str, result: str) -> None:
module = MagicMock()
backend = OpenSSLCLIBackend(module, openssl_binary="openssl")
backend = OpenSSLCLIBackend(module=module, openssl_binary="openssl")
assert backend._normalize_ip(ip) == result
@@ -96,7 +96,7 @@ def test_certdays_cryptography(
fn.write(TEST_CERT)
module = MagicMock()
module.run_command = MagicMock(return_value=(0, TEST_CERT_OPENSSL_OUTPUT, 0))
backend = OpenSSLCLIBackend(module, openssl_binary="openssl")
backend = OpenSSLCLIBackend(module=module, openssl_binary="openssl")
days = backend.get_cert_days(cert_filename=str(fn), now=now)
assert days == expected_days
days = backend.get_cert_days(cert_content=TEST_CERT, now=now)
@@ -116,7 +116,7 @@ def test_get_cert_information(
fn.write(cert_content)
module = MagicMock()
module.run_command = MagicMock(return_value=(0, openssl_output, 0))
backend = OpenSSLCLIBackend(module, openssl_binary="openssl")
backend = OpenSSLCLIBackend(module=module, openssl_binary="openssl")
expected_cert_info = expected_cert_info._replace(
not_valid_after=ensure_utc_timezone(expected_cert_info.not_valid_after),
@@ -136,7 +136,7 @@ def test_get_cert_information(
def test_now(timezone: datetime.timedelta) -> None:
with freeze_time("2024-02-03 04:05:06", tz_offset=timezone):
module = MagicMock()
backend = OpenSSLCLIBackend(module, openssl_binary="openssl")
backend = OpenSSLCLIBackend(module=module, openssl_binary="openssl")
now = backend.get_now()
assert now.tzinfo is not None
assert now == datetime.datetime(2024, 2, 3, 4, 5, 6, tzinfo=UTC)
@@ -148,7 +148,7 @@ def test_parse_acme_timestamp(
) -> None:
with freeze_time("2024-02-03 04:05:06", tz_offset=timezone):
module = MagicMock()
backend = OpenSSLCLIBackend(module, openssl_binary="openssl")
backend = OpenSSLCLIBackend(module=module, openssl_binary="openssl")
ts_expected = backend.get_utc_datetime(**expected)
timestamp = backend.parse_acme_timestamp(input)
assert ts_expected == timestamp
@@ -166,9 +166,11 @@ def test_interpolate_timestamp(
) -> None:
with freeze_time("2024-02-03 04:05:06", tz_offset=timezone):
module = MagicMock()
backend = OpenSSLCLIBackend(module, openssl_binary="openssl")
backend = OpenSSLCLIBackend(module=module, openssl_binary="openssl")
ts_start = backend.get_utc_datetime(**start)
ts_end = backend.get_utc_datetime(**end)
ts_expected = backend.get_utc_datetime(**expected)
timestamp = backend.interpolate_timestamp(ts_start, ts_end, percentage)
timestamp = backend.interpolate_timestamp(
ts_start, ts_end, percentage=percentage
)
assert ts_expected == timestamp

View File

@@ -23,8 +23,8 @@ from ansible_collections.community.crypto.plugins.module_utils._acme.errors impo
def test_combine_identifier() -> None:
assert combine_identifier("", "") == ":"
assert combine_identifier("a", "b") == "a:b"
assert combine_identifier(identifier_type="", identifier="") == ":"
assert combine_identifier(identifier_type="a", identifier="b") == "a:b"
def test_split_identifier() -> None:
@@ -45,7 +45,7 @@ def test_challenge_from_to_json() -> None:
"status": "valid",
}
client.version = 2
challenge = Challenge.from_json(client, data)
challenge = Challenge.from_json(client=client, data=data)
assert challenge.data == data
assert challenge.type == "type"
assert challenge.url == "xxx"
@@ -58,7 +58,7 @@ def test_challenge_from_to_json() -> None:
"status": "valid",
"token": "foo",
}
challenge = Challenge.from_json(None, data, url="xxx") # type: ignore
challenge = Challenge.from_json(client=None, data=data, url="xxx") # type: ignore
assert challenge.data == data
assert challenge.type == "type"
assert challenge.url == "xxx"
@@ -81,7 +81,7 @@ def test_authorization_from_to_json() -> None:
"value": "example.com",
},
}
authz = Authorization.from_json(client, data, "xxx")
authz = Authorization.from_json(client=client, data=data, url="xxx")
assert authz.url == "xxx"
assert authz.status == "valid"
assert authz.identifier == "example.com"
@@ -112,7 +112,7 @@ def test_authorization_from_to_json() -> None:
},
"wildcard": True,
}
authz = Authorization.from_json(client, data, "xxx")
authz = Authorization.from_json(client=client, data=data, url="xxx")
assert authz.url == "xxx"
assert authz.status == "valid"
assert authz.identifier == "*.example.com"
@@ -146,7 +146,9 @@ def test_authorization_create_error() -> None:
client.version = 2
client.directory.directory = {}
with pytest.raises(ACMEProtocolException) as exc:
Authorization.create(client, "dns", "example.com")
Authorization.create(
client=client, identifier_type="dns", identifier="example.com"
)
assert exc.value.msg == "ACME endpoint does not support pre-authorization."
@@ -197,9 +199,9 @@ def test_wait_for_validation_error() -> None:
},
}
client.get_request = MagicMock(return_value=(data, {}))
authz = Authorization.from_json(client, data, "xxx")
authz = Authorization.from_json(client=client, data=data, url="xxx")
with pytest.raises(ACMEProtocolException) as exc:
authz.wait_for_validation(client, "dns")
authz.wait_for_validation(client=client)
assert exc.value.msg == (
'Failed to validate challenge for dns:example.com: Status is "invalid". Challenge dns-01: Error dns-failed Subproblems:\n'

View File

@@ -94,7 +94,7 @@ TEST_FORMAT_ERROR_PROBLEM: list[tuple[dict[str, t.Any], str, str]] = [
def test_format_error_problem(
problem: dict[str, t.Any], subproblem_prefix: str, result: str
) -> None:
res = format_error_problem(problem, subproblem_prefix)
res = format_error_problem(problem, subproblem_prefix=subproblem_prefix)
assert res == result
@@ -358,7 +358,7 @@ def test_acme_protocol_exception(
module = MagicMock()
module.from_json = from_json
with pytest.raises(ACMEProtocolException) as exc:
raise ACMEProtocolException(module, **input) # type: ignore
raise ACMEProtocolException(module=module, **input) # type: ignore
print(exc.value.msg)
print(exc.value.module_fail_args)

View File

@@ -27,5 +27,5 @@ def test_read_file(tmpdir) -> None:
def test_write_file(tmpdir) -> None:
fn = tmpdir / "test.txt"
module = MagicMock()
write_file(module, str(fn), TEST_TEXT.encode("utf-8"))
write_file(module=module, dest=str(fn), content=TEST_TEXT.encode("utf-8"))
assert fn.read() == TEST_TEXT

View File

@@ -24,7 +24,7 @@ def test_order_from_json() -> None:
"authorizations": [],
}
client.version = 2
order = Order.from_json(client, data, "xxx")
order = Order.from_json(client=client, data=data, url="xxx")
assert order.data == data
assert order.url == "xxx"
assert order.status == "valid"
@@ -44,11 +44,11 @@ def test_wait_for_finalization_error() -> None:
"identifiers": [],
"authorizations": [],
}
order = Order.from_json(client, data, "xxx")
order = Order.from_json(client=client, data=data, url="xxx")
client.get_request = MagicMock(return_value=(data, {}))
with pytest.raises(ACMEProtocolException) as exc:
order.wait_for_finalization(client)
order.wait_for_finalization(client=client)
assert exc.value.msg.startswith(
'Failed to wait for order to complete; got status "invalid". The JSON result: '

View File

@@ -103,7 +103,8 @@ def test_nopad_b64(value: str, result: str) -> None:
def test_pem_to_der(pem: str, der: bytes, tmpdir):
fn = tmpdir / "test.pem"
fn.write(pem)
assert pem_to_der(str(fn)) == der
assert pem_to_der(pem_filename=str(fn)) == der
assert pem_to_der(pem_content=pem) == der
@pytest.mark.parametrize("value, expected_result", TEST_LINKS_HEADER)
@@ -115,7 +116,7 @@ def test_process_links(
def callback(url, rel):
data.append((url, rel))
process_links(value, callback)
process_links(info=value, callback=callback)
assert expected_result == data

View File

@@ -40,27 +40,27 @@ def test_adjust_idn(unicode: str, idna: str, cycled_unicode: str | None) -> None
if cycled_unicode is None:
cycled_unicode = unicode
result = _adjust_idn(unicode, "ignore")
result = _adjust_idn(unicode, idn_rewrite="ignore")
print(result, unicode)
assert result == unicode
result = _adjust_idn(idna, "ignore")
result = _adjust_idn(idna, idn_rewrite="ignore")
print(result, idna)
assert result == idna
result = _adjust_idn(unicode, "unicode")
result = _adjust_idn(unicode, idn_rewrite="unicode")
print(result, unicode)
assert result == unicode
result = _adjust_idn(idna, "unicode")
result = _adjust_idn(idna, idn_rewrite="unicode")
print(result, cycled_unicode)
assert result == cycled_unicode
result = _adjust_idn(unicode, "idna")
result = _adjust_idn(unicode, idn_rewrite="idna")
print(result, idna)
assert result == idna
result = _adjust_idn(idna, "idna")
result = _adjust_idn(idna, idn_rewrite="idna")
print(result, idna)
assert result == idna
@@ -74,7 +74,7 @@ def test_adjust_idn(unicode: str, idna: str, cycled_unicode: str | None) -> None
def test_adjust_idn_fail_valueerror(value: str, idn_rewrite: str, message: str) -> None:
with pytest.raises(ValueError, match=message):
idn_rewrite_: t.Literal["ignore", "idna", "unicode"] = idn_rewrite # type: ignore
_adjust_idn(value, idn_rewrite_)
_adjust_idn(value, idn_rewrite=idn_rewrite_)
@pytest.mark.parametrize(
@@ -93,7 +93,7 @@ def test_adjust_idn_fail_valueerror(value: str, idn_rewrite: str, message: str)
def test_adjust_idn_fail_user_error(value: str, idn_rewrite: str, message: str) -> None:
with pytest.raises(OpenSSLObjectError, match=message):
idn_rewrite_: t.Literal["ignore", "idna", "unicode"] = idn_rewrite # type: ignore
_adjust_idn(value, idn_rewrite_)
_adjust_idn(value, idn_rewrite=idn_rewrite_)
def test_cryptography_get_name_invalid_prefix() -> None:

View File

@@ -27,7 +27,7 @@ from ansible_collections.community.crypto.plugins.module_utils._crypto.math impo
],
)
def test_binary_exp_mod(f: int, e: int, m: int, result: int) -> None:
value = binary_exp_mod(f, e, m)
value = binary_exp_mod(f, e, m=m)
print(value)
assert value == result

View File

@@ -121,16 +121,30 @@ INVALID_DATA = (
b"yDspTN+BJzvIK2Q+CRD3qBDVSi+YqSxwyz432VEaHKlXbuLURirY0QpuBCqgR6tCtWW5vEGkXKZ3"
)
VALID_OPTS = [OpensshCertificateOption("critical", "force-command", "/usr/bin/csh")]
INVALID_OPTS = [OpensshCertificateOption("critical", "test", "undefined")]
VALID_EXTENSIONS = [
OpensshCertificateOption("extension", "permit-x11-forwarding", ""),
OpensshCertificateOption("extension", "permit-agent-forwarding", ""),
OpensshCertificateOption("extension", "permit-port-forwarding", ""),
OpensshCertificateOption("extension", "permit-pty", ""),
OpensshCertificateOption("extension", "permit-user-rc", ""),
VALID_OPTS = [
OpensshCertificateOption(
option_type="critical", name="force-command", data="/usr/bin/csh"
)
]
INVALID_OPTS = [
OpensshCertificateOption(option_type="critical", name="test", data="undefined")
]
VALID_EXTENSIONS = [
OpensshCertificateOption(
option_type="extension", name="permit-x11-forwarding", data=""
),
OpensshCertificateOption(
option_type="extension", name="permit-agent-forwarding", data=""
),
OpensshCertificateOption(
option_type="extension", name="permit-port-forwarding", data=""
),
OpensshCertificateOption(option_type="extension", name="permit-pty", data=""),
OpensshCertificateOption(option_type="extension", name="permit-user-rc", data=""),
]
INVALID_EXTENSIONS = [
OpensshCertificateOption(option_type="extension", name="test", data="")
]
INVALID_EXTENSIONS = [OpensshCertificateOption("extension", "test", "")]
VALID_TIME_PARAMETERS: list[
tuple[int | str, int | str, str, int, int | str, str, str, int, str]
@@ -249,22 +263,36 @@ INVALID_VALIDITY_TEST: list[tuple[str, str, str]] = [
VALID_OPTIONS: list[tuple[str, OpensshCertificateOption]] = [
(
"force-command=/usr/bin/csh",
OpensshCertificateOption("critical", "force-command", "/usr/bin/csh"),
OpensshCertificateOption(
option_type="critical", name="force-command", data="/usr/bin/csh"
),
),
(
"Force-Command=/Usr/Bin/Csh",
OpensshCertificateOption("critical", "force-command", "/Usr/Bin/Csh"),
OpensshCertificateOption(
option_type="critical", name="force-command", data="/Usr/Bin/Csh"
),
),
(
"permit-x11-forwarding",
OpensshCertificateOption("extension", "permit-x11-forwarding", ""),
OpensshCertificateOption(
option_type="extension", name="permit-x11-forwarding", data=""
),
),
(
"permit-X11-forwarding",
OpensshCertificateOption("extension", "permit-x11-forwarding", ""),
OpensshCertificateOption(
option_type="extension", name="permit-x11-forwarding", data=""
),
),
(
"critical:foo=bar",
OpensshCertificateOption(option_type="critical", name="foo", data="bar"),
),
(
"extension:foo",
OpensshCertificateOption(option_type="extension", name="foo", data=""),
),
("critical:foo=bar", OpensshCertificateOption("critical", "foo", "bar")),
("extension:foo", OpensshCertificateOption("extension", "foo", "")),
]
INVALID_OPTIONS: list[str | list] = [
@@ -368,19 +396,21 @@ def test_valid_time_parameters(
@pytest.mark.parametrize("valid_from,valid_to", INVALID_TIME_PARAMETERS)
def test_invalid_time_parameters(valid_from: int | str, valid_to: int | str) -> None:
with pytest.raises(ValueError):
OpensshCertificateTimeParameters(valid_from, valid_to)
OpensshCertificateTimeParameters(valid_from=valid_from, valid_to=valid_to)
@pytest.mark.parametrize("valid_from,valid_to,valid_at", VALID_VALIDITY_TEST)
def test_valid_validity_test(valid_from: str, valid_to: str, valid_at: str) -> None:
assert OpensshCertificateTimeParameters(valid_from, valid_to).within_range(valid_at)
assert OpensshCertificateTimeParameters(
valid_from=valid_from, valid_to=valid_to
).within_range(valid_at)
@pytest.mark.parametrize("valid_from,valid_to,valid_at", INVALID_VALIDITY_TEST)
def test_invalid_validity_test(valid_from: str, valid_to: str, valid_at: str) -> None:
assert not OpensshCertificateTimeParameters(valid_from, valid_to).within_range(
valid_at
)
assert not OpensshCertificateTimeParameters(
valid_from=valid_from, valid_to=valid_to
).within_range(valid_at)
@pytest.mark.parametrize("option_string,option_object", VALID_OPTIONS)

View File

@@ -66,7 +66,10 @@ def test_parse_openssh_version() -> None:
@pytest.mark.parametrize("boolean", VALID_BOOLEAN)
def test_valid_boolean(boolean: bool) -> None:
assert OpensshParser(_OpensshWriter().boolean(boolean).bytes()).boolean() == boolean
assert (
OpensshParser(data=_OpensshWriter().boolean(boolean).bytes()).boolean()
== boolean
)
@pytest.mark.parametrize("boolean", INVALID_BOOLEAN)
@@ -77,7 +80,9 @@ def test_invalid_boolean(boolean: t.Any) -> None:
@pytest.mark.parametrize("uint32", VALID_UINT32)
def test_valid_uint32(uint32: int) -> None:
assert OpensshParser(_OpensshWriter().uint32(uint32).bytes()).uint32() == uint32
assert (
OpensshParser(data=_OpensshWriter().uint32(uint32).bytes()).uint32() == uint32
)
@pytest.mark.parametrize("uint32", INVALID_UINT32)
@@ -88,7 +93,9 @@ def test_invalid_uint32(uint32: int) -> None:
@pytest.mark.parametrize("uint64", VALID_UINT64)
def test_valid_uint64(uint64: int) -> None:
assert OpensshParser(_OpensshWriter().uint64(uint64).bytes()).uint64() == uint64
assert (
OpensshParser(data=_OpensshWriter().uint64(uint64).bytes()).uint64() == uint64
)
@pytest.mark.parametrize("uint64", INVALID_UINT64)
@@ -100,7 +107,7 @@ def test_invalid_uint64(uint64: int) -> None:
@pytest.mark.parametrize("ssh_string", VALID_STRING)
def test_valid_string(ssh_string: bytes) -> None:
assert (
OpensshParser(_OpensshWriter().string(ssh_string).bytes()).string()
OpensshParser(data=_OpensshWriter().string(ssh_string).bytes()).string()
== ssh_string
)
@@ -113,7 +120,7 @@ def test_invalid_string(ssh_string: t.Any) -> None:
@pytest.mark.parametrize("mpint", VALID_MPINT)
def test_valid_mpint(mpint: int) -> None:
assert OpensshParser(_OpensshWriter().mpint(mpint).bytes()).mpint() == mpint
assert OpensshParser(data=_OpensshWriter().mpint(mpint).bytes()).mpint() == mpint
@pytest.mark.parametrize("mpint", INVALID_MPINT)
@@ -124,7 +131,7 @@ def test_invalid_mpint(mpint: t.Any) -> None:
def test_valid_seek() -> None:
buffer = bytearray(b"buffer")
parser = OpensshParser(buffer)
parser = OpensshParser(data=buffer)
parser.seek(len(buffer))
assert parser.remaining_bytes() == 0
parser.seek(-len(buffer))
@@ -133,7 +140,7 @@ def test_valid_seek() -> None:
def test_invalid_seek() -> None:
buffer = b"buffer"
parser = OpensshParser(buffer)
parser = OpensshParser(data=buffer)
with pytest.raises(ValueError):
parser.seek(len(buffer) + 1)
@@ -144,4 +151,4 @@ def test_invalid_seek() -> None:
def test_writer_bytes() -> None:
buffer = bytearray(b"buffer")
assert _OpensshWriter(buffer).bytes() == buffer
assert _OpensshWriter(buffer=buffer).bytes() == buffer

View File

@@ -383,7 +383,7 @@ def test_get_relative_time_option(
with freeze_time("2024-02-03 04:05:06", tz_offset=timezone):
output = get_relative_time_option(
input_string,
input_name,
input_name=input_name,
with_timezone=with_timezone,
now=now,
)