Skip to content

Handle KeyImportError for invalid SSH private key files#13

Merged
gensyn merged 4 commits intomainfrom
copilot/fix-invalid-private-key-error
Mar 25, 2026
Merged

Handle KeyImportError for invalid SSH private key files#13
gensyn merged 4 commits intomainfrom
copilot/fix-invalid-private-key-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 25, 2026

When a key file exists but contains invalid private key data, asyncssh raises KeyImportError, which was previously uncaught and surfaced as a generic "Unknown Error" in the UI.

Changes

  • coordinator.py — Catch asyncssh.KeyImportError and raise a ServiceValidationError with translation key invalid_key_file
  • strings.json, translations/en.json, translations/de.json — Add invalid_key_file exception message
  • test_async_execute.py — Add test_invalid_key_file covering the new error path
except KeyImportError as exc:
    _LOGGER.warning("Invalid key file for %s@%s: %s", username, host, exc)
    raise ServiceValidationError(
        "The key file is not a valid private key.",
        translation_domain=DOMAIN,
        translation_key="invalid_key_file",
    ) from exc
Original prompt

This section details on the original issue you should resolve

<issue_title>Unknown Error when key file exists but is not a valid private key</issue_title>
<issue_description>SSH Command version
v1.0.0

Home Assistant version
2026.3.2

Issue / Experienced behavior
"Unknown Error" is shown in the UI when the stated key file exists but is not a valid private key. The error in the logs is "asyncssh.public_key.KeyImportError: Invalid private key"

Expected behavior
A meaningful error should be presented to the user.
</issue_description>

Comments on the Issue (you are @copilot in this section)


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Copilot AI changed the title [WIP] Fix unknown error for invalid private key file Handle KeyImportError for invalid SSH private key files Mar 25, 2026
Copilot AI requested a review from gensyn March 25, 2026 07:41
…rivate-key-error

# Conflicts:
#	coordinator.py
@gensyn
Copy link
Copy Markdown
Owner

gensyn commented Mar 25, 2026

@copilot Please fix the unit tests that broke in this branch.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 25, 2026

@copilot Please fix the unit tests that broke in this branch.

Fixed in 22baf84. The test_invalid_key_file test had a stale patch("ssh_command.coordinator.exists") left over from before the coordinator was updated to use pathlib.Path.exists. Since SERVICE_DATA_BASE has no input and check_known_hosts=False, no exists call happens at all — so the patch was simply removed.

@gensyn gensyn marked this pull request as ready for review March 25, 2026 08:02
@gensyn gensyn merged commit 79fc1b4 into main Mar 25, 2026
5 checks passed
@gensyn gensyn deleted the copilot/fix-invalid-private-key-error branch March 25, 2026 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unknown Error when key file exists but is not a valid private key

2 participants