Skip to content

Commit

Permalink
RSDK-6043 Support API key auth in dialdbg (#85)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjirewis authored Dec 15, 2023
1 parent f279332 commit fb68a9c
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 18 deletions.
5 changes: 5 additions & 0 deletions src/dialdbg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ viam-dialdbg --uri myremote.com --credential mycredential --output ./foo.txt --c
```
Same as above, but only examines WebRTC connection establishment.

``` shell
viam-dialdbg --uri myremote.com --credential mycredential --output ./foo.txt --credential-type api-key --entity myentity --nogrpc
```
Same as above, but uses "api-key" credential type and "myentity" auth entity for "mycredential".

Use `viam-dialdbg --help` for more information.

## License
Expand Down
69 changes: 59 additions & 10 deletions src/dialdbg/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,28 @@ pub(crate) struct Args {
#[arg(short('t'), long, requires("credential"))]
credential_type: Option<String>,

/// Authentication entity with which to connect to the URI. Can only be provided with
/// "--credential" and must be provided with "--credential-type api-key".
#[arg(
short('e'),
long,
requires("credential"),
requires("credential_type"),
required_if_eq("credential_type", "api-key")
)]
entity: Option<String>,

/// URI to dial. Must be provided.
#[arg(short, long, required(true), display_order(0))]
uri: Option<String>,
}

async fn dial_grpc(uri: &str, credential: &str, credential_type: &str) -> Option<ViamChannel> {
async fn dial_grpc(
uri: &str,
credential: &str,
credential_type: &str,
entity: Option<String>,
) -> Option<ViamChannel> {
let dial_result = match credential {
"" => {
dial::DialOptions::builder()
Expand All @@ -65,7 +81,7 @@ async fn dial_grpc(uri: &str, credential: &str, credential_type: &str) -> Option
}
_ => {
let creds = dial::RPCCredentials::new(
None,
entity,
credential_type.to_string(),
credential.to_string(),
);
Expand All @@ -90,7 +106,12 @@ async fn dial_grpc(uri: &str, credential: &str, credential_type: &str) -> Option
}
}

async fn dial_webrtc(uri: &str, credential: &str, credential_type: &str) -> Option<ViamChannel> {
async fn dial_webrtc(
uri: &str,
credential: &str,
credential_type: &str,
entity: Option<String>,
) -> Option<ViamChannel> {
let dial_result = match credential {
"" => {
dial::DialOptions::builder()
Expand All @@ -102,7 +123,7 @@ async fn dial_webrtc(uri: &str, credential: &str, credential_type: &str) -> Opti
}
_ => {
let creds = dial::RPCCredentials::new(
None,
entity,
credential_type.to_string(),
credential.to_string(),
);
Expand Down Expand Up @@ -198,17 +219,31 @@ pub(crate) async fn main_inner(args: Args) -> Result<()> {
)?;
log_config_setter = Some(log4rs::init_config(config)?);

let ch = dial_grpc(uri.as_str(), credential.as_str(), credential_type.as_str()).await;
let ch = dial_grpc(
uri.as_str(),
credential.as_str(),
credential_type.as_str(),
args.entity.clone(),
)
.await;
let grpc_res = parse::parse_grpc_logs(log_path.clone(), &mut out)?;
write!(out, "{grpc_res}")?;

if let Some(ch) = ch {
if !args.nortt {
let average_rtt = rtt::measure_rtt(ch, 10).await?;
let average_rtt = rtt::measure_rtt(ch, 10).await?.as_millis();

// If average RTT is less than 1ms, report < 1ms instead of
// floored "0ms" value.
let millis_str = if average_rtt < 1 {
"<1".to_string()
} else {
average_rtt.to_string()
};
writeln!(
out,
"average RTT across established gRPC connection: {}ms",
average_rtt.as_millis()
millis_str,
)?;
}
}
Expand Down Expand Up @@ -247,17 +282,31 @@ pub(crate) async fn main_inner(args: Args) -> Result<()> {
log4rs::init_config(config)?;
}

let ch = dial_webrtc(uri.as_str(), credential.as_str(), credential_type.as_str()).await;
let ch = dial_webrtc(
uri.as_str(),
credential.as_str(),
credential_type.as_str(),
args.entity.clone(),
)
.await;
let wrtc_res = parse::parse_webrtc_logs(log_path.clone(), &mut out)?;
write!(out, "{wrtc_res}")?;

if let Some(ch) = ch {
if !args.nortt {
let average_rtt = rtt::measure_rtt(ch.clone(), 10).await?;
let average_rtt = rtt::measure_rtt(ch.clone(), 10).await?.as_millis();

// If average RTT is less than 1ms, report < 1ms instead of
// floored "0ms" value.
let millis_str = if average_rtt < 1 {
"<1".to_string()
} else {
average_rtt.to_string()
};
writeln!(
out,
"average RTT across established WebRTC connection: {}ms",
average_rtt.as_millis()
millis_str,
)?;
}

Expand Down
14 changes: 6 additions & 8 deletions src/rpc/webrtc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,12 @@ pub(crate) async fn action_with_timeout<T>(
let f = f;
}

loop {
tokio::select! {
res = &mut f => {
return Ok(res);
}
_ = &mut timeout => {
return Err(anyhow::anyhow!("Action timed out"));
}
tokio::select! {
res = &mut f => {
return Ok(res);

Check warning on line 305 in src/rpc/webrtc.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> src/rpc/webrtc.rs:305:13 | 305 | return Ok(res); | ^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return = note: `#[warn(clippy::needless_return)]` on by default
}
_ = &mut timeout => {
return Err(anyhow::anyhow!("Action timed out"));

Check warning on line 308 in src/rpc/webrtc.rs

View workflow job for this annotation

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> src/rpc/webrtc.rs:308:13 | 308 | return Err(anyhow::anyhow!("Action timed out")); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return
}
}
}
Expand Down

0 comments on commit fb68a9c

Please sign in to comment.