-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Authentication using ssh-agent #248
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, please try installing pre-commit in your virtual environment and run pre-commit
when you stage changes. You can also run it against specific files or --all-files
.
This will get your checks passing!
broker/session.py
Outdated
if key_filename: | ||
if not Path(key_filename).exists(): | ||
raise FileNotFoundError(f"Key not found in '{key_filename}'") | ||
self.session.userauth_publickey_fromfile(user, key_filename) | ||
elif kwargs.get("password"): | ||
self.session.userauth_password(user, kwargs["password"]) | ||
try: | ||
self.session.userauth_publickey_fromfile(user, key_filename) | ||
except Exception as err: | ||
raise exceptions.AuthenticationError("Key-based authentication failed.") from err | ||
elif password: | ||
try: | ||
self.session.userauth_password(user, password) | ||
except Exception as err: | ||
raise exceptions.AuthenticationError("Password-based authentication failed.") from err | ||
elif user: | ||
try: | ||
self.session.agent_auth(user) | ||
except Exception as err: | ||
raise exceptions.AuthenticationError("Agent-based authentication failed.") from err | ||
else: | ||
raise exceptions.AuthenticationError("No password or key file provided.") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like where you're going with, but it looks a bit more repetitive than I'd prefer. Perhaps we could wrap the entire section in a try/except and raise a formatted message like this.
try:
if key_filename:
auth_type = "Key"
if not Path(key_filename).exists():
raise FileNotFoundError(f"Key not found in '{key_filename}'")
self.session.userauth_publickey_fromfile(user, key_filename)
elif password:
auth_type = "Password"
self.session.userauth_password(user, password)
elif user:
auth_type = "Agent"
self.session.agent_auth(user)
else:
raise exceptions.AuthenticationError("No password or key file provided.")
except Exception as err:
raise exceptions.AuthenticationError(f"{auth_type}-based authentication failed.") from err
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will lead to an error when the else clause is called since in this case auth_type
is not defined for the broad Exception and the error message will also be misleading.
Since ruff is complaining about blind exceptions, would it be okay to just omit the outer except block?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
auth_type
isn't used in the else clause, so shouldn't cause an issue.
as for ruff, you can add this to the right of the except statement # noqa: BLE001
there are likely a few different exceptions that could be raised here and I'd rather not waste your time chasing them all down.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you provide a demonstration of agent/session-based authentication working for you locally?
I am not exactly sure what I should put here but I have written some tests and ran them locally:
with the wollowing results:
|
Awesome, thanks for the contribution @dosas! |
This PR adds authentication using ssh-agent, see #246
Also exception handling for authentication is unified.