From 39bc21b21aa8ce9f28fd929058fc69cf57f017ba Mon Sep 17 00:00:00 2001 From: caleb Date: Tue, 17 Nov 2026 12:24:48 -0500 Subject: [PATCH 1/3] Replace flake8 with ruff for linting Ruff runs the same pyflakes and pycodestyle rules as flake8 and passes on the current code. Its settings now live in pyproject.toml, so tox.ini is no longer needed. The job keeps the name lint so existing required checks still match. --- .github/workflows/lint.yml | 15 +++------------ pyproject.toml | 7 +++++++ tox.ini | 3 --- 3 files changed, 10 insertions(+), 15 deletions(-) delete mode 100644 tox.ini diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 1c70454..91cc08f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,4 +1,4 @@ -name: flake8 +name: ruff check on: pull_request: @@ -10,14 +10,5 @@ jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v3 - - name: Set up Python 3.11 - uses: actions/setup-python@v3 - with: - python-version: 3.11 - - name: Install dependencies - run: | - python -m pip install --upgrade pip - pip install flake8 - - name: Analyze the code with flake8 - run: flake8 . + - uses: actions/checkout@v4 + - uses: astral-sh/ruff-action@v4.1.0 diff --git a/pyproject.toml b/pyproject.toml index 74a0a18..3f9f2d4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -28,3 +28,10 @@ include = ["truenas_api_client*"] [tool.mypy] check_untyped_defs = true + +[tool.ruff.lint] +select = ["E", "W", "F"] +ignore = ["F403", "F405"] + +[tool.ruff.lint.pycodestyle] +max-line-length = 120 diff --git a/tox.ini b/tox.ini deleted file mode 100644 index b31d7aa..0000000 --- a/tox.ini +++ /dev/null @@ -1,3 +0,0 @@ -[flake8] -ignore = F403,F405,W504 -max-line-length = 120 From debb1ae5bd467cfcaeb54dc4c5eb231d3e58dd53 Mon Sep 17 00:00:00 2001 From: caleb Date: Tue, 17 Nov 2026 12:24:48 -0500 Subject: [PATCH 2/3] Format the code with ruff This is the output of ruff format with its default settings. One type ignore comment moved with its argument so mypy still applies it. --- README.md | 29 +- examples/create_smb_time_machine_share.py | 9 +- .../two_factor_challenge_response_auth.py | 23 +- tests/scram/_cb_test_vectors.py | 240 ++--- tests/scram/test_compatibility.py | 73 +- tests/scram/test_py_scram.py | 198 ++-- tests/scram/test_scram_client.py | 121 ++- tests/test_auth_api_key.py | 157 +-- tests/test_close_race.py | 5 +- tests/test_ejson.py | 9 +- tests/test_job_disconnect.py | 68 +- tests/test_job_leak.py | 81 +- tests/test_midclt_login_failure.py | 53 +- tests/test_reserved_ports_tls.py | 46 +- truenas_api_client/__init__.py | 890 +++++++++++------- truenas_api_client/auth_api_key.py | 214 +++-- truenas_api_client/ejson.py | 52 +- truenas_api_client/exc.py | 19 +- truenas_api_client/jsonrpc.py | 32 +- truenas_api_client/legacy.py | 333 ++++--- truenas_api_client/py_scram/README.md | 4 +- truenas_api_client/py_scram/__init__.py | 82 +- .../py_scram/channel_binding.py | 90 +- truenas_api_client/py_scram/client_final.py | 116 ++- truenas_api_client/py_scram/client_first.py | 30 +- truenas_api_client/py_scram/common.py | 59 +- truenas_api_client/py_scram/error.py | 20 +- truenas_api_client/py_scram/scram_crypto.py | 106 ++- truenas_api_client/py_scram/server_final.py | 68 +- truenas_api_client/py_scram/server_first.py | 60 +- truenas_api_client/py_scram/verify.py | 50 +- truenas_api_client/scram_impl.py | 86 +- truenas_api_client/utils.py | 33 +- 33 files changed, 2024 insertions(+), 1432 deletions(-) diff --git a/README.md b/README.md index 889c41d..a5e52ab 100644 --- a/README.md +++ b/README.md @@ -136,9 +136,14 @@ The TrueNAS API client can also be used in Python scripts. from truenas_api_client import Client with Client() as c: - user = {"full_name": "John Doe", "username": "jdoe", "password": "Canary", "group_create": True} - new_user_entry = c.call("user.create", user) - print(new_user_entry["full_name"]) # John Doe + user = { + "full_name": "John Doe", + "username": "jdoe", + "password": "Canary", + "group_create": True, + } + new_user_entry = c.call("user.create", user) + print(new_user_entry["full_name"]) # John Doe ``` #### Login with a user account or an API key @@ -146,15 +151,15 @@ with Client() as c: ```python # User account with Client(uri="ws://some.other.truenas/api/current") as c: - c.login_with_password(username, password) + c.login_with_password(username, password) # API key with Client(uri="ws://some.other.truenas/api/current") as c: - c.login_with_api_key(username, key) + c.login_with_api_key(username, key) # API key file with Client(uri="ws://some.other.truenas/api/current") as c: - c.login_with_api_key(username, "/path/to/keyfile.json") + c.login_with_api_key(username, "/path/to/keyfile.json") ``` API-key login uses **SCRAM-SHA-512** by default (TrueNAS 26 and later), which never transmits @@ -167,7 +172,7 @@ otherwise strip SCRAM to force the client to divulge the cleartext key: from truenas_api_client import APIKeyAuthMech with Client(uri="ws://old.truenas/api/current") as c: - c.login_with_api_key(username, key, auth_mechanism=APIKeyAuthMech.PLAIN) + c.login_with_api_key(username, key, auth_mechanism=APIKeyAuthMech.PLAIN) ``` The `midclt` CLI exposes this as `--plain`. **Warning:** PLAIN transmits the raw API key; only @@ -189,7 +194,7 @@ Pass `channel_binding=False` to authenticate without it: ```python # Reach a server without channel-binding support, or a plain ws:// endpoint with Client(uri="wss://some.other.truenas/api/current") as c: - c.login_with_api_key(username, key, channel_binding=False) + c.login_with_api_key(username, key, channel_binding=False) ``` The `midclt` CLI exposes the same override as `--no-channel-binding`. @@ -252,10 +257,10 @@ For single-section files, any section name is accepted. Files without section he ```python with Client() as c: - is_locked = c.call("pool.dataset.lock", "mypool/mydataset", job=True) - if is_locked: - args = {"datasets": [{"name": "mypool/mydataset", "passphrase": "passphrase"}]} - c.call("pool.dataset.unlock", "mypool/mydataset", args, job=True) + is_locked = c.call("pool.dataset.lock", "mypool/mydataset", job=True) + if is_locked: + args = {"datasets": [{"name": "mypool/mydataset", "passphrase": "passphrase"}]} + c.call("pool.dataset.unlock", "mypool/mydataset", args, job=True) ``` ## API Rate Limits diff --git a/examples/create_smb_time_machine_share.py b/examples/create_smb_time_machine_share.py index 6cd10e7..f1aa6e5 100644 --- a/examples/create_smb_time_machine_share.py +++ b/examples/create_smb_time_machine_share.py @@ -20,8 +20,7 @@ ds = c.call("pool.dataset.create", {"name": DS_NAME, "share_type": "SMB"}) # Create the actual SMB share for time machine purposes - c.call("sharing.smb.create", { - "path": ds["mountpoint"], - "name": SHARE_NAME, - "purpose": "TIMEMACHINE" - }) + c.call( + "sharing.smb.create", + {"path": ds["mountpoint"], "name": SHARE_NAME, "purpose": "TIMEMACHINE"}, + ) diff --git a/examples/two_factor_challenge_response_auth.py b/examples/two_factor_challenge_response_auth.py index de391ea..c9cccd4 100644 --- a/examples/two_factor_challenge_response_auth.py +++ b/examples/two_factor_challenge_response_auth.py @@ -17,8 +17,8 @@ def get_totp_secret() -> str: - """ This assumes the TOTP secret is written somewhere on client """ - with open(TOTP_FILE, 'r') as f: + """This assumes the TOTP secret is written somewhere on client""" + with open(TOTP_FILE, "r") as f: return f.read() @@ -28,11 +28,10 @@ def get_2fa_token(secret: str) -> str: def authenticate_client(c: truenas_api_client.Client) -> bool: - resp = c.call("auth.login_ex", { - "mechanism": "PASSWORD_PLAIN", - "username": USERNAME, - "password": PASSWORD - }) + resp = c.call( + "auth.login_ex", + {"mechanism": "PASSWORD_PLAIN", "username": USERNAME, "password": PASSWORD}, + ) match resp["response_type"]: case "SUCCESS": @@ -51,17 +50,17 @@ def authenticate_client(c: truenas_api_client.Client) -> bool: else: otp_token = get_2fa_token(secret) - resp = c.call("auth.login_ex_continue", { - "mechanism": "OTP_TOKEN", - "otp_token": otp_token - }) + resp = c.call( + "auth.login_ex_continue", + {"mechanism": "OTP_TOKEN", "otp_token": otp_token}, + ) # For interactive session the `auth.login_ex_continue` may # reply with OTP_REQUIRED again if the user fat-fingered input # since this is machine input no amount of attempts will succeed return resp["response_type"] == "SUCCESS" case _: - raise ValueError(f'{resp["response_type"]}: Unexpected response type') + raise ValueError(f"{resp['response_type']}: Unexpected response type") if not interactive: diff --git a/tests/scram/_cb_test_vectors.py b/tests/scram/_cb_test_vectors.py index c54d069..760c2aa 100644 --- a/tests/scram/_cb_test_vectors.py +++ b/tests/scram/_cb_test_vectors.py @@ -6,120 +6,144 @@ lives in the signature parameters), DSA, ECDSA-SHA384, and SHA-1 (promoted to SHA-256 per RFC 5929 4.1). py_scram and truenas_pyscram both equal `expected`. """ + from base64 import b64decode CB_VECTORS = [ - ("rsa_pss_sha256", b64decode( - "MIIDezCCAi+gAwIBAgIUbvFLoNTxKxJMSKWyAwDtovtPfIwwQQYJKoZIhvcNAQEKMDSgDzAN" - "BglghkgBZQMEAgEFAKEcMBoGCSqGSIb3DQEBCDANBglghkgBZQMEAgEFAKIDAgEgMBkxFzAV" - "BgNVBAMMDnJzYV9wc3Nfc2hhMjU2MB4XDTI2MDYyMjIwNTkzNloXDTM2MDYxOTIwNTkzNlow" - "GTEXMBUGA1UEAwwOcnNhX3Bzc19zaGEyNTYwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK" - "AoIBAQDpa19BgN87rCwx3j+OkZN2JvpBcM7OQF3O0otYaHePBaGuMYNBApo0PqBp9E1oakm7" - "ZktqSVpANOp/G4b4+VNIEEi2AO8AUU+hJgw1t2xlLzzxlynstRVVaB3RqYhSdC4HJaBeCC5O" - "fZ9c14qjzIj6jQANpO/bb2N2aPr5uwSViLiV3MXvN8+g5pXzlSbdRPJyqJdvLgXHA/tS3axR" - "DHb6P/loIhrE8yaWMJvY1wpp856+WwOu8mVSTwMv0mhScZnJ5BtF3UJJyPjIY8B/VSK189Sj" - "PidhhXZNk8+1QaHCe4RwuFnMUybGoOmcTBaXJPDErfwt6pGD0ZcjXnW7myUFAgMBAAGjUzBR" - "MB0GA1UdDgQWBBTuhQOK9oKQhMFCzFx+tYgScnTgHDAfBgNVHSMEGDAWgBTuhQOK9oKQhMFC" - "zFx+tYgScnTgHDAPBgNVHRMBAf8EBTADAQH/MEEGCSqGSIb3DQEBCjA0oA8wDQYJYIZIAWUD" - "BAIBBQChHDAaBgkqhkiG9w0BAQgwDQYJYIZIAWUDBAIBBQCiAwIBIAOCAQEAob7QddtJ12v7" - "b5RNPSzEP+2GD+1nUGQkOa1lfBzTP0jzvO2f/fgiybKCvZtk/u+PV5fnRsHShgXvhDPzWwzX" - "BJG6BZ1D/jWfyodoiAOM8RPvA73hIRxtgCtCxGjcC8z2DJi0/GOxNo7RvtGkRwo4JDeG30nA" - "4UIe8/m2QkO+fHNqBeCUWfi8VGqbXELoGSkL3gWNVHYDWh269GpZBpIu7fw48tuoGxPm7QOV" - "E2VTAhbvJp3TlFU46ykyp3XdFlGG4+06e5E31/KoFOZH98l3fSw8P4USGoHnUos7XtqnGkTl" - "Xseuk0DCagMSMiSDsik9pQ9dAk46rg5yZAEzZehC5g==" - ), bytes.fromhex( - "a0de0778a5718d214b015d441510876b5b5731bbd622edce1e85be4ad740b250" - )), - ("rsa_pss_sha512", b64decode( - "MIIDezCCAi+gAwIBAgIUI4DhlL/djqOWQrT2Djf/oCZZuP4wQQYJKoZIhvcNAQEKMDSgDzAN" - "BglghkgBZQMEAgMFAKEcMBoGCSqGSIb3DQEBCDANBglghkgBZQMEAgMFAKIDAgFAMBkxFzAV" - "BgNVBAMMDnJzYV9wc3Nfc2hhNTEyMB4XDTI2MDYyMjIwNTkzNloXDTM2MDYxOTIwNTkzNlow" - "GTEXMBUGA1UEAwwOcnNhX3Bzc19zaGE1MTIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK" - "AoIBAQCvn0EEVFvC+1YpBcs9pshNI/3RQueSeG4TrOOccthZWQSpal7nuXNgjYHuRcnASykr" - "0QUcdkNnWkrejILE0o3yHNc3jrEFGkVW2nhuG6rRTcRmqzCL9CQ4n5DH3tyilnwLDZXZ0Xdu" - "xcMPnj8QR45pZIfMxY7x8BXItTjfTFR3Ym7a+zeGLGwj+xYsPeOGlCPO7TsPaYySM8ZyS4ak" - "PN39A3xMHJuH8d3hIDm0oNBA78hqxF0tx8UK8UY2KPOucehwIQYZ9HFyWo5NxbX23a3mfXe6" - "MHoOD+6y1nD80YFStK8aUlU6JrrcDDnVO7QzPGnx6njc0HAlYM3V/XuvHny5AgMBAAGjUzBR" - "MB0GA1UdDgQWBBSbk4AXUwNJhOrmzv9NMmq5CIeWPDAfBgNVHSMEGDAWgBSbk4AXUwNJhOrm" - "zv9NMmq5CIeWPDAPBgNVHRMBAf8EBTADAQH/MEEGCSqGSIb3DQEBCjA0oA8wDQYJYIZIAWUD" - "BAIDBQChHDAaBgkqhkiG9w0BAQgwDQYJYIZIAWUDBAIDBQCiAwIBQAOCAQEAR1emzB16GHIL" - "xWiTq7kAirHxYlh80phTMRv0AarLDNavW0ZptncQlh2EeyzsUEknokIjqvpL9r+NrLWa+iIr" - "+3J8ZaWiiBLdGEmENebMnM1OGnCQB9yBwneyoLEFZiPNFDM6S2/uSbyM0ir1P95zi51MYBAE" - "pQ3YWjAtXB1CgrIjOJ2b08CkJVy3uyKk7CiDRmDXFsM6pVC0cI4yso9a8/wrhxmO4Evq2v8o" - "hXyduSqu1BNUTg3NvDGwwlU9Uik/hp5iV/P47mhONfv+hZP/cCdnCNzJT2V7dB3+h7wQUBmC" - "UKWiFtC03S7iR8d2+Pxrd9WShBydRU8gHDqjG1ZwHw==" - ), bytes.fromhex( - "4b2eb9c0b129e10c0a09b29a4d670062efd0da53be232a54b65b0c4a0f193e3a" - "133d79cb8f569681e3bc131dae654bd5f8a34b7215a18d42bae6343313bdabe8" - )), - ("ecdsa_sha384", b64decode( - "MIIBwTCCAUagAwIBAgIUf+oUEWmBYwffug4hBlnRAtFdejQwCgYIKoZIzj0EAwMwFzEVMBMG" - "A1UEAwwMZWNkc2Ffc2hhMzg0MB4XDTI2MDYyMjIwNTkzNloXDTM2MDYxOTIwNTkzNlowFzEV" - "MBMGA1UEAwwMZWNkc2Ffc2hhMzg0MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEQXRbllZNtIe8" - "nEqVC0Pnv9XrtYuK3JjTX8h8+sd5yZtXoKoXvTZA3s8oRyTp+Ua9Y/154OqiDufPBpiVJVa1" - "v28xiw9d2AiWa19VxyTQTTyrHofUnXb0VWWFQAi+UFMQo1MwUTAdBgNVHQ4EFgQUeRKlokr7" - "DgnFjJdRNzpPunHBb0MwHwYDVR0jBBgwFoAUeRKlokr7DgnFjJdRNzpPunHBb0MwDwYDVR0T" - "AQH/BAUwAwEB/zAKBggqhkjOPQQDAwNpADBmAjEA9/opjXvE22x675TqsjWogAbJJMe9tAJG" - "I7RoMiGWaRO3Jam06Ik64/nAXn7tDLP4AjEAryelYXb9DOOS6sanz4D+puRLU4CdiNlA89iT" - "WTjtkH7w+ctmj8jKXNBeLKdbrCEL" - ), bytes.fromhex( - "a16feae6f8a8e471fcd17c797a3d9357f3df17e16e3f2e30da1bb92d81723f7faebf9e29c7cdb73b4755e781e433d785" - )), - ("rsa_sha1", b64decode( - "MIIDBzCCAe+gAwIBAgIUPJQDMd+Kp0m5FxshY+PfE0Z47fIwDQYJKoZIhvcNAQEFBQAwEzER" - "MA8GA1UEAwwIcnNhX3NoYTEwHhcNMjYwNjIyMjA1OTM2WhcNMzYwNjE5MjA1OTM2WjATMREw" - "DwYDVQQDDAhyc2Ffc2hhMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMkVoiEJ" - "x7ANcSzCThZSdu/oXYQf4bYpvFGH7xa+23l7Kbei9KBSCcB12HWSGeQb+bik9OU7fIC5DWMH" - "H0nolgNIwclebPORL+6tUYA+xxqIN0dXsjMcD7J9MR6KLX8YcwlnmCjPw9qOTRrmXiUsqWsE" - "jwytsqxuMvTMUnJG2gNJNLyuCabBWHVzPWXoVKBT7j6b93RSSl1Z4GhN7gpfLmSVQPaJ0Iw5" - "2hTOgj5pffPIKVx6Se4diS8Wp/9QReFO2tZW7ccFcwCUeml72CJlp8zgFanfuwdeVRJF1DEn" - "eKKo/egmONTWG074shpnFB1jdgqxXRXAcaztNZ2i0k+L3ZECAwEAAaNTMFEwHQYDVR0OBBYE" - "FHSiQey2fSDnEc20AkTScG71uDsHMB8GA1UdIwQYMBaAFHSiQey2fSDnEc20AkTScG71uDsH" - "MA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAHI8I9RBh0FBX3Oi3U1vQCwC" - "JG2NrKiA73FoqohUxXVCq6JUojG8l484uJHw2P2zaUTaai1vdG+zKmwbX235uitxXtQ8oY9b" - "juIQjBsUwyikldSsZVE30EFmKUXIvhWDssvI6pU/t+jINuac1Ht/zMkz0aAzVrLwaAMrNbtU" - "DUaB4BMtGaC+TNMzALBYiD51dfPlRbDZKqc7l2zhXXyu+WoaIsK+pVqTA69aTdO0pYNBt34c" - "dq/lzuj/eU27A96MOIRMrGE4VB9yc1phkUfEWmSWNEglREOnYWMWm466z4E6jbAjb5zJGMev" - "iAvEGMy6pUKgTRW7FEVxvFtDUDjL5pw=" - ), bytes.fromhex( - "9cf8083887b586b8aa1b511190141c824e73fd09eac93f0945c0199080023710" - )), - ("dsa_sha256", b64decode( - "MIIEZzCCBBOgAwIBAgIUZRfnSemmaQReHYhIXH/LT3EEQdgwCwYJYIZIAWUDBAMCMBUxEzAR" - "BgNVBAMMCmRzYV9zaGEyNTYwHhcNMjYwNjIyMjA1OTM2WhcNMzYwNjE5MjA1OTM2WjAVMRMw" - "EQYDVQQDDApkc2Ffc2hhMjU2MIIDRDCCAjYGByqGSM44BAEwggIpAoIBAQCJo/kAk+fYjTtW" - "oDagfPciNzvMIQnY/y+rBu+jzMMjPA0/gsSNlI1ixGYdSwMGee8ZpI+VIlK6FynXPJgeCVmJ" - "SxTVoXGT+AdSoWbhw7q9CDlqPeUb431rOplUL5O0pklZHVcznl7q3EvvhNLaJ1iMXDvE1nsm" - "LlnKxdQYdA7j69vgZCawyGMl6UwP5deox8/MLcdvgK49p4ryI43rzAS+17KhlftXKt+KZktI" - "RIdhacOimJZgfy6oELEh4z6FlF3SXGqv5tWpWlpnSKQcDDVXG9CRmcU16zNc9MkJTsrZcDMh" - "BGSkDW8jAK/bd6E9YuqZ9nB6gNN3d+XzhW5sN6gZAh0A41ap4k32+1Cj0CIMKLVFXWCd8562" - "FUZhRHuZIQKCAQEAiZ387IoEmYZX/8U5tR1aczxDkVcy8wgyOvhobXhj/aMD6q84KK6I2mlx" - "AyLxpC5sZBUiu7h+Mkftb0gqWZFwtQiczuawuQET0YhoqeuAOniGyZryZEGZeuG5BgTb9eaW" - "7a3D2fYm50hfFhFKSlnZAJR2ZNFZNaxOoYraeCSqVukbBwmLQgbr95YKPTX3puwzf5gY7I6e" - "u15mXgZpVcy5wrZ1pxRyLLYQPoCoI3bzOHoqgmnJmykWFzPObRWJiZlpkQLQ3O9T8LR6Wqmi" - "jhdWpYDcPchNbWVOq37Mpl7OrVottTy+pYoYjwM+j32hH5iequWDgdcw/cDpd+FH7i4LVgOC" - "AQYAAoIBAQCIc1x0JSROUVfeDr0nIbqgZIN6zMg7YCC9mJMdQcIiSahnSaHOJzRtGapcN6Ta" - "HfY3zApx53JMU81gwWzWYCJcWs2gbjaLHSlm3lDNyPTRgzuWWKvyvKHvL7TGRfNnOS/VrvzO" - "/hxA2w1fpTWBcn5tIJQk1ri2yWxUJUQ52/t7WojUgen14Bi2qeITvELmwXFnuSs8UhsRwal1" - "2fxMC/fdnh4jR8gBfvirlH0vvRB4ZrOIhU+ZwjwebZKNFdH9IJcvMcz3XnYA8FvP3R99VWkx" - "JubCeWVlNJZzUCs7L5C0+cPBPkPce9sOrS3iqHMpk4J62JjM/CEgSBJ3VDNvh3iXo1MwUTAd" - "BgNVHQ4EFgQUzVmJizDr+Rth2910JRdW45fV6XkwHwYDVR0jBBgwFoAUzVmJizDr+Rth2910" - "JRdW45fV6XkwDwYDVR0TAQH/BAUwAwEB/zALBglghkgBZQMEAwIDQQAwPgIdANTFiRLd6oV6" - "mknToIBJjTAp9csMwFRPsF+nS9ACHQC24O9iRcqXU+rta7ocM6SDhSiDn1KZapUIAqHU" - ), bytes.fromhex( - "6e19e230f917fd3922f39fff90a263f1df618fd4d8080037f75f1b32f97b0e27" - )), + ( + "rsa_pss_sha256", + b64decode( + "MIIDezCCAi+gAwIBAgIUbvFLoNTxKxJMSKWyAwDtovtPfIwwQQYJKoZIhvcNAQEKMDSgDzAN" + "BglghkgBZQMEAgEFAKEcMBoGCSqGSIb3DQEBCDANBglghkgBZQMEAgEFAKIDAgEgMBkxFzAV" + "BgNVBAMMDnJzYV9wc3Nfc2hhMjU2MB4XDTI2MDYyMjIwNTkzNloXDTM2MDYxOTIwNTkzNlow" + "GTEXMBUGA1UEAwwOcnNhX3Bzc19zaGEyNTYwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK" + "AoIBAQDpa19BgN87rCwx3j+OkZN2JvpBcM7OQF3O0otYaHePBaGuMYNBApo0PqBp9E1oakm7" + "ZktqSVpANOp/G4b4+VNIEEi2AO8AUU+hJgw1t2xlLzzxlynstRVVaB3RqYhSdC4HJaBeCC5O" + "fZ9c14qjzIj6jQANpO/bb2N2aPr5uwSViLiV3MXvN8+g5pXzlSbdRPJyqJdvLgXHA/tS3axR" + "DHb6P/loIhrE8yaWMJvY1wpp856+WwOu8mVSTwMv0mhScZnJ5BtF3UJJyPjIY8B/VSK189Sj" + "PidhhXZNk8+1QaHCe4RwuFnMUybGoOmcTBaXJPDErfwt6pGD0ZcjXnW7myUFAgMBAAGjUzBR" + "MB0GA1UdDgQWBBTuhQOK9oKQhMFCzFx+tYgScnTgHDAfBgNVHSMEGDAWgBTuhQOK9oKQhMFC" + "zFx+tYgScnTgHDAPBgNVHRMBAf8EBTADAQH/MEEGCSqGSIb3DQEBCjA0oA8wDQYJYIZIAWUD" + "BAIBBQChHDAaBgkqhkiG9w0BAQgwDQYJYIZIAWUDBAIBBQCiAwIBIAOCAQEAob7QddtJ12v7" + "b5RNPSzEP+2GD+1nUGQkOa1lfBzTP0jzvO2f/fgiybKCvZtk/u+PV5fnRsHShgXvhDPzWwzX" + "BJG6BZ1D/jWfyodoiAOM8RPvA73hIRxtgCtCxGjcC8z2DJi0/GOxNo7RvtGkRwo4JDeG30nA" + "4UIe8/m2QkO+fHNqBeCUWfi8VGqbXELoGSkL3gWNVHYDWh269GpZBpIu7fw48tuoGxPm7QOV" + "E2VTAhbvJp3TlFU46ykyp3XdFlGG4+06e5E31/KoFOZH98l3fSw8P4USGoHnUos7XtqnGkTl" + "Xseuk0DCagMSMiSDsik9pQ9dAk46rg5yZAEzZehC5g==" + ), + bytes.fromhex( + "a0de0778a5718d214b015d441510876b5b5731bbd622edce1e85be4ad740b250" + ), + ), + ( + "rsa_pss_sha512", + b64decode( + "MIIDezCCAi+gAwIBAgIUI4DhlL/djqOWQrT2Djf/oCZZuP4wQQYJKoZIhvcNAQEKMDSgDzAN" + "BglghkgBZQMEAgMFAKEcMBoGCSqGSIb3DQEBCDANBglghkgBZQMEAgMFAKIDAgFAMBkxFzAV" + "BgNVBAMMDnJzYV9wc3Nfc2hhNTEyMB4XDTI2MDYyMjIwNTkzNloXDTM2MDYxOTIwNTkzNlow" + "GTEXMBUGA1UEAwwOcnNhX3Bzc19zaGE1MTIwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEK" + "AoIBAQCvn0EEVFvC+1YpBcs9pshNI/3RQueSeG4TrOOccthZWQSpal7nuXNgjYHuRcnASykr" + "0QUcdkNnWkrejILE0o3yHNc3jrEFGkVW2nhuG6rRTcRmqzCL9CQ4n5DH3tyilnwLDZXZ0Xdu" + "xcMPnj8QR45pZIfMxY7x8BXItTjfTFR3Ym7a+zeGLGwj+xYsPeOGlCPO7TsPaYySM8ZyS4ak" + "PN39A3xMHJuH8d3hIDm0oNBA78hqxF0tx8UK8UY2KPOucehwIQYZ9HFyWo5NxbX23a3mfXe6" + "MHoOD+6y1nD80YFStK8aUlU6JrrcDDnVO7QzPGnx6njc0HAlYM3V/XuvHny5AgMBAAGjUzBR" + "MB0GA1UdDgQWBBSbk4AXUwNJhOrmzv9NMmq5CIeWPDAfBgNVHSMEGDAWgBSbk4AXUwNJhOrm" + "zv9NMmq5CIeWPDAPBgNVHRMBAf8EBTADAQH/MEEGCSqGSIb3DQEBCjA0oA8wDQYJYIZIAWUD" + "BAIDBQChHDAaBgkqhkiG9w0BAQgwDQYJYIZIAWUDBAIDBQCiAwIBQAOCAQEAR1emzB16GHIL" + "xWiTq7kAirHxYlh80phTMRv0AarLDNavW0ZptncQlh2EeyzsUEknokIjqvpL9r+NrLWa+iIr" + "+3J8ZaWiiBLdGEmENebMnM1OGnCQB9yBwneyoLEFZiPNFDM6S2/uSbyM0ir1P95zi51MYBAE" + "pQ3YWjAtXB1CgrIjOJ2b08CkJVy3uyKk7CiDRmDXFsM6pVC0cI4yso9a8/wrhxmO4Evq2v8o" + "hXyduSqu1BNUTg3NvDGwwlU9Uik/hp5iV/P47mhONfv+hZP/cCdnCNzJT2V7dB3+h7wQUBmC" + "UKWiFtC03S7iR8d2+Pxrd9WShBydRU8gHDqjG1ZwHw==" + ), + bytes.fromhex( + "4b2eb9c0b129e10c0a09b29a4d670062efd0da53be232a54b65b0c4a0f193e3a" + "133d79cb8f569681e3bc131dae654bd5f8a34b7215a18d42bae6343313bdabe8" + ), + ), + ( + "ecdsa_sha384", + b64decode( + "MIIBwTCCAUagAwIBAgIUf+oUEWmBYwffug4hBlnRAtFdejQwCgYIKoZIzj0EAwMwFzEVMBMG" + "A1UEAwwMZWNkc2Ffc2hhMzg0MB4XDTI2MDYyMjIwNTkzNloXDTM2MDYxOTIwNTkzNlowFzEV" + "MBMGA1UEAwwMZWNkc2Ffc2hhMzg0MHYwEAYHKoZIzj0CAQYFK4EEACIDYgAEQXRbllZNtIe8" + "nEqVC0Pnv9XrtYuK3JjTX8h8+sd5yZtXoKoXvTZA3s8oRyTp+Ua9Y/154OqiDufPBpiVJVa1" + "v28xiw9d2AiWa19VxyTQTTyrHofUnXb0VWWFQAi+UFMQo1MwUTAdBgNVHQ4EFgQUeRKlokr7" + "DgnFjJdRNzpPunHBb0MwHwYDVR0jBBgwFoAUeRKlokr7DgnFjJdRNzpPunHBb0MwDwYDVR0T" + "AQH/BAUwAwEB/zAKBggqhkjOPQQDAwNpADBmAjEA9/opjXvE22x675TqsjWogAbJJMe9tAJG" + "I7RoMiGWaRO3Jam06Ik64/nAXn7tDLP4AjEAryelYXb9DOOS6sanz4D+puRLU4CdiNlA89iT" + "WTjtkH7w+ctmj8jKXNBeLKdbrCEL" + ), + bytes.fromhex( + "a16feae6f8a8e471fcd17c797a3d9357f3df17e16e3f2e30da1bb92d81723f7faebf9e29c7cdb73b4755e781e433d785" + ), + ), + ( + "rsa_sha1", + b64decode( + "MIIDBzCCAe+gAwIBAgIUPJQDMd+Kp0m5FxshY+PfE0Z47fIwDQYJKoZIhvcNAQEFBQAwEzER" + "MA8GA1UEAwwIcnNhX3NoYTEwHhcNMjYwNjIyMjA1OTM2WhcNMzYwNjE5MjA1OTM2WjATMREw" + "DwYDVQQDDAhyc2Ffc2hhMTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAMkVoiEJ" + "x7ANcSzCThZSdu/oXYQf4bYpvFGH7xa+23l7Kbei9KBSCcB12HWSGeQb+bik9OU7fIC5DWMH" + "H0nolgNIwclebPORL+6tUYA+xxqIN0dXsjMcD7J9MR6KLX8YcwlnmCjPw9qOTRrmXiUsqWsE" + "jwytsqxuMvTMUnJG2gNJNLyuCabBWHVzPWXoVKBT7j6b93RSSl1Z4GhN7gpfLmSVQPaJ0Iw5" + "2hTOgj5pffPIKVx6Se4diS8Wp/9QReFO2tZW7ccFcwCUeml72CJlp8zgFanfuwdeVRJF1DEn" + "eKKo/egmONTWG074shpnFB1jdgqxXRXAcaztNZ2i0k+L3ZECAwEAAaNTMFEwHQYDVR0OBBYE" + "FHSiQey2fSDnEc20AkTScG71uDsHMB8GA1UdIwQYMBaAFHSiQey2fSDnEc20AkTScG71uDsH" + "MA8GA1UdEwEB/wQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAHI8I9RBh0FBX3Oi3U1vQCwC" + "JG2NrKiA73FoqohUxXVCq6JUojG8l484uJHw2P2zaUTaai1vdG+zKmwbX235uitxXtQ8oY9b" + "juIQjBsUwyikldSsZVE30EFmKUXIvhWDssvI6pU/t+jINuac1Ht/zMkz0aAzVrLwaAMrNbtU" + "DUaB4BMtGaC+TNMzALBYiD51dfPlRbDZKqc7l2zhXXyu+WoaIsK+pVqTA69aTdO0pYNBt34c" + "dq/lzuj/eU27A96MOIRMrGE4VB9yc1phkUfEWmSWNEglREOnYWMWm466z4E6jbAjb5zJGMev" + "iAvEGMy6pUKgTRW7FEVxvFtDUDjL5pw=" + ), + bytes.fromhex( + "9cf8083887b586b8aa1b511190141c824e73fd09eac93f0945c0199080023710" + ), + ), + ( + "dsa_sha256", + b64decode( + "MIIEZzCCBBOgAwIBAgIUZRfnSemmaQReHYhIXH/LT3EEQdgwCwYJYIZIAWUDBAMCMBUxEzAR" + "BgNVBAMMCmRzYV9zaGEyNTYwHhcNMjYwNjIyMjA1OTM2WhcNMzYwNjE5MjA1OTM2WjAVMRMw" + "EQYDVQQDDApkc2Ffc2hhMjU2MIIDRDCCAjYGByqGSM44BAEwggIpAoIBAQCJo/kAk+fYjTtW" + "oDagfPciNzvMIQnY/y+rBu+jzMMjPA0/gsSNlI1ixGYdSwMGee8ZpI+VIlK6FynXPJgeCVmJ" + "SxTVoXGT+AdSoWbhw7q9CDlqPeUb431rOplUL5O0pklZHVcznl7q3EvvhNLaJ1iMXDvE1nsm" + "LlnKxdQYdA7j69vgZCawyGMl6UwP5deox8/MLcdvgK49p4ryI43rzAS+17KhlftXKt+KZktI" + "RIdhacOimJZgfy6oELEh4z6FlF3SXGqv5tWpWlpnSKQcDDVXG9CRmcU16zNc9MkJTsrZcDMh" + "BGSkDW8jAK/bd6E9YuqZ9nB6gNN3d+XzhW5sN6gZAh0A41ap4k32+1Cj0CIMKLVFXWCd8562" + "FUZhRHuZIQKCAQEAiZ387IoEmYZX/8U5tR1aczxDkVcy8wgyOvhobXhj/aMD6q84KK6I2mlx" + "AyLxpC5sZBUiu7h+Mkftb0gqWZFwtQiczuawuQET0YhoqeuAOniGyZryZEGZeuG5BgTb9eaW" + "7a3D2fYm50hfFhFKSlnZAJR2ZNFZNaxOoYraeCSqVukbBwmLQgbr95YKPTX3puwzf5gY7I6e" + "u15mXgZpVcy5wrZ1pxRyLLYQPoCoI3bzOHoqgmnJmykWFzPObRWJiZlpkQLQ3O9T8LR6Wqmi" + "jhdWpYDcPchNbWVOq37Mpl7OrVottTy+pYoYjwM+j32hH5iequWDgdcw/cDpd+FH7i4LVgOC" + "AQYAAoIBAQCIc1x0JSROUVfeDr0nIbqgZIN6zMg7YCC9mJMdQcIiSahnSaHOJzRtGapcN6Ta" + "HfY3zApx53JMU81gwWzWYCJcWs2gbjaLHSlm3lDNyPTRgzuWWKvyvKHvL7TGRfNnOS/VrvzO" + "/hxA2w1fpTWBcn5tIJQk1ri2yWxUJUQ52/t7WojUgen14Bi2qeITvELmwXFnuSs8UhsRwal1" + "2fxMC/fdnh4jR8gBfvirlH0vvRB4ZrOIhU+ZwjwebZKNFdH9IJcvMcz3XnYA8FvP3R99VWkx" + "JubCeWVlNJZzUCs7L5C0+cPBPkPce9sOrS3iqHMpk4J62JjM/CEgSBJ3VDNvh3iXo1MwUTAd" + "BgNVHQ4EFgQUzVmJizDr+Rth2910JRdW45fV6XkwHwYDVR0jBBgwFoAUzVmJizDr+Rth2910" + "JRdW45fV6XkwDwYDVR0TAQH/BAUwAwEB/zALBglghkgBZQMEAwIDQQAwPgIdANTFiRLd6oV6" + "mknToIBJjTAp9csMwFRPsF+nS9ACHQC24O9iRcqXU+rta7ocM6SDhSiDn1KZapUIAqHU" + ), + bytes.fromhex( + "6e19e230f917fd3922f39fff90a263f1df618fd4d8080037f75f1b32f97b0e27" + ), + ), ] # Signature algorithms with no single well-defined hash: rejected by both # py_scram and truenas_pyscram (RFC 5929 4.1 -> undefined). REJECTED_VECTORS = [ - ("ed25519", b64decode( - "MIIBODCB66ADAgECAhRF/i2VZfabTYtGC96jEmDKQWs7HzAFBgMrZXAwEjEQMA4GA1UEAwwH" - "ZWQyNTUxOTAeFw0yNjA2MjIyMDU5MzZaFw0zNjA2MTkyMDU5MzZaMBIxEDAOBgNVBAMMB2Vk" - "MjU1MTkwKjAFBgMrZXADIQBG6mG6PIveRZbDEpUA0h2+zqcfkFflhjFU6WMltek8H6NTMFEw" - "HQYDVR0OBBYEFI0SqZaciNfleT/XktQEcIjga8GhMB8GA1UdIwQYMBaAFI0SqZaciNfleT/X" - "ktQEcIjga8GhMA8GA1UdEwEB/wQFMAMBAf8wBQYDK2VwA0EAMvh4cKf/RBo2Pa9xlyAI1PxL" - "R43eNEd6siuiayO5lojZy92QlmHI3Y1SEF7RQd6E9PeKsGmdo/Pcpu75xrfTDw==" - )), + ( + "ed25519", + b64decode( + "MIIBODCB66ADAgECAhRF/i2VZfabTYtGC96jEmDKQWs7HzAFBgMrZXAwEjEQMA4GA1UEAwwH" + "ZWQyNTUxOTAeFw0yNjA2MjIyMDU5MzZaFw0zNjA2MTkyMDU5MzZaMBIxEDAOBgNVBAMMB2Vk" + "MjU1MTkwKjAFBgMrZXADIQBG6mG6PIveRZbDEpUA0h2+zqcfkFflhjFU6WMltek8H6NTMFEw" + "HQYDVR0OBBYEFI0SqZaciNfleT/XktQEcIjga8GhMB8GA1UdIwQYMBaAFI0SqZaciNfleT/X" + "ktQEcIjga8GhMA8GA1UdEwEB/wQFMAMBAf8wBQYDK2VwA0EAMvh4cKf/RBo2Pa9xlyAI1PxL" + "R43eNEd6siuiayO5lojZy92QlmHI3Y1SEF7RQd6E9PeKsGmdo/Pcpu75xrfTDw==" + ), + ), ] diff --git a/tests/scram/test_compatibility.py b/tests/scram/test_compatibility.py index 0a4c0e8..f38c5d2 100644 --- a/tests/scram/test_compatibility.py +++ b/tests/scram/test_compatibility.py @@ -80,7 +80,9 @@ def test_with_api_key_id(self): username = "testuser" api_key_id = 12345 - c_msg = truenas_pyscram.ClientFirstMessage(username=username, api_key_id=api_key_id) + c_msg = truenas_pyscram.ClientFirstMessage( + username=username, api_key_id=api_key_id + ) py_msg = py_scram.ClientFirstMessage(username=username, api_key_id=api_key_id) self.assertEqual(c_msg.username, py_msg.username) @@ -114,7 +116,9 @@ def test_serialization_with_api_key(self): username = "testuser" api_key_id = 789 - c_msg = truenas_pyscram.ClientFirstMessage(username=username, api_key_id=api_key_id) + c_msg = truenas_pyscram.ClientFirstMessage( + username=username, api_key_id=api_key_id + ) py_msg = py_scram.ClientFirstMessage(username=username, api_key_id=api_key_id) c_str = str(c_msg) @@ -171,14 +175,10 @@ def test_creation_from_client_first(self): # Create server first messages c_server_first = truenas_pyscram.ServerFirstMessage( - client_first=c_client_first, - salt=self.salt, - iterations=self.iterations + client_first=c_client_first, salt=self.salt, iterations=self.iterations ) py_server_first = py_scram.ServerFirstMessage( - client_first=py_client_first, - salt=self.py_salt, - iterations=self.iterations + client_first=py_client_first, salt=self.py_salt, iterations=self.iterations ) # Iterations should match @@ -246,9 +246,7 @@ def test_scram_hi_compatibility(self): # We test indirectly through auth data generation # For now, we'll test the pure Python implementation result = py_scram.scram_hi( - py_scram.CryptoDatum(key_data), - py_scram.CryptoDatum(salt), - iterations + py_scram.CryptoDatum(key_data), py_scram.CryptoDatum(salt), iterations ) # Result should be 64 bytes (SHA-512) @@ -271,8 +269,7 @@ def test_scram_hmac_sha512_compatibility(self): data = b"test_data" result = py_scram.scram_hmac_sha512( - py_scram.CryptoDatum(key), - py_scram.CryptoDatum(data) + py_scram.CryptoDatum(key), py_scram.CryptoDatum(data) ) # Result should be 64 bytes (SHA-512) @@ -292,7 +289,9 @@ def setUp(self): self.py_auth_data = py_scram.ScramAuthData( salt=py_scram.CryptoDatum(bytes(self.c_auth_data.salt)), iterations=self.c_auth_data.iterations, - salted_password=py_scram.CryptoDatum(bytes(self.c_auth_data.salted_password)), + salted_password=py_scram.CryptoDatum( + bytes(self.c_auth_data.salted_password) + ), client_key=py_scram.CryptoDatum(bytes(self.c_auth_data.client_key)), stored_key=py_scram.CryptoDatum(bytes(self.c_auth_data.stored_key)), server_key=py_scram.CryptoDatum(bytes(self.c_auth_data.server_key)), @@ -309,7 +308,7 @@ def test_round_trip_with_same_nonce(self): c_server_first = truenas_pyscram.ServerFirstMessage( client_first=c_client_first, salt=self.c_auth_data.salt, - iterations=self.c_auth_data.iterations + iterations=self.c_auth_data.iterations, ) # Parse server first in Python implementation @@ -332,12 +331,12 @@ def setUp(self): def test_verification_functions_exist(self): """Test that both implementations have verification functions.""" # C extension functions - self.assertTrue(hasattr(truenas_pyscram, 'verify_client_final_message')) - self.assertTrue(hasattr(truenas_pyscram, 'verify_server_signature')) + self.assertTrue(hasattr(truenas_pyscram, "verify_client_final_message")) + self.assertTrue(hasattr(truenas_pyscram, "verify_server_signature")) # Python implementation functions - self.assertTrue(hasattr(py_scram, 'verify_client_final_message')) - self.assertTrue(hasattr(py_scram, 'verify_server_signature')) + self.assertTrue(hasattr(py_scram, "verify_client_final_message")) + self.assertTrue(hasattr(py_scram, "verify_server_signature")) def test_verify_server_signature_with_c_messages(self): """Test Python verify_server_signature with C extension messages.""" @@ -346,20 +345,20 @@ def test_verify_server_signature_with_c_messages(self): server_first = truenas_pyscram.ServerFirstMessage( client_first=client_first, salt=self.auth_data.salt, - iterations=self.auth_data.iterations + iterations=self.auth_data.iterations, ) client_final = truenas_pyscram.ClientFinalMessage( client_first=client_first, server_first=server_first, client_key=self.auth_data.client_key, - stored_key=self.auth_data.stored_key + stored_key=self.auth_data.stored_key, ) server_final = truenas_pyscram.ServerFinalMessage( client_first=client_first, server_first=server_first, client_final=client_final, stored_key=self.auth_data.stored_key, - server_key=self.auth_data.server_key + server_key=self.auth_data.server_key, ) # Parse into Python implementation types @@ -371,7 +370,7 @@ def test_verify_server_signature_with_c_messages(self): server_first=server_first, client_final=client_final, server_final=server_final, - server_key=self.auth_data.server_key + server_key=self.auth_data.server_key, ) @@ -389,8 +388,8 @@ def test_signature_algorithm_parity(self): with self.subTest(cert=name): p = bytes(py_scram.compute_tls_server_end_point(cert_der)) c = bytes(truenas_pyscram.compute_tls_server_end_point(cert_der)) - self.assertEqual(p, expected, f'{name}: py_scram diverged') - self.assertEqual(c, expected, f'{name}: C extension diverged') + self.assertEqual(p, expected, f"{name}: py_scram diverged") + self.assertEqual(c, expected, f"{name}: C extension diverged") def test_undefined_signature_algorithm_rejected_by_both(self): """EdDSA (no single signature hash) is rejected by py_scram and the C @@ -403,11 +402,14 @@ def test_undefined_signature_algorithm_rejected_by_both(self): truenas_pyscram.compute_tls_server_end_point(cert_der) def test_cb_name_constant_parity(self): - self.assertEqual(py_scram.CB_TLS_SERVER_END_POINT, truenas_pyscram.CB_TLS_SERVER_END_POINT) + self.assertEqual( + py_scram.CB_TLS_SERVER_END_POINT, truenas_pyscram.CB_TLS_SERVER_END_POINT + ) def _build_py_bound_exchange(self, auth, binding): py_cf = py_scram.ClientFirstMessage( - username="u", api_key_id=5, + username="u", + api_key_id=5, channel_binding_type=py_scram.CB_TLS_SERVER_END_POINT, ) py_sf = py_scram.ServerFirstMessage( @@ -416,7 +418,8 @@ def _build_py_bound_exchange(self, auth, binding): iterations=auth.iterations, ) py_fin = py_scram.ClientFinalMessage( - client_first=py_cf, server_first=py_sf, + client_first=py_cf, + server_first=py_sf, client_key=py_scram.CryptoDatum(bytes(auth.client_key)), stored_key=py_scram.CryptoDatum(bytes(auth.stored_key)), channel_binding=binding, @@ -430,8 +433,12 @@ def test_py_bound_exchange_verified_by_c_extension(self): py_cf, py_sf, py_fin = self._build_py_bound_exchange(auth, binding) # c= must be base64("p=tls-server-end-point,," + binding). - c_attr = next(part[2:] for part in str(py_fin).split(",") if part.startswith("c=")) - self.assertEqual(c_attr, b64encode(b"p=tls-server-end-point,," + bytes(binding)).decode()) + c_attr = next( + part[2:] for part in str(py_fin).split(",") if part.startswith("c=") + ) + self.assertEqual( + c_attr, b64encode(b"p=tls-server-end-point,," + bytes(binding)).decode() + ) # The C-extension server verifier accepts the py-built bound final. truenas_pyscram.verify_client_final_message( @@ -439,7 +446,9 @@ def test_py_bound_exchange_verified_by_c_extension(self): truenas_pyscram.ServerFirstMessage(rfc_string=str(py_sf)), truenas_pyscram.ClientFinalMessage(rfc_string=str(py_fin)), auth.stored_key, - channel_binding=truenas_pyscram.compute_tls_server_end_point(_CB_TEST_CERT_DER), + channel_binding=truenas_pyscram.compute_tls_server_end_point( + _CB_TEST_CERT_DER + ), require_channel_binding=True, ) @@ -459,5 +468,5 @@ def test_wrong_binding_rejected_by_c_extension(self): ) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/scram/test_py_scram.py b/tests/scram/test_py_scram.py index cb7f9de..e58d5b6 100644 --- a/tests/scram/test_py_scram.py +++ b/tests/scram/test_py_scram.py @@ -21,6 +21,7 @@ # Test-only oracle (not a runtime dependency of the client). from cryptography import x509 as _x509 # type: ignore from cryptography.exceptions import UnsupportedAlgorithm as _CryptoUnsupported # type: ignore + _HAVE_CRYPTOGRAPHY = True except ImportError: _HAVE_CRYPTOGRAPHY = False @@ -100,7 +101,8 @@ def test_with_gs2_header(self): def test_channel_binding_type_builds_p_flag(self): """A channel_binding_type produces a 'p=,,' gs2 cbind flag.""" msg = py_scram.ClientFirstMessage( - username="testuser", channel_binding_type="tls-server-end-point") + username="testuser", channel_binding_type="tls-server-end-point" + ) self.assertEqual(msg.gs2_header, "p=tls-server-end-point") self.assertTrue(str(msg).startswith("p=tls-server-end-point,,")) @@ -150,7 +152,7 @@ def test_invalid_api_key_id(self): def test_saslprep_normalization(self): """Test that username is normalized using SASLprep (RFC 5802, Section 5.1).""" # U+200B (zero-width space) should be mapped to nothing per RFC 3454, Table B.1 - username_with_zwsp = "test\u200Buser" + username_with_zwsp = "test\u200buser" msg = py_scram.ClientFirstMessage(username=username_with_zwsp) # Zero-width space should be removed self.assertEqual(msg.username, "testuser") @@ -166,7 +168,7 @@ def test_saslprep_nfkc_normalization(self): def test_saslprep_nbsp_to_space(self): """Test that non-breaking space is normalized to regular space via NFKC.""" # U+00A0 (non-breaking space) is normalized to U+0020 (regular space) by NFKC - username_with_nbsp = "test\u00A0user" + username_with_nbsp = "test\u00a0user" msg = py_scram.ClientFirstMessage(username=username_with_nbsp) # Non-breaking space should be normalized to regular space self.assertEqual(msg.username, "test user") @@ -189,7 +191,9 @@ class TestServerFirstMessage(unittest.TestCase): def test_from_rfc_string(self): """Test parsing ServerFirstMessage from RFC string.""" - nonce = b64encode(b'\x22' * 64).decode() # combined client+server nonce is 64 bytes + nonce = b64encode( + b"\x22" * 64 + ).decode() # combined client+server nonce is 64 bytes rfc_string = f"r={nonce},s=c2FsdA==,i=50000" msg = py_scram.ServerFirstMessage(rfc_string=rfc_string) @@ -205,9 +209,7 @@ def test_from_client_first(self): iterations = 100000 server_first = py_scram.ServerFirstMessage( - client_first=client_first, - salt=salt, - iterations=iterations + client_first=client_first, salt=salt, iterations=iterations ) self.assertEqual(server_first.iterations, iterations) @@ -223,9 +225,7 @@ def test_serialization(self): salt = py_scram.CryptoDatum(b"test_salt") server_first = py_scram.ServerFirstMessage( - client_first=client_first, - salt=salt, - iterations=100000 + client_first=client_first, salt=salt, iterations=100000 ) serialized = str(server_first) @@ -251,7 +251,7 @@ def test_mutually_exclusive_parameters(self): client_first=client_first, salt=salt, iterations=100000, - rfc_string="r=test,s=test,i=100000" + rfc_string="r=test,s=test,i=100000", ) def test_missing_parameters(self): @@ -268,17 +268,13 @@ def setUp(self): self.client_first = py_scram.ClientFirstMessage(username="testuser") self.salt = py_scram.CryptoDatum(b"test_salt_16bytes") self.server_first = py_scram.ServerFirstMessage( - client_first=self.client_first, - salt=self.salt, - iterations=100000 + client_first=self.client_first, salt=self.salt, iterations=100000 ) # Generate keys using crypto functions key_data = b"test_password" salted_password = py_scram.scram_hi( - py_scram.CryptoDatum(key_data), - self.salt, - 100000 + py_scram.CryptoDatum(key_data), self.salt, 100000 ) self.client_key = py_scram.scram_create_client_key(salted_password) self.stored_key = py_scram.scram_create_stored_key(self.client_key) @@ -289,7 +285,7 @@ def test_creation(self): client_first=self.client_first, server_first=self.server_first, client_key=self.client_key, - stored_key=self.stored_key + stored_key=self.stored_key, ) self.assertIsInstance(client_final.nonce, py_scram.CryptoDatum) @@ -302,7 +298,7 @@ def test_serialization(self): client_first=self.client_first, server_first=self.server_first, client_key=self.client_key, - stored_key=self.stored_key + stored_key=self.stored_key, ) serialized = str(client_final) @@ -312,8 +308,8 @@ def test_serialization(self): def test_from_rfc_string(self): """Test parsing ClientFinalMessage from RFC string.""" - nonce = b64encode(b'\x22' * 64).decode() - proof = b64encode(b'\x33' * 64).decode() + nonce = b64encode(b"\x22" * 64).decode() + proof = b64encode(b"\x33" * 64).decode() rfc_string = f"c=biws,r={nonce},p={proof}" client_final = py_scram.ClientFinalMessage(rfc_string=rfc_string) @@ -329,17 +325,13 @@ def setUp(self): self.client_first = py_scram.ClientFirstMessage(username="testuser") self.salt = py_scram.CryptoDatum(b"test_salt_16bytes") self.server_first = py_scram.ServerFirstMessage( - client_first=self.client_first, - salt=self.salt, - iterations=100000 + client_first=self.client_first, salt=self.salt, iterations=100000 ) # Generate keys key_data = b"test_password" salted_password = py_scram.scram_hi( - py_scram.CryptoDatum(key_data), - self.salt, - 100000 + py_scram.CryptoDatum(key_data), self.salt, 100000 ) self.client_key = py_scram.scram_create_client_key(salted_password) self.stored_key = py_scram.scram_create_stored_key(self.client_key) @@ -349,7 +341,7 @@ def setUp(self): client_first=self.client_first, server_first=self.server_first, client_key=self.client_key, - stored_key=self.stored_key + stored_key=self.stored_key, ) def test_creation(self): @@ -359,7 +351,7 @@ def test_creation(self): server_first=self.server_first, client_final=self.client_final, stored_key=self.stored_key, - server_key=self.server_key + server_key=self.server_key, ) self.assertIsInstance(server_final.signature, py_scram.CryptoDatum) @@ -372,7 +364,7 @@ def test_serialization(self): server_first=self.server_first, client_final=self.client_final, stored_key=self.stored_key, - server_key=self.server_key + server_key=self.server_key, ) serialized = str(server_final) @@ -393,7 +385,7 @@ def test_invalid_client_proof(self): client_first=self.client_first, server_first=self.server_first, client_key=self.client_key, - stored_key=wrong_stored_key + stored_key=wrong_stored_key, ) # Server final should fail to create with mismatched keys @@ -403,7 +395,7 @@ def test_invalid_client_proof(self): server_first=self.server_first, client_final=wrong_client_final, stored_key=self.stored_key, - server_key=self.server_key + server_key=self.server_key, ) @@ -415,17 +407,13 @@ def setUp(self): self.client_first = py_scram.ClientFirstMessage(username="testuser") self.salt = py_scram.CryptoDatum(b"test_salt_16bytes") self.server_first = py_scram.ServerFirstMessage( - client_first=self.client_first, - salt=self.salt, - iterations=100000 + client_first=self.client_first, salt=self.salt, iterations=100000 ) # Generate keys key_data = b"test_password" salted_password = py_scram.scram_hi( - py_scram.CryptoDatum(key_data), - self.salt, - 100000 + py_scram.CryptoDatum(key_data), self.salt, 100000 ) self.client_key = py_scram.scram_create_client_key(salted_password) self.stored_key = py_scram.scram_create_stored_key(self.client_key) @@ -435,7 +423,7 @@ def setUp(self): client_first=self.client_first, server_first=self.server_first, client_key=self.client_key, - stored_key=self.stored_key + stored_key=self.stored_key, ) self.server_final = py_scram.ServerFinalMessage( @@ -443,7 +431,7 @@ def setUp(self): server_first=self.server_first, client_final=self.client_final, stored_key=self.stored_key, - server_key=self.server_key + server_key=self.server_key, ) def test_verify_server_signature_success(self): @@ -454,7 +442,7 @@ def test_verify_server_signature_success(self): server_first=self.server_first, client_final=self.client_final, server_final=self.server_final, - server_key=self.server_key + server_key=self.server_key, ) def test_verify_server_signature_failure(self): @@ -467,7 +455,7 @@ def test_verify_server_signature_failure(self): server_first=self.server_first, client_final=self.client_final, server_final=self.server_final, - server_key=wrong_server_key + server_key=wrong_server_key, ) self.assertEqual(ctx.exception.code, py_scram.SCRAM_E_AUTH_FAILED) @@ -479,7 +467,7 @@ def test_verify_client_final_message_success(self): client_first=self.client_first, server_first=self.server_first, client_final=self.client_final, - stored_key=self.stored_key + stored_key=self.stored_key, ) def test_verify_client_final_message_failure(self): @@ -491,7 +479,7 @@ def test_verify_client_final_message_failure(self): client_first=self.client_first, server_first=self.server_first, client_final=self.client_final, - stored_key=wrong_stored_key + stored_key=wrong_stored_key, ) self.assertEqual(ctx.exception.code, py_scram.SCRAM_E_AUTH_FAILED) @@ -504,7 +492,8 @@ def setUp(self): self.salt = py_scram.CryptoDatum(b"test_salt_16bytes") self.client_first = py_scram.ClientFirstMessage(username="testuser") self.server_first = py_scram.ServerFirstMessage( - client_first=self.client_first, salt=self.salt, iterations=100000) + client_first=self.client_first, salt=self.salt, iterations=100000 + ) salted = py_scram.scram_hi(py_scram.CryptoDatum(b"pw"), self.salt, 100000) self.client_key = py_scram.scram_create_client_key(salted) self.stored_key = py_scram.scram_create_stored_key(self.client_key) @@ -518,45 +507,59 @@ def test_username_with_comma_or_equals_rejected(self): def test_channel_binding_c_value(self): """c= is base64(gs2-header + ',,' + cbind-data), as the C serializer builds it.""" - cb = py_scram.CryptoDatum(b'\xaa' * 32) + cb = py_scram.CryptoDatum(b"\xaa" * 32) cfirst = py_scram.ClientFirstMessage( - username="testuser", channel_binding_type="tls-server-end-point") + username="testuser", channel_binding_type="tls-server-end-point" + ) sfirst = py_scram.ServerFirstMessage( - client_first=cfirst, salt=self.salt, iterations=100000) + client_first=cfirst, salt=self.salt, iterations=100000 + ) cfinal = py_scram.ClientFinalMessage( - client_first=cfirst, server_first=sfirst, - client_key=self.client_key, stored_key=self.stored_key, channel_binding=cb) - c_b64 = str(cfinal).split(',', 1)[0][len('c='):] - self.assertEqual(b64decode(c_b64), b'p=tls-server-end-point,,' + b'\xaa' * 32) + client_first=cfirst, + server_first=sfirst, + client_key=self.client_key, + stored_key=self.stored_key, + channel_binding=cb, + ) + c_b64 = str(cfinal).split(",", 1)[0][len("c=") :] + self.assertEqual(b64decode(c_b64), b"p=tls-server-end-point,," + b"\xaa" * 32) def test_p_flag_requires_channel_binding(self): """gs2 'p' flag without binding data is rejected (check_gs2_cb_consistency).""" cfirst = py_scram.ClientFirstMessage( - username="testuser", channel_binding_type="tls-server-end-point") + username="testuser", channel_binding_type="tls-server-end-point" + ) sfirst = py_scram.ServerFirstMessage( - client_first=cfirst, salt=self.salt, iterations=100000) + client_first=cfirst, salt=self.salt, iterations=100000 + ) with self.assertRaises(ValueError): py_scram.ClientFinalMessage( - client_first=cfirst, server_first=sfirst, - client_key=self.client_key, stored_key=self.stored_key) + client_first=cfirst, + server_first=sfirst, + client_key=self.client_key, + stored_key=self.stored_key, + ) def test_channel_binding_requires_p_flag(self): """Binding data with a non-'p' flag is rejected (check_gs2_cb_consistency).""" with self.assertRaises(ValueError): py_scram.ClientFinalMessage( - client_first=self.client_first, server_first=self.server_first, - client_key=self.client_key, stored_key=self.stored_key, - channel_binding=py_scram.CryptoDatum(b'\xaa' * 32)) + client_first=self.client_first, + server_first=self.server_first, + client_key=self.client_key, + stored_key=self.stored_key, + channel_binding=py_scram.CryptoDatum(b"\xaa" * 32), + ) def test_server_first_rejects_wrong_nonce_size(self): """The C parser accepts only 32- or 64-byte nonces (scram_parse_nonce).""" - short = b64encode(b'\x22' * 11).decode() + short = b64encode(b"\x22" * 11).decode() with self.assertRaises(py_scram.ScramError): py_scram.ServerFirstMessage(rfc_string=f"r={short},s=c2FsdA==,i=50000") def test_server_first_rejects_low_iterations(self): """The C parser enforces the minimum iteration count (scram_parse_iteration_cnt).""" - nonce = b64encode(b'\x22' * 64).decode() + nonce = b64encode(b"\x22" * 64).decode() with self.assertRaises(py_scram.ScramError): py_scram.ServerFirstMessage(rfc_string=f"r={nonce},s=c2FsdA==,i=100") @@ -642,9 +645,11 @@ def test_signature_algorithm_hashes(self): the signature parameters), DSA, ECDSA-SHA384, and the SHA-1 -> SHA-256 promotion each yield the expected digest.""" expected_len = { - 'rsa_pss_sha256': 32, 'rsa_pss_sha512': 64, 'ecdsa_sha384': 48, - 'rsa_sha1': 32, # SHA-1 promoted to SHA-256 - 'dsa_sha256': 32, + "rsa_pss_sha256": 32, + "rsa_pss_sha512": 64, + "ecdsa_sha384": 48, + "rsa_sha1": 32, # SHA-1 promoted to SHA-256 + "dsa_sha256": 32, } for name, cert_der, expected in CB_VECTORS: with self.subTest(cert=name): @@ -664,58 +669,73 @@ class TestChannelBindingServerSignatureRoundTrip(unittest.TestCase): @staticmethod def _auth_data(): - salt = py_scram.CryptoDatum(b'\x11' * 16) + salt = py_scram.CryptoDatum(b"\x11" * 16) iters = py_scram.SCRAM_MIN_ITERS salted = py_scram.CryptoDatum( - hashlib.pbkdf2_hmac('sha512', b'secret', bytes(salt), iters) + hashlib.pbkdf2_hmac("sha512", b"secret", bytes(salt), iters) ) - return salt, iters, py_scram.generate_scram_auth_data( - salted_password=salted, salt=salt, iterations=iters + return ( + salt, + iters, + py_scram.generate_scram_auth_data( + salted_password=salted, salt=salt, iterations=iters + ), ) @staticmethod def _server_final(cf, sf, fin, server_key): """Build the SERVER_FINAL_MESSAGE the way a server would: sign the AuthMessage (which embeds the client's c= verbatim) with HMAC(ServerKey, ...).""" - c_attr = next(p[2:] for p in str(fin).split(',') if p.startswith('c=')) - r_attr = next(p[2:] for p in str(fin).split(',') if p.startswith('r=')) - bare = str(cf).split(',,', 1)[1] - auth_msg = f'{bare},{sf},c={c_attr},r={r_attr}' - sig = hmac.new(bytes(server_key), auth_msg.encode(), 'sha512').digest() - return py_scram.ServerFinalMessage(rfc_string=f'v={b64encode(sig).decode()}') + c_attr = next(p[2:] for p in str(fin).split(",") if p.startswith("c=")) + r_attr = next(p[2:] for p in str(fin).split(",") if p.startswith("r=")) + bare = str(cf).split(",,", 1)[1] + auth_msg = f"{bare},{sf},c={c_attr},r={r_attr}" + sig = hmac.new(bytes(server_key), auth_msg.encode(), "sha512").digest() + return py_scram.ServerFinalMessage(rfc_string=f"v={b64encode(sig).decode()}") def test_bound_round_trip(self): salt, iters, auth = self._auth_data() binding = py_scram.compute_tls_server_end_point(_CB_TEST_CERT_DER) cf = py_scram.ClientFirstMessage( - username='u', api_key_id=5, + username="u", + api_key_id=5, channel_binding_type=py_scram.CB_TLS_SERVER_END_POINT, ) sf = py_scram.ServerFirstMessage(client_first=cf, salt=salt, iterations=iters) fin = py_scram.ClientFinalMessage( - client_first=cf, server_first=sf, - client_key=auth.client_key, stored_key=auth.stored_key, + client_first=cf, + server_first=sf, + client_key=auth.client_key, + stored_key=auth.stored_key, channel_binding=binding, ) server_final = self._server_final(cf, sf, fin, auth.server_key) # Must not raise: the reconstructed c= must match what the client sent. py_scram.verify_server_signature( - client_first=cf, server_first=sf, client_final=fin, - server_final=server_final, server_key=auth.server_key, + client_first=cf, + server_first=sf, + client_final=fin, + server_final=server_final, + server_key=auth.server_key, ) def test_unbound_round_trip(self): salt, iters, auth = self._auth_data() - cf = py_scram.ClientFirstMessage(username='u', api_key_id=5) + cf = py_scram.ClientFirstMessage(username="u", api_key_id=5) sf = py_scram.ServerFirstMessage(client_first=cf, salt=salt, iterations=iters) fin = py_scram.ClientFinalMessage( - client_first=cf, server_first=sf, - client_key=auth.client_key, stored_key=auth.stored_key, + client_first=cf, + server_first=sf, + client_key=auth.client_key, + stored_key=auth.stored_key, ) server_final = self._server_final(cf, sf, fin, auth.server_key) py_scram.verify_server_signature( - client_first=cf, server_first=sf, client_final=fin, - server_final=server_final, server_key=auth.server_key, + client_first=cf, + server_first=sf, + client_final=fin, + server_final=server_final, + server_key=auth.server_key, ) @@ -729,14 +749,14 @@ def _cryptography_tls_server_end_point(cert_der): except _CryptoUnsupported: sig_hash = None if sig_hash is None: - raise ValueError('tls-server-end-point is undefined for this certificate') + raise ValueError("tls-server-end-point is undefined for this certificate") name = sig_hash.name - if name in ('md5', 'sha1'): - name = 'sha256' + if name in ("md5", "sha1"): + name = "sha256" return hashlib.new(name, bytes(cert_der)).digest() -@unittest.skipUnless(_HAVE_CRYPTOGRAPHY, 'python-cryptography not installed') +@unittest.skipUnless(_HAVE_CRYPTOGRAPHY, "python-cryptography not installed") class TestChannelBindingCryptographyConformance(unittest.TestCase): """Cross-check the pure-python (fallback) channel-binding parser against an independent python-cryptography reference -- a third oracle alongside the @@ -764,5 +784,5 @@ def test_rejection_agrees_with_cryptography(self): _cryptography_tls_server_end_point(cert_der) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/scram/test_scram_client.py b/tests/scram/test_scram_client.py index a4a939e..b4432e7 100644 --- a/tests/scram/test_scram_client.py +++ b/tests/scram/test_scram_client.py @@ -37,9 +37,7 @@ def test_init_with_precomputed_keys(self): server_key = truenas_pyscram.CryptoDatum(b"z" * 64) client = TNScramClient( - client_key=client_key, - stored_key=stored_key, - server_key=server_key + client_key=client_key, stored_key=stored_key, server_key=server_key ) self.assertIsNone(client.raw_key_material) @@ -78,7 +76,9 @@ def test_get_client_first_message_basic(self): client.get_client_first_message(username="testuser") - self.assertIsInstance(client.client_first_message, truenas_pyscram.ClientFirstMessage) + self.assertIsInstance( + client.client_first_message, truenas_pyscram.ClientFirstMessage + ) assert client.client_first_message is not None self.assertEqual(client.client_first_message.username, "testuser") self.assertEqual(client.client_first_message.api_key_id, 0) @@ -98,10 +98,7 @@ def test_get_client_first_message_with_gs2_header(self): """Test client first message with GS2 header.""" client = TNScramClient(raw_key_material="test_password") - client.get_client_first_message( - username="testuser", - gs2_header="p=tls-unique" - ) + client.get_client_first_message(username="testuser", gs2_header="p=tls-unique") assert client.client_first_message is not None self.assertEqual(client.client_first_message.gs2_header, "p=tls-unique") @@ -113,7 +110,9 @@ def test_client_first_message_stored(self): client.get_client_first_message(username="testuser") self.assertIsNotNone(client.client_first_message) - self.assertIsInstance(client.client_first_message, truenas_pyscram.ClientFirstMessage) + self.assertIsInstance( + client.client_first_message, truenas_pyscram.ClientFirstMessage + ) class TestTNScramClientFinalMessage(unittest.TestCase): @@ -136,17 +135,19 @@ def test_get_client_final_message_with_raw_key(self): server_first_msg = truenas_pyscram.ServerFirstMessage( client_first=client.client_first_message, salt=self.auth_data.salt, - iterations=self.auth_data.iterations + iterations=self.auth_data.iterations, ) server_resp: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FIRST_RESPONSE, - 'rfc_str': str(server_first_msg) + "scram_type": ScramMessageType.SERVER_FIRST_RESPONSE, + "rfc_str": str(server_first_msg), } client.get_client_final_message(server_resp) - self.assertIsInstance(client.client_final_message, truenas_pyscram.ClientFinalMessage) + self.assertIsInstance( + client.client_final_message, truenas_pyscram.ClientFinalMessage + ) self.assertIsNotNone(client.client_key) self.assertIsNotNone(client.stored_key) self.assertIsNotNone(client.server_key) @@ -156,24 +157,26 @@ def test_get_client_final_message_with_precomputed_keys(self): client = TNScramClient( client_key=self.auth_data.client_key, stored_key=self.auth_data.stored_key, - server_key=self.auth_data.server_key + server_key=self.auth_data.server_key, ) client.get_client_first_message(username=self.username) server_first_msg = truenas_pyscram.ServerFirstMessage( client_first=client.client_first_message, salt=self.auth_data.salt, - iterations=self.auth_data.iterations + iterations=self.auth_data.iterations, ) server_resp: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FIRST_RESPONSE, - 'rfc_str': str(server_first_msg) + "scram_type": ScramMessageType.SERVER_FIRST_RESPONSE, + "rfc_str": str(server_first_msg), } client.get_client_final_message(server_resp) - self.assertIsInstance(client.client_final_message, truenas_pyscram.ClientFinalMessage) + self.assertIsInstance( + client.client_final_message, truenas_pyscram.ClientFinalMessage + ) def test_get_client_final_message_wrong_type(self): """Test error handling for wrong message type.""" @@ -181,8 +184,8 @@ def test_get_client_final_message_wrong_type(self): client.get_client_first_message(username=self.username) server_resp: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FINAL_RESPONSE, - 'rfc_str': 'v=test' + "scram_type": ScramMessageType.SERVER_FINAL_RESPONSE, + "rfc_str": "v=test", } with self.assertRaises(TypeError) as ctx: @@ -207,8 +210,8 @@ def test_get_client_final_message_high_iterations(self): rfc_string = f"r={nonce_b64},s={salt_b64},i={high_iters}" server_resp: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FIRST_RESPONSE, - 'rfc_str': rfc_string + "scram_type": ScramMessageType.SERVER_FIRST_RESPONSE, + "rfc_str": rfc_string, } # Both C extension and pure Python raise ScramError @@ -236,11 +239,11 @@ def test_verify_server_final_message_success(self): server_first_msg = truenas_pyscram.ServerFirstMessage( client_first=client.client_first_message, salt=self.auth_data.salt, - iterations=self.auth_data.iterations + iterations=self.auth_data.iterations, ) server_resp_first: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FIRST_RESPONSE, - 'rfc_str': str(server_first_msg) + "scram_type": ScramMessageType.SERVER_FIRST_RESPONSE, + "rfc_str": str(server_first_msg), } client.get_client_final_message(server_resp_first) @@ -250,11 +253,11 @@ def test_verify_server_final_message_success(self): server_first=client.server_first_message, client_final=client.client_final_message, stored_key=client.stored_key, - server_key=client.server_key + server_key=client.server_key, ) server_resp_final: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FINAL_RESPONSE, - 'rfc_str': str(server_final_msg) + "scram_type": ScramMessageType.SERVER_FINAL_RESPONSE, + "rfc_str": str(server_final_msg), } result = client.verify_server_final_message(server_resp_final) @@ -267,8 +270,8 @@ def test_verify_server_final_message_wrong_type(self): client = TNScramClient(raw_key_material=self.password) server_resp: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FIRST_RESPONSE, - 'rfc_str': 'r=test,s=test,i=100000' + "scram_type": ScramMessageType.SERVER_FIRST_RESPONSE, + "rfc_str": "r=test,s=test,i=100000", } with self.assertRaises(TypeError) as ctx: @@ -280,26 +283,25 @@ def test_verify_server_final_message_no_server_key(self): """Test error when server_key is not available.""" # Create client with only client_key and stored_key (no server_key) client = TNScramClient( - client_key=self.auth_data.client_key, - stored_key=self.auth_data.stored_key + client_key=self.auth_data.client_key, stored_key=self.auth_data.stored_key ) client.get_client_first_message(username=self.username) server_first_msg = truenas_pyscram.ServerFirstMessage( client_first=client.client_first_message, salt=self.auth_data.salt, - iterations=self.auth_data.iterations + iterations=self.auth_data.iterations, ) server_resp_first: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FIRST_RESPONSE, - 'rfc_str': str(server_first_msg) + "scram_type": ScramMessageType.SERVER_FIRST_RESPONSE, + "rfc_str": str(server_first_msg), } client.get_client_final_message(server_resp_first) # Try to verify without server_key server_resp_final: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FINAL_RESPONSE, - 'rfc_str': 'v=dGVzdA==' + "scram_type": ScramMessageType.SERVER_FINAL_RESPONSE, + "rfc_str": "v=dGVzdA==", } with self.assertRaises(ValueError) as ctx: @@ -330,11 +332,11 @@ def test_full_auth_flow_with_raw_key(self): server_first = truenas_pyscram.ServerFirstMessage( client_first=client.client_first_message, salt=auth_data.salt, - iterations=auth_data.iterations + iterations=auth_data.iterations, ) server_resp_first: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FIRST_RESPONSE, - 'rfc_str': str(server_first) + "scram_type": ScramMessageType.SERVER_FIRST_RESPONSE, + "rfc_str": str(server_first), } # Step 3: Client final message @@ -352,11 +354,11 @@ def test_full_auth_flow_with_raw_key(self): server_first=server_first, client_final=client.client_final_message, stored_key=client.stored_key, - server_key=client.server_key + server_key=client.server_key, ) server_resp_final: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FINAL_RESPONSE, - 'rfc_str': str(server_final) + "scram_type": ScramMessageType.SERVER_FINAL_RESPONSE, + "rfc_str": str(server_final), } # Step 5: Verify server signature @@ -374,7 +376,7 @@ def test_full_auth_flow_with_precomputed_keys(self): client = TNScramClient( client_key=auth_data.client_key, stored_key=auth_data.stored_key, - server_key=auth_data.server_key + server_key=auth_data.server_key, ) # Complete flow @@ -383,11 +385,11 @@ def test_full_auth_flow_with_precomputed_keys(self): server_first = truenas_pyscram.ServerFirstMessage( client_first=client.client_first_message, salt=auth_data.salt, - iterations=auth_data.iterations + iterations=auth_data.iterations, ) server_resp_first: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FIRST_RESPONSE, - 'rfc_str': str(server_first) + "scram_type": ScramMessageType.SERVER_FIRST_RESPONSE, + "rfc_str": str(server_first), } client.get_client_final_message(server_resp_first) @@ -397,11 +399,11 @@ def test_full_auth_flow_with_precomputed_keys(self): server_first=server_first, client_final=client.client_final_message, stored_key=auth_data.stored_key, - server_key=auth_data.server_key + server_key=auth_data.server_key, ) server_resp_final: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FINAL_RESPONSE, - 'rfc_str': str(server_final) + "scram_type": ScramMessageType.SERVER_FINAL_RESPONSE, + "rfc_str": str(server_final), } result = client.verify_server_final_message(server_resp_final) @@ -415,10 +417,7 @@ def test_full_auth_flow_with_api_key(self): auth_data = truenas_pyscram.generate_scram_auth_data() - client = TNScramClient( - raw_key_material=api_key, - api_key_id=api_key_id - ) + client = TNScramClient(raw_key_material=api_key, api_key_id=api_key_id) client.get_client_first_message(username=username) assert client.client_first_message is not None @@ -427,11 +426,11 @@ def test_full_auth_flow_with_api_key(self): server_first = truenas_pyscram.ServerFirstMessage( client_first=client.client_first_message, salt=auth_data.salt, - iterations=auth_data.iterations + iterations=auth_data.iterations, ) server_resp_first: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FIRST_RESPONSE, - 'rfc_str': str(server_first) + "scram_type": ScramMessageType.SERVER_FIRST_RESPONSE, + "rfc_str": str(server_first), } client.get_client_final_message(server_resp_first) @@ -441,11 +440,11 @@ def test_full_auth_flow_with_api_key(self): server_first=server_first, client_final=client.client_final_message, stored_key=client.stored_key, - server_key=client.server_key + server_key=client.server_key, ) server_resp_final: ScramMessage = { - 'scram_type': ScramMessageType.SERVER_FINAL_RESPONSE, - 'rfc_str': str(server_final) + "scram_type": ScramMessageType.SERVER_FINAL_RESPONSE, + "rfc_str": str(server_final), } result = client.verify_server_final_message(server_resp_final) @@ -470,5 +469,5 @@ def test_client_first_without_binding_is_unbound(self): self.assertTrue(msg["rfc_str"].startswith("n,,")) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_auth_api_key.py b/tests/test_auth_api_key.py index f26fe3c..20773c6 100644 --- a/tests/test_auth_api_key.py +++ b/tests/test_auth_api_key.py @@ -33,7 +33,7 @@ class TestRawKeySeparator(unittest.TestCase): def test_separator_is_dash(self): """Verify the separator is a dash as documented.""" - self.assertEqual(RAW_KEY_SEPARATOR, '-') + self.assertEqual(RAW_KEY_SEPARATOR, "-") class TestGetKeyMaterialRawKey(unittest.TestCase): @@ -47,7 +47,7 @@ def test_raw_key_simple(self): self.assertIsInstance(result, KeyData) self.assertEqual(result.key_data_type, KeyDataType.RAW) self.assertIsInstance(result.key_data, dict) - self.assertEqual(cast(RawKeyData, result.key_data)['raw_key'], key) + self.assertEqual(cast(RawKeyData, result.key_data)["raw_key"], key) def test_raw_key_realistic_format(self): """Test raw key with realistic alphanumeric format (no dashes in key material).""" @@ -56,7 +56,7 @@ def test_raw_key_realistic_format(self): result = get_key_material(key) self.assertEqual(result.key_data_type, KeyDataType.RAW) - self.assertEqual(cast(RawKeyData, result.key_data)['raw_key'], key) + self.assertEqual(cast(RawKeyData, result.key_data)["raw_key"], key) def test_raw_key_long_id(self): """Test raw key with long numeric ID.""" @@ -64,7 +64,7 @@ def test_raw_key_long_id(self): result = get_key_material(key) self.assertEqual(result.key_data_type, KeyDataType.RAW) - self.assertEqual(cast(RawKeyData, result.key_data)['raw_key'], key) + self.assertEqual(cast(RawKeyData, result.key_data)["raw_key"], key) class TestGetKeyMaterialJSON(unittest.TestCase): @@ -77,7 +77,7 @@ def test_json_with_raw_key(self): result = get_key_material(json_str) self.assertEqual(result.key_data_type, KeyDataType.RAW) - self.assertEqual(cast(RawKeyData, result.key_data)['raw_key'], "789-jsonkey123") + self.assertEqual(cast(RawKeyData, result.key_data)["raw_key"], "789-jsonkey123") def test_json_with_precomputed_keys(self): """Test JSON with pre-computed cryptographic keys.""" @@ -85,17 +85,17 @@ def test_json_with_precomputed_keys(self): "client_key": "Y2xpZW50a2V5MTIz", "stored_key": "c3RvcmVka2V5NDU2", "server_key": "c2VydmVya2V5Nzg5", - "api_key_id": 42 + "api_key_id": 42, } json_str = json.dumps(data) result = get_key_material(json_str) self.assertEqual(result.key_data_type, KeyDataType.PRECOMPUTED) precomputed = cast(PrecomputedKeyData, result.key_data) - self.assertEqual(precomputed['client_key'], "Y2xpZW50a2V5MTIz") - self.assertEqual(precomputed['stored_key'], "c3RvcmVka2V5NDU2") - self.assertEqual(precomputed['server_key'], "c2VydmVya2V5Nzg5") - self.assertEqual(precomputed['api_key_id'], 42) + self.assertEqual(precomputed["client_key"], "Y2xpZW50a2V5MTIz") + self.assertEqual(precomputed["stored_key"], "c3RvcmVka2V5NDU2") + self.assertEqual(precomputed["server_key"], "c2VydmVya2V5Nzg5") + self.assertEqual(precomputed["api_key_id"], 42) def test_json_missing_required_field(self): """Test JSON missing a required field raises ValueError.""" @@ -103,7 +103,7 @@ def test_json_missing_required_field(self): "client_key": "Y2xpZW50a2V5MTIz", "stored_key": "c3RvcmVka2V5NDU2", # Missing server_key - "api_key_id": 42 + "api_key_id": 42, } json_str = json.dumps(data) @@ -127,8 +127,8 @@ def test_ini_with_section_header(self): self.assertEqual(result.key_data_type, KeyDataType.PRECOMPUTED) precomputed = cast(PrecomputedKeyData, result.key_data) - self.assertEqual(precomputed['api_key_id'], 100) - self.assertEqual(precomputed['client_key'], "Y2xpZW50a2V5MTIz") + self.assertEqual(precomputed["api_key_id"], 100) + self.assertEqual(precomputed["client_key"], "Y2xpZW50a2V5MTIz") def test_ini_without_section_header(self): """Test INI format without section header uses [DEFAULT] section.""" @@ -142,7 +142,7 @@ def test_ini_without_section_header(self): result = get_key_material(ini_str) self.assertEqual(result.key_data_type, KeyDataType.PRECOMPUTED) - self.assertEqual(cast(PrecomputedKeyData, result.key_data)['api_key_id'], 200) + self.assertEqual(cast(PrecomputedKeyData, result.key_data)["api_key_id"], 200) def test_ini_single_section_any_name(self): """Test INI with single section accepts any name.""" @@ -155,7 +155,7 @@ def test_ini_single_section_any_name(self): result = get_key_material(ini_str) self.assertEqual(result.key_data_type, KeyDataType.PRECOMPUTED) - self.assertEqual(cast(PrecomputedKeyData, result.key_data)['api_key_id'], 300) + self.assertEqual(cast(PrecomputedKeyData, result.key_data)["api_key_id"], 300) def test_ini_with_multiple_sections_requires_truenas_api_key(self): """Test INI with multiple sections requires [TRUENAS_API_KEY].""" @@ -174,8 +174,10 @@ def test_ini_with_multiple_sections_requires_truenas_api_key(self): result = get_key_material(ini_str) self.assertEqual(result.key_data_type, KeyDataType.PRECOMPUTED) - self.assertEqual(cast(PrecomputedKeyData, result.key_data)['api_key_id'], 400) - self.assertEqual(cast(PrecomputedKeyData, result.key_data)['client_key'], "correct1") + self.assertEqual(cast(PrecomputedKeyData, result.key_data)["api_key_id"], 400) + self.assertEqual( + cast(PrecomputedKeyData, result.key_data)["client_key"], "correct1" + ) def test_ini_with_multiple_sections_without_truenas_api_key_fails(self): """Test INI with multiple sections but no [TRUENAS_API_KEY] raises error.""" @@ -197,7 +199,7 @@ def test_ini_with_raw_key(self): result = get_key_material(ini_str) self.assertEqual(result.key_data_type, KeyDataType.RAW) - self.assertEqual(cast(RawKeyData, result.key_data)['raw_key'], "500-rawkeydata") + self.assertEqual(cast(RawKeyData, result.key_data)["raw_key"], "500-rawkeydata") class TestParseINIConfig(unittest.TestCase): @@ -211,8 +213,8 @@ def test_single_section(self): """ result = _parse_ini_config(ini_str) - self.assertEqual(result['client_key'], "test1") - self.assertEqual(result['api_key_id'], 123) # Should be converted to int + self.assertEqual(result["client_key"], "test1") + self.assertEqual(result["api_key_id"], 123) # Should be converted to int def test_default_section_explicit(self): """Test parsing INI with explicit [DEFAULT] section.""" @@ -222,8 +224,8 @@ def test_default_section_explicit(self): """ result = _parse_ini_config(ini_str) - self.assertEqual(result['client_key'], "test2") - self.assertEqual(result['api_key_id'], 456) + self.assertEqual(result["client_key"], "test2") + self.assertEqual(result["api_key_id"], 456) def test_api_key_id_type_conversion(self): """Test that api_key_id is converted from string to int.""" @@ -233,9 +235,9 @@ def test_api_key_id_type_conversion(self): """ result = _parse_ini_config(ini_str) - self.assertIsInstance(result['api_key_id'], int) - self.assertEqual(result['api_key_id'], 789) - self.assertIsInstance(result['other_field'], str) + self.assertIsInstance(result["api_key_id"], int) + self.assertEqual(result["api_key_id"], 789) + self.assertIsInstance(result["other_field"], str) class TestGetKeyMaterialFromFile(unittest.TestCase): @@ -247,10 +249,10 @@ def test_read_json_from_file(self): "client_key": "fromfile1", "stored_key": "fromfile2", "server_key": "fromfile3", - "api_key_id": 600 + "api_key_id": 600, } - with tempfile.NamedTemporaryFile(mode='w', suffix='.json', delete=False) as f: + with tempfile.NamedTemporaryFile(mode="w", suffix=".json", delete=False) as f: json.dump(data, f) f.flush() temp_path = f.name @@ -259,7 +261,9 @@ def test_read_json_from_file(self): result = get_key_material(temp_path) self.assertEqual(result.key_data_type, KeyDataType.PRECOMPUTED) - self.assertEqual(cast(PrecomputedKeyData, result.key_data)['api_key_id'], 600) + self.assertEqual( + cast(PrecomputedKeyData, result.key_data)["api_key_id"], 600 + ) finally: Path(temp_path).unlink() @@ -267,7 +271,7 @@ def test_read_raw_key_from_file(self): """Test reading raw API key from a file.""" raw_key = "700-keyfromfile" - with tempfile.NamedTemporaryFile(mode='w', suffix='.txt', delete=False) as f: + with tempfile.NamedTemporaryFile(mode="w", suffix=".txt", delete=False) as f: f.write(raw_key) f.flush() temp_path = f.name @@ -276,7 +280,7 @@ def test_read_raw_key_from_file(self): result = get_key_material(temp_path) self.assertEqual(result.key_data_type, KeyDataType.RAW) - self.assertEqual(cast(RawKeyData, result.key_data)['raw_key'], raw_key) + self.assertEqual(cast(RawKeyData, result.key_data)["raw_key"], raw_key) finally: Path(temp_path).unlink() @@ -289,7 +293,7 @@ def test_read_ini_from_file(self): api_key_id = 800 """ - with tempfile.NamedTemporaryFile(mode='w', suffix='.ini', delete=False) as f: + with tempfile.NamedTemporaryFile(mode="w", suffix=".ini", delete=False) as f: f.write(ini_content) f.flush() temp_path = f.name @@ -298,7 +302,9 @@ def test_read_ini_from_file(self): result = get_key_material(temp_path) self.assertEqual(result.key_data_type, KeyDataType.PRECOMPUTED) - self.assertEqual(cast(PrecomputedKeyData, result.key_data)['api_key_id'], 800) + self.assertEqual( + cast(PrecomputedKeyData, result.key_data)["api_key_id"], 800 + ) finally: Path(temp_path).unlink() @@ -350,7 +356,7 @@ def test_precomputed_client_key_wrong_type(self): "client_key": 123, # Should be string "stored_key": "c3RvcmVka2V5NDU2", "server_key": "c2VydmVya2V5Nzg5", - "api_key_id": 42 + "api_key_id": 42, } json_str = json.dumps(data) @@ -364,7 +370,7 @@ def test_precomputed_api_key_id_wrong_type(self): "client_key": "Y2xpZW50a2V5MTIz", "stored_key": "c3RvcmVka2V5NDU2", "server_key": "c2VydmVya2V5Nzg5", - "api_key_id": "should_be_int" # Should be int + "api_key_id": "should_be_int", # Should be int } json_str = json.dumps(data) @@ -378,8 +384,8 @@ class TestKeyDataType(unittest.TestCase): def test_enum_values(self): """Test KeyDataType enum has correct values.""" - self.assertEqual(KeyDataType.RAW, 'RAW') - self.assertEqual(KeyDataType.PRECOMPUTED, 'PRECOMPUTED') + self.assertEqual(KeyDataType.RAW, "RAW") + self.assertEqual(KeyDataType.PRECOMPUTED, "PRECOMPUTED") def test_enum_comparison(self): """Test KeyDataType can be compared.""" @@ -417,7 +423,7 @@ def test_tls_socket_returns_der(self): class _StubWS: """Minimal WSClient stand-in for channel-binding policy tests.""" - def __init__(self, *, cert_der=None, url=''): + def __init__(self, *, cert_der=None, url=""): self._cert_der = cert_der self.url = url @@ -439,38 +445,39 @@ class TestResolveChannelBinding(unittest.TestCase): """ def test_disabled_is_unbound_over_tls(self): - c = _StubClient(_StubWS(cert_der=b'DER', url='wss://nas/api/current')) + c = _StubClient(_StubWS(cert_der=b"DER", url="wss://nas/api/current")) self.assertIsNone(_resolve_channel_binding(c, False)) def test_disabled_is_unbound_over_non_tls(self): - c = _StubClient(_StubWS(url='ws://nas/api/current')) + c = _StubClient(_StubWS(url="ws://nas/api/current")) self.assertIsNone(_resolve_channel_binding(c, False)) def test_tls_returns_binding(self): - c = _StubClient(_StubWS(cert_der=b'DER', url='wss://nas/api/current')) - with mock.patch.object(auth_api_key, 'compute_tls_server_end_point', - return_value='BINDING') as m: - self.assertEqual(_resolve_channel_binding(c, True), 'BINDING') - m.assert_called_once_with(b'DER') + c = _StubClient(_StubWS(cert_der=b"DER", url="wss://nas/api/current")) + with mock.patch.object( + auth_api_key, "compute_tls_server_end_point", return_value="BINDING" + ) as m: + self.assertEqual(_resolve_channel_binding(c, True), "BINDING") + m.assert_called_once_with(b"DER") def test_tls_but_backend_without_support_raises(self): - c = _StubClient(_StubWS(cert_der=b'DER', url='wss://nas/api/current')) - with mock.patch.object(auth_api_key, 'compute_tls_server_end_point', None): + c = _StubClient(_StubWS(cert_der=b"DER", url="wss://nas/api/current")) + with mock.patch.object(auth_api_key, "compute_tls_server_end_point", None): with self.assertRaises(ValueError) as ctx: _resolve_channel_binding(c, True) - self.assertIn('channel_binding=False', str(ctx.exception)) + self.assertIn("channel_binding=False", str(ctx.exception)) def test_unix_socket_is_exempt(self): # Local IPC: a required binding resolves to unbound rather than failing. - c = _StubClient(_StubWS(url='ws+unix:///run/middleware.sock')) + c = _StubClient(_StubWS(url="ws+unix:///run/middleware.sock")) self.assertIsNone(_resolve_channel_binding(c, True)) def test_non_tls_network_raises(self): - c = _StubClient(_StubWS(url='ws://nas/api/current')) + c = _StubClient(_StubWS(url="ws://nas/api/current")) with self.assertRaises(ValueError) as ctx: _resolve_channel_binding(c, True) - self.assertIn('TLS', str(ctx.exception)) - self.assertIn('channel_binding=False', str(ctx.exception)) + self.assertIn("TLS", str(ctx.exception)) + self.assertIn("channel_binding=False", str(ctx.exception)) def test_missing_transport_raises_when_required(self): with self.assertRaises(ValueError): @@ -484,19 +491,19 @@ class TestMidcltChannelBindingFlag(unittest.TestCase): """Test the midclt --no-channel-binding and --plain CLI flags.""" def test_default_keeps_channel_binding_enabled(self): - args = get_parser().parse_args(['call', 'system.info']) + args = get_parser().parse_args(["call", "system.info"]) self.assertFalse(args.no_channel_binding) def test_flag_disables_channel_binding(self): - args = get_parser().parse_args(['--no-channel-binding', 'call', 'system.info']) + args = get_parser().parse_args(["--no-channel-binding", "call", "system.info"]) self.assertTrue(args.no_channel_binding) def test_default_uses_scram_not_plain(self): - args = get_parser().parse_args(['call', 'system.info']) + args = get_parser().parse_args(["call", "system.info"]) self.assertFalse(args.plain) def test_plain_flag_selects_plain(self): - args = get_parser().parse_args(['--plain', 'call', 'system.info']) + args = get_parser().parse_args(["--plain", "call", "system.info"]) self.assertTrue(args.plain) @@ -510,15 +517,15 @@ def __init__(self, mechanisms, *, responses=None): def call(self, method, *args): self.calls.append(method) - if method == 'auth.mechanism_choices': + if method == "auth.mechanism_choices": return self._mechanisms # Default to a generic SUCCESS so the PLAIN/legacy paths complete. - return self._responses.get(method, {'response_type': 'SUCCESS'}) + return self._responses.get(method, {"response_type": "SUCCESS"}) # A syntactically valid raw API key (-). The SCRAM path (which would run # PBKDF2 on this) is never reached by these mechanism-policy tests. -_RAW_KEY = '7-' + 'a' * 64 +_RAW_KEY = "7-" + "a" * 64 class TestApiKeyMechanismPolicy(unittest.TestCase): @@ -527,44 +534,54 @@ class TestApiKeyMechanismPolicy(unittest.TestCase): explicit opt-in that sends the key.""" def test_scram_no_scram_server_raises_without_sending_key(self): - c = _CallStubClient(['API_KEY_PLAIN']) + c = _CallStubClient(["API_KEY_PLAIN"]) with self.assertRaises(ValueError) as ctx: auth_api_key.api_key_authenticate( - c, auth_api_key.APIKeyAuthMech.SCRAM, 'user', _RAW_KEY, + c, + auth_api_key.APIKeyAuthMech.SCRAM, + "user", + _RAW_KEY, ) # Points the caller at the explicit opt-in... - self.assertIn('PLAIN', str(ctx.exception)) + self.assertIn("PLAIN", str(ctx.exception)) # ...and, security-critical, the key was NOT transmitted before the refusal. - self.assertNotIn('auth.login_ex', c.calls) + self.assertNotIn("auth.login_ex", c.calls) def test_explicit_plain_sends_key(self): - c = _CallStubClient(['API_KEY_PLAIN']) + c = _CallStubClient(["API_KEY_PLAIN"]) auth_api_key.api_key_authenticate( - c, auth_api_key.APIKeyAuthMech.PLAIN, 'user', _RAW_KEY, + c, + auth_api_key.APIKeyAuthMech.PLAIN, + "user", + _RAW_KEY, ) # The explicit opt-in completes the plaintext exchange. - self.assertIn('auth.login_ex', c.calls) + self.assertIn("auth.login_ex", c.calls) def test_legacy_endpoint_uses_plain_path(self): # LegacyClient authenticates via PLAIN over the legacy /websocket endpoint. - c = _CallStubClient(['API_KEY_PLAIN']) + c = _CallStubClient(["API_KEY_PLAIN"]) auth_api_key.api_key_authenticate( - c, auth_api_key.APIKeyAuthMech.PLAIN, 'user', _RAW_KEY, + c, + auth_api_key.APIKeyAuthMech.PLAIN, + "user", + _RAW_KEY, use_legacy_endpoint=True, ) - self.assertIn('auth.login_with_api_key', c.calls) + self.assertIn("auth.login_with_api_key", c.calls) def test_jsonrpc_login_defaults_to_scram(self): # Lock the security-relevant default: API-key login must default to SCRAM, never a # mechanism that could transmit the key in plaintext. import inspect from truenas_api_client import JSONRPCClient + sig = inspect.signature(JSONRPCClient.login_with_api_key) self.assertEqual( - sig.parameters['auth_mechanism'].default, + sig.parameters["auth_mechanism"].default, auth_api_key.APIKeyAuthMech.SCRAM, ) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_close_race.py b/tests/test_close_race.py index cdb6eef..423dfa8 100644 --- a/tests/test_close_race.py +++ b/tests/test_close_race.py @@ -5,6 +5,7 @@ handshake, but the run_forever thread's teardown() clears self.sock concurrently, raising AttributeError (github.com/websocket-client/websocket-client/issues/1056). """ + import unittest from websocket import WebSocketApp @@ -53,7 +54,7 @@ def close(self, **kwargs): class TestCloseRace(unittest.TestCase): def test_close_survives_teardown_race(self): """Replays the race against the installed websocket-client version.""" - app = WebSocketApp('ws://127.0.0.1/api/current') + app = WebSocketApp("ws://127.0.0.1/api/current") app.sock = _RacingSock(app) # type: ignore[assignment] client = _ClientStub() ws = _bare(WSClient, app=app, client=client) @@ -73,5 +74,5 @@ def test_close_swallows_attribute_error(self): self.assertEqual(client.close_codes, [STATUS_NORMAL]) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_ejson.py b/tests/test_ejson.py index c738a5d..6904dff 100644 --- a/tests/test_ejson.py +++ b/tests/test_ejson.py @@ -1,5 +1,6 @@ # SPDX-License-Identifier: LGPL-3.0-or-later """ejson round-trips its supported types, and decodes $date without double-counting sub-second ms.""" + import unittest from datetime import date, datetime, time, timezone from ipaddress import IPv4Interface, IPv6Interface @@ -32,18 +33,18 @@ def test_time_with_utc_offset(self): self.assertEqual(loads(dumps(v)), v) def test_set(self): - v = {1, 2, 3, 'a'} + v = {1, 2, 3, "a"} self.assertEqual(loads(dumps(v)), v) def test_frozenset_decodes_to_set(self): self.assertEqual(loads(dumps(frozenset({1, 2, 3}))), {1, 2, 3}) def test_ipv4_interface(self): - v = IPv4Interface('192.168.1.10/24') + v = IPv4Interface("192.168.1.10/24") self.assertEqual(loads(dumps(v)), v) def test_ipv6_interface(self): - v = IPv6Interface('2001:db8::1/64') + v = IPv6Interface("2001:db8::1/64") self.assertEqual(loads(dumps(v)), v) @@ -63,5 +64,5 @@ def test_whole_seconds(self): ) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_job_disconnect.py b/tests/test_job_disconnect.py index fceb98c..06e8480 100644 --- a/tests/test_job_disconnect.py +++ b/tests/test_job_disconnect.py @@ -3,6 +3,7 @@ Both the current JSON-RPC client and the legacy client share this path, so both are covered. """ + import errno import unittest from collections import defaultdict @@ -22,12 +23,26 @@ def _bare(cls, **attrs): def _current(): - return _bare(JSONRPCClient, _calls={}, _jobs=defaultdict(dict), _jobs_lock=Lock(), _unclaimed_jobs={}) + return _bare( + JSONRPCClient, + _calls={}, + _jobs=defaultdict(dict), + _jobs_lock=Lock(), + _unclaimed_jobs={}, + ) def _legacy(): - return _bare(LegacyClient, _calls={}, _jobs=defaultdict(dict), _jobs_lock=Lock(), _unclaimed_jobs={}, - _connected=Event(), _closed=Event(), _connection_error=None) + return _bare( + LegacyClient, + _calls={}, + _jobs=defaultdict(dict), + _jobs_lock=Lock(), + _unclaimed_jobs={}, + _connected=Event(), + _closed=Event(), + _connection_error=None, + ) class _MutatingEvent(Event): @@ -38,7 +53,7 @@ def __init__(self, jobs): self._jobs = jobs def set(self): - self._jobs['inserted-during-iteration'] = {} + self._jobs["inserted-during-iteration"] = {} super().set() @@ -46,9 +61,11 @@ class TestAbortedJobResult(unittest.TestCase): """An aborted job (no exc_info) reports failure via ClientException.""" def _check(self, client, job_cls): - job = job_cls(client, 'j1') - client._jobs['j1'].update(state='ABORTED', exc_info=None, exception=None, error=None) - client._jobs['j1']['__ready'].set() + job = job_cls(client, "j1") + client._jobs["j1"].update( + state="ABORTED", exc_info=None, exception=None, error=None + ) + client._jobs["j1"]["__ready"].set() with self.assertRaises(ClientException): job.result() @@ -63,13 +80,18 @@ class TestValidationJobResult(unittest.TestCase): """A validation-failed job surfaces as ValidationErrors, not a generic ClientException.""" def _check(self, client, job_cls): - job = job_cls(client, 'j1') - client._jobs['j1'].update( - state='FAILED', - exc_info={'type': 'VALIDATION', 'repr': 'ValidationErrors', - 'extra': [('field', 'is required', errno.EINVAL)]}, - exception='validation failed', error='validation failed') - client._jobs['j1']['__ready'].set() + job = job_cls(client, "j1") + client._jobs["j1"].update( + state="FAILED", + exc_info={ + "type": "VALIDATION", + "repr": "ValidationErrors", + "extra": [("field", "is required", errno.EINVAL)], + }, + exception="validation failed", + error="validation failed", + ) + client._jobs["j1"]["__ready"].set() with self.assertRaises(ValidationErrors): job.result() @@ -85,14 +107,14 @@ class TestJobResultAfterDisconnect(unittest.TestCase): def test_current(self): c = _current() - job = Job(c, 'j1') - c._broadcast_error(ClientException('closed', errno.ECONNABORTED)) + job = Job(c, "j1") + c._broadcast_error(ClientException("closed", errno.ECONNABORTED)) with self.assertRaises(ClientException): job.result() def test_legacy(self): c = _legacy() - job = LegacyJob(c, 'j1') + job = LegacyJob(c, "j1") c.on_close(1006) with self.assertRaises(ClientException): job.result() @@ -103,16 +125,16 @@ class TestBroadcastConcurrentModification(unittest.TestCase): def test_current(self): c = _current() - c._jobs['j1'] = {'__ready': _MutatingEvent(c._jobs)} - c._broadcast_error(ClientException('closed', errno.ECONNABORTED)) - self.assertIn('inserted-during-iteration', c._jobs) + c._jobs["j1"] = {"__ready": _MutatingEvent(c._jobs)} + c._broadcast_error(ClientException("closed", errno.ECONNABORTED)) + self.assertIn("inserted-during-iteration", c._jobs) def test_legacy(self): c = _legacy() - c._jobs['j1'] = {'__ready': _MutatingEvent(c._jobs)} + c._jobs["j1"] = {"__ready": _MutatingEvent(c._jobs)} c.on_close(1006) - self.assertIn('inserted-during-iteration', c._jobs) + self.assertIn("inserted-during-iteration", c._jobs) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_job_leak.py b/tests/test_job_leak.py index 2bbd507..c7a7667 100644 --- a/tests/test_job_leak.py +++ b/tests/test_job_leak.py @@ -4,6 +4,7 @@ The server sends `core.get_jobs` events for every job the credentials can read, so a long-lived client sees jobs started by every other client on the system. """ + import unittest from collections import defaultdict from threading import Event, Lock @@ -21,33 +22,61 @@ def _bare(cls, **attrs): def _current(new_style=True): - client = _bare(JSONRPCClient, _calls={}, _jobs=defaultdict(dict), _jobs_lock=Lock(), - _unclaimed_jobs={}, _new_style_jobs=new_style, - _event_callbacks=defaultdict(list)) - client._event_callbacks['core.get_jobs'].append( - {'callback': client._jobs_callback, 'sync': True, 'event': Event()} + client = _bare( + JSONRPCClient, + _calls={}, + _jobs=defaultdict(dict), + _jobs_lock=Lock(), + _unclaimed_jobs={}, + _new_style_jobs=new_style, + _event_callbacks=defaultdict(list), + ) + client._event_callbacks["core.get_jobs"].append( + {"callback": client._jobs_callback, "sync": True, "event": Event()} ) return client def _legacy(): - return _bare(LegacyClient, _calls={}, _jobs=defaultdict(dict), _jobs_lock=Lock(), - _unclaimed_jobs={}) + return _bare( + LegacyClient, + _calls={}, + _jobs=defaultdict(dict), + _jobs_lock=Lock(), + _unclaimed_jobs={}, + ) def _fields(job_id, state, message_ids, result=None): - return {'id': job_id, 'message_ids': message_ids, 'method': 'pool.dataset.query', - 'arguments': [], 'state': state, 'result': result, 'error': None, 'exception': None, - 'exc_info': None, 'logs_excerpt': None, - 'progress': {'percent': 100, 'description': '', 'extra': None}} + return { + "id": job_id, + "message_ids": message_ids, + "method": "pool.dataset.query", + "arguments": [], + "state": state, + "result": result, + "error": None, + "exception": None, + "exc_info": None, + "logs_excerpt": None, + "progress": {"percent": 100, "description": "", "extra": None}, + } def _run(client, job_id, message_ids, result=None): - for state in ('RUNNING', 'SUCCESS'): + for state in ("RUNNING", "SUCCESS"): fields = _fields(job_id, state, message_ids, result) - client._recv({'method': 'collection_update', 'params': { - 'collection': 'core.get_jobs', 'msg': 'changed', 'id': job_id, 'fields': fields, - }}) + client._recv( + { + "method": "collection_update", + "params": { + "collection": "core.get_jobs", + "msg": "changed", + "id": job_id, + "fields": fields, + }, + } + ) class TestForeignJobsDropped(unittest.TestCase): @@ -56,14 +85,16 @@ class TestForeignJobsDropped(unittest.TestCase): def test_current(self): client = _current() for job_id in range(500): - _run(client, job_id, ['another-clients-message-id'], result='x' * 512) + _run(client, job_id, ["another-clients-message-id"], result="x" * 512) self.assertEqual(len(client._jobs), 0) def test_legacy_keeps_bounded_window(self): client = _legacy() for job_id in range(500): - client._jobs_callback('CHANGED', fields=_fields(job_id, 'SUCCESS', [], 'x' * 512)) + client._jobs_callback( + "CHANGED", fields=_fields(job_id, "SUCCESS", [], "x" * 512) + ) self.assertLessEqual(len(client._jobs), UNCLAIMED_JOBS_MAX) @@ -73,20 +104,20 @@ class TestOurJobsKept(unittest.TestCase): def test_result_arrives_before_job_object_exists(self): client = _current() - call = Call('pool.scrub.run', (), wants_job=True) + call = Call("pool.scrub.run", (), wants_job=True) client._calls[call.id] = call - _run(client, 42, [call.id], result='done') + _run(client, 42, [call.id], result="done") self.assertEqual(call.job_id, 42) - self.assertEqual(Job(client, call.result).result(), 'done') + self.assertEqual(Job(client, call.result).result(), "done") self.assertEqual(len(client._jobs), 0) def test_legacy_result_arrives_before_job_object_exists(self): client = _legacy() - client._jobs_callback('CHANGED', fields=_fields(7, 'SUCCESS', [], 'done')) + client._jobs_callback("CHANGED", fields=_fields(7, "SUCCESS", [], "done")) - self.assertEqual(LegacyJob(client, 7).result(), 'done') + self.assertEqual(LegacyJob(client, 7).result(), "done") self.assertEqual(len(client._jobs), 0) @@ -96,14 +127,14 @@ class TestFireAndForgetJob(unittest.TestCase): def test_current(self): client = _current() for job_id in range(500): - call = Call('pool.scrub.run', (), wants_job=False) + call = Call("pool.scrub.run", (), wants_job=False) client._calls[call.id] = call - _run(client, job_id, [call.id], result='x' * 512) + _run(client, job_id, [call.id], result="x" * 512) self.assertEqual(call.result, job_id) self.assertEqual(len(client._jobs), 0) self.assertEqual(len(client._calls), 0) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_midclt_login_failure.py b/tests/test_midclt_login_failure.py index c983413..a7cf03d 100644 --- a/tests/test_midclt_login_failure.py +++ b/tests/test_midclt_login_failure.py @@ -5,6 +5,7 @@ code or capturing stdout saw a successful, JSON-producing run. It must now write to stderr and exit non-zero. """ + import contextlib import io import sys @@ -17,35 +18,59 @@ class TestMidcltLoginFailureExit(unittest.TestCase): def _run_main(self, argv): stdout, stderr = io.StringIO(), io.StringIO() - with mock.patch.object(sys, 'argv', argv), \ - mock.patch.object(truenas_api_client, 'Client') as mock_client, \ - contextlib.redirect_stdout(stdout), \ - contextlib.redirect_stderr(stderr): + with ( + mock.patch.object(sys, "argv", argv), + mock.patch.object(truenas_api_client, "Client") as mock_client, + contextlib.redirect_stdout(stdout), + contextlib.redirect_stderr(stderr), + ): # `with Client(...) as c:` -> c is the entered context manager. Make login fail. - mock_client.return_value.__exit__.return_value = False # do not swallow SystemExit + mock_client.return_value.__exit__.return_value = ( + False # do not swallow SystemExit + ) c = mock_client.return_value.__enter__.return_value - c.login_with_password.side_effect = ValueError('Invalid username or password') - c.login_with_api_key.side_effect = ValueError('Invalid API key') + c.login_with_password.side_effect = ValueError( + "Invalid username or password" + ) + c.login_with_api_key.side_effect = ValueError("Invalid API key") with self.assertRaises(SystemExit) as ctx: truenas_api_client.main() return ctx.exception.code, stdout.getvalue(), stderr.getvalue() def test_password_login_failure_exits_nonzero_on_stderr(self): code, out, err = self._run_main( - ['midclt', '-u', 'ws://h/api/current', '-U', 'admin', '-P', 'wrong', 'call', 'system.info'] + [ + "midclt", + "-u", + "ws://h/api/current", + "-U", + "admin", + "-P", + "wrong", + "call", + "system.info", + ] ) self.assertEqual(code, 1) - self.assertIn('Failed to login', err) - self.assertNotIn('Failed to login', out) + self.assertIn("Failed to login", err) + self.assertNotIn("Failed to login", out) def test_api_key_login_failure_exits_nonzero_on_stderr(self): code, out, err = self._run_main( - ['midclt', '-u', 'ws://h/api/current', '-K', '1-badkey', 'call', 'system.info'] + [ + "midclt", + "-u", + "ws://h/api/current", + "-K", + "1-badkey", + "call", + "system.info", + ] ) self.assertEqual(code, 1) - self.assertIn('Failed to login', err) - self.assertNotIn('Failed to login', out) + self.assertIn("Failed to login", err) + self.assertNotIn("Failed to login", out) -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/tests/test_reserved_ports_tls.py b/tests/test_reserved_ports_tls.py index e09fb11..9a6256b 100644 --- a/tests/test_reserved_ports_tls.py +++ b/tests/test_reserved_ports_tls.py @@ -6,6 +6,7 @@ some other scheme it cannot handle), connect() requires an explicit ws:// URI. The modern JSON-RPC client and the legacy client share this path, so both are covered here. """ + import sys import unittest from unittest import mock @@ -26,18 +27,25 @@ def test_wss_uri_is_refused(self): """wss:// + reserved_ports raises rather than connecting in plaintext.""" for cls in WS_CLIENT_CLASSES: with self.subTest(client=cls.__module__): - ws = cls('wss://nas.example.com/api/current', client=None, # type: ignore[arg-type] - reserved_ports=True) + ws = cls( + "wss://nas.example.com/api/current", + client=None, # type: ignore[arg-type] + reserved_ports=True, + ) with self.assertRaises(ClientException) as ctx: ws.connect() - self.assertIn('reserved_ports', str(ctx.exception)) + self.assertIn("reserved_ports", str(ctx.exception)) def test_wss_refused_regardless_of_verify_ssl(self): """The guard is purely by scheme, so verify_ssl=False does not let a wss:// URI through.""" for cls in WS_CLIENT_CLASSES: with self.subTest(client=cls.__module__): - ws = cls('wss://nas.example.com/api/current', client=None, # type: ignore[arg-type] - reserved_ports=True, verify_ssl=False) + ws = cls( + "wss://nas.example.com/api/current", + client=None, # type: ignore[arg-type] + reserved_ports=True, + verify_ssl=False, + ) with self.assertRaises(ClientException): ws.connect() @@ -45,26 +53,38 @@ def test_non_ws_scheme_is_refused(self): """Any scheme other than ws:// (e.g. http://) is refused, not only wss://.""" for cls in WS_CLIENT_CLASSES: with self.subTest(client=cls.__module__): - ws = cls('http://nas.example.com:6000/api/current', client=None, # type: ignore[arg-type] - reserved_ports=True) + ws = cls( + "http://nas.example.com:6000/api/current", + client=None, # type: ignore[arg-type] + reserved_ports=True, + ) with self.assertRaises(ClientException) as ctx: ws.connect() - self.assertIn('reserved_ports', str(ctx.exception)) + self.assertIn("reserved_ports", str(ctx.exception)) def test_plaintext_ws_passes_the_guard(self): """ws:// + reserved_ports (the failover use case) is allowed through to the bind step.""" for cls in WS_CLIENT_CLASSES: with self.subTest(client=cls.__module__): - ws = cls('ws://nas.example.com:6000/api/current', client=None, # type: ignore[arg-type] - reserved_ports=True) + ws = cls( + "ws://nas.example.com:6000/api/current", + client=None, # type: ignore[arg-type] + reserved_ports=True, + ) module = sys.modules[cls.__module__] # Avoid real network I/O: fake the socket and stop at the bind step. If the guard # wrongly rejected ws://, we'd see ClientException instead of _ReachedBind. - with mock.patch.object(module.socket, 'socket', return_value=mock.MagicMock()), \ - mock.patch.object(ws, '_bind_to_reserved_port', side_effect=_ReachedBind): + with ( + mock.patch.object( + module.socket, "socket", return_value=mock.MagicMock() + ), + mock.patch.object( + ws, "_bind_to_reserved_port", side_effect=_ReachedBind + ), + ): with self.assertRaises(_ReachedBind): ws.connect() -if __name__ == '__main__': +if __name__ == "__main__": unittest.main() diff --git a/truenas_api_client/__init__.py b/truenas_api_client/__init__.py index 3cf2987..773d56b 100644 --- a/truenas_api_client/__init__.py +++ b/truenas_api_client/__init__.py @@ -28,6 +28,7 @@ c.close() """ + import argparse from base64 import b64decode from collections import defaultdict @@ -57,23 +58,52 @@ from . import ejson as json from .auth_api_key import api_key_authenticate, APIKeyAuthMech from .config import CALL_TIMEOUT, UNCLAIMED_JOBS_MAX -from .exc import ReserveFDException, ClientException, ErrnoMixin, ValidationErrors, CallTimeout # noqa +from .exc import ( + ReserveFDException, + ClientException, + ErrnoMixin, + ValidationErrors, + CallTimeout, +) # noqa from .legacy import LegacyClient -from .jsonrpc import CollectionUpdateParams, ErrorObj, JobFields, JSONRPCError, JSONRPCMessage, TruenasError -from .utils import MIDDLEWARE_RUN_DIR, ProgressBar, undefined, UndefinedType, set_socket_options +from .jsonrpc import ( + CollectionUpdateParams, + ErrorObj, + JobFields, + JSONRPCError, + JSONRPCMessage, + TruenasError, +) +from .utils import ( + MIDDLEWARE_RUN_DIR, + ProgressBar, + undefined, + UndefinedType, + set_socket_options, +) logger = logging.getLogger(__name__) UNIX_SOCKET_PREFIX = "ws+unix://" -DUMMY_HOSTNAME = "ws://localhost/api/current" # Advised by official docs to use dummy hostname +DUMMY_HOSTNAME = ( + "ws://localhost/api/current" # Advised by official docs to use dummy hostname +) class Client: """Implicit wrapper of either a `JSONRPCClient` or a `LegacyClient`.""" - def __init__(self, uri: str | None = None, reserved_ports=False, private_methods=False, py_exceptions=False, - log_py_exceptions=False, call_timeout: float | UndefinedType = undefined, verify_ssl=True): + def __init__( + self, + uri: str | None = None, + reserved_ports=False, + private_methods=False, + py_exceptions=False, + log_py_exceptions=False, + call_timeout: float | UndefinedType = undefined, + verify_ssl=True, + ): """Initialize either a `JSONRPCClient` or a `LegacyClient`. Use `JSONRPCClient` unless `uri` ends with '/websocket'. @@ -93,21 +123,30 @@ def __init__(self, uri: str | None = None, reserved_ports=False, private_methods ClientException: `WSClient` timed out or some other connection error occurred. """ - if uri is not None and uri.endswith('/websocket'): + if uri is not None and uri.endswith("/websocket"): client_class: type[LegacyClient] | type[JSONRPCClient] = LegacyClient else: client_class = JSONRPCClient self.uri_check(uri, py_exceptions) - self.__client = client_class(uri, reserved_ports, private_methods, py_exceptions, log_py_exceptions, - call_timeout, verify_ssl) + self.__client = client_class( + uri, + reserved_ports, + private_methods, + py_exceptions, + log_py_exceptions, + call_timeout, + verify_ssl, + ) def uri_check(self, uri: str | None, py_exceptions: bool): # We pickle_load when handling py_exceptions, reduce risk of MITM on client causing a pickle.load # of malicious information by only allowing this over unix domain socket. if uri and py_exceptions and not uri.startswith(UNIX_SOCKET_PREFIX): - raise ClientException('py_exceptions are only allowed for connections to unix domain socket') + raise ClientException( + "py_exceptions are only allowed for connections to unix domain socket" + ) def __getattr__(self, item): return getattr(self.__client, item) @@ -125,7 +164,15 @@ class WSClient: The object used by `JSONRPCClient` to send and receive data. """ - def __init__(self, url: str, *, client: 'JSONRPCClient', reserved_ports: bool = False, verify_ssl: bool = True): + + def __init__( + self, + url: str, + *, + client: "JSONRPCClient", + reserved_ports: bool = False, + verify_ssl: bool = True, + ): """Initialize a `WSClient`. Args: @@ -159,22 +206,28 @@ def connect(self): # plaintext ws:// URI. Require that scheme explicitly rather than, for example, # connecting a wss:// URI in cleartext. scheme = urllib.parse.urlparse(self.url).scheme - if scheme != 'ws': + if scheme != "ws": raise ClientException( - f'reserved_ports connections require a ws:// URI, got {scheme!r}' + f"reserved_ports connections require a ws:// URI, got {scheme!r}" ) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.settimeout(10) self._bind_to_reserved_port() try: - self.socket.connect((urllib.parse.urlparse(self.url).hostname, - urllib.parse.urlparse(self.url).port or 80)) + self.socket.connect( + ( + urllib.parse.urlparse(self.url).hostname, + urllib.parse.urlparse(self.url).port or 80, + ) + ) except Exception: self.socket.close() raise app_url = DUMMY_HOSTNAME else: - sockopt = sock_opt(None, None if self.verify_ssl else {"cert_reqs": ssl.CERT_NONE}) + sockopt = sock_opt( + None, None if self.verify_ssl else {"cert_reqs": ssl.CERT_NONE} + ) sockopt.timeout = 10 self.socket = connect(self.url, sockopt, proxy_info(), None)[0] app_url = self.url @@ -200,7 +253,7 @@ def get_peer_cert_der(self) -> bytes | None: flow, which runs after `connect()`. Before the handshake `getpeercert()` returns an empty value, which callers treat as "no certificate" (a falsy result). """ - sock = getattr(self, 'socket', None) + sock = getattr(self, "socket", None) if isinstance(sock, ssl.SSLSocket): return sock.getpeercert(binary_form=True) @@ -246,7 +299,7 @@ def _bind_to_reserved_port(self): for port in ports_to_try: try: - self.socket.bind(('', port)) + self.socket.bind(("", port)) return except OSError: time.sleep(0.1) @@ -285,15 +338,15 @@ def _on_error(self, app, e): """ code = None - reason = 'UNKNOWN' + reason = "UNKNOWN" if isinstance(e, ABNF): # Always try to extract whatever information is available from the ABNF object try: # If there's data, try to extract code and reason (typically for close frames) if isinstance(e.data, bytes): if len(e.data) >= 2: - code = struct.unpack('!H', e.data[:2])[0] - reason = e.data[2:].decode('utf-8', errors='ignore') + code = struct.unpack("!H", e.data[:2])[0] + reason = e.data[2:].decode("utf-8", errors="ignore") except Exception: pass @@ -337,9 +390,10 @@ def __init__(self, method: str, params: tuple, wants_job: bool = True): class _JobDict(JobFields): """Contains data received from the server for a particular running job.""" + __ready: Event """Is set when the job returns or ends in error.""" - __callback: '_JobCallback | None' + __callback: "_JobCallback | None" """Procedure to execute each time a job update is received.""" @@ -352,7 +406,10 @@ class Job: Every `Job` is responsible for a corresponding `_JobDict` in the client's list of jobs. """ - def __init__(self, client: 'JSONRPCClient', job_id: str, callback: _JobCallback | None = None): + + def __init__( + self, client: "JSONRPCClient", job_id: str, callback: _JobCallback | None = None + ): """Initialize `Job`. Args: @@ -370,13 +427,13 @@ def __init__(self, client: 'JSONRPCClient', job_id: str, callback: _JobCallback with client._jobs_lock: client._unclaimed_jobs.pop(job_id, None) job = client._jobs[job_id] - self.event = job.get('__ready') + self.event = job.get("__ready") if self.event is None: - self.event = job['__ready'] = Event() - job['__callback'] = callback + self.event = job["__ready"] = Event() + job["__callback"] = callback def __repr__(self): - return f'' + return f"" def result(self): """Wait for the job to finish and return its result. @@ -393,33 +450,36 @@ def result(self): self.event.wait() job = self.client._jobs.pop(self.job_id, None) if job is None: - raise ClientException('No job event was received.') - state = job.get('state') - if state == 'SUCCESS': - return job['result'] + raise ClientException("No job event was received.") + state = job.get("state") + if state == "SUCCESS": + return job["result"] - exc_info = job.get('exc_info') + exc_info = job.get("exc_info") if exc_info: - if exc_info['type'] == 'VALIDATION': - raise ValidationErrors(exc_info['extra'] or []) + if exc_info["type"] == "VALIDATION": + raise ValidationErrors(exc_info["extra"] or []) raise ClientException( - job['error'], - errno=exc_info.get('errno'), + job["error"], + errno=exc_info.get("errno"), trace={ - 'class': exc_info['type'], - 'frames': [], - 'formatted': job['exception'], - 'repr': exc_info.get('repr') or job['exception'].splitlines()[-1], + "class": exc_info["type"], + "frames": [], + "formatted": job["exception"], + "repr": exc_info.get("repr") or job["exception"].splitlines()[-1], }, - extra=exc_info['extra'], + extra=exc_info["extra"], ) # Aborted or interrupted jobs have no exc_info to build a trace from. - raise ClientException(job.get('error') or f'Job {self.job_id} did not succeed (state={state!r})') + raise ClientException( + job.get("error") or f"Job {self.job_id} did not succeed (state={state!r})" + ) class _EventCallbackProtocol(Protocol): """Specifies how event callbacks should be defined.""" + def __call__(self, mtype: str, **message: Any) -> None: pass @@ -430,6 +490,7 @@ class _PartialPayload(TypedDict): Contains the required fields of `_Payload`. """ + callback: _EventCallbackProtocol | None sync: bool event: Event @@ -447,6 +508,7 @@ class _Payload(_PartialPayload, total=False): id: Random UUID assigned by `core.subscribe`. """ + error: str | TruenasError | None id: str @@ -457,8 +519,17 @@ class JSONRPCClient: Keeps track of the calls made, jobs submitted, and callbacks. Maintains a websocket connection using a `WSClient`. """ - def __init__(self, uri: str | None = None, reserved_ports=False, private_methods=False, py_exceptions=False, - log_py_exceptions=False, call_timeout: float | UndefinedType = undefined, verify_ssl=True): + + def __init__( + self, + uri: str | None = None, + reserved_ports=False, + private_methods=False, + py_exceptions=False, + log_py_exceptions=False, + call_timeout: float | UndefinedType = undefined, + verify_ssl=True, + ): """Initialize a `JSONRPCClient`. Args: @@ -477,7 +548,7 @@ def __init__(self, uri: str | None = None, reserved_ports=False, private_methods """ if uri is None: - uri = f'{UNIX_SOCKET_PREFIX}{MIDDLEWARE_RUN_DIR}/middlewared.sock' + uri = f"{UNIX_SOCKET_PREFIX}{MIDDLEWARE_RUN_DIR}/middlewared.sock" if call_timeout is undefined: call_timeout = CALL_TIMEOUT @@ -508,8 +579,8 @@ def __init__(self, uri: str | None = None, reserved_ports=False, private_methods self._ws.connect() self._connected.wait(30) if not self._connected.is_set(): - raise ClientException('Failed connection handshake') - if hasattr(self, '_ws_connection_error'): + raise ClientException("Failed connection handshake") + if hasattr(self, "_ws_connection_error"): if isinstance(self._ws_connection_error, WebSocketException): raise self._ws_connection_error if self._connection_error is not None: @@ -536,7 +607,9 @@ def _send(self, data): except (AttributeError, WebSocketConnectionClosedException): # happens when other node on HA is rebooted, for example, and there are # running tasks in the event loop (i.e. failover.call_remote failover.get_disks_local) - raise ClientException('Unexpected closure of remote connection', errno.ECONNABORTED) + raise ClientException( + "Unexpected closure of remote connection", errno.ECONNABORTED + ) def _recv(self, message: JSONRPCMessage): """Process a deserialized JSON-RPC v2.0 message from the server. @@ -556,90 +629,145 @@ def _recv(self, message: JSONRPCMessage): """ try: - if 'method' in message: - match message['method']: - case 'collection_update': - params = message['params'] + if "method" in message: + match message["method"]: + case "collection_update": + params = message["params"] if self._new_style_jobs: # With new-style jobs, the method return value will not be sent until the job is completed # We expect just to receive job ID immediately, so let's set call return value to the # newly added job ID. - if params['collection'] == 'core.get_jobs' and params['msg'] in ['added', 'changed']: - for message_id in params['fields']['message_ids']: - if (call := self._calls.get(message_id)) is not None: + if params["collection"] == "core.get_jobs" and params[ + "msg" + ] in ["added", "changed"]: + for message_id in params["fields"]["message_ids"]: + if ( + call := self._calls.get(message_id) + ) is not None: if call.wants_job: with self._jobs_lock: - self._jobs.setdefault(params['id'], {}) # type: ignore[typeddict-item] - self._unclaimed_jobs.pop(params['id'], None) - call.result = params['id'] - call.job_id = params['id'] + self._jobs.setdefault(params["id"], {}) # type: ignore[typeddict-item] + self._unclaimed_jobs.pop( + params["id"], None + ) + call.result = params["id"] + call.job_id = params["id"] call.returned.set() self._unregister_call(call) if self._event_callbacks: - if '*' in self._event_callbacks: - for event in self._event_callbacks['*']: - self._run_callback(event, [params['msg'].upper()], params) - if params['collection'] in self._event_callbacks: - for event in self._event_callbacks[params['collection']]: - self._run_callback(event, [params['msg'].upper()], params) - case 'notify_unsubscribed': - unsubscribed = message['params'] - if unsubscribed['collection'] in self._event_callbacks: - for event in self._event_callbacks[unsubscribed['collection']]: - if 'error' in unsubscribed: - err = unsubscribed['error'] - event['error'] = reason if err and (reason := err['reason']) else err - event['event'].set() + if "*" in self._event_callbacks: + for event in self._event_callbacks["*"]: + self._run_callback( + event, [params["msg"].upper()], params + ) + if params["collection"] in self._event_callbacks: + for event in self._event_callbacks[ + params["collection"] + ]: + self._run_callback( + event, [params["msg"].upper()], params + ) + case "notify_unsubscribed": + unsubscribed = message["params"] + if unsubscribed["collection"] in self._event_callbacks: + for event in self._event_callbacks[ + unsubscribed["collection"] + ]: + if "error" in unsubscribed: + err = unsubscribed["error"] + event["error"] = ( + reason + if err and (reason := err["reason"]) + else err + ) + event["event"].set() case _: - logger.error('Received unknown notification %r', message['method']) - elif 'id' in message: - if self._set_options_call and message['id'] == self._set_options_call.id: - if 'result' in message: - if isinstance(message['result'], dict) and 'legacy_jobs' in message['result']: - self._new_style_jobs = not message['result']['legacy_jobs'] - if 'error' in message: + logger.error( + "Received unknown notification %r", message["method"] + ) + elif "id" in message: + if ( + self._set_options_call + and message["id"] == self._set_options_call.id + ): + if "result" in message: + if ( + isinstance(message["result"], dict) + and "legacy_jobs" in message["result"] + ): + self._new_style_jobs = not message["result"]["legacy_jobs"] + if "error" in message: try: - self._parse_error(message['error'], self._set_options_call) + self._parse_error(message["error"], self._set_options_call) except Exception: - logger.error('Unhandled exception in JSONRPCClient._parse_error', exc_info=True) + logger.error( + "Unhandled exception in JSONRPCClient._parse_error", + exc_info=True, + ) else: - logger.error('Error setting client options: %r', self._set_options_call.error) + logger.error( + "Error setting client options: %r", + self._set_options_call.error, + ) self._connected.set() - elif message['id'] is not None and (call := self._calls.get(message['id'])): - if 'result' in message: - call.result = message['result'] - if 'error' in message: + elif message["id"] is not None and ( + call := self._calls.get(message["id"]) + ): + if "result" in message: + call.result = message["result"] + if "error" in message: try: - self._parse_error(message['error'], call) + self._parse_error(message["error"], call) except Exception: - logger.error('Unhandled exception in JSONRPCClient._parse_error', exc_info=True) + logger.error( + "Unhandled exception in JSONRPCClient._parse_error", + exc_info=True, + ) call.returned.set() self._unregister_call(call) else: - if 'result' in message: + if "result" in message: if not self._new_style_jobs: - logger.error('Received a success response for non-registered method call %r', message['id']) - elif 'error' in message: + logger.error( + "Received a success response for non-registered method call %r", + message["id"], + ) + elif "error" in message: try: - error = self._parse_error_and_unpickle_exception(message['error'])[0] + error = self._parse_error_and_unpickle_exception( + message["error"] + )[0] except Exception: - logger.error('Unhandled exception in JSONRPCClient._parse_error', exc_info=True) - error = ClientException(f'Could not parse error from server: {message["error"]!r}') - - if message['id'] is None: - logger.error('Received a global connection error: %r', error) + logger.error( + "Unhandled exception in JSONRPCClient._parse_error", + exc_info=True, + ) + error = ClientException( + f"Could not parse error from server: {message['error']!r}" + ) + + if message["id"] is None: + logger.error( + "Received a global connection error: %r", error + ) self._broadcast_error(error) else: if not self._new_style_jobs: - logger.error('Received an error response for non-registered method call %r: %r', - message['id'], error) + logger.error( + "Received an error response for non-registered method call %r: %r", + message["id"], + error, + ) else: if not self._new_style_jobs: - logger.error('Received a response for non-registered method call %r', message['id']) + logger.error( + "Received a response for non-registered method call %r", + message["id"], + ) else: - logger.error('Received unknown message %r', message) + logger.error("Received unknown message %r", message) except Exception: - logger.error('Unhandled exception in JSONRPCClient._recv', exc_info=True) + logger.error("Unhandled exception in JSONRPCClient._recv", exc_info=True) def _parse_error(self, error: ErrorObj, call: Call): """Convert an error received from the server into a `ClientException` and store it. @@ -650,41 +778,55 @@ def _parse_error(self, error: ErrorObj, call: Call): """ call.error, call.py_exception = self._parse_error_and_unpickle_exception(error) - def _parse_error_and_unpickle_exception(self, error: ErrorObj) -> tuple[ClientException, Exception | None]: + def _parse_error_and_unpickle_exception( + self, error: ErrorObj + ) -> tuple[ClientException, Exception | None]: """Convert an error received from the server into a `ClientException` and, possibly, unpickle original exception. Args: error: The JSON object received in an error Response. """ - code = JSONRPCError(error['code']) + code = JSONRPCError(error["code"]) py_exception = None - if self._py_exceptions and code in [JSONRPCError.INVALID_PARAMS, JSONRPCError.TRUENAS_CALL_ERROR]: - data = error['data'] - exc = ClientException(data['reason'], data['error'], data['trace'], data['extra']) - if 'py_exception' in data: + if self._py_exceptions and code in [ + JSONRPCError.INVALID_PARAMS, + JSONRPCError.TRUENAS_CALL_ERROR, + ]: + data = error["data"] + exc = ClientException( + data["reason"], data["error"], data["trace"], data["extra"] + ) + if "py_exception" in data: try: - py_exception = pickle.loads(b64decode(data['py_exception'])) + py_exception = pickle.loads(b64decode(data["py_exception"])) except Exception as e: logger.warning("Error unpickling call exception: %r", e) elif code == JSONRPCError.INVALID_PARAMS: - exc = ValidationErrors(error['data']['extra']) + exc = ValidationErrors(error["data"]["extra"]) elif code == JSONRPCError.TRUENAS_CALL_ERROR: - data = error['data'] - exc = ClientException(data['reason'], data['error'], data['trace'], data['extra']) + data = error["data"] + exc = ClientException( + data["reason"], data["error"], data["trace"], data["extra"] + ) elif code == JSONRPCError.METHOD_NOT_FOUND: - exc = ClientException(error.get('message') or code.name, ErrnoMixin.ENOMETHOD) + exc = ClientException( + error.get("message") or code.name, ErrnoMixin.ENOMETHOD + ) else: if code == JSONRPCError.TRUENAS_TOO_MANY_CONCURRENT_CALLS: - logger.debug('A TRUENAS_TOO_MANY_CONCURRENT_CALLS error occurred. Currently running calls: %r', [ - call.method for call in self._calls.values() - ]) + logger.debug( + "A TRUENAS_TOO_MANY_CONCURRENT_CALLS error occurred. Currently running calls: %r", + [call.method for call in self._calls.values()], + ) - exc = ClientException(error.get('message') or code.name) + exc = ClientException(error.get("message") or code.name) return exc, py_exception - def _run_callback(self, event: _Payload, args: Iterable[str], kwargs: CollectionUpdateParams): + def _run_callback( + self, event: _Payload, args: Iterable[str], kwargs: CollectionUpdateParams + ): """Call the passed `_Payload`'s callback function. Block until the callback returns if `event['sync']` is set. Otherwise, run in a separate daemon `Thread`. @@ -695,20 +837,26 @@ def _run_callback(self, event: _Payload, args: Iterable[str], kwargs: Collection kwargs: Keyword arguments to the callback. """ - if event['callback'] is None: + if event["callback"] is None: return - if event['sync']: - event['callback'](*args, **kwargs) + if event["sync"]: + event["callback"](*args, **kwargs) else: - Thread(target=event['callback'], args=args, kwargs=kwargs, daemon=True).start() + Thread( + target=event["callback"], args=args, kwargs=kwargs, daemon=True + ).start() def on_open(self): """Make an API call to `core.set_options` to configure how middlewared sends its responses.""" - self._set_options_call = self.call("core.set_options", { - "legacy_jobs": False, - "private_methods": self._private_methods, - "py_exceptions": self._py_exceptions, - }, background=True) + self._set_options_call = self.call( + "core.set_options", + { + "legacy_jobs": False, + "private_methods": self._private_methods, + "py_exceptions": self._py_exceptions, + }, + background=True, + ) def on_close(self, code: int | None, reason: str | None = None): """Close this `JSONRPCClient` in response to the `WebSocketApp` closing. @@ -720,7 +868,7 @@ def on_close(self, code: int | None, reason: str | None = None): reason: A message to accompany the closing code and provide more information. """ - error = f'WebSocket connection closed with code={code!r}, reason={reason!r}' + error = f"WebSocket connection closed with code={code!r}, reason={reason!r}" self._connection_error = error self._connected.set() @@ -738,21 +886,21 @@ def _broadcast_error(self, error: ClientException): # Snapshot: _jobs can be resized concurrently (a woken result() pops, the reader inserts). for job in list(self._jobs.values()): - event = job.get('__ready') + event = job.get("__ready") if event is None: - event = job['__ready'] = Event() + event = job["__ready"] = Event() if not event.is_set(): error_repr = repr(error) # The connection dropped, so the real outcome is unknown (the job may still be # running server-side); just mark it non-SUCCESS for Job.result(). - job['state'] = 'UNKNOWN' - job['error'] = error_repr - job['exception'] = error_repr - job['exc_info'] = { - 'type': 'Exception', - 'repr': error_repr, - 'extra': None, + job["state"] = "UNKNOWN" + job["error"] = error_repr + job["exception"] = error_repr + job["exc_info"] = { + "type": "Exception", + "repr": error_repr, + "extra": None, } event.set() @@ -778,7 +926,7 @@ def _jobs_callback(self, mtype: str, *, fields: JobFields, **message): fields (JobFields): Contains job id and other information about the job from the server. """ - job_id = fields['id'] + job_id = fields["id"] with self._jobs_lock: if fields: if job_id not in self._jobs: @@ -797,16 +945,20 @@ def _jobs_callback(self, mtype: str, *, fields: JobFields, **message): self._jobs.pop(evicted, None) job = self._jobs[job_id] job.update(fields) - if callable(job.get('__callback')): - Thread(target=job['__callback'], args=(job,), daemon=True).start() - if mtype == 'CHANGED' and job['state'] in ('SUCCESS', 'FAILED', 'ABORTED'): + if callable(job.get("__callback")): + Thread(target=job["__callback"], args=(job,), daemon=True).start() + if mtype == "CHANGED" and job["state"] in ( + "SUCCESS", + "FAILED", + "ABORTED", + ): # If an Event already exist we just set it to mark it finished. # Otherwise, we create a new Event. # This is to prevent a race-condition of job finishing before # the client can create the Event. - event = job.get('__ready') + event = job.get("__ready") if event is None: - event = job['__ready'] = Event() + event = job["__ready"] = Event() event.set() def _jobs_subscribe(self, silent=False): @@ -814,7 +966,9 @@ def _jobs_subscribe(self, silent=False): self._jobs_watching = True try: self.subscribe( - 'core.get_jobs', self._jobs_callback, sync=True, # type: ignore[arg-type] # requires fields + "core.get_jobs", + self._jobs_callback, # type: ignore[arg-type] # requires fields + sync=True, _log_py_enotauthenticated=not silent, ) except Exception as e: @@ -827,23 +981,24 @@ def _jobs_subscribe(self, silent=False): def _is_enotauthenticated(self, e: BaseException): if isinstance(e, ClientException) or ( - e.__module__ == "middlewared.service_exception" and e.__class__.__name__ == "CallError" + e.__module__ == "middlewared.service_exception" + and e.__class__.__name__ == "CallError" ): - if getattr(e, 'errno', None) == ClientException.ENOTAUTHENTICATED: + if getattr(e, "errno", None) == ClientException.ENOTAUTHENTICATED: return True return False def call( - self, - method: str, - *params, - background=False, - callback: _JobCallback | None = None, - job: Literal['RETURN'] | bool = False, - register_call: bool | None = None, - timeout: float | UndefinedType = undefined, - _log_py_enotauthenticated: bool = True, + self, + method: str, + *params, + background=False, + callback: _JobCallback | None = None, + job: Literal["RETURN"] | bool = False, + register_call: bool | None = None, + timeout: float | UndefinedType = undefined, + _log_py_enotauthenticated: bool = True, ) -> Any: """The primary way to send call requests to the API. @@ -892,30 +1047,37 @@ def call( if register_call: self._register_call(c) try: - self._send({ - 'jsonrpc': '2.0', - 'method': c.method, - 'id': c.id, - 'params': c.params, - }) + self._send( + { + "jsonrpc": "2.0", + "method": c.method, + "id": c.id, + "params": c.params, + } + ) if background: return c - return self.wait(c, callback=callback, job=job, timeout=timeout, - _log_py_enotauthenticated=_log_py_enotauthenticated) + return self.wait( + c, + callback=callback, + job=job, + timeout=timeout, + _log_py_enotauthenticated=_log_py_enotauthenticated, + ) finally: if not background: self._unregister_call(c) def wait( - self, - c: Call, - *, - callback: _JobCallback | None = None, - job: Literal['RETURN'] | bool = False, - timeout: float | UndefinedType = undefined, - _log_py_enotauthenticated: bool = True + self, + c: Call, + *, + callback: _JobCallback | None = None, + job: Literal["RETURN"] | bool = False, + timeout: float | UndefinedType = undefined, + _log_py_enotauthenticated: bool = True, ) -> Any: """Wait for an API call to return and return its result. @@ -945,7 +1107,9 @@ def wait( if c.error: if c.py_exception: if self._log_py_exceptions and c.error.trace: - if _log_py_enotauthenticated or not self._is_enotauthenticated(c.py_exception): + if _log_py_enotauthenticated or not self._is_enotauthenticated( + c.py_exception + ): logger.error(c.error.trace["formatted"]) raise c.py_exception else: @@ -957,7 +1121,7 @@ def wait( raise ClientException("The method is not a job.") jobobj = Job(self, c.result, callback=callback) - if job == 'RETURN': + if job == "RETURN": return jobobj return jobobj.result() @@ -975,19 +1139,19 @@ def event_payload() -> _Payload: """ return { - 'callback': None, - 'sync': False, - 'event': Event(), + "callback": None, + "sync": False, + "event": Event(), } def subscribe( - self, - name: str, - callback: _EventCallbackProtocol, - payload: _Payload | None = None, - sync: bool = False, - *, - _log_py_enotauthenticated: bool = True + self, + name: str, + callback: _EventCallbackProtocol, + payload: _Payload | None = None, + sync: bool = False, + *, + _log_py_enotauthenticated: bool = True, ) -> str: """Subscribe to an event by calling `core.subscribe`. @@ -1003,21 +1167,26 @@ def subscribe( """ payload = payload or self.event_payload() - payload.update({ - 'callback': callback, - 'sync': sync, - }) + payload.update( + { + "callback": callback, + "sync": sync, + } + ) self._event_callbacks[name].append(payload) try: - payload['id'] = self.call( - 'core.subscribe', name, timeout=10, _log_py_enotauthenticated=_log_py_enotauthenticated, + payload["id"] = self.call( + "core.subscribe", + name, + timeout=10, + _log_py_enotauthenticated=_log_py_enotauthenticated, ) except Exception: self._event_callbacks[name].remove(payload) raise - return payload['id'] + return payload["id"] def unsubscribe(self, id_: str): """Call `core.unsubscribe` and remove all associated `_Payload`s @@ -1026,15 +1195,15 @@ def unsubscribe(self, id_: str): id_: `id` of the `_Payload` to remove. """ - self.call('core.unsubscribe', id_) + self.call("core.unsubscribe", id_) for k, events in list(self._event_callbacks.items()): - events = [v for v in events if v.get('id') != id_] + events = [v for v in events if v.get("id") != id_] if events: self._event_callbacks[k] = events else: self._event_callbacks.pop(k) - def ping(self, timeout: float = 10) -> Literal['pong']: + def ping(self, timeout: float = 10) -> Literal["pong"]: """Call `core.ping` to verify connection to the server. Args: @@ -1045,7 +1214,7 @@ def ping(self, timeout: float = 10) -> Literal['pong']: CallTimeout: The call took longer than `timeout` seconds to return. """ - c = self.call('core.ping', background=True, register_call=True) + c = self.call("core.ping", background=True, register_call=True) return self.wait(c, timeout=timeout) def close(self): @@ -1091,9 +1260,13 @@ def login_with_api_key( ValueError: an error occurred during authentication, or channel binding was required but could not be established. """ - api_key_authenticate(self, auth_mechanism, username, api_key, channel_binding=channel_binding) + api_key_authenticate( + self, auth_mechanism, username, api_key, channel_binding=channel_binding + ) - def login_with_password(self, username: str, password: str, *, otp_token: str | None = None) -> None: + def login_with_password( + self, username: str, password: str, *, otp_token: str | None = None + ) -> None: """ Authenticate via username and password. @@ -1110,166 +1283,194 @@ def login_with_password(self, username: str, password: str, *, otp_token: str | account credentials expired, account lacks API access, or redirect required """ try: - resp = self.call('auth.login_ex', { - 'mechanism': 'PASSWORD_PLAIN', - 'username': username, - 'password': password, - }) + resp = self.call( + "auth.login_ex", + { + "mechanism": "PASSWORD_PLAIN", + "username": username, + "password": password, + }, + ) except ClientException as exc: - if exc.error == 'Method does not exist': + if exc.error == "Method does not exist": # Pre-25.04 server - if not self.call('auth.login', username, password, otp_token): - raise ValueError('Invalid username or password') + if not self.call("auth.login", username, password, otp_token): + raise ValueError("Invalid username or password") return raise self._handle_login_ex_response(resp, otp_token) def _handle_login_ex_response(self, resp: dict, otp_token: str | None) -> None: - match resp['response_type']: - case 'SUCCESS': + match resp["response_type"]: + case "SUCCESS": return - case 'OTP_REQUIRED': + case "OTP_REQUIRED": if otp_token is None: raise ValueError( - 'Two-factor authentication is required for this account. ' - 'Call login_with_password again with otp_token specified.' + "Two-factor authentication is required for this account. " + "Call login_with_password again with otp_token specified." ) - otp_resp = self.call('auth.login_ex_continue', { - 'mechanism': 'OTP_TOKEN', - 'otp_token': otp_token, - }) + otp_resp = self.call( + "auth.login_ex_continue", + { + "mechanism": "OTP_TOKEN", + "otp_token": otp_token, + }, + ) self._handle_login_ex_response(otp_resp, None) - case 'REDIRECT': - urls = ', '.join(resp.get('urls', [])) + case "REDIRECT": + urls = ", ".join(resp.get("urls", [])) raise ValueError( - f'Authentication must be performed on active storage controller. ' - f'Redirect URLs: {urls}' + f"Authentication must be performed on active storage controller. " + f"Redirect URLs: {urls}" ) - case 'EXPIRED': - raise ValueError('Account credentials have expired') - case 'DENIED': - raise ValueError('Account does not have API access') + case "EXPIRED": + raise ValueError("Account credentials have expired") + case "DENIED": + raise ValueError("Account does not have API access") case _: - raise ValueError('Invalid username or password') + raise ValueError("Invalid username or password") def get_parser(): """Construct the argument parser for `midclt`.""" parser = argparse.ArgumentParser( - prog='midclt', + prog="midclt", description=( - 'Command-line client for the TrueNAS middleware API. ' - 'When run locally on a TrueNAS server, midclt uses the middleware ' - 'UNIX socket by default. Pass --uri to reach a remote server over ' - 'a WebSocket: the JSON-RPC 2.0 endpoint on TrueNAS 25.04 and later ' - '(e.g. ws://host/api/current), or the legacy WebSocket endpoint on ' - 'earlier releases (e.g. ws://host/websocket).' + "Command-line client for the TrueNAS middleware API. " + "When run locally on a TrueNAS server, midclt uses the middleware " + "UNIX socket by default. Pass --uri to reach a remote server over " + "a WebSocket: the JSON-RPC 2.0 endpoint on TrueNAS 25.04 and later " + "(e.g. ws://host/api/current), or the legacy WebSocket endpoint on " + "earlier releases (e.g. ws://host/websocket)." ), epilog=( - 'Examples:\n' - ' midclt ping\n' - ' midclt call system.info\n' + "Examples:\n" + " midclt ping\n" + " midclt call system.info\n" ' midclt call user.query \'[["username", "=", "root"]]\'\n' - ' midclt -t 120 call interface.sync true\n' - ' midclt --job --job-print description call pool.import_on_boot\n' - ' midclt -u ws://nas.example/api/current -U admin -P secret call system.info\n' - ' midclt -K /root/apikey.json call user.create - < new_user.json\n' - ' midclt subscribe -n 1 core.get_jobs\n' + " midclt -t 120 call interface.sync true\n" + " midclt --job --job-print description call pool.import_on_boot\n" + " midclt -u ws://nas.example/api/current -U admin -P secret call system.info\n" + " midclt -K /root/apikey.json call user.create - < new_user.json\n" + " midclt subscribe -n 1 core.get_jobs\n" ), formatter_class=argparse.RawDescriptionHelpFormatter, ) # midclt options parser.add_argument( - '-u', '--uri', metavar='URI', + "-u", + "--uri", + metavar="URI", help=( - 'WebSocket URI of the TrueNAS server. Use ws(s)://host/api/current ' - 'for TrueNAS 25.04 and later (JSON-RPC 2.0), or ws(s)://host/websocket ' - 'for earlier releases (legacy protocol). Defaults to the local ' - 'middleware UNIX socket when run on a TrueNAS server.' + "WebSocket URI of the TrueNAS server. Use ws(s)://host/api/current " + "for TrueNAS 25.04 and later (JSON-RPC 2.0), or ws(s)://host/websocket " + "for earlier releases (legacy protocol). Defaults to the local " + "middleware UNIX socket when run on a TrueNAS server." ), ) parser.add_argument( - '-U', '--username', metavar='USERNAME', - help='Username for authentication. Used with --password or --api-key.', + "-U", + "--username", + metavar="USERNAME", + help="Username for authentication. Used with --password or --api-key.", ) parser.add_argument( - '-P', '--password', metavar='PASSWORD', + "-P", + "--password", + metavar="PASSWORD", help=( - 'Password for authentication. Prompted interactively if ' - '--username is given without --password or --api-key.' + "Password for authentication. Prompted interactively if " + "--username is given without --password or --api-key." ), ) parser.add_argument( - '-K', '--api-key', metavar='KEY', - help='API key string, or path to a file containing the key.', + "-K", + "--api-key", + metavar="KEY", + help="API key string, or path to a file containing the key.", ) parser.add_argument( - '-t', '--timeout', type=int, metavar='SECONDS', - help='Timeout in seconds for the API call.', + "-t", + "--timeout", + type=int, + metavar="SECONDS", + help="Timeout in seconds for the API call.", ) parser.add_argument( - '--insecure', action='store_true', - help='Disable SSL certificate verification (WARNING: not for production).', + "--insecure", + action="store_true", + help="Disable SSL certificate verification (WARNING: not for production).", ) parser.add_argument( - '--no-channel-binding', action='store_true', + "--no-channel-binding", + action="store_true", help=( - 'Disable SCRAM-PLUS channel binding for API-key login. By default, API-key ' - 'authentication over TLS (wss://) binds to the server certificate and fails ' - 'if binding cannot be negotiated. Use this over a plain ws:// connection or ' - 'against a server that does not support channel binding.' + "Disable SCRAM-PLUS channel binding for API-key login. By default, API-key " + "authentication over TLS (wss://) binds to the server certificate and fails " + "if binding cannot be negotiated. Use this over a plain ws:// connection or " + "against a server that does not support channel binding." ), ) parser.add_argument( - '--plain', action='store_true', + "--plain", + action="store_true", help=( - 'Authenticate the API key using PLAIN (send the key in plaintext) instead of ' - 'SCRAM. Required for servers that do not support SCRAM (TrueNAS before 26). ' - 'WARNING: transmits the raw API key; only the TLS layer protects it.' + "Authenticate the API key using PLAIN (send the key in plaintext) instead of " + "SCRAM. Required for servers that do not support SCRAM (TrueNAS before 26). " + "WARNING: transmits the raw API key; only the TLS layer protects it." ), ) subparsers = parser.add_subparsers( - dest='name', metavar='', - title='subcommands', + dest="name", + metavar="", + title="subcommands", ) # call options call_description = ( - 'Invoke a TrueNAS API method. Any arguments after the method name are ' - 'parsed as JSON when possible and otherwise passed as raw strings. ' + "Invoke a TrueNAS API method. Any arguments after the method name are " + "parsed as JSON when possible and otherwise passed as raw strings. " "Use '-' as the final argument to read a single JSON payload from stdin, " - 'which is useful for keeping secrets out of shell history and process listings.' + "which is useful for keeping secrets out of shell history and process listings." ) iparser = subparsers.add_parser( - 'call', - help='Call a TrueNAS API method.', + "call", + help="Call a TrueNAS API method.", description=call_description, ) iparser.add_argument( - '-q', '--quiet', action='store_true', - help='Suppress error output on API call failure.', + "-q", + "--quiet", + action="store_true", + help="Suppress error output on API call failure.", ) iparser.add_argument( - '-j', '--job', action='store_true', - help='Treat the call as a long-running job and track progress until completion.', + "-j", + "--job", + action="store_true", + help="Treat the call as a long-running job and track progress until completion.", ) iparser.add_argument( - '-jp', '--job-print', + "-jp", + "--job-print", type=str, - choices=('progressbar', 'description'), - default='progressbar', + choices=("progressbar", "description"), + default="progressbar", help=( "How to render job progress: 'progressbar' (animated bar, default) " "or 'description' (status text lines, suitable for systemd units and logs)." ), ) iparser.add_argument( - 'method', nargs='+', metavar='METHOD', + "method", + nargs="+", + metavar="METHOD", help=( - 'API method name followed by zero or more arguments. Each argument ' + "API method name followed by zero or more arguments. Each argument " "is parsed as JSON when possible, otherwise passed as a string. " "Use '-' as the final argument to read one JSON payload from stdin." ), @@ -1277,36 +1478,43 @@ def get_parser(): # ping subparsers.add_parser( - 'ping', - help='Call core.ping on the server and print the response.', + "ping", + help="Call core.ping on the server and print the response.", description=( "Call the core.ping API method to verify connectivity. Prints 'pong' " - 'on success. This endpoint does not require authentication and is ' - 'rate-limited on the server side.' + "on success. This endpoint does not require authentication and is " + "rate-limited on the server side." ), ) # subscribe options iparser = subparsers.add_parser( - 'subscribe', - help='Receive event messages in a continuous stream.', + "subscribe", + help="Receive event messages in a continuous stream.", description=( - 'Subscribe to a middleware event and print each message as it arrives. ' - 'Runs until interrupted, until --number events have been received, ' - 'or until --timeout seconds have elapsed.' + "Subscribe to a middleware event and print each message as it arrives. " + "Runs until interrupted, until --number events have been received, " + "or until --timeout seconds have elapsed." ), ) iparser.add_argument( - 'event', metavar='EVENT', - help='Event name to subscribe to (e.g. core.get_jobs).', + "event", + metavar="EVENT", + help="Event name to subscribe to (e.g. core.get_jobs).", ) iparser.add_argument( - '-n', '--number', type=int, metavar='N', - help='Exit after receiving this many events.', + "-n", + "--number", + type=int, + metavar="N", + help="Exit after receiving this many events.", ) iparser.add_argument( - '-t', '--timeout', type=int, metavar='SECONDS', - help='Stop waiting after this many seconds.', + "-t", + "--timeout", + type=int, + metavar="SECONDS", + help="Stop waiting after this many seconds.", ) return parser @@ -1332,12 +1540,18 @@ def main(): def from_json(args): stdin_content = None for i in args: - if i == '-': + if i == "-": # Read from stdin once and cache it if stdin_content is None: if sys.stdin.isatty(): - print("Error: Cannot read from stdin when connected to a terminal.", file=sys.stderr) - print("Please pipe input or provide the argument directly.", file=sys.stderr) + print( + "Error: Cannot read from stdin when connected to a terminal.", + file=sys.stderr, + ) + print( + "Please pipe input or provide the argument directly.", + file=sys.stderr, + ) sys.exit(1) stdin_content = sys.stdin.read() try: @@ -1350,7 +1564,7 @@ def from_json(args): except Exception: yield i - if args.name == 'call': + if args.name == "call": try: with Client(uri=args.uri, verify_ssl=not args.insecure) as c: try: @@ -1358,9 +1572,11 @@ def from_json(args): c.login_with_password(args.username, args.password) elif args.api_key: c.login_with_api_key( - args.username, args.api_key, + args.username, + args.api_key, auth_mechanism=( - APIKeyAuthMech.PLAIN if args.plain + APIKeyAuthMech.PLAIN + if args.plain else APIKeyAuthMech.SCRAM ), channel_binding=not args.no_channel_binding, @@ -1371,45 +1587,61 @@ def from_json(args): try: kwargs = {} if args.timeout: - kwargs['timeout'] = args.timeout + kwargs["timeout"] = args.timeout if args.job: - if args.job_print == 'progressbar': + if args.job_print == "progressbar": # display the job progress and status message while we wait def pb_callback(progress_bar: ProgressBar, job: _JobDict): """Update `progress_bar` with information in `job['progress']`.""" try: progress_bar.update( - job['progress']['percent'], job['progress']['description'] + job["progress"]["percent"], + job["progress"]["description"], ) except Exception as e: - print(f'Failed to update progress bar: {e!s}', file=sys.stderr) + print( + f"Failed to update progress bar: {e!s}", + file=sys.stderr, + ) with ProgressBar() as progress_bar: kwargs.update( job=True, - callback=lambda job: pb_callback(progress_bar, job) + callback=lambda job: pb_callback(progress_bar, job), + ) + rv = c.call( + args.method[0], + *list(from_json(args.method[1:])), + **kwargs, ) - rv = c.call(args.method[0], *list(from_json(args.method[1:])), **kwargs) progress_bar.finish() else: - lastdesc = '' + lastdesc = "" def callback(job: _JobDict): """Print `job`'s description to `stderr` if it has changed.""" nonlocal lastdesc - desc = job['progress']['description'] + desc = job["progress"]["description"] if desc is not None and desc != lastdesc: print(desc, file=sys.stderr) lastdesc = desc - kwargs.update({ - 'job': True, - 'callback': callback, - }) - rv = c.call(args.method[0], *list(from_json(args.method[1:])), **kwargs) + kwargs.update( + { + "job": True, + "callback": callback, + } + ) + rv = c.call( + args.method[0], + *list(from_json(args.method[1:])), + **kwargs, + ) else: - rv = c.call(args.method[0], *list(from_json(args.method[1:])), **kwargs) + rv = c.call( + args.method[0], *list(from_json(args.method[1:])), **kwargs + ) if isinstance(rv, (int, str)): print(rv) else: @@ -1419,30 +1651,34 @@ def callback(job: _JobDict): if e.error: print(e.error, file=sys.stderr) if e.trace: - print(e.trace['formatted'], file=sys.stderr) + print(e.trace["formatted"], file=sys.stderr) if e.extra: pprint.pprint(e.extra, stream=sys.stderr) sys.exit(1) except ssl.SSLCertVerificationError: - print('SSL certificate verification failed.\n', file=sys.stderr) - print('You can either:', file=sys.stderr) - print(' 1. Install the server\'s SSL certificate in your system\'s trust store', file=sys.stderr) - print(' 2. Use the --insecure flag to disable SSL verification (not recommended for production)', - file=sys.stderr) + print("SSL certificate verification failed.\n", file=sys.stderr) + print("You can either:", file=sys.stderr) + print( + " 1. Install the server's SSL certificate in your system's trust store", + file=sys.stderr, + ) + print( + " 2. Use the --insecure flag to disable SSL verification (not recommended for production)", + file=sys.stderr, + ) sys.exit(1) except (FileNotFoundError, ConnectionRefusedError): - print('Failed to run middleware call. Daemon not running?', file=sys.stderr) + print("Failed to run middleware call. Daemon not running?", file=sys.stderr) sys.exit(1) - elif args.name == 'ping': + elif args.name == "ping": with Client(uri=args.uri, verify_ssl=not args.insecure) as c: if not (result := c.ping()): sys.exit(1) print(result) - elif args.name == 'subscribe': + elif args.name == "subscribe": with Client(uri=args.uri, verify_ssl=not args.insecure) as c: - subscribe_payload = c.event_payload() - event = subscribe_payload['event'] + event = subscribe_payload["event"] number = 0 def cb(mtype: str, **message): @@ -1458,12 +1694,12 @@ def cb(mtype: str, **message): if not event.wait(timeout=args.timeout): sys.exit(1) - if 'error' in subscribe_payload and subscribe_payload['error']: - raise ValueError(subscribe_payload['error']) + if "error" in subscribe_payload and subscribe_payload["error"]: + raise ValueError(subscribe_payload["error"]) sys.exit(0) else: parser.print_help() -if __name__ == '__main__': +if __name__ == "__main__": main() diff --git a/truenas_api_client/auth_api_key.py b/truenas_api_client/auth_api_key.py index 1bd11ab..715f492 100644 --- a/truenas_api_client/auth_api_key.py +++ b/truenas_api_client/auth_api_key.py @@ -9,45 +9,50 @@ from .ejson import loads from .exc import ClientException -from .scram_impl import CB_TLS_SERVER_END_POINT, CryptoDatum, TNScramClient, compute_tls_server_end_point +from .scram_impl import ( + CB_TLS_SERVER_END_POINT, + CryptoDatum, + TNScramClient, + compute_tls_server_end_point, +) class Caller(Protocol): """Any client with a call method, such as JSONRPCClient or LegacyClient.""" - def call(self, method: str, *params: Any) -> Any: - ... + def call(self, method: str, *params: Any) -> Any: ... # Raw API keys have the format: {api_key_id}-{raw_key_material} # Example: 1-uz8DhKHFhRIUQIvjzabPYtpy5wf1DJ3ZBLlDgNVhRAFT7Y6pJGUlm0n3apwxWEU4 -RAW_KEY_SEPARATOR = '-' -RAW_KEY_PATTERN = re.compile(r'^\d+-[a-zA-Z0-9]+$') +RAW_KEY_SEPARATOR = "-" +RAW_KEY_PATTERN = re.compile(r"^\d+-[a-zA-Z0-9]+$") # Authentication mechanism constants -MECHANISM_SCRAM = 'SCRAM' -MECHANISM_API_KEY_PLAIN = 'API_KEY_PLAIN' -RESPONSE_TYPE_SCRAM = 'SCRAM_RESPONSE' -RESPONSE_TYPE_AUTH_ERR = 'AUTH_ERR' +MECHANISM_SCRAM = "SCRAM" +MECHANISM_API_KEY_PLAIN = "API_KEY_PLAIN" +RESPONSE_TYPE_SCRAM = "SCRAM_RESPONSE" +RESPONSE_TYPE_AUTH_ERR = "AUTH_ERR" # Websocket URI prefix for the local middleware UNIX domain socket. Channel binding is # meaningless for local IPC (no TLS, no MITM surface), so this transport is exempt from # the channel_binding=True requirement. -UNIX_SOCKET_PREFIX = 'ws+unix://' +UNIX_SOCKET_PREFIX = "ws+unix://" class APIKeyAuthMech(StrEnum): - SCRAM = 'SCRAM' # SCRAM-SHA-512 (default); never transmits the API key in plaintext - PLAIN = 'PLAIN' # explicit opt-in; transmits the raw API key (only TLS protects it) + SCRAM = "SCRAM" # SCRAM-SHA-512 (default); never transmits the API key in plaintext + PLAIN = "PLAIN" # explicit opt-in; transmits the raw API key (only TLS protects it) class KeyDataType(StrEnum): - RAW = 'RAW' - PRECOMPUTED = 'PRECOMPUTED' + RAW = "RAW" + PRECOMPUTED = "PRECOMPUTED" class RawKeyData(TypedDict): """Raw API key material as returned when generated by server.""" + raw_key: str @@ -58,6 +63,7 @@ class PrecomputedKeyData(TypedDict): Avoids expensive PBKDF2 computation on the client. All fields are required. """ + client_key: str stored_key: str server_key: str @@ -67,6 +73,7 @@ class PrecomputedKeyData(TypedDict): @dataclass class KeyData: """Wrapper object to help type checking.""" + key_data_type: KeyDataType key_data: RawKeyData | PrecomputedKeyData @@ -92,41 +99,41 @@ def _parse_ini_config(key: str) -> dict[str, str | int]: config_dict: dict[str, str | int] if len(config.sections()) == 0: # No section headers, try DEFAULT - config_dict = dict(config['DEFAULT']) - elif 'TRUENAS_API_KEY' in config: + config_dict = dict(config["DEFAULT"]) + elif "TRUENAS_API_KEY" in config: # Use TRUENAS_API_KEY section - config_dict = dict(config['TRUENAS_API_KEY']) + config_dict = dict(config["TRUENAS_API_KEY"]) elif len(config.sections()) == 1: # Single section, use it config_dict = dict(config[config.sections()[0]]) else: # Multiple sections without TRUENAS_API_KEY - error raise ValueError( - f'Multiple sections found but [TRUENAS_API_KEY] not present. ' - f'Available sections: {config.sections()}' + f"Multiple sections found but [TRUENAS_API_KEY] not present. " + f"Available sections: {config.sections()}" ) # Convert string values to appropriate types - if 'api_key_id' in config_dict: - config_dict['api_key_id'] = int(config_dict['api_key_id']) + if "api_key_id" in config_dict: + config_dict["api_key_id"] = int(config_dict["api_key_id"]) return config_dict def get_key_material(key: str) -> KeyData: - """ User may have provided a string containing key info or path to key file. - In the case of the key file, we assume it is JSON-formatted or INI-formatted with above fields. """ + """User may have provided a string containing key info or path to key file. + In the case of the key file, we assume it is JSON-formatted or INI-formatted with above fields.""" if os.path.isabs(key): # User has provided path to a key file, read the material try: - with open(key, 'r') as f: + with open(key, "r") as f: key = f.read().strip() except FileNotFoundError: - raise ValueError(f'Key file not found: {key}') + raise ValueError(f"Key file not found: {key}") except PermissionError: - raise ValueError(f'Permission denied reading key file: {key}') + raise ValueError(f"Permission denied reading key file: {key}") except OSError as e: - raise ValueError(f'Error reading key file {key}: {e}') + raise ValueError(f"Error reading key file {key}: {e}") else: # Strip whitespace from input to ensure clean matching key = key.strip() @@ -149,35 +156,43 @@ def get_key_material(key: str) -> KeyData: # so that internal error isn't re-surfaced. ValueError/KeyError raised by # _parse_ini_config itself are useful diagnostics and are allowed to propagate. raise ValueError( - 'Key material must be either a raw API key (format: -), ' - 'valid JSON, or valid INI/ConfigParser format' + "Key material must be either a raw API key (format: -), " + "valid JSON, or valid INI/ConfigParser format" ) from None # Construct the appropriate type based on the fields present try: - if 'raw_key' in data: - raw_key_value = data['raw_key'] + if "raw_key" in data: + raw_key_value = data["raw_key"] if not isinstance(raw_key_value, str): - raise ValueError(f'raw_key must be a string, got {type(raw_key_value)}') + raise ValueError(f"raw_key must be a string, got {type(raw_key_value)}") return KeyData( key_data_type=KeyDataType.RAW, - key_data=RawKeyData(raw_key=raw_key_value) + key_data=RawKeyData(raw_key=raw_key_value), ) else: # Validate and extract precomputed key fields with type checking - client_key_value = data['client_key'] - stored_key_value = data['stored_key'] - server_key_value = data['server_key'] - api_key_id_value = data['api_key_id'] + client_key_value = data["client_key"] + stored_key_value = data["stored_key"] + server_key_value = data["server_key"] + api_key_id_value = data["api_key_id"] if not isinstance(client_key_value, str): - raise ValueError(f'client_key must be a string, got {type(client_key_value)}') + raise ValueError( + f"client_key must be a string, got {type(client_key_value)}" + ) if not isinstance(stored_key_value, str): - raise ValueError(f'stored_key must be a string, got {type(stored_key_value)}') + raise ValueError( + f"stored_key must be a string, got {type(stored_key_value)}" + ) if not isinstance(server_key_value, str): - raise ValueError(f'server_key must be a string, got {type(server_key_value)}') + raise ValueError( + f"server_key must be a string, got {type(server_key_value)}" + ) if not isinstance(api_key_id_value, int): - raise ValueError(f'api_key_id must be an int, got {type(api_key_id_value)}') + raise ValueError( + f"api_key_id must be an int, got {type(api_key_id_value)}" + ) return KeyData( key_data_type=KeyDataType.PRECOMPUTED, @@ -185,32 +200,32 @@ def get_key_material(key: str) -> KeyData: client_key=client_key_value, stored_key=stored_key_value, server_key=server_key_value, - api_key_id=api_key_id_value - ) + api_key_id=api_key_id_value, + ), ) except KeyError as e: - raise ValueError(f'Missing required field in key data: {e}') from e + raise ValueError(f"Missing required field in key data: {e}") from e def _raise_for_api_key_response(resp: dict) -> None: """Raise ValueError with a specific message for non-SUCCESS API_KEY_PLAIN responses.""" - match resp['response_type']: - case 'SUCCESS': + match resp["response_type"]: + case "SUCCESS": return - case 'AUTH_ERR': - raise ValueError('Invalid API key') - case 'EXPIRED': - raise ValueError('API key has expired') - case 'DENIED': - raise ValueError('Account does not have API access') - case 'REDIRECT': - urls = ', '.join(resp.get('urls', [])) + case "AUTH_ERR": + raise ValueError("Invalid API key") + case "EXPIRED": + raise ValueError("API key has expired") + case "DENIED": + raise ValueError("Account does not have API access") + case "REDIRECT": + urls = ", ".join(resp.get("urls", [])) raise ValueError( - f'Authentication must be performed on active storage controller. ' - f'Redirect URLs: {urls}' + f"Authentication must be performed on active storage controller. " + f"Redirect URLs: {urls}" ) case other: - raise ValueError(f'{other}: unexpected server response') + raise ValueError(f"{other}: unexpected server response") def _resolve_channel_binding(c: object, channel_binding: bool) -> CryptoDatum | None: @@ -237,25 +252,25 @@ def _resolve_channel_binding(c: object, channel_binding: bool) -> CryptoDatum | if not channel_binding: return None - ws = getattr(c, '_ws', None) - get_cert = getattr(ws, 'get_peer_cert_der', None) + ws = getattr(c, "_ws", None) + get_cert = getattr(ws, "get_peer_cert_der", None) cert_der = get_cert() if get_cert is not None else None if cert_der: if compute_tls_server_end_point is None: raise ValueError( - 'channel binding is required but the installed truenas_pyscram does not ' - 'support it; upgrade truenas_pyscram or pass channel_binding=False' + "channel binding is required but the installed truenas_pyscram does not " + "support it; upgrade truenas_pyscram or pass channel_binding=False" ) return compute_tls_server_end_point(cert_der) # No TLS certificate is available on this transport. The local UNIX socket is exempt; # any other non-TLS transport cannot carry channel binding. - if (getattr(ws, 'url', '') or '').startswith(UNIX_SOCKET_PREFIX): + if (getattr(ws, "url", "") or "").startswith(UNIX_SOCKET_PREFIX): return None raise ValueError( - 'channel binding is required but the connection is not TLS (wss://); ' - 'pass channel_binding=False to authenticate without it' + "channel binding is required but the connection is not TLS (wss://); " + "pass channel_binding=False to authenticate without it" ) @@ -302,15 +317,15 @@ def api_key_authenticate( could not be established (see `channel_binding`). """ if key_in is None: - raise ValueError('API key is required') + raise ValueError("API key is required") auth_data = get_key_material(key_in) do_legacy_auth = False try: - available_mechanisms = c.call('auth.mechanism_choices') + available_mechanisms = c.call("auth.mechanism_choices") except ClientException as exc: - if exc.error == 'Method does not exist': + if exc.error == "Method does not exist": # We have an older version of TrueNAS. Minimally, API key authentication of some sort should be available available_mechanisms = [MECHANISM_API_KEY_PLAIN] elif "'NoneType' object has no attribute 'may_create_auth_token'" in exc.error: @@ -332,15 +347,17 @@ def api_key_authenticate( # therefore an explicit, informed choice (auth_mechanism=PLAIN). if MECHANISM_SCRAM not in available_mechanisms: raise ValueError( - 'SCRAM authentication is not supported on the remote server; pass ' + "SCRAM authentication is not supported on the remote server; pass " 'auth_mechanism="PLAIN" to authenticate with a plaintext API key' ) if not username: - raise ValueError('username is required for SCRAM authentication') + raise ValueError("username is required for SCRAM authentication") if do_legacy_auth and auth_data.key_data_type != KeyDataType.RAW: - raise ValueError('Raw API key is required in order to do legacy API key authentication') + raise ValueError( + "Raw API key is required in order to do legacy API key authentication" + ) if do_legacy_auth: # Type narrowing: key_data is RawKeyData here @@ -349,17 +366,20 @@ def api_key_authenticate( if use_legacy_endpoint: # Legacy server reached via /websocket has no auth.login_ex. - if not c.call('auth.login_with_api_key', legacy_key_data['raw_key']): - raise ValueError('Invalid API key') + if not c.call("auth.login_with_api_key", legacy_key_data["raw_key"]): + raise ValueError("Invalid API key") return if not username: - raise ValueError('username is required for plain API key authentication') - resp = c.call('auth.login_ex', { - 'mechanism': MECHANISM_API_KEY_PLAIN, - 'username': username, - 'api_key': legacy_key_data['raw_key'], - }) + raise ValueError("username is required for plain API key authentication") + resp = c.call( + "auth.login_ex", + { + "mechanism": MECHANISM_API_KEY_PLAIN, + "username": username, + "api_key": legacy_key_data["raw_key"], + }, + ) _raise_for_api_key_response(resp) return @@ -369,19 +389,19 @@ def api_key_authenticate( if auth_data.key_data_type == KeyDataType.RAW: # Type narrowing: key_data is RawKeyData here raw_key_data: RawKeyData = auth_data.key_data # type: ignore[assignment] - api_key_id, raw_key = raw_key_data['raw_key'].split(RAW_KEY_SEPARATOR, 1) + api_key_id, raw_key = raw_key_data["raw_key"].split(RAW_KEY_SEPARATOR, 1) sc = TNScramClient(raw_key_material=raw_key, api_key_id=int(api_key_id)) else: # Type narrowing: key_data is PrecomputedKeyData here precomputed_data: PrecomputedKeyData = auth_data.key_data # type: ignore[assignment] - client_datum = CryptoDatum(b64decode(precomputed_data['client_key'])) - stored_datum = CryptoDatum(b64decode(precomputed_data['stored_key'])) - server_datum = CryptoDatum(b64decode(precomputed_data['server_key'])) + client_datum = CryptoDatum(b64decode(precomputed_data["client_key"])) + stored_datum = CryptoDatum(b64decode(precomputed_data["stored_key"])) + server_datum = CryptoDatum(b64decode(precomputed_data["server_key"])) sc = TNScramClient( client_key=client_datum, stored_key=stored_datum, server_key=server_datum, - api_key_id=precomputed_data['api_key_id'] + api_key_id=precomputed_data["api_key_id"], ) # SCRAM-PLUS channel binding (RFC 5929 tls-server-end-point): by default the exchange @@ -390,34 +410,40 @@ def api_key_authenticate( # local UNIX socket is exempt, other non-TLS transports raise) and the # channel_binding=False escape hatch for servers without channel-binding support. binding = _resolve_channel_binding(c, channel_binding) - first_kwargs: dict[str, str] = {'channel_binding_type': CB_TLS_SERVER_END_POINT} if binding else {} + first_kwargs: dict[str, str] = ( + {"channel_binding_type": CB_TLS_SERVER_END_POINT} if binding else {} + ) client_first_message = sc.get_client_first_message(username, **first_kwargs) # Send our first client SCRAM message that provides client nonce to server and provides what key identifier # is being used server-side. - resp = c.call('auth.login_ex', {'mechanism': MECHANISM_SCRAM} | client_first_message) - resp_type = resp.pop('response_type') + resp = c.call( + "auth.login_ex", {"mechanism": MECHANISM_SCRAM} | client_first_message + ) + resp_type = resp.pop("response_type") if resp_type == RESPONSE_TYPE_AUTH_ERR: - raise ValueError('Invalid API key') + raise ValueError("Invalid API key") elif resp_type != RESPONSE_TYPE_SCRAM: - raise ValueError(f'{resp_type}: unexpected server response') + raise ValueError(f"{resp_type}: unexpected server response") client_final_message = sc.get_client_final_message( server_resp_dict=resp, channel_binding=binding ) # Send our client SCRAM final message that provides client proof to server - resp = c.call('auth.login_ex', {'mechanism': MECHANISM_SCRAM} | client_final_message) - resp_type = resp.pop('response_type') + resp = c.call( + "auth.login_ex", {"mechanism": MECHANISM_SCRAM} | client_final_message + ) + resp_type = resp.pop("response_type") if resp_type == RESPONSE_TYPE_AUTH_ERR: - raise ValueError('Failed to authenticate with API key') + raise ValueError("Failed to authenticate with API key") if resp_type != RESPONSE_TYPE_SCRAM: - raise ValueError(f'{resp_type}: unexpected server response') + raise ValueError(f"{resp_type}: unexpected server response") # Now validate that the server final message is OK try: @@ -425,7 +451,7 @@ def api_key_authenticate( except Exception as e: # Attempt to logout, but don't mask the original verification error try: - c.call('auth.logout') + c.call("auth.logout") except Exception: pass # Ignore logout errors, the verification failure is more critical - raise ValueError(f'Remote server validation failed! {e}') + raise ValueError(f"Remote server validation failed! {e}") diff --git a/truenas_api_client/ejson.py b/truenas_api_client/ejson.py index 80a986a..2c31d98 100644 --- a/truenas_api_client/ejson.py +++ b/truenas_api_client/ejson.py @@ -15,6 +15,7 @@ >>> deserialized = loads(serialized) """ + import calendar import dataclasses from datetime import date, datetime, time, timedelta, timezone @@ -60,26 +61,26 @@ def __init__(self, *args, repr_allowlist=None, **kwargs): def default(self, obj): if type(obj) is date: - return {'$type': 'date', '$value': obj.isoformat()} + return {"$type": "date", "$value": obj.isoformat()} elif type(obj) is datetime: if obj.tzinfo is not None: obj = obj.astimezone(timezone.utc) # Total milliseconds since EPOCH - return {'$date': int(calendar.timegm(obj.timetuple()) * 1000)} + return {"$date": int(calendar.timegm(obj.timetuple()) * 1000)} elif type(obj) is time: - return {'$time': str(obj)} + return {"$time": str(obj)} elif isinstance(obj, (set, frozenset)): - return {'$set': list(obj)} + return {"$set": list(obj)} elif isinstance(obj, IPv4Interface): - return {'$ipv4_interface': str(obj)} + return {"$ipv4_interface": str(obj)} elif isinstance(obj, IPv6Interface): - return {'$ipv6_interface': str(obj)} + return {"$ipv6_interface": str(obj)} elif BaseModel is not None and isinstance(obj, BaseModel): return obj.model_dump(warnings=False, by_alias=True) elif dataclasses.is_dataclass(obj) and not isinstance(obj, type): return dataclasses.asdict(obj) elif self.repr_allowlist and isinstance(obj, self.repr_allowlist): - return {'$repr': repr(obj)} + return {"$repr": repr(obj)} return super(JSONEncoder, self).default(obj) @@ -90,35 +91,34 @@ def object_hook(obj: dict): Passed as a kwarg to a JSON deserialization function like `json.dump()`. """ - error_key = '' + error_key = "" try: obj_len = len(obj) if obj_len == 1: error_key = list(obj.keys())[0] - if '$date' in obj: + if "$date" in obj: # floor-divide for whole seconds; the remainder is added as milliseconds. # (fromtimestamp already accounts for the fraction, so true division would # count the sub-second part twice.) - return ( - datetime.fromtimestamp(obj['$date'] // 1000, tz=timezone.utc) + - timedelta(milliseconds=obj['$date'] % 1000) - ) - if '$time' in obj: + return datetime.fromtimestamp( + obj["$date"] // 1000, tz=timezone.utc + ) + timedelta(milliseconds=obj["$date"] % 1000) + if "$time" in obj: # inverse of the encoder's str(time), which is ISO format and may carry a # fractional-second (.ffffff) or UTC offset that the old int-split couldn't parse. - return time.fromisoformat(obj['$time']) - if '$set' in obj: - return set(obj['$set']) - if '$ipv4_interface' in obj: - return IPv4Interface(obj['$ipv4_interface']) - if '$ipv6_interface' in obj: - return IPv6Interface(obj['$ipv6_interface']) - if obj_len == 2 and '$type' in obj and '$value' in obj: - error_key = obj['$type'] - if obj['$type'] == 'date': - return date(*[int(i) for i in obj['$value'].split('-')]) + return time.fromisoformat(obj["$time"]) + if "$set" in obj: + return set(obj["$set"]) + if "$ipv4_interface" in obj: + return IPv4Interface(obj["$ipv4_interface"]) + if "$ipv6_interface" in obj: + return IPv6Interface(obj["$ipv6_interface"]) + if obj_len == 2 and "$type" in obj and "$value" in obj: + error_key = obj["$type"] + if obj["$type"] == "date": + return date(*[int(i) for i in obj["$value"].split("-")]) except Exception as e: - raise EJSONDecodeError(f'Error parsing {error_key}: {e}') + raise EJSONDecodeError(f"Error parsing {error_key}: {e}") return obj diff --git a/truenas_api_client/exc.py b/truenas_api_client/exc.py index 2ac65b1..9b620f7 100644 --- a/truenas_api_client/exc.py +++ b/truenas_api_client/exc.py @@ -16,6 +16,7 @@ class ReserveFDException(Exception): """A `WSClient` instance failed to bind to a reserved port.""" + pass @@ -56,7 +57,7 @@ def _get_errname(cls, code: int) -> str | None: """ if LIBZFS and 2000 <= code <= 2100: - return 'EZFS_' + ZFSError(code).name + return "EZFS_" + ZFSError(code).name for k, v in cls.__dict__.items(): if k.startswith("E") and v == code: return k @@ -66,8 +67,13 @@ def _get_errname(cls, code: int) -> str | None: class ClientException(ErrnoMixin, Exception): """Represents any exception that might arise from a `Client`.""" - def __init__(self, error: str, errno: int | None = None, trace: TruenasTraceback | None = None, - extra: list[ErrorExtra] | None = None): + def __init__( + self, + error: str, + errno: int | None = None, + trace: TruenasTraceback | None = None, + extra: list[ErrorExtra] | None = None, + ): """Initialize `ClientException`. Args: @@ -105,13 +111,14 @@ def __init__(self, errors: Iterable[ErrorExtra]): def __str__(self): msgs = [] for e in self.errors: - errcode = errno.errorcode.get(e.errcode, 'EUNKNOWN') - msgs.append(f'[{errcode}] {e.attribute or "ALL"}: {e.errmsg}') - return '\n'.join(msgs) + errcode = errno.errorcode.get(e.errcode, "EUNKNOWN") + msgs.append(f"[{errcode}] {e.attribute or 'ALL'}: {e.errmsg}") + return "\n".join(msgs) class CallTimeout(ClientException): """A special `ClientException` raised when a `Call` times out before it can return a result.""" + def __init__(self): """Initiate a `ClientException` with message `"Call timeout"`.""" super().__init__("Call timeout", errno.ETIMEDOUT) diff --git a/truenas_api_client/jsonrpc.py b/truenas_api_client/jsonrpc.py index febd337..ec901d2 100644 --- a/truenas_api_client/jsonrpc.py +++ b/truenas_api_client/jsonrpc.py @@ -3,6 +3,7 @@ https://www.jsonrpc.org/specification """ + import enum from typing import Any, Literal, NamedTuple, NotRequired, TypeAlias, TypedDict, final @@ -58,17 +59,20 @@ class CollectionUpdateParams(TypedDict): @final class CollectionUpdate(TypedDict): - jsonrpc: Literal['2.0'] - method: Literal['collection_update'] + jsonrpc: Literal["2.0"] + method: Literal["collection_update"] params: CollectionUpdateParams -TruenasTraceback = TypedDict('TruenasTraceback', { - 'class': str, - 'frames': list[dict[str, Any]], - 'formatted': str, - 'repr': str, -}) +TruenasTraceback = TypedDict( + "TruenasTraceback", + { + "class": str, + "frames": list[dict[str, Any]], + "formatted": str, + "repr": str, + }, +) # Has to be defined this way because `class` is a keyword. @@ -88,14 +92,14 @@ class NotifyUnsubscribedParams(TypedDict): @final class NotifyUnsubscribed(TypedDict): - jsonrpc: Literal['2.0'] - method: Literal['notify_unsubscribed'] + jsonrpc: Literal["2.0"] + method: Literal["notify_unsubscribed"] params: NotifyUnsubscribedParams @final class SuccessResponse(TypedDict): - jsonrpc: Literal['2.0'] + jsonrpc: Literal["2.0"] result: Any id: str @@ -108,11 +112,13 @@ class ErrorObj(TypedDict): @final class ErrorResponse(TypedDict): - jsonrpc: Literal['2.0'] + jsonrpc: Literal["2.0"] error: ErrorObj id: str | None Notification: TypeAlias = CollectionUpdate | NotifyUnsubscribed Response: TypeAlias = SuccessResponse | ErrorResponse -JSONRPCMessage: TypeAlias = CollectionUpdate | NotifyUnsubscribed | SuccessResponse | ErrorResponse +JSONRPCMessage: TypeAlias = ( + CollectionUpdate | NotifyUnsubscribed | SuccessResponse | ErrorResponse +) diff --git a/truenas_api_client/legacy.py b/truenas_api_client/legacy.py index 69d2600..7a71724 100644 --- a/truenas_api_client/legacy.py +++ b/truenas_api_client/legacy.py @@ -50,22 +50,28 @@ def connect(self): # plaintext ws:// URI. Require that scheme explicitly rather than, for example, # connecting a wss:// URI in cleartext. scheme = urllib.parse.urlparse(self.url).scheme - if scheme != 'ws': + if scheme != "ws": raise ClientException( - f'reserved_ports connections require a ws:// URI, got {scheme!r}' + f"reserved_ports connections require a ws:// URI, got {scheme!r}" ) self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) self.socket.settimeout(10) self._bind_to_reserved_port() try: - self.socket.connect((urllib.parse.urlparse(self.url).hostname, - urllib.parse.urlparse(self.url).port or 80)) + self.socket.connect( + ( + urllib.parse.urlparse(self.url).hostname, + urllib.parse.urlparse(self.url).port or 80, + ) + ) except Exception: self.socket.close() raise app_url = "ws://localhost/websocket" # Adviced by official docs to use dummy hostname else: - sockopt = sock_opt(None, None if self.verify_ssl else {"cert_reqs": ssl.CERT_NONE}) + sockopt = sock_opt( + None, None if self.verify_ssl else {"cert_reqs": ssl.CERT_NONE} + ) sockopt.timeout = 10 self.socket = connect(self.url, sockopt, proxy_info(), None)[0] app_url = self.url @@ -103,7 +109,7 @@ def _bind_to_reserved_port(self): for port in ports_to_try: try: - self.socket.bind(('', port)) + self.socket.bind(("", port)) return except OSError: time.sleep(0.1) @@ -113,7 +119,7 @@ def _bind_to_reserved_port(self): def _on_open(self, app): # TCP keepalive settings don't apply to local unix sockets - if 'ws+unix' not in self.url: + if "ws+unix" not in self.url: set_socket_options(self.socket) # if we're able to connect put socket in blocking mode @@ -172,51 +178,61 @@ def __init__(self, client, job_id, callback=None): with client._jobs_lock: client._unclaimed_jobs.pop(job_id, None) job = client._jobs[job_id] - self.event = job.get('__ready') + self.event = job.get("__ready") if self.event is None: - self.event = job['__ready'] = Event() - job['__callback'] = callback + self.event = job["__ready"] = Event() + job["__callback"] = callback def __repr__(self): - return f'' + return f"" def result(self): # Wait indefinitely for the job event with state SUCCESS/FAILED/ABORTED self.event.wait() job = self.client._jobs.pop(self.job_id, None) if job is None: - raise ClientException('No job event was received.') - state = job.get('state') - if state == 'SUCCESS': - return job['result'] + raise ClientException("No job event was received.") + state = job.get("state") + if state == "SUCCESS": + return job["result"] - exc_info = job.get('exc_info') + exc_info = job.get("exc_info") if exc_info: - if exc_info['type'] == 'VALIDATION': - raise ValidationErrors(exc_info['extra'] or []) + if exc_info["type"] == "VALIDATION": + raise ValidationErrors(exc_info["extra"] or []) raise ClientException( - job['error'], + job["error"], trace={ - 'class': exc_info['type'], - 'frames': [], - 'formatted': job['exception'], - 'repr': exc_info.get('repr') or job['exception'].splitlines()[-1], + "class": exc_info["type"], + "frames": [], + "formatted": job["exception"], + "repr": exc_info.get("repr") or job["exception"].splitlines()[-1], }, - extra=exc_info['extra'], + extra=exc_info["extra"], ) # Aborted or interrupted jobs have no exc_info to build a trace from. - raise ClientException(job.get('error') or f'Job {self.job_id} did not succeed (state={state!r})') + raise ClientException( + job.get("error") or f"Job {self.job_id} did not succeed (state={state!r})" + ) class LegacyClient: - def __init__(self, uri=None, reserved_ports=False, private_methods=False, py_exceptions=False, - log_py_exceptions=False, call_timeout: float | UndefinedType = undefined, verify_ssl=True): + def __init__( + self, + uri=None, + reserved_ports=False, + private_methods=False, + py_exceptions=False, + log_py_exceptions=False, + call_timeout: float | UndefinedType = undefined, + verify_ssl=True, + ): """ Arguments: :reserved_ports(bool): should the local socket used a reserved port """ if uri is None: - uri = f'ws+unix://{MIDDLEWARE_RUN_DIR}/middlewared.sock' + uri = f"ws+unix://{MIDDLEWARE_RUN_DIR}/middlewared.sock" if call_timeout is undefined: call_timeout = CALL_TIMEOUT @@ -231,7 +247,9 @@ def __init__(self, uri=None, reserved_ports=False, private_methods=False, py_exc self._py_exceptions = py_exceptions self._log_py_exceptions = log_py_exceptions self._call_timeout = call_timeout - self._event_callbacks: defaultdict[str, list[dict[str, Any]]] = defaultdict(list) + self._event_callbacks: defaultdict[str, list[dict[str, Any]]] = defaultdict( + list + ) self._closed = Event() self._connected = Event() self._connection_error: str | None = None @@ -244,7 +262,7 @@ def __init__(self, uri=None, reserved_ports=False, private_methods=False, py_exc self._ws.connect() self._connected.wait(10) if not self._connected.is_set(): - raise ClientException('Failed connection handshake') + raise ClientException("Failed connection handshake") if self._connection_error is not None: raise ClientException(self._connection_error) @@ -262,30 +280,34 @@ def _send(self, data): except (AttributeError, WebSocketConnectionClosedException): # happens when other node on HA is rebooted, for example, and there are # running tasks in the event loop (i.e. failover.call_remote failover.get_disks_local) - raise ClientException('Unexpected closure of remote connection', errno.ECONNABORTED) + raise ClientException( + "Unexpected closure of remote connection", errno.ECONNABORTED + ) def _recv(self, message): - _id = message.get('id') - msg = message.get('msg') - if msg == 'connected': + _id = message.get("id") + msg = message.get("msg") + if msg == "connected": self._connected.set() - elif msg == 'failed': - self._connection_error = 'Unsupported protocol version' + elif msg == "failed": + self._connection_error = "Unsupported protocol version" self._connected.set() - elif msg == 'pong' and _id is not None: + elif msg == "pong" and _id is not None: ping_event = self._pings.get(_id) if ping_event: ping_event.set() - elif _id is not None and msg == 'result': + elif _id is not None and msg == "result": if call := self._calls.get(_id): - call.result = message.get('result') - if 'error' in message: - call.errno = message['error'].get('error') - call.error = message['error'].get('reason') - call.trace = message['error'].get('trace') - call.type = message['error'].get('type') - call.extra = message['error'].get('extra') - if self._py_exceptions and (py_exception := message['error'].get('py_exception')): + call.result = message.get("result") + if "error" in message: + call.errno = message["error"].get("error") + call.error = message["error"].get("reason") + call.trace = message["error"].get("trace") + call.type = message["error"].get("type") + call.extra = message["error"].get("extra") + if self._py_exceptions and ( + py_exception := message["error"].get("py_exception") + ): try: call.py_exception = pickle.loads(b64decode(py_exception)) except Exception as e: @@ -293,60 +315,67 @@ def _recv(self, message): call.returned.set() self._unregister_call(call) else: - if 'error' in message: + if "error" in message: for events in self._event_callbacks.values(): for event in events: - if event['id'] == _id: - event['error'] = message['error'] - event['ready'].set() + if event["id"] == _id: + event["error"] = message["error"] + event["ready"].set() break - elif msg in ('added', 'changed', 'removed'): + elif msg in ("added", "changed", "removed"): if self._event_callbacks: - if '*' in self._event_callbacks: - for event in self._event_callbacks['*']: + if "*" in self._event_callbacks: + for event in self._event_callbacks["*"]: self._run_callback(event, [msg.upper()], message) - if message['collection'] in self._event_callbacks: - for event in self._event_callbacks[message['collection']]: + if message["collection"] in self._event_callbacks: + for event in self._event_callbacks[message["collection"]]: self._run_callback(event, [msg.upper()], message) - elif msg == 'ready': - for subid in message['subs']: + elif msg == "ready": + for subid in message["subs"]: # FIXME: We may need to keep a different index for id # so we don't hve to iterate through all. # This is fine for just a dozen subscriptions for events in self._event_callbacks.values(): for event in events: - if subid == event['id']: - event['ready'].set() + if subid == event["id"]: + event["ready"].set() break - elif msg == 'nosub': - if message['collection'] in self._event_callbacks: - for event in self._event_callbacks[message['collection']]: - if 'error' in message: - event['error'] = message['error']['reason'] or message['error']['error'] - event['ready'].set() - event['event'].set() + elif msg == "nosub": + if message["collection"] in self._event_callbacks: + for event in self._event_callbacks[message["collection"]]: + if "error" in message: + event["error"] = ( + message["error"]["reason"] or message["error"]["error"] + ) + event["ready"].set() + event["event"].set() def _run_callback(self, event, args, kwargs): - if event['sync']: - event['callback'](*args, **kwargs) + if event["sync"]: + event["callback"](*args, **kwargs) else: Thread( - target=event['callback'], args=args, kwargs=kwargs, daemon=True, + target=event["callback"], + args=args, + kwargs=kwargs, + daemon=True, ).start() def on_open(self): features = [] if self._py_exceptions: - features.append('PY_EXCEPTIONS') - self._send({ - 'msg': 'connect', - 'version': '1', - 'support': ['1'], - 'features': features, - }) + features.append("PY_EXCEPTIONS") + self._send( + { + "msg": "connect", + "version": "1", + "support": ["1"], + "features": features, + } + ) def on_close(self, code, reason=None): - error = f'WebSocket connection closed with code={code!r}, reason={reason!r}' + error = f"WebSocket connection closed with code={code!r}, reason={reason!r}" self._connection_error = error self._connected.set() @@ -359,20 +388,20 @@ def on_close(self, code, reason=None): call.returned.set() for job in list(self._jobs.values()): - event = job.get('__ready') + event = job.get("__ready") if event is None: - event = job['__ready'] = Event() + event = job["__ready"] = Event() if not event.is_set(): # The connection dropped, so the real outcome is unknown (the job may still be # running server-side); just mark it non-SUCCESS for Job.result(). - job['state'] = 'UNKNOWN' - job['error'] = error - job['exception'] = error - job['exc_info'] = { - 'type': 'Exception', - 'repr': error, - 'extra': None, + job["state"] = "UNKNOWN" + job["error"] = error + job["exception"] = error + job["exc_info"] = { + "type": "Exception", + "repr": error, + "extra": None, } event.set() @@ -388,8 +417,8 @@ def _jobs_callback(self, mtype, **message): """ Method to process the received job events. """ - fields: Any = message.get('fields') - job_id = fields['id'] + fields: Any = message.get("fields") + job_id = fields["id"] with self._jobs_lock: if fields: if job_id not in self._jobs: @@ -403,18 +432,24 @@ def _jobs_callback(self, mtype, **message): self._jobs.pop(evicted, None) job = self._jobs[job_id] job.update(fields) - if callable(job.get('__callback')): + if callable(job.get("__callback")): Thread( - target=job['__callback'], args=(job,), daemon=True, + target=job["__callback"], + args=(job,), + daemon=True, ).start() - if mtype == 'CHANGED' and job['state'] in ('SUCCESS', 'FAILED', 'ABORTED'): + if mtype == "CHANGED" and job["state"] in ( + "SUCCESS", + "FAILED", + "ABORTED", + ): # If an Event already exist we just set it to mark it finished. # Otherwise, we create a new Event. # This is to prevent a race-condition of job finishing before # the client can create the Event. - event = job.get('__ready') + event = job.get("__ready") if event is None: - event = job['__ready'] = Event() + event = job["__ready"] = Event() event.set() def _jobs_subscribe(self): @@ -422,9 +457,17 @@ def _jobs_subscribe(self): Subscribe to job updates, calling `_jobs_callback` on every new event. """ self._jobs_watching = True - self.subscribe('core.get_jobs', self._jobs_callback, sync=True) + self.subscribe("core.get_jobs", self._jobs_callback, sync=True) - def call(self, method, *params, background=False, callback=None, job=False, timeout=undefined): + def call( + self, + method, + *params, + background=False, + callback=None, + job=False, + timeout=undefined, + ): if timeout is undefined: timeout = self._call_timeout @@ -435,12 +478,14 @@ def call(self, method, *params, background=False, callback=None, job=False, time c = Call(method, params) self._register_call(c) try: - self._send({ - 'msg': 'method', - 'method': c.method, - 'id': c.id, - 'params': c.params, - }) + self._send( + { + "msg": "method", + "method": c.method, + "id": c.id, + "params": c.params, + } + ) if background: return c @@ -463,13 +508,13 @@ def wait(self, c, *, callback=None, job=False, timeout=undefined): if self._log_py_exceptions: logger.error(c.trace["formatted"]) raise c.py_exception - if c.trace and c.type == 'VALIDATION': + if c.trace and c.type == "VALIDATION": raise ValidationErrors(c.extra) raise ClientException(c.error, c.errno, c.trace, c.extra) if job: jobobj = Job(self, c.result, callback=callback) - if job == 'RETURN': + if job == "RETURN": return jobobj return jobobj.result() @@ -480,39 +525,45 @@ def wait(self, c, *, callback=None, job=False, timeout=undefined): @staticmethod def event_payload(): return { - 'id': str(uuid.uuid4()), - 'callback': None, - 'sync': False, - 'ready': Event(), - 'error': None, - 'event': Event(), + "id": str(uuid.uuid4()), + "callback": None, + "sync": False, + "ready": Event(), + "error": None, + "event": Event(), } def subscribe(self, name, callback, payload=None, sync=False): payload = payload or self.event_payload() - payload.update({ - 'callback': callback, - 'sync': sync, - }) + payload.update( + { + "callback": callback, + "sync": sync, + } + ) self._event_callbacks[name].append(payload) - self._send({ - 'msg': 'sub', - 'id': payload['id'], - 'name': name, - }) - if not payload['ready'].wait(10): - raise ValueError('Did not receive a response to the subscription request') - if payload['error']: - raise ValueError(payload['error']) - return payload['id'] + self._send( + { + "msg": "sub", + "id": payload["id"], + "name": name, + } + ) + if not payload["ready"].wait(10): + raise ValueError("Did not receive a response to the subscription request") + if payload["error"]: + raise ValueError(payload["error"]) + return payload["id"] def unsubscribe(self, id_): - self._send({ - 'msg': 'unsub', - 'id': id_, - }) + self._send( + { + "msg": "unsub", + "id": id_, + } + ) for k, events in list(self._event_callbacks.items()): - events = [v for v in events if v['id'] != id_] + events = [v for v in events if v["id"] != id_] if events: self._event_callbacks[k] = events else: @@ -521,10 +572,12 @@ def unsubscribe(self, id_): def ping(self, timeout=10): _id = str(uuid.uuid4()) event = self._pings[_id] = Event() - self._send({ - 'msg': 'ping', - 'id': _id, - }) + self._send( + { + "msg": "ping", + "id": _id, + } + ) if not event.wait(timeout): return False @@ -562,11 +615,17 @@ def login_with_api_key( ValueError: an error occurred during authentication. """ if auth_mechanism == APIKeyAuthMech.SCRAM: - raise ValueError('Legacy TrueNAS servers do not implement SCRAM authentication') + raise ValueError( + "Legacy TrueNAS servers do not implement SCRAM authentication" + ) - api_key_authenticate(self, auth_mechanism, username, api_key, use_legacy_endpoint=True) + api_key_authenticate( + self, auth_mechanism, username, api_key, use_legacy_endpoint=True + ) - def login_with_password(self, username: str, password: str, *, otp_token: str | None = None) -> None: + def login_with_password( + self, username: str, password: str, *, otp_token: str | None = None + ) -> None: """ Authenticate via username and password. @@ -581,8 +640,8 @@ def login_with_password(self, username: str, password: str, *, otp_token: str | Raises: ValueError: authentication failed """ - if not self.call('auth.login', username, password, otp_token): - raise ValueError('Invalid username or password') + if not self.call("auth.login", username, password, otp_token): + raise ValueError("Invalid username or password") def close(self): self._ws.close() diff --git a/truenas_api_client/py_scram/README.md b/truenas_api_client/py_scram/README.md index 5902dac..484dfa3 100644 --- a/truenas_api_client/py_scram/README.md +++ b/truenas_api_client/py_scram/README.md @@ -76,9 +76,7 @@ client = TNScramClient(raw_key_material="password123") # Or with pre-computed keys client = TNScramClient( - client_key=client_key, - stored_key=stored_key, - server_key=server_key + client_key=client_key, stored_key=stored_key, server_key=server_key ) # Follow SCRAM authentication flow diff --git a/truenas_api_client/py_scram/__init__.py b/truenas_api_client/py_scram/__init__.py index fcef9fb..6186b70 100644 --- a/truenas_api_client/py_scram/__init__.py +++ b/truenas_api_client/py_scram/__init__.py @@ -58,57 +58,49 @@ __all__ = [ # Core types - 'CryptoDatum', - 'ScramAuthData', - + "CryptoDatum", + "ScramAuthData", # Exceptions - 'ScramError', - + "ScramError", # Message classes - 'ClientFirstMessage', - 'ServerFirstMessage', - 'ClientFinalMessage', - 'ServerFinalMessage', - + "ClientFirstMessage", + "ServerFirstMessage", + "ClientFinalMessage", + "ServerFinalMessage", # Verification functions - 'verify_server_signature', - 'verify_client_final_message', - + "verify_server_signature", + "verify_client_final_message", # Channel binding - 'CB_TLS_SERVER_END_POINT', - 'compute_tls_server_end_point', - + "CB_TLS_SERVER_END_POINT", + "compute_tls_server_end_point", # Auth data generation - 'generate_scram_auth_data', - + "generate_scram_auth_data", # Cryptographic functions - 'generate_nonce', - 'scram_hi', - 'scram_h', - 'scram_hmac_sha512', - 'scram_create_client_key', - 'scram_create_server_key', - 'scram_create_stored_key', - 'scram_xor_bytes', - 'scram_constant_time_compare', - 'scram_create_auth_message', - + "generate_nonce", + "scram_hi", + "scram_h", + "scram_hmac_sha512", + "scram_create_client_key", + "scram_create_server_key", + "scram_create_stored_key", + "scram_xor_bytes", + "scram_constant_time_compare", + "scram_create_auth_message", # Error codes - 'SCRAM_E_SUCCESS', - 'SCRAM_E_INVALID_REQUEST', - 'SCRAM_E_MEMORY_ERROR', - 'SCRAM_E_CRYPTO_ERROR', - 'SCRAM_E_BASE64_ERROR', - 'SCRAM_E_PARSE_ERROR', - 'SCRAM_E_FORMAT_ERROR', - 'SCRAM_E_AUTH_FAILED', - 'SCRAM_E_FAULT', - + "SCRAM_E_SUCCESS", + "SCRAM_E_INVALID_REQUEST", + "SCRAM_E_MEMORY_ERROR", + "SCRAM_E_CRYPTO_ERROR", + "SCRAM_E_BASE64_ERROR", + "SCRAM_E_PARSE_ERROR", + "SCRAM_E_FORMAT_ERROR", + "SCRAM_E_AUTH_FAILED", + "SCRAM_E_FAULT", # Constants - 'GS2_SEPARATOR', - 'GS2_NO_CHANNEL_BINDING', - 'SCRAM_MAX_ITERS', - 'SCRAM_MIN_ITERS', - 'SCRAM_DEFAULT_ITERS', - 'SCRAM_NONCE_SIZE', + "GS2_SEPARATOR", + "GS2_NO_CHANNEL_BINDING", + "SCRAM_MAX_ITERS", + "SCRAM_MIN_ITERS", + "SCRAM_DEFAULT_ITERS", + "SCRAM_NONCE_SIZE", ] diff --git a/truenas_api_client/py_scram/channel_binding.py b/truenas_api_client/py_scram/channel_binding.py index 0ff8f5b..f65ad1d 100644 --- a/truenas_api_client/py_scram/channel_binding.py +++ b/truenas_api_client/py_scram/channel_binding.py @@ -20,11 +20,11 @@ from .scram_crypto import CryptoDatum -__all__ = ['CB_TLS_SERVER_END_POINT', 'compute_tls_server_end_point'] +__all__ = ["CB_TLS_SERVER_END_POINT", "compute_tls_server_end_point"] # RFC 5929 channel-binding type name (the "cb-name" in a "p=" gs2 flag). -CB_TLS_SERVER_END_POINT = 'tls-server-end-point' +CB_TLS_SERVER_END_POINT = "tls-server-end-point" # Map a certificate signatureAlgorithm OID to its *raw* signature digest -- before @@ -35,37 +35,37 @@ # EdDSA is intentionally absent (no single well-defined hash -> undefined, rejected). _SIG_OID_TO_HASH = { # RSASSA-PKCS1-v1_5 - '1.2.840.113549.1.1.4': 'md5', # md5WithRSAEncryption -> promoted to sha256 - '1.2.840.113549.1.1.5': 'sha1', # sha1WithRSAEncryption -> promoted to sha256 - '1.2.840.113549.1.1.14': 'sha224', # sha224WithRSAEncryption - '1.2.840.113549.1.1.11': 'sha256', # sha256WithRSAEncryption - '1.2.840.113549.1.1.12': 'sha384', # sha384WithRSAEncryption - '1.2.840.113549.1.1.13': 'sha512', # sha512WithRSAEncryption + "1.2.840.113549.1.1.4": "md5", # md5WithRSAEncryption -> promoted to sha256 + "1.2.840.113549.1.1.5": "sha1", # sha1WithRSAEncryption -> promoted to sha256 + "1.2.840.113549.1.1.14": "sha224", # sha224WithRSAEncryption + "1.2.840.113549.1.1.11": "sha256", # sha256WithRSAEncryption + "1.2.840.113549.1.1.12": "sha384", # sha384WithRSAEncryption + "1.2.840.113549.1.1.13": "sha512", # sha512WithRSAEncryption # ECDSA - '1.2.840.10045.4.1': 'sha1', # ecdsa-with-SHA1 -> promoted to sha256 - '1.2.840.10045.4.3.1': 'sha224', # ecdsa-with-SHA224 - '1.2.840.10045.4.3.2': 'sha256', # ecdsa-with-SHA256 - '1.2.840.10045.4.3.3': 'sha384', # ecdsa-with-SHA384 - '1.2.840.10045.4.3.4': 'sha512', # ecdsa-with-SHA512 + "1.2.840.10045.4.1": "sha1", # ecdsa-with-SHA1 -> promoted to sha256 + "1.2.840.10045.4.3.1": "sha224", # ecdsa-with-SHA224 + "1.2.840.10045.4.3.2": "sha256", # ecdsa-with-SHA256 + "1.2.840.10045.4.3.3": "sha384", # ecdsa-with-SHA384 + "1.2.840.10045.4.3.4": "sha512", # ecdsa-with-SHA512 # DSA (OpenSSL resolves these via OBJ_find_sigid_algs; SHA-384/512 DSA have no # OpenSSL sigid and are therefore omitted so we reject them exactly as C does). - '1.2.840.10040.4.3': 'sha1', # id-dsa-with-sha1 -> promoted to sha256 - '2.16.840.1.101.3.4.3.1': 'sha224', # id-dsa-with-sha224 - '2.16.840.1.101.3.4.3.2': 'sha256', # id-dsa-with-sha256 + "1.2.840.10040.4.3": "sha1", # id-dsa-with-sha1 -> promoted to sha256 + "2.16.840.1.101.3.4.3.1": "sha224", # id-dsa-with-sha224 + "2.16.840.1.101.3.4.3.2": "sha256", # id-dsa-with-sha256 } # RSASSA-PSS encodes its message digest in the signatureAlgorithm parameters # (RSASSA-PSS-params), not in the OID, so it is resolved separately. -_RSASSA_PSS_OID = '1.2.840.113549.1.1.10' +_RSASSA_PSS_OID = "1.2.840.113549.1.1.10" # Digest OIDs that can appear in an RSASSA-PSS hashAlgorithm field. _HASH_OID_TO_NAME = { - '1.2.840.113549.2.5': 'md5', - '1.3.14.3.2.26': 'sha1', - '2.16.840.1.101.3.4.2.4': 'sha224', - '2.16.840.1.101.3.4.2.1': 'sha256', - '2.16.840.1.101.3.4.2.2': 'sha384', - '2.16.840.1.101.3.4.2.3': 'sha512', + "1.2.840.113549.2.5": "md5", + "1.3.14.3.2.26": "sha1", + "2.16.840.1.101.3.4.2.4": "sha224", + "2.16.840.1.101.3.4.2.1": "sha256", + "2.16.840.1.101.3.4.2.2": "sha384", + "2.16.840.1.101.3.4.2.3": "sha512", } @@ -78,7 +78,7 @@ def _read_tlv(buf: bytes, offset: int) -> tuple[int, int, int]: input, since the certificate comes off the wire. """ if offset + 2 > len(buf): - raise ValueError('truncated DER (TLV header)') + raise ValueError("truncated DER (TLV header)") tag = buf[offset] ptr = offset + 1 length_byte = buf[ptr] @@ -86,13 +86,13 @@ def _read_tlv(buf: bytes, offset: int) -> tuple[int, int, int]: if length_byte < 0x80: length = length_byte else: - num = length_byte & 0x7f + num = length_byte & 0x7F if ptr + num > len(buf): - raise ValueError('truncated DER (length)') - length = int.from_bytes(buf[ptr:ptr + num], 'big') + raise ValueError("truncated DER (length)") + length = int.from_bytes(buf[ptr : ptr + num], "big") ptr += num if ptr + length > len(buf): - raise ValueError('truncated DER (value)') + raise ValueError("truncated DER (value)") return tag, ptr, ptr + length @@ -101,11 +101,11 @@ def _decode_oid(data: bytes) -> str: parts = [str(first // 40), str(first % 40)] value = 0 for byte in data[1:]: - value = (value << 7) | (byte & 0x7f) + value = (value << 7) | (byte & 0x7F) if not byte & 0x80: parts.append(str(value)) value = 0 - return '.'.join(parts) + return ".".join(parts) def _signature_algorithm(cert_der: bytes) -> tuple[str, bytes]: @@ -118,21 +118,21 @@ def _signature_algorithm(cert_der: bytes) -> tuple[str, bytes]: # Certificate ::= SEQUENCE { tbsCertificate SEQUENCE, signatureAlgorithm SEQUENCE, ... } tag, cert_start, _ = _read_tlv(cert_der, 0) if tag != 0x30: - raise ValueError('not a DER certificate') + raise ValueError("not a DER certificate") # Skip tbsCertificate. tag, _, after_tbs = _read_tlv(cert_der, cert_start) if tag != 0x30: - raise ValueError('malformed certificate (tbsCertificate)') + raise ValueError("malformed certificate (tbsCertificate)") # signatureAlgorithm ::= SEQUENCE { algorithm OBJECT IDENTIFIER, parameters ANY } tag, sig_alg_start, sig_alg_end = _read_tlv(cert_der, after_tbs) if tag != 0x30: - raise ValueError('malformed certificate (signatureAlgorithm)') + raise ValueError("malformed certificate (signatureAlgorithm)") tag, oid_start, oid_end = _read_tlv(cert_der, sig_alg_start) if tag != 0x06: - raise ValueError('malformed certificate (algorithm OID)') + raise ValueError("malformed certificate (algorithm OID)") oid = _decode_oid(cert_der[oid_start:oid_end]) parameters = cert_der[oid_end:sig_alg_end] @@ -150,31 +150,31 @@ def _rsassa_pss_hash(parameters: bytes) -> str: SHA-256). Raises ValueError for an unrecognized digest, as the C library does. """ if not parameters: - return 'sha1' # all params defaulted + return "sha1" # all params defaulted tag, seq_start, seq_end = _read_tlv(parameters, 0) if tag != 0x30: - raise ValueError('malformed RSASSA-PSS parameters') + raise ValueError("malformed RSASSA-PSS parameters") if seq_start >= seq_end: - return 'sha1' # empty SEQUENCE -> all defaults + return "sha1" # empty SEQUENCE -> all defaults # First element, if present, is the [0] EXPLICIT hashAlgorithm. tag, h_start, h_end = _read_tlv(parameters, seq_start) if tag != 0xA0: - return 'sha1' # hashAlgorithm omitted -> default SHA-1 + return "sha1" # hashAlgorithm omitted -> default SHA-1 tag, alg_start, _ = _read_tlv(parameters, h_start) if tag != 0x30: - raise ValueError('malformed RSASSA-PSS hashAlgorithm') + raise ValueError("malformed RSASSA-PSS hashAlgorithm") tag, oid_start, oid_end = _read_tlv(parameters, alg_start) if tag != 0x06: - raise ValueError('malformed RSASSA-PSS hashAlgorithm OID') + raise ValueError("malformed RSASSA-PSS hashAlgorithm OID") oid = _decode_oid(parameters[oid_start:oid_end]) name = _HASH_OID_TO_NAME.get(oid) if name is None: - raise ValueError(f'tls-server-end-point: unsupported RSASSA-PSS hash {oid}') + raise ValueError(f"tls-server-end-point: unsupported RSASSA-PSS hash {oid}") return name @@ -196,11 +196,13 @@ def compute_tls_server_end_point(cert_der: bytes) -> CryptoDatum: else: hash_name = _SIG_OID_TO_HASH.get(oid) if hash_name is None: - raise ValueError(f'tls-server-end-point is undefined for signature algorithm {oid}') + raise ValueError( + f"tls-server-end-point is undefined for signature algorithm {oid}" + ) # RFC 5929 4.1: MD5 and SHA-1 signature hashes are promoted to SHA-256. - if hash_name in ('md5', 'sha1'): - hash_name = 'sha256' + if hash_name in ("md5", "sha1"): + hash_name = "sha256" digest = hashlib.new(hash_name, cert_der).digest() return CryptoDatum(digest) diff --git a/truenas_api_client/py_scram/client_final.py b/truenas_api_client/py_scram/client_final.py index 715ff95..3fa8cc2 100644 --- a/truenas_api_client/py_scram/client_final.py +++ b/truenas_api_client/py_scram/client_final.py @@ -3,13 +3,19 @@ from base64 import b64encode, b64decode -from .scram_crypto import CryptoDatum, scram_hmac_sha512, scram_xor_bytes, SCRAM_NONCE_SIZE, SHA512_DIGEST_SIZE +from .scram_crypto import ( + CryptoDatum, + scram_hmac_sha512, + scram_xor_bytes, + SCRAM_NONCE_SIZE, + SHA512_DIGEST_SIZE, +) from .common import GS2_SEPARATOR from .client_first import ClientFirstMessage from .server_first import ServerFirstMessage -__all__ = ['ClientFinalMessage'] +__all__ = ["ClientFinalMessage"] class ClientFinalMessage: @@ -23,7 +29,7 @@ def __compute_auth_message( self, client_first: ClientFirstMessage, server_first: ServerFirstMessage, - channel_binding_b64: str + channel_binding_b64: str, ) -> str: """Compute the auth message as per RFC 5802. @@ -44,15 +50,12 @@ def __compute_auth_message( # Create client-final-message-without-proof nonce_b64 = b64encode(self.__nonce).decode() - client_final_no_proof = f'c={channel_binding_b64},r={nonce_b64}' + client_final_no_proof = f"c={channel_binding_b64},r={nonce_b64}" - return f'{client_first_bare},{server_first_str},{client_final_no_proof}' + return f"{client_first_bare},{server_first_str},{client_final_no_proof}" def __compute_client_proof( - self, - client_key: CryptoDatum, - stored_key: CryptoDatum, - auth_message: str + self, client_key: CryptoDatum, stored_key: CryptoDatum, auth_message: str ) -> CryptoDatum: """Compute the client proof as per RFC 5802. @@ -66,10 +69,12 @@ def __compute_client_proof( def __generate_rfc_string(self, channel_binding_b64: str) -> str: """Generate RFC 5802 formatted client final message.""" - assert self.__client_proof is not None, "client_proof must be set before generating RFC string" + assert self.__client_proof is not None, ( + "client_proof must be set before generating RFC string" + ) nonce_b64 = b64encode(self.__nonce).decode() client_proof_b64 = b64encode(self.__client_proof).decode() - return f'c={channel_binding_b64},r={nonce_b64},p={client_proof_b64}' + return f"c={channel_binding_b64},r={nonce_b64},p={client_proof_b64}" def __init__( self, @@ -83,10 +88,10 @@ def __init__( ): # Check for mutually exclusive parameters if rfc_string and client_first: - raise ValueError('Cannot specify both rfc_string and other parameters') + raise ValueError("Cannot specify both rfc_string and other parameters") if not rfc_string and not client_first: - raise ValueError('Must specify either rfc_string or message parameters') + raise ValueError("Must specify either rfc_string or message parameters") if rfc_string: # Parse from RFC string @@ -94,19 +99,23 @@ def __init__( else: # Create new message from parameters if not isinstance(client_first, ClientFirstMessage): - raise TypeError('client_first must be a ClientFirstMessage instance') + raise TypeError("client_first must be a ClientFirstMessage instance") if not isinstance(server_first, ServerFirstMessage): - raise TypeError('server_first must be a ServerFirstMessage instance') + raise TypeError("server_first must be a ServerFirstMessage instance") if not isinstance(client_key, CryptoDatum): - raise TypeError('client_key must be a CryptoDatum instance') + raise TypeError("client_key must be a CryptoDatum instance") if not isinstance(stored_key, CryptoDatum): - raise TypeError('stored_key must be a CryptoDatum instance') + raise TypeError("stored_key must be a CryptoDatum instance") - if channel_binding is not None and not isinstance(channel_binding, CryptoDatum): - raise TypeError('channel_binding must be a CryptoDatum instance or None') + if channel_binding is not None and not isinstance( + channel_binding, CryptoDatum + ): + raise TypeError( + "channel_binding must be a CryptoDatum instance or None" + ) # Use the nonce from server_first (combined client+server nonce) self.__nonce = server_first.nonce @@ -116,25 +125,35 @@ def __init__( # Validate the gs2 flag / channel-binding combination (RFC 5802 6), matching the C # library's check_gs2_cb_consistency: a 'p' flag requires binding data, and any other # flag ('n'/'y', or no header) must not carry any. - gs2_flag = (client_first.gs2_header or 'n')[0] + gs2_flag = (client_first.gs2_header or "n")[0] have_cb = bool(channel_binding) - if gs2_flag == 'p' and not have_cb: - raise ValueError("gs2 channel-binding flag 'p' requires channel binding data") - if gs2_flag != 'p' and have_cb: - raise ValueError("channel binding data provided but gs2 flag is not 'p'") + if gs2_flag == "p" and not have_cb: + raise ValueError( + "gs2 channel-binding flag 'p' requires channel binding data" + ) + if gs2_flag != "p" and have_cb: + raise ValueError( + "channel binding data provided but gs2 flag is not 'p'" + ) # c= is base64(cbind-input) where cbind-input = gs2-header + ",," + [cbind-data] # (RFC 5802 7); the gs2-header defaults to "n". Mirrors the C library's # scram_serialize_client_final_message so both backends emit the same c= value. - gs2_header_str = (client_first.gs2_header or 'n') + GS2_SEPARATOR - cb_data = gs2_header_str.encode() + (bytes(channel_binding) if channel_binding else b'') + gs2_header_str = (client_first.gs2_header or "n") + GS2_SEPARATOR + cb_data = gs2_header_str.encode() + ( + bytes(channel_binding) if channel_binding else b"" + ) channel_binding_b64 = b64encode(cb_data).decode() # Compute auth message - auth_message = self.__compute_auth_message(client_first, server_first, channel_binding_b64) + auth_message = self.__compute_auth_message( + client_first, server_first, channel_binding_b64 + ) # Compute client proof - self.__client_proof = self.__compute_client_proof(client_key, stored_key, auth_message) + self.__client_proof = self.__compute_client_proof( + client_key, stored_key, auth_message + ) # Generate RFC string self.__rfc_str = self.__generate_rfc_string(channel_binding_b64) @@ -144,9 +163,9 @@ def __parse_rfc_string(self, rfc_string: str): Format: c=,r=,p= """ - parts = rfc_string.split(',') + parts = rfc_string.split(",") if len(parts) < 2: - raise ValueError('Invalid client final message format') + raise ValueError("Invalid client final message format") channel_binding_b64 = None nonce_b64 = None @@ -154,30 +173,30 @@ def __parse_rfc_string(self, rfc_string: str): for part in parts: if not part: - raise ValueError('Invalid client final message format') + raise ValueError("Invalid client final message format") - key, _, value = part.partition('=') + key, _, value = part.partition("=") if not value: - raise ValueError('Invalid client final message format') + raise ValueError("Invalid client final message format") - if key == 'c': + if key == "c": channel_binding_b64 = value - elif key == 'r': + elif key == "r": nonce_b64 = value - elif key == 'p': + elif key == "p": client_proof_b64 = value else: - raise ValueError(f'Unknown key in client final message: {key}') + raise ValueError(f"Unknown key in client final message: {key}") if channel_binding_b64 is None or nonce_b64 is None: - raise ValueError('Missing required fields in client final message') + raise ValueError("Missing required fields in client final message") try: nonce_bytes = b64decode(nonce_b64) channel_binding_bytes: bytes | None = b64decode(channel_binding_b64) # Check if channel binding is GS2_NO_CHANNEL_BINDING ("biws" = base64("n,,")) - if channel_binding_bytes == b'n,,': + if channel_binding_bytes == b"n,,": channel_binding_bytes = None # Client proof is optional when parsing (might be parsing without-proof variant) @@ -187,18 +206,25 @@ def __parse_rfc_string(self, rfc_string: str): else: client_proof_bytes = None except Exception as e: - raise ValueError(f'Invalid base64 encoding in client final message: {e}') + raise ValueError(f"Invalid base64 encoding in client final message: {e}") # Match the C parser (scram_attr_parse): the nonce is a 32- or 64-byte value and the # client proof, when present, is a SHA-512 digest. if len(nonce_bytes) not in (SCRAM_NONCE_SIZE, SCRAM_NONCE_SIZE * 2): - raise ValueError(f'{len(nonce_bytes)}: unexpected nonce size') - if client_proof_bytes is not None and len(client_proof_bytes) != SHA512_DIGEST_SIZE: - raise ValueError(f'{len(client_proof_bytes)}: unexpected client proof size') + raise ValueError(f"{len(nonce_bytes)}: unexpected nonce size") + if ( + client_proof_bytes is not None + and len(client_proof_bytes) != SHA512_DIGEST_SIZE + ): + raise ValueError(f"{len(client_proof_bytes)}: unexpected client proof size") self.__nonce = CryptoDatum(nonce_bytes) - self.__channel_binding = CryptoDatum(channel_binding_bytes) if channel_binding_bytes else None - self.__client_proof = CryptoDatum(client_proof_bytes) if client_proof_bytes else None + self.__channel_binding = ( + CryptoDatum(channel_binding_bytes) if channel_binding_bytes else None + ) + self.__client_proof = ( + CryptoDatum(client_proof_bytes) if client_proof_bytes else None + ) self.__gs2_header = None self.__rfc_str = rfc_string diff --git a/truenas_api_client/py_scram/client_first.py b/truenas_api_client/py_scram/client_first.py index 7b6db4f..22ec036 100644 --- a/truenas_api_client/py_scram/client_first.py +++ b/truenas_api_client/py_scram/client_first.py @@ -7,23 +7,23 @@ from .common import GS2_SEPARATOR, saslprep -__all__ = ['ClientFirstMessage'] +__all__ = ["ClientFirstMessage"] class ClientFirstMessage: - __rfc_str = '' + __rfc_str = "" def __generate_rfc_string(self): nonce_b64 = b64encode(self.nonce).decode() name = self.username - hdr = self.gs2_header or 'n' + hdr = self.gs2_header or "n" # an API key id of zero means that we're doing scram auth with actual user credential if self.api_key_id != 0: # API key auth - name += f':{self.api_key_id}' + name += f":{self.api_key_id}" - return f'{hdr}{GS2_SEPARATOR}n={name},r={nonce_b64}' + return f"{hdr}{GS2_SEPARATOR}n={name},r={nonce_b64}" def __init__( self, @@ -34,35 +34,37 @@ def __init__( channel_binding_type: str | None = None, ): if not username: - raise ValueError('Must specify username') + raise ValueError("Must specify username") if not isinstance(username, str): - raise TypeError('Username must be string') + raise TypeError("Username must be string") # SCRAM messages are comma-separated and use '=' for the "=2C"/"=3D" username escapes # (RFC 5802 5.1) that this library does not emit. Reject both characters up front, # matching the C library's scram_create_client_first_message(). - if ',' in username or '=' in username: + if "," in username or "=" in username: raise ValueError("username must not contain ',' or '='") if not isinstance(api_key_id, int): - raise TypeError('API key ID must be an integer') + raise TypeError("API key ID must be an integer") if gs2_header and not isinstance(gs2_header, str): - raise TypeError('GS2 header must be string if provided') + raise TypeError("GS2 header must be string if provided") if channel_binding_type is not None: if not isinstance(channel_binding_type, str): - raise TypeError('channel_binding_type must be string if provided') + raise TypeError("channel_binding_type must be string if provided") if not channel_binding_type: # A "p=" gs2 cbind flag requires a non-empty cb-name; an empty string # would emit a malformed "p=,," header. Reject it, matching the C # library's scram_build_gs2_header() (rejects empty cb_name). - raise ValueError('channel_binding_type must not be empty if provided') + raise ValueError("channel_binding_type must not be empty if provided") if gs2_header is not None: - raise ValueError('Cannot specify both gs2_header and channel_binding_type') + raise ValueError( + "Cannot specify both gs2_header and channel_binding_type" + ) # Client requires channel binding: the gs2 cbind flag becomes "p=". - gs2_header = f'p={channel_binding_type}' + gs2_header = f"p={channel_binding_type}" self.__nonce = generate_nonce() # RFC 5802, Section 5.1: "Before sending the username to the server, the client SHOULD diff --git a/truenas_api_client/py_scram/common.py b/truenas_api_client/py_scram/common.py index 6a6fa7e..c121c31 100644 --- a/truenas_api_client/py_scram/common.py +++ b/truenas_api_client/py_scram/common.py @@ -9,12 +9,18 @@ from .scram_crypto import CryptoDatum -__all__ = ['GS2_SEPARATOR', 'GS2_NO_CHANNEL_BINDING', 'ScramAuthData', 'CryptoDatum', 'saslprep'] +__all__ = [ + "GS2_SEPARATOR", + "GS2_NO_CHANNEL_BINDING", + "ScramAuthData", + "CryptoDatum", + "saslprep", +] # Constants -GS2_SEPARATOR = ',,' -GS2_NO_CHANNEL_BINDING = 'biws' # base64 of "n,," +GS2_SEPARATOR = ",," +GS2_NO_CHANNEL_BINDING = "biws" # base64 of "n,," def saslprep(input_str: str) -> str: @@ -44,7 +50,7 @@ def saslprep(input_str: str) -> str: RFC 3454 - Preparation of Internationalized Strings ("stringprep") """ if not isinstance(input_str, str): - raise TypeError('input_str must be a string') + raise TypeError("input_str must be a string") if not input_str: return input_str @@ -52,11 +58,11 @@ def saslprep(input_str: str) -> str: # RFC 4013, Section 2.1: Mapping # Map using RFC 3454, Table B.1 (commonly mapped to nothing) # Non-ASCII space characters are removed - mapped = ''.join(c for c in input_str if not stringprep.in_table_b1(c)) + mapped = "".join(c for c in input_str if not stringprep.in_table_b1(c)) # RFC 4013, Section 2.2: Normalization # Normalize using Unicode normalization form KC (RFC 3454, Section 4) - normalized = unicodedata.normalize('NFKC', mapped) + normalized = unicodedata.normalize("NFKC", mapped) # RFC 4013, Section 2.3: Prohibited Output # Check for prohibited characters as defined in RFC 3454 @@ -64,64 +70,64 @@ def saslprep(input_str: str) -> str: # RFC 3454, Table C.1.2: Non-ASCII space characters if stringprep.in_table_c12(c): raise ValueError( - f'Character at position {i} is prohibited (RFC 3454, C.1.2: Non-ASCII space)' + f"Character at position {i} is prohibited (RFC 3454, C.1.2: Non-ASCII space)" ) # RFC 3454, Table C.2.1: ASCII control characters if stringprep.in_table_c21(c): raise ValueError( - f'Character at position {i} is prohibited (RFC 3454, C.2.1: ASCII control)' + f"Character at position {i} is prohibited (RFC 3454, C.2.1: ASCII control)" ) # RFC 3454, Table C.2.2: Non-ASCII control characters if stringprep.in_table_c22(c): raise ValueError( - f'Character at position {i} is prohibited (RFC 3454, C.2.2: Non-ASCII control)' + f"Character at position {i} is prohibited (RFC 3454, C.2.2: Non-ASCII control)" ) # RFC 3454, Table C.3: Private use characters if stringprep.in_table_c3(c): raise ValueError( - f'Character at position {i} is prohibited (RFC 3454, C.3: Private use)' + f"Character at position {i} is prohibited (RFC 3454, C.3: Private use)" ) # RFC 3454, Table C.4: Non-character code points if stringprep.in_table_c4(c): raise ValueError( - f'Character at position {i} is prohibited (RFC 3454, C.4: Non-character)' + f"Character at position {i} is prohibited (RFC 3454, C.4: Non-character)" ) # RFC 3454, Table C.5: Surrogate codes if stringprep.in_table_c5(c): raise ValueError( - f'Character at position {i} is prohibited (RFC 3454, C.5: Surrogate)' + f"Character at position {i} is prohibited (RFC 3454, C.5: Surrogate)" ) # RFC 3454, Table C.6: Inappropriate for plain text if stringprep.in_table_c6(c): raise ValueError( - f'Character at position {i} is prohibited ' - f'(RFC 3454, C.6: Inappropriate for plain text)' + f"Character at position {i} is prohibited " + f"(RFC 3454, C.6: Inappropriate for plain text)" ) # RFC 3454, Table C.7: Inappropriate for canonical representation if stringprep.in_table_c7(c): raise ValueError( - f'Character at position {i} is prohibited ' - f'(RFC 3454, C.7: Inappropriate for canonical representation)' + f"Character at position {i} is prohibited " + f"(RFC 3454, C.7: Inappropriate for canonical representation)" ) # RFC 3454, Table C.8: Change display properties or deprecated if stringprep.in_table_c8(c): raise ValueError( - f'Character at position {i} is prohibited ' - f'(RFC 3454, C.8: Change display properties)' + f"Character at position {i} is prohibited " + f"(RFC 3454, C.8: Change display properties)" ) # RFC 3454, Table C.9: Tagging characters if stringprep.in_table_c9(c): raise ValueError( - f'Character at position {i} is prohibited (RFC 3454, C.9: Tagging character)' + f"Character at position {i} is prohibited (RFC 3454, C.9: Tagging character)" ) # RFC 3454, Table A.1: Unassigned code points are allowed per RFC 5802, Section 5.1 @@ -139,7 +145,7 @@ def saslprep(input_str: str) -> str: # the string MUST NOT contain any LCat character if has_LCat: raise ValueError( - 'String contains both RandALCat and LCat characters (RFC 3454, Section 6)' + "String contains both RandALCat and LCat characters (RFC 3454, Section 6)" ) # RFC 3454, Section 6, Rule 3: If a string contains any RandALCat character, @@ -147,13 +153,13 @@ def saslprep(input_str: str) -> str: # a RandALCat character MUST be the last character of the string if not stringprep.in_table_d1(normalized[0]): raise ValueError( - 'First character must be RandALCat when string contains RandALCat ' - '(RFC 3454, Section 6)' + "First character must be RandALCat when string contains RandALCat " + "(RFC 3454, Section 6)" ) if not stringprep.in_table_d1(normalized[-1]): raise ValueError( - 'Last character must be RandALCat when string contains RandALCat ' - '(RFC 3454, Section 6)' + "Last character must be RandALCat when string contains RandALCat " + "(RFC 3454, Section 6)" ) return normalized @@ -163,9 +169,12 @@ def saslprep(input_str: str) -> str: class ScramAuthData: """Client-side authentication information dataclass that is used for generating client requests and to validate server responses""" + salt: CryptoDatum iterations: int - salted_password: CryptoDatum | None # technically only ClientKey is required for client protocol + salted_password: ( + CryptoDatum | None + ) # technically only ClientKey is required for client protocol client_key: CryptoDatum stored_key: CryptoDatum | None server_key: CryptoDatum | None diff --git a/truenas_api_client/py_scram/error.py b/truenas_api_client/py_scram/error.py index b092849..fbd9c35 100644 --- a/truenas_api_client/py_scram/error.py +++ b/truenas_api_client/py_scram/error.py @@ -55,14 +55,14 @@ def __repr__(self): __all__ = [ - 'ScramError', - 'SCRAM_E_SUCCESS', - 'SCRAM_E_INVALID_REQUEST', - 'SCRAM_E_MEMORY_ERROR', - 'SCRAM_E_CRYPTO_ERROR', - 'SCRAM_E_BASE64_ERROR', - 'SCRAM_E_PARSE_ERROR', - 'SCRAM_E_FORMAT_ERROR', - 'SCRAM_E_AUTH_FAILED', - 'SCRAM_E_FAULT', + "ScramError", + "SCRAM_E_SUCCESS", + "SCRAM_E_INVALID_REQUEST", + "SCRAM_E_MEMORY_ERROR", + "SCRAM_E_CRYPTO_ERROR", + "SCRAM_E_BASE64_ERROR", + "SCRAM_E_PARSE_ERROR", + "SCRAM_E_FORMAT_ERROR", + "SCRAM_E_AUTH_FAILED", + "SCRAM_E_FAULT", ] diff --git a/truenas_api_client/py_scram/scram_crypto.py b/truenas_api_client/py_scram/scram_crypto.py index 414b87a..3921851 100644 --- a/truenas_api_client/py_scram/scram_crypto.py +++ b/truenas_api_client/py_scram/scram_crypto.py @@ -9,24 +9,24 @@ __all__ = [ - 'CryptoDatum', - 'generate_nonce', - 'scram_hi', - 'scram_h', - 'scram_hmac_sha512', - 'scram_create_client_key', - 'scram_create_server_key', - 'scram_create_stored_key', - 'scram_xor_bytes', - 'scram_constant_time_compare', - 'scram_create_auth_message', - 'generate_scram_auth_data', - 'SCRAM_MAX_ITERS', - 'SCRAM_MIN_ITERS', - 'SCRAM_DEFAULT_ITERS', - 'SCRAM_NONCE_SIZE', - 'SCRAM_MAX_SALT_SZ', - 'SHA512_DIGEST_SIZE', + "CryptoDatum", + "generate_nonce", + "scram_hi", + "scram_h", + "scram_hmac_sha512", + "scram_create_client_key", + "scram_create_server_key", + "scram_create_stored_key", + "scram_xor_bytes", + "scram_constant_time_compare", + "scram_create_auth_message", + "generate_scram_auth_data", + "SCRAM_MAX_ITERS", + "SCRAM_MIN_ITERS", + "SCRAM_DEFAULT_ITERS", + "SCRAM_NONCE_SIZE", + "SCRAM_MAX_SALT_SZ", + "SHA512_DIGEST_SIZE", ] @@ -35,18 +35,23 @@ SCRAM_MIN_ITERS = 50000 SCRAM_DEFAULT_ITERS = 500000 SCRAM_NONCE_SIZE = 32 -SCRAM_MAX_SALT_SZ = 1024 # salt length cap enforced by the C parser (scram_attr_parse.c) -SHA512_DIGEST_SIZE = 64 # SHA-512 output length; SCRAM proofs/signatures/keys are this size +SCRAM_MAX_SALT_SZ = ( + 1024 # salt length cap enforced by the C parser (scram_attr_parse.c) +) +SHA512_DIGEST_SIZE = ( + 64 # SHA-512 output length; SCRAM proofs/signatures/keys are this size +) class CryptoDatum(bytes): """Wrapper around bytes object to provide consistency with CryptoDatum type in the cpython extension.""" + def __new__(cls, value): return super().__new__(cls, value) def __repr__(self): - return f'CryptoDatum({hex(id(self))})' + return f"CryptoDatum({hex(id(self))})" def generate_nonce() -> CryptoDatum: @@ -81,19 +86,21 @@ def scram_hi(key: CryptoDatum, salt: CryptoDatum, iterations: int) -> CryptoDatu ValueError: If parameters are invalid or iterations out of range """ if not isinstance(key, (CryptoDatum, bytes)) or len(key) == 0: - raise ValueError('Invalid key parameter') + raise ValueError("Invalid key parameter") if not isinstance(salt, (CryptoDatum, bytes)) or len(salt) == 0: - raise ValueError('Invalid salt parameter') + raise ValueError("Invalid salt parameter") if not isinstance(iterations, int): - raise TypeError('Iterations must be an integer') + raise TypeError("Iterations must be an integer") if iterations < SCRAM_MIN_ITERS or iterations > SCRAM_MAX_ITERS: - raise ValueError(f'Iterations must be between {SCRAM_MIN_ITERS} and {SCRAM_MAX_ITERS}') + raise ValueError( + f"Iterations must be between {SCRAM_MIN_ITERS} and {SCRAM_MAX_ITERS}" + ) # PBKDF2-HMAC-SHA512 - derived_key = hashlib.pbkdf2_hmac('sha512', bytes(key), bytes(salt), iterations) + derived_key = hashlib.pbkdf2_hmac("sha512", bytes(key), bytes(salt), iterations) return CryptoDatum(derived_key) @@ -115,7 +122,7 @@ def scram_h(data: CryptoDatum) -> CryptoDatum: ValueError: If data is invalid """ if not isinstance(data, (CryptoDatum, bytes)) or len(data) == 0: - raise ValueError('Invalid data parameter') + raise ValueError("Invalid data parameter") digest = hashlib.sha512(bytes(data)).digest() return CryptoDatum(digest) @@ -140,10 +147,10 @@ def scram_hmac_sha512(key: CryptoDatum, data: CryptoDatum) -> CryptoDatum: ValueError: If parameters are invalid """ if not isinstance(key, (CryptoDatum, bytes)) or len(key) == 0: - raise ValueError('Invalid key parameter') + raise ValueError("Invalid key parameter") if not isinstance(data, (CryptoDatum, bytes)) or len(data) == 0: - raise ValueError('Invalid data parameter') + raise ValueError("Invalid data parameter") result = hmac.digest(bytes(key), bytes(data), hashlib.sha512) return CryptoDatum(result) @@ -166,7 +173,7 @@ def scram_create_client_key(salted_password: CryptoDatum) -> CryptoDatum: Raises: ValueError: If salted_password is invalid """ - client_key_string = CryptoDatum(b'Client Key') + client_key_string = CryptoDatum(b"Client Key") return scram_hmac_sha512(salted_password, client_key_string) @@ -187,7 +194,7 @@ def scram_create_server_key(salted_password: CryptoDatum) -> CryptoDatum: Raises: ValueError: If salted_password is invalid """ - server_key_string = CryptoDatum(b'Server Key') + server_key_string = CryptoDatum(b"Server Key") return scram_hmac_sha512(salted_password, server_key_string) @@ -230,13 +237,13 @@ def scram_xor_bytes(a: CryptoDatum, b: CryptoDatum) -> CryptoDatum: ValueError: If parameters are invalid or sizes don't match """ if not isinstance(a, (CryptoDatum, bytes)) or len(a) == 0: - raise ValueError('Invalid first parameter') + raise ValueError("Invalid first parameter") if not isinstance(b, (CryptoDatum, bytes)) or len(b) == 0: - raise ValueError('Invalid second parameter') + raise ValueError("Invalid second parameter") if len(a) != len(b): - raise ValueError('Byte array sizes do not match') + raise ValueError("Byte array sizes do not match") result = bytes(x ^ y for x, y in zip(bytes(a), bytes(b))) return CryptoDatum(result) @@ -261,19 +268,17 @@ def scram_constant_time_compare(a: CryptoDatum, b: CryptoDatum) -> bool: ValueError: If parameters are invalid """ if not isinstance(a, (CryptoDatum, bytes)) or len(a) == 0: - raise ValueError('Invalid first parameter') + raise ValueError("Invalid first parameter") if not isinstance(b, (CryptoDatum, bytes)) or len(b) == 0: - raise ValueError('Invalid second parameter') + raise ValueError("Invalid second parameter") # hmac.compare_digest handles size mismatches gracefully and securely return hmac.compare_digest(bytes(a), bytes(b)) def scram_create_auth_message( - client_first_bare: str, - server_first_msg: str, - client_final_without_proof: str + client_first_bare: str, server_first_msg: str, client_final_without_proof: str ) -> str: """ Create SCRAM authentication message as specified in RFC 5802. @@ -296,15 +301,18 @@ def scram_create_auth_message( ValueError: If any parameter is invalid """ if not isinstance(client_first_bare, str) or not client_first_bare: - raise ValueError('Invalid client_first_bare parameter') + raise ValueError("Invalid client_first_bare parameter") if not isinstance(server_first_msg, str) or not server_first_msg: - raise ValueError('Invalid server_first_msg parameter') + raise ValueError("Invalid server_first_msg parameter") - if not isinstance(client_final_without_proof, str) or not client_final_without_proof: - raise ValueError('Invalid client_final_without_proof parameter') + if ( + not isinstance(client_final_without_proof, str) + or not client_final_without_proof + ): + raise ValueError("Invalid client_final_without_proof parameter") - return f'{client_first_bare},{server_first_msg},{client_final_without_proof}' + return f"{client_first_bare},{server_first_msg},{client_final_without_proof}" def generate_scram_auth_data( @@ -335,16 +343,18 @@ def generate_scram_auth_data( from .common import ScramAuthData if not isinstance(salted_password, CryptoDatum): - raise TypeError('salted_password must be a CryptoDatum instance') + raise TypeError("salted_password must be a CryptoDatum instance") if not isinstance(salt, CryptoDatum): - raise TypeError('salt must be a CryptoDatum instance') + raise TypeError("salt must be a CryptoDatum instance") if not isinstance(iterations, int): - raise TypeError('iterations must be an integer') + raise TypeError("iterations must be an integer") if iterations < SCRAM_MIN_ITERS or iterations > SCRAM_MAX_ITERS: - raise ValueError(f'iterations must be between {SCRAM_MIN_ITERS} and {SCRAM_MAX_ITERS}') + raise ValueError( + f"iterations must be between {SCRAM_MIN_ITERS} and {SCRAM_MAX_ITERS}" + ) # Generate client key and stored key client_key = scram_create_client_key(salted_password) diff --git a/truenas_api_client/py_scram/server_final.py b/truenas_api_client/py_scram/server_final.py index 1140849..0e463f0 100644 --- a/truenas_api_client/py_scram/server_final.py +++ b/truenas_api_client/py_scram/server_final.py @@ -3,24 +3,30 @@ from base64 import b64encode, b64decode -from .scram_crypto import CryptoDatum, scram_hmac_sha512, scram_xor_bytes, scram_h, scram_constant_time_compare +from .scram_crypto import ( + CryptoDatum, + scram_hmac_sha512, + scram_xor_bytes, + scram_h, + scram_constant_time_compare, +) from .common import GS2_SEPARATOR, GS2_NO_CHANNEL_BINDING from .client_first import ClientFirstMessage from .server_first import ServerFirstMessage from .client_final import ClientFinalMessage -__all__ = ['ServerFinalMessage'] +__all__ = ["ServerFinalMessage"] class ServerFinalMessage: - __rfc_str = '' + __rfc_str = "" def __compute_auth_message( self, client_first: ClientFirstMessage, server_first: ServerFirstMessage, - client_final: ClientFinalMessage + client_final: ClientFinalMessage, ) -> str: """Compute the auth message as per RFC 5802. @@ -41,22 +47,22 @@ def __compute_auth_message( # Get client-final-message-without-proof # Reconstruct the channel binding part if client_final.channel_binding: - gs2_header_str = client_first.gs2_header or 'n,,' + gs2_header_str = client_first.gs2_header or "n,," cb_data = gs2_header_str.encode() + bytes(client_final.channel_binding) channel_binding_b64 = b64encode(cb_data).decode() else: channel_binding_b64 = GS2_NO_CHANNEL_BINDING nonce_b64 = b64encode(client_final.nonce).decode() - client_final_no_proof = f'c={channel_binding_b64},r={nonce_b64}' + client_final_no_proof = f"c={channel_binding_b64},r={nonce_b64}" - return f'{client_first_bare},{server_first_str},{client_final_no_proof}' + return f"{client_first_bare},{server_first_str},{client_final_no_proof}" def __verify_client_proof( self, client_final: ClientFinalMessage, stored_key: CryptoDatum, - auth_message: str + auth_message: str, ) -> bool: """Verify the client proof as per RFC 5802. @@ -66,22 +72,22 @@ def __verify_client_proof( 3. Computing H(ClientKey) and comparing with StoredKey """ if not client_final.client_proof: - raise ValueError('Client final message has no proof') + raise ValueError("Client final message has no proof") auth_message_bytes = CryptoDatum(auth_message.encode()) client_signature = scram_hmac_sha512(stored_key, auth_message_bytes) # Recover ClientKey from ClientProof XOR ClientSignature - recovered_client_key = scram_xor_bytes(client_final.client_proof, client_signature) + recovered_client_key = scram_xor_bytes( + client_final.client_proof, client_signature + ) # Verify that H(ClientKey) == StoredKey recovered_stored_key = scram_h(recovered_client_key) return scram_constant_time_compare(recovered_stored_key, stored_key) def __compute_server_signature( - self, - server_key: CryptoDatum, - auth_message: str + self, server_key: CryptoDatum, auth_message: str ) -> CryptoDatum: """Compute the server signature as per RFC 5802. @@ -93,7 +99,7 @@ def __compute_server_signature( def __generate_rfc_string(self) -> str: """Generate RFC 5802 formatted server final message.""" signature_b64 = b64encode(self.__signature).decode() - return f'v={signature_b64}' + return f"v={signature_b64}" def __init__( self, @@ -107,10 +113,10 @@ def __init__( ): # Check for mutually exclusive parameters if rfc_string and client_first: - raise ValueError('Cannot specify both rfc_string and other parameters') + raise ValueError("Cannot specify both rfc_string and other parameters") if not rfc_string and not client_first: - raise ValueError('Must specify either rfc_string or message parameters') + raise ValueError("Must specify either rfc_string or message parameters") if rfc_string: # Parse from RFC string @@ -118,26 +124,28 @@ def __init__( else: # Create new message from parameters if not isinstance(client_first, ClientFirstMessage): - raise TypeError('client_first must be a ClientFirstMessage instance') + raise TypeError("client_first must be a ClientFirstMessage instance") if not isinstance(server_first, ServerFirstMessage): - raise TypeError('server_first must be a ServerFirstMessage instance') + raise TypeError("server_first must be a ServerFirstMessage instance") if not isinstance(client_final, ClientFinalMessage): - raise TypeError('client_final must be a ClientFinalMessage instance') + raise TypeError("client_final must be a ClientFinalMessage instance") if not isinstance(stored_key, CryptoDatum): - raise TypeError('stored_key must be a CryptoDatum instance') + raise TypeError("stored_key must be a CryptoDatum instance") if not isinstance(server_key, CryptoDatum): - raise TypeError('server_key must be a CryptoDatum instance') + raise TypeError("server_key must be a CryptoDatum instance") # Compute auth message - auth_message = self.__compute_auth_message(client_first, server_first, client_final) + auth_message = self.__compute_auth_message( + client_first, server_first, client_final + ) # Verify client proof first (server should do this before sending final message) if not self.__verify_client_proof(client_final, stored_key, auth_message): - raise ValueError('Client proof verification failed') + raise ValueError("Client proof verification failed") # Compute server signature self.__signature = self.__compute_server_signature(server_key, auth_message) @@ -150,19 +158,23 @@ def __parse_rfc_string(self, rfc_string: str): Format: v= """ - if not rfc_string.startswith('v='): - raise ValueError('Invalid server final message format: must start with "v="') + if not rfc_string.startswith("v="): + raise ValueError( + 'Invalid server final message format: must start with "v="' + ) signature_b64 = rfc_string[2:] # Skip "v=" # Check for unexpected additional attributes - if ',' in signature_b64: - raise ValueError('Invalid server final message format: unexpected additional attributes') + if "," in signature_b64: + raise ValueError( + "Invalid server final message format: unexpected additional attributes" + ) try: signature_bytes = b64decode(signature_b64) except Exception as e: - raise ValueError(f'Invalid base64 encoding in server final message: {e}') + raise ValueError(f"Invalid base64 encoding in server final message: {e}") self.__signature = CryptoDatum(signature_bytes) self.__rfc_str = rfc_string diff --git a/truenas_api_client/py_scram/server_first.py b/truenas_api_client/py_scram/server_first.py index 5c9d3bb..49c9a61 100644 --- a/truenas_api_client/py_scram/server_first.py +++ b/truenas_api_client/py_scram/server_first.py @@ -4,22 +4,27 @@ from base64 import b64encode, b64decode from .scram_crypto import ( - CryptoDatum, generate_nonce, SCRAM_MAX_ITERS, SCRAM_MIN_ITERS, SCRAM_NONCE_SIZE, SCRAM_MAX_SALT_SZ, + CryptoDatum, + generate_nonce, + SCRAM_MAX_ITERS, + SCRAM_MIN_ITERS, + SCRAM_NONCE_SIZE, + SCRAM_MAX_SALT_SZ, ) from .client_first import ClientFirstMessage from .error import ScramError, SCRAM_E_INVALID_REQUEST -__all__ = ['ServerFirstMessage'] +__all__ = ["ServerFirstMessage"] class ServerFirstMessage: - __rfc_str = '' + __rfc_str = "" def __generate_rfc_string(self): nonce_b64 = b64encode(self.nonce).decode() salt_b64 = b64encode(self.salt).decode() - return f'r={nonce_b64},s={salt_b64},i={self.iterations}' + return f"r={nonce_b64},s={salt_b64},i={self.iterations}" def __init__( self, @@ -31,10 +36,12 @@ def __init__( ): # Check for mutually exclusive parameters if rfc_string and client_first: - raise ValueError('Cannot specify both rfc_string and client_first parameters') + raise ValueError( + "Cannot specify both rfc_string and client_first parameters" + ) if not rfc_string and not client_first: - raise ValueError('Must specify either rfc_string or client_first parameter') + raise ValueError("Must specify either rfc_string or client_first parameter") if rfc_string: # Parse from RFC string @@ -42,13 +49,13 @@ def __init__( else: # Create new message from parameters if not isinstance(client_first, ClientFirstMessage): - raise TypeError('client_first must be a ClientFirstMessage instance') + raise TypeError("client_first must be a ClientFirstMessage instance") if not isinstance(salt, CryptoDatum): - raise TypeError('salt must be a CryptoDatum instance') + raise TypeError("salt must be a CryptoDatum instance") if not isinstance(iterations, int): - raise TypeError('iterations must be an integer') + raise TypeError("iterations must be an integer") # Generate server nonce and combine with client nonce server_nonce = generate_nonce() @@ -63,9 +70,9 @@ def __parse_rfc_string(self, rfc_string: str): """Parse ServerFirstMessage from RFC 5802 formatted string. Format: r=,s=,i= """ - parts = rfc_string.split(',') + parts = rfc_string.split(",") if len(parts) != 3: - raise ValueError('Invalid server first message format') + raise ValueError("Invalid server first message format") nonce_b64 = None salt_b64 = None @@ -73,51 +80,50 @@ def __parse_rfc_string(self, rfc_string: str): for part in parts: if not part: - raise ValueError('Invalid server first message format') + raise ValueError("Invalid server first message format") - key, _, value = part.partition('=') + key, _, value = part.partition("=") if not value: - raise ValueError('Invalid server first message format') + raise ValueError("Invalid server first message format") - if key == 'r': + if key == "r": nonce_b64 = value - elif key == 's': + elif key == "s": salt_b64 = value - elif key == 'i': + elif key == "i": try: iterations = int(value) except ValueError: - raise ValueError('Invalid iteration count in server first message') + raise ValueError("Invalid iteration count in server first message") else: - raise ValueError(f'Unknown key in server first message: {key}') + raise ValueError(f"Unknown key in server first message: {key}") if nonce_b64 is None or salt_b64 is None or iterations is None: - raise ValueError('Missing required fields in server first message') + raise ValueError("Missing required fields in server first message") # Validate iterations against the same bounds as the C parser (scram_parse_iteration_cnt). if iterations < SCRAM_MIN_ITERS or iterations > SCRAM_MAX_ITERS: raise ScramError( - f'{iterations}: iteration count outside [{SCRAM_MIN_ITERS}, {SCRAM_MAX_ITERS}]', - SCRAM_E_INVALID_REQUEST + f"{iterations}: iteration count outside [{SCRAM_MIN_ITERS}, {SCRAM_MAX_ITERS}]", + SCRAM_E_INVALID_REQUEST, ) try: nonce_bytes = b64decode(nonce_b64) salt_bytes = b64decode(salt_b64) except Exception as e: - raise ValueError(f'Invalid base64 encoding in server first message: {e}') + raise ValueError(f"Invalid base64 encoding in server first message: {e}") # The C parser accepts only a 32- or 64-byte nonce and caps the salt length; match both # (scram_parse_nonce / scram_parse_b64_datum in scram_attr_parse.c). if len(nonce_bytes) not in (SCRAM_NONCE_SIZE, SCRAM_NONCE_SIZE * 2): raise ScramError( - f'{len(nonce_bytes)}: unexpected nonce size', - SCRAM_E_INVALID_REQUEST + f"{len(nonce_bytes)}: unexpected nonce size", SCRAM_E_INVALID_REQUEST ) if len(salt_bytes) > SCRAM_MAX_SALT_SZ: raise ScramError( - f'{len(salt_bytes)}: salt exceeds maximum of {SCRAM_MAX_SALT_SZ}', - SCRAM_E_INVALID_REQUEST + f"{len(salt_bytes)}: salt exceeds maximum of {SCRAM_MAX_SALT_SZ}", + SCRAM_E_INVALID_REQUEST, ) self.__nonce = CryptoDatum(nonce_bytes) diff --git a/truenas_api_client/py_scram/verify.py b/truenas_api_client/py_scram/verify.py index f41b129..ec9248f 100644 --- a/truenas_api_client/py_scram/verify.py +++ b/truenas_api_client/py_scram/verify.py @@ -3,7 +3,13 @@ from base64 import b64encode -from .scram_crypto import CryptoDatum, scram_hmac_sha512, scram_xor_bytes, scram_h, scram_constant_time_compare +from .scram_crypto import ( + CryptoDatum, + scram_hmac_sha512, + scram_xor_bytes, + scram_h, + scram_constant_time_compare, +) from .common import GS2_SEPARATOR, GS2_NO_CHANNEL_BINDING from .client_first import ClientFirstMessage from .server_first import ServerFirstMessage @@ -12,7 +18,7 @@ from .error import ScramError, SCRAM_E_AUTH_FAILED -__all__ = ['verify_server_signature', 'verify_client_final_message'] +__all__ = ["verify_server_signature", "verify_client_final_message"] def verify_server_signature( @@ -20,7 +26,7 @@ def verify_server_signature( server_first: ServerFirstMessage, client_final: ClientFinalMessage, server_final: ServerFinalMessage, - server_key: CryptoDatum + server_key: CryptoDatum, ): """Verify the server signature in the ServerFinalMessage. @@ -48,19 +54,19 @@ def verify_server_signature( """ # Validate parameters if not isinstance(client_first, ClientFirstMessage): - raise TypeError('client_first must be a ClientFirstMessage instance') + raise TypeError("client_first must be a ClientFirstMessage instance") if not isinstance(server_first, ServerFirstMessage): - raise TypeError('server_first must be a ServerFirstMessage instance') + raise TypeError("server_first must be a ServerFirstMessage instance") if not isinstance(client_final, ClientFinalMessage): - raise TypeError('client_final must be a ClientFinalMessage instance') + raise TypeError("client_final must be a ClientFinalMessage instance") if not isinstance(server_final, ServerFinalMessage): - raise TypeError('server_final must be a ServerFinalMessage instance') + raise TypeError("server_final must be a ServerFinalMessage instance") if not isinstance(server_key, CryptoDatum): - raise TypeError('server_key must be a CryptoDatum instance') + raise TypeError("server_key must be a CryptoDatum instance") # Compute the auth message # Get client-first-message-bare (without GS2 header and separator) @@ -80,16 +86,16 @@ def verify_server_signature( # flag plus the empty-authzid separator, exactly as ClientFinalMessage emitted it # (e.g. "p=tls-server-end-point,,"); this must match client_final.py byte-for-byte # or the reconstructed AuthMessage -- and thus signature verification -- will fail. - gs2_header_str = (client_first.gs2_header or 'n') + GS2_SEPARATOR + gs2_header_str = (client_first.gs2_header or "n") + GS2_SEPARATOR cb_data = gs2_header_str.encode() + bytes(client_final.channel_binding) channel_binding_b64 = b64encode(cb_data).decode() else: channel_binding_b64 = GS2_NO_CHANNEL_BINDING nonce_b64 = b64encode(client_final.nonce).decode() - client_final_no_proof = f'c={channel_binding_b64},r={nonce_b64}' + client_final_no_proof = f"c={channel_binding_b64},r={nonce_b64}" - auth_message = f'{client_first_bare},{server_first_str},{client_final_no_proof}' + auth_message = f"{client_first_bare},{server_first_str},{client_final_no_proof}" # Calculate expected ServerSignature = HMAC(ServerKey, AuthMessage) auth_message_bytes = CryptoDatum(auth_message.encode()) @@ -97,7 +103,7 @@ def verify_server_signature( # Compare the expected signature with the received signature if not scram_constant_time_compare(expected_signature, server_final.signature): - raise ScramError('server signature verification failed', SCRAM_E_AUTH_FAILED) + raise ScramError("server signature verification failed", SCRAM_E_AUTH_FAILED) # Return None on success (like C extension) return None @@ -107,7 +113,7 @@ def verify_client_final_message( client_first: ClientFirstMessage, server_first: ServerFirstMessage, client_final: ClientFinalMessage, - stored_key: CryptoDatum + stored_key: CryptoDatum, ): """Verify the client proof in the ClientFinalMessage. @@ -136,19 +142,19 @@ def verify_client_final_message( """ # Validate parameters if not isinstance(client_first, ClientFirstMessage): - raise TypeError('client_first must be a ClientFirstMessage instance') + raise TypeError("client_first must be a ClientFirstMessage instance") if not isinstance(server_first, ServerFirstMessage): - raise TypeError('server_first must be a ServerFirstMessage instance') + raise TypeError("server_first must be a ServerFirstMessage instance") if not isinstance(client_final, ClientFinalMessage): - raise TypeError('client_final must be a ClientFinalMessage instance') + raise TypeError("client_final must be a ClientFinalMessage instance") if not isinstance(stored_key, CryptoDatum): - raise TypeError('stored_key must be a CryptoDatum instance') + raise TypeError("stored_key must be a CryptoDatum instance") if not client_final.client_proof: - raise ValueError('Client final message has no proof') + raise ValueError("Client final message has no proof") # Compute the auth message # Get client-first-message-bare (without GS2 header and separator) @@ -168,16 +174,16 @@ def verify_client_final_message( # flag plus the empty-authzid separator, exactly as ClientFinalMessage emitted it # (e.g. "p=tls-server-end-point,,"); this must match client_final.py byte-for-byte # or the reconstructed AuthMessage -- and thus signature verification -- will fail. - gs2_header_str = (client_first.gs2_header or 'n') + GS2_SEPARATOR + gs2_header_str = (client_first.gs2_header or "n") + GS2_SEPARATOR cb_data = gs2_header_str.encode() + bytes(client_final.channel_binding) channel_binding_b64 = b64encode(cb_data).decode() else: channel_binding_b64 = GS2_NO_CHANNEL_BINDING nonce_b64 = b64encode(client_final.nonce).decode() - client_final_no_proof = f'c={channel_binding_b64},r={nonce_b64}' + client_final_no_proof = f"c={channel_binding_b64},r={nonce_b64}" - auth_message = f'{client_first_bare},{server_first_str},{client_final_no_proof}' + auth_message = f"{client_first_bare},{server_first_str},{client_final_no_proof}" # Calculate ClientSignature = HMAC(StoredKey, AuthMessage) auth_message_bytes = CryptoDatum(auth_message.encode()) @@ -189,7 +195,7 @@ def verify_client_final_message( # Verify that H(ClientKey) == StoredKey recovered_stored_key = scram_h(recovered_client_key) if not scram_constant_time_compare(recovered_stored_key, stored_key): - raise ScramError('client proof verification failed', SCRAM_E_AUTH_FAILED) + raise ScramError("client proof verification failed", SCRAM_E_AUTH_FAILED) # Return None on success (like C extension) return None diff --git a/truenas_api_client/scram_impl.py b/truenas_api_client/scram_impl.py index a8807d5..204102d 100644 --- a/truenas_api_client/scram_impl.py +++ b/truenas_api_client/scram_impl.py @@ -110,8 +110,8 @@ # Import constants -SCRAM_MAX_ITERS = getattr(truenas_pyscram, 'SCRAM_MAX_ITERS', 5000000) -SCRAM_MIN_ITERS = getattr(truenas_pyscram, 'SCRAM_MIN_ITERS', 50000) +SCRAM_MAX_ITERS = getattr(truenas_pyscram, "SCRAM_MAX_ITERS", 5000000) +SCRAM_MIN_ITERS = getattr(truenas_pyscram, "SCRAM_MIN_ITERS", 50000) # Export message classes, exception, and error codes for compatibility ClientFirstMessage = truenas_pyscram.ClientFirstMessage @@ -122,20 +122,24 @@ ScramError = truenas_pyscram.ScramError # Export error codes -SCRAM_E_AUTH_FAILED = getattr(truenas_pyscram, 'SCRAM_E_AUTH_FAILED', 7) +SCRAM_E_AUTH_FAILED = getattr(truenas_pyscram, "SCRAM_E_AUTH_FAILED", 7) # Channel binding (RFC 5929 tls-server-end-point). Present on truenas_pyscram >= 0.2.0 # and on the bundled py_scram fallback; absent on older C extensions, in which case # compute_tls_server_end_point is None and callers must skip channel binding. -CB_TLS_SERVER_END_POINT = getattr(truenas_pyscram, 'CB_TLS_SERVER_END_POINT', 'tls-server-end-point') -compute_tls_server_end_point = getattr(truenas_pyscram, 'compute_tls_server_end_point', None) +CB_TLS_SERVER_END_POINT = getattr( + truenas_pyscram, "CB_TLS_SERVER_END_POINT", "tls-server-end-point" +) +compute_tls_server_end_point = getattr( + truenas_pyscram, "compute_tls_server_end_point", None +) class ScramMessageType(StrEnum): - CLIENT_FIRST_MESSAGE = 'CLIENT_FIRST_MESSAGE' - SERVER_FIRST_RESPONSE = 'SERVER_FIRST_RESPONSE' - CLIENT_FINAL_MESSAGE = 'CLIENT_FINAL_MESSAGE' - SERVER_FINAL_RESPONSE = 'SERVER_FINAL_RESPONSE' + CLIENT_FIRST_MESSAGE = "CLIENT_FIRST_MESSAGE" + SERVER_FIRST_RESPONSE = "SERVER_FIRST_RESPONSE" + CLIENT_FINAL_MESSAGE = "CLIENT_FINAL_MESSAGE" + SERVER_FINAL_RESPONSE = "SERVER_FINAL_RESPONSE" class ScramMessage(TypedDict): @@ -153,6 +157,7 @@ class TNScramClient: The TrueNAS API client will convert client / server JSON-RPC communication to the dataclasses defined above. """ + def __init__( self, *, @@ -177,7 +182,9 @@ def __init__( """ if raw_key_material is None: if client_key is None or stored_key is None: - raise ValueError('Must provide either raw_key_material or both client_key and stored_key') + raise ValueError( + "Must provide either raw_key_material or both client_key and stored_key" + ) self.raw_key_material = raw_key_material self.client_key = client_key @@ -215,22 +222,22 @@ def get_client_first_message( """ # Validate username if not username or not username.strip(): - raise ValueError('Username cannot be empty') + raise ValueError("Username cannot be empty") # Build kwargs, only include gs2_header if it's not None kwargs = { - 'username': username, - 'api_key_id': self.api_key_id, + "username": username, + "api_key_id": self.api_key_id, } if gs2_header is not None: - kwargs['gs2_header'] = gs2_header + kwargs["gs2_header"] = gs2_header if channel_binding_type is not None: - kwargs['channel_binding_type'] = channel_binding_type + kwargs["channel_binding_type"] = channel_binding_type self.client_first_message = truenas_pyscram.ClientFirstMessage(**kwargs) return ScramMessage( scram_type=ScramMessageType.CLIENT_FIRST_MESSAGE, - rfc_str=str(self.client_first_message) + rfc_str=str(self.client_first_message), ) def get_client_final_message( @@ -257,14 +264,21 @@ def get_client_final_message( Returns: ClientFinalMessage object """ - if server_resp_dict['scram_type'] != ScramMessageType.SERVER_FIRST_RESPONSE: - raise TypeError(f'{server_resp_dict["scram_type"]}: unexpected response type') + if server_resp_dict["scram_type"] != ScramMessageType.SERVER_FIRST_RESPONSE: + raise TypeError( + f"{server_resp_dict['scram_type']}: unexpected response type" + ) - server_resp = truenas_pyscram.ServerFirstMessage(rfc_string=server_resp_dict['rfc_str']) - if server_resp.iterations < SCRAM_MIN_ITERS or server_resp.iterations > SCRAM_MAX_ITERS: + server_resp = truenas_pyscram.ServerFirstMessage( + rfc_string=server_resp_dict["rfc_str"] + ) + if ( + server_resp.iterations < SCRAM_MIN_ITERS + or server_resp.iterations > SCRAM_MAX_ITERS + ): raise ValueError( - f'{server_resp.iterations}: iteration count from server is outside valid range ' - f'[{SCRAM_MIN_ITERS}, {SCRAM_MAX_ITERS}]' + f"{server_resp.iterations}: iteration count from server is outside valid range " + f"[{SCRAM_MIN_ITERS}, {SCRAM_MAX_ITERS}]" ) self.server_first_message = server_resp @@ -272,14 +286,14 @@ def get_client_final_message( # Generate keys if we have raw_key_material if self.raw_key_material is not None: # Convert raw password/key to bytes and compute salted password - password_bytes = self.raw_key_material.encode('utf-8') + password_bytes = self.raw_key_material.encode("utf-8") # Compute salted password using PBKDF2-HMAC-SHA512 (scram_hi) salted_password_bytes = hashlib.pbkdf2_hmac( - 'sha512', + "sha512", password_bytes, bytes(server_resp.salt), - server_resp.iterations + server_resp.iterations, ) salted_password = truenas_pyscram.CryptoDatum(salted_password_bytes) @@ -287,7 +301,7 @@ def get_client_final_message( auth_data = truenas_pyscram.generate_scram_auth_data( salted_password=salted_password, salt=server_resp.salt, - iterations=server_resp.iterations + iterations=server_resp.iterations, ) self.client_key = auth_data.client_key self.stored_key = auth_data.stored_key @@ -300,11 +314,11 @@ def get_client_final_message( server_first=self.server_first_message, client_key=self.client_key, stored_key=self.stored_key, - channel_binding=channel_binding + channel_binding=channel_binding, ) return ScramMessage( scram_type=ScramMessageType.CLIENT_FINAL_MESSAGE, - rfc_str=str(self.client_final_message) + rfc_str=str(self.client_final_message), ) def verify_server_final_message(self, server_resp_dict: ScramMessage) -> bool: @@ -325,18 +339,22 @@ def verify_server_final_message(self, server_resp_dict: ScramMessage) -> bool: TypeError: If response type is not SERVER_FINAL_RESPONSE ValueError: If server response lacks signature or verification fails """ - if server_resp_dict['scram_type'] != ScramMessageType.SERVER_FINAL_RESPONSE: - raise TypeError(f'{server_resp_dict["scram_type"]}: unexpected response type') + if server_resp_dict["scram_type"] != ScramMessageType.SERVER_FINAL_RESPONSE: + raise TypeError( + f"{server_resp_dict['scram_type']}: unexpected response type" + ) - server_resp = truenas_pyscram.ServerFinalMessage(rfc_string=server_resp_dict['rfc_str']) + server_resp = truenas_pyscram.ServerFinalMessage( + rfc_string=server_resp_dict["rfc_str"] + ) if not server_resp.signature: - raise ValueError('Server response lacks signature') + raise ValueError("Server response lacks signature") self.server_final_message = server_resp # Verify the server signature if self.server_key is None: - raise ValueError('Cannot verify server signature without server_key') + raise ValueError("Cannot verify server signature without server_key") # Type narrowing: assert helps type checker understand server_key is not None assert self.server_key is not None @@ -348,7 +366,7 @@ def verify_server_final_message(self, server_resp_dict: ScramMessage) -> bool: server_first=self.server_first_message, client_final=self.client_final_message, server_final=self.server_final_message, - server_key=self.server_key + server_key=self.server_key, ) return True diff --git a/truenas_api_client/utils.py b/truenas_api_client/utils.py index c5ccbd8..764ac74 100644 --- a/truenas_api_client/utils.py +++ b/truenas_api_client/utils.py @@ -8,21 +8,22 @@ undefined: A dummy object similar in purpose to `None` that indicates an unset variable. """ + import socket import sys from typing import Any, ClassVar, final, Mapping -MIDDLEWARE_RUN_DIR = '/run/middleware' +MIDDLEWARE_RUN_DIR = "/run/middleware" @final class UndefinedType: - _instance: ClassVar['UndefinedType'] + _instance: ClassVar["UndefinedType"] def __new__(cls): - if not hasattr(cls, '_instance'): + if not hasattr(cls, "_instance"): cls._instance = super().__new__(cls) return cls._instance @@ -46,6 +47,7 @@ class Struct: 6 """ + def __init__(self, mapping: Mapping[str, Any]): """Initialize a `Struct` with a `Mapping`. @@ -81,6 +83,7 @@ class ProgressBar(object): extra: A string or printable object to display after the status message. """ + def __init__(self): self.message = None self.percentage = 0.0 @@ -99,15 +102,17 @@ def draw(self): """ progress_width = 40 filled_width = int(self.percentage * progress_width) - self.write_stream.write('\033[2K\033[A\033[2K\r') + self.write_stream.write("\033[2K\033[A\033[2K\r") self.write_stream.write( - f'Status: {(self.message or "(none)").strip()}' + ( - f' Extra: {self.extra}' if self.extra else '' - ) + '\n' + f"Status: {(self.message or '(none)').strip()}" + + (f" Extra: {self.extra}" if self.extra else "") + + "\n" ) self.write_stream.write( - 'Total Progress: [{}{}] {:.2%}'.format( - '#' * filled_width, '_' * (progress_width - filled_width), self.percentage + "Total Progress: [{}{}] {:.2%}".format( + "#" * filled_width, + "_" * (progress_width - filled_width), + self.percentage, ) ) self.write_stream.flush() @@ -121,7 +126,7 @@ def update(self, percentage: float | None = None, message: str | None = None): """ if not self.used_flag: - self.write_stream.write('\n') + self.write_stream.write("\n") self.used_flag = True if percentage: self.percentage = float(percentage / 100.0) @@ -136,13 +141,13 @@ def finish(self): def __exit__(self, typ, value, traceback): if self.used_flag: self.draw() - self.write_stream.write('\n') + self.write_stream.write("\n") def set_socket_options(socobj): plat = sys.platform - if plat not in ('win32', 'linux', 'freebsd', 'darwin'): - raise RuntimeError('Unsupported platform') + if plat not in ("win32", "linux", "freebsd", "darwin"): + raise RuntimeError("Unsupported platform") # enable keepalives on the socket socobj.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE, 1) @@ -156,7 +161,7 @@ def set_socket_options(socobj): # 3. for a maximum up to 5 times # # after 5 times (5 seconds of no response), the socket will be closed - if sys.platform == 'darwin': + if sys.platform == "darwin": socobj.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPALIVE, 1) else: socobj.setsockopt(socket.IPPROTO_TCP, socket.TCP_KEEPIDLE, 1) From 572acdb3be22a317a307a9d4c0e2106b16dc7b37 Mon Sep 17 00:00:00 2001 From: caleb Date: Tue, 17 Nov 2026 12:29:01 -0500 Subject: [PATCH 3/3] Check the formatting of changed files in CI Pull requests now fail when a new or changed Python or Markdown file does not match ruff format. Files a pull request does not touch are not checked. --- .github/workflows/lint.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index 91cc08f..b1cfabf 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,4 +1,4 @@ -name: ruff check +name: ruff on: pull_request: @@ -11,4 +11,12 @@ jobs: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - uses: astral-sh/ruff-action@v4.1.0 + - name: Check formatting of changed and new files + env: + BASE: ${{ github.base_ref }} + run: | + git diff -z --name-only --diff-filter=ACMR "origin/$BASE...HEAD" -- '*.py' '*.pyi' '*.md' \ + | xargs -0 -r ruff format --check --diff