-
Notifications
You must be signed in to change notification settings - Fork 3
For msm/python #13
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
Open
quic-bjorande
wants to merge
5
commits into
linux-msm:master
Choose a base branch
from
quic-bjorande:for-msm/python
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
For msm/python #13
Changes from 1 commit
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3bcc928
ssh: fall back to private keys without ssh-agent
quic-bjorande 2d8c79c
client: read stdout and stderr concurrently
quic-bjorande 3977c90
proto: reduce per-frame overhead during image upload
quic-bjorande a9584dc
python: expose sk8brd as a Python module
quic-bjorande 2c39d69
python: Add python example
quic-bjorande File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,9 @@ use anyhow::{Context as _, bail}; | |
| use asynchronous_codec::BytesMut; | ||
| use russh::Channel; | ||
| use russh::client::{self, Msg}; | ||
| use russh::keys::{HashAlg, ssh_key}; | ||
| use russh::keys::load_secret_key; | ||
| use russh::keys::{HashAlg, PrivateKeyWithHashAlg, ssh_key}; | ||
| use std::path::{Path, PathBuf}; | ||
| use std::pin::Pin; | ||
| use std::sync::Arc; | ||
| use std::task::{Context, Poll}; | ||
|
|
@@ -29,40 +31,78 @@ pub async fn ssh_connect(farm: &str, username: String) -> anyhow::Result<Channel | |
| // Connect to the local SSH server | ||
| let config = client::Config::default(); | ||
| let client = Client {}; | ||
| #[cfg(unix)] | ||
| let agent = russh::keys::agent::client::AgentClient::connect_env().await; | ||
| #[cfg(windows)] | ||
| let agent = russh::keys::agent::client::AgentClient::connect_named_pipe( | ||
| "\\\\.\\\\pipe\\\\openssh-ssh-agent", | ||
| ) | ||
| .await; | ||
|
|
||
| let mut agent = agent.expect("Couldn't authenticate with the ssh agent"); | ||
|
|
||
| let mut sess = client::connect(Arc::new(config), farm, client) | ||
| .await | ||
| .with_context(|| format!("Couldn't connect to {farm}"))?; | ||
|
|
||
| let keys = agent | ||
| .request_identities() | ||
| let mut authenticated = false; | ||
|
|
||
| #[cfg(unix)] | ||
| let mut agent = russh::keys::agent::client::AgentClient::connect_env() | ||
| .await | ||
| .expect("Couldn't get identities from the ssh agent"); | ||
| while let Some(key) = keys.first() { | ||
| if sess | ||
| .authenticate_publickey_with( | ||
| &username, | ||
| key.to_owned(), | ||
| Some(HashAlg::Sha256), | ||
| &mut agent, | ||
| ) | ||
| .ok(); | ||
| #[cfg(windows)] | ||
| let mut agent = | ||
| russh::keys::agent::client::AgentClient::connect_named_pipe(r"\\.\pipe\openssh-ssh-agent") | ||
| .await | ||
| .is_ok() | ||
| { | ||
| break; | ||
| .ok(); | ||
|
|
||
| if let Some(agent) = agent.as_mut() | ||
| && let Ok(keys) = agent.request_identities().await | ||
| { | ||
| for key in keys { | ||
| if sess | ||
| .authenticate_publickey_with( | ||
| &username, | ||
| key.to_owned(), | ||
| Some(HashAlg::Sha256), | ||
| agent, | ||
| ) | ||
| .await | ||
| .map(|result| result.success()) | ||
| .unwrap_or(false) | ||
| { | ||
| authenticated = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if sess.is_closed() { | ||
| if !authenticated { | ||
| for path in candidate_private_keys() { | ||
| if !path.is_file() { | ||
| continue; | ||
| } | ||
|
|
||
| let key_pair = match load_secret_key(&path, None) { | ||
| Ok(key_pair) => key_pair, | ||
| Err(_) => continue, | ||
| }; | ||
|
|
||
| let hash_alg = if key_pair.algorithm().is_rsa() { | ||
| sess.best_supported_rsa_hash() | ||
| .await | ||
| .with_context(|| format!("Could not check RSA signatures for {path:?}"))? | ||
| .flatten() | ||
| } else { | ||
| None | ||
| }; | ||
| let private_key = PrivateKeyWithHashAlg::new(Arc::new(key_pair), hash_alg); | ||
|
|
||
| if sess | ||
| .authenticate_publickey(&username, private_key) | ||
| .await | ||
| .map(|result| result.success()) | ||
| .unwrap_or(false) | ||
| { | ||
| authenticated = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if !authenticated || sess.is_closed() { | ||
| bail!("No key was accepted by the server"); | ||
| } | ||
|
|
||
|
|
@@ -74,6 +114,31 @@ pub async fn ssh_connect(farm: &str, username: String) -> anyhow::Result<Channel | |
| Ok(chan) | ||
| } | ||
|
|
||
| fn candidate_private_keys() -> Vec<PathBuf> { | ||
| let mut keys = Vec::new(); | ||
|
|
||
| if let Ok(key_path) = std::env::var("SK8BRD_SSH_KEY") { | ||
| keys.push(expand_tilde(key_path)); | ||
| } | ||
| if let Ok(home) = std::env::var("HOME") { | ||
| keys.push(Path::new(&home).join(".ssh").join("id_ed25519")); | ||
| keys.push(Path::new(&home).join(".ssh").join("id_rsa")); | ||
| } | ||
|
|
||
| keys | ||
| } | ||
|
|
||
| fn expand_tilde(path: String) -> PathBuf { | ||
| let path = path.trim().to_string(); | ||
| if let Some(tail) = path.strip_prefix("~/") { | ||
| return std::env::var("HOME") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| .ok() | ||
| .map_or_else(PathBuf::new, |home| Path::new(&home).join(tail)); | ||
| } | ||
|
|
||
| Path::new(&path).to_path_buf() | ||
| } | ||
|
|
||
| pub struct Wrap(Receiver<Vec<u8>>, BytesMut); | ||
|
|
||
| impl Wrap { | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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'm majorly annoyed the library doesn't do that for us..