Skip to content

Commit 9c001af

Browse files
committed
cli: use connection token file in serve-web
1 parent 414d829 commit 9c001af

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

cli/src/commands/args.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,9 @@ pub struct ServeWebArgs {
197197
/// A secret that must be included with all requests.
198198
#[clap(long)]
199199
pub connection_token: Option<String>,
200+
/// A file containing a secret that must be included with all requests.
201+
#[clap(long)]
202+
pub connection_token_file: Option<String>,
200203
/// Run without a connection token. Only use this if the connection is secured by other means.
201204
#[clap(long)]
202205
pub without_connection_token: bool,

cli/src/commands/serve_web.rs

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66
use std::collections::HashMap;
77
use std::convert::Infallible;
88
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
9-
use std::path::PathBuf;
9+
use std::path::{Path, PathBuf};
1010
use std::sync::{Arc, Mutex};
1111
use std::time::{Duration, Instant};
1212

1313
use const_format::concatcp;
1414
use hyper::service::{make_service_fn, service_fn};
1515
use hyper::{Body, Request, Response, Server};
16+
use tempfile::{tempdir, TempDir};
1617
use tokio::io::{AsyncBufReadExt, BufReader};
1718
use tokio::pin;
1819

@@ -76,16 +77,21 @@ pub async fn serve_web(ctx: CommandContext, mut args: ServeWebArgs) -> Result<i3
7677
legal::require_consent(&ctx.paths, args.accept_server_license_terms)?;
7778

7879
let platform: crate::update_service::Platform = PreReqChecker::new().verify().await?;
79-
80-
if !args.without_connection_token {
80+
let token_file = if !args.without_connection_token {
8181
// Ensure there's a defined connection token, since if multiple server versions
8282
// are excuted, they will need to have a single shared token.
83-
args.connection_token = Some(
84-
args.connection_token
85-
.clone()
86-
.unwrap_or_else(|| uuid::Uuid::new_v4().to_string()),
87-
);
88-
}
83+
let connection_token = args
84+
.connection_token
85+
.clone()
86+
.unwrap_or_else(|| uuid::Uuid::new_v4().to_string());
87+
let tf = ConnectionTokenFile::new(&connection_token)
88+
.map_err(CodeError::CouldNotCreateConnectionTokenFile)?;
89+
args.connection_token = Some(connection_token);
90+
args.connection_token_file = Some(tf.path().to_string_lossy().to_string());
91+
Some(tf)
92+
} else {
93+
None
94+
};
8995

9096
let cm = ConnectionManager::new(&ctx, platform, args.clone());
9197
let key = get_server_key_half(&ctx.paths);
@@ -137,6 +143,7 @@ pub async fn serve_web(ctx: CommandContext, mut args: ServeWebArgs) -> Result<i3
137143
};
138144

139145
r.map_err(CodeError::CouldNotListenOnInterface)?;
146+
drop(token_file); // ensure it lives long enough
140147

141148
Ok(0)
142149
}
@@ -704,8 +711,10 @@ impl ConnectionManager {
704711
if args.args.without_connection_token {
705712
cmd.arg("--without-connection-token");
706713
}
707-
if let Some(ct) = &args.args.connection_token {
708-
cmd.arg("--connection-token");
714+
// Note: intentional that we don't pass --connection-token here, we always
715+
// convert it into the file variant.
716+
if let Some(ct) = &args.args.connection_token_file {
717+
cmd.arg("--connection-token-file");
709718
cmd.arg(ct);
710719
}
711720

@@ -779,3 +788,21 @@ struct StartArgs {
779788
release: Release,
780789
opener: BarrierOpener<Result<StartData, String>>,
781790
}
791+
792+
struct ConnectionTokenFile {
793+
path: PathBuf,
794+
_dir: TempDir, // implements Drop to delete the dir
795+
}
796+
797+
impl ConnectionTokenFile {
798+
fn new(connection_token: &str) -> std::io::Result<Self> {
799+
let d = tempdir()?;
800+
let path = d.path().join("connection-token");
801+
std::fs::write(&path, connection_token)?;
802+
Ok(ConnectionTokenFile { path, _dir: d })
803+
}
804+
805+
fn path(&self) -> &Path {
806+
&self.path
807+
}
808+
}

cli/src/util/errors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,8 @@ pub enum CodeError {
512512
// todo: can be specialized when update service is moved to CodeErrors
513513
#[error("Could not check for update: {0}")]
514514
UpdateCheckFailed(String),
515+
#[error("Could not write connection token file: {0}")]
516+
CouldNotCreateConnectionTokenFile(std::io::Error)
515517
}
516518

517519
makeAnyError!(

0 commit comments

Comments
 (0)