Skip to content

Commit

Permalink
Make SSH timeout adjustable, document more container options
Browse files Browse the repository at this point in the history
commit-id:55297dc4
  • Loading branch information
vlad-ivanov-name committed Dec 1, 2022
1 parent e105436 commit 5c41263
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 7 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,24 @@ See [here](https://josh-project.github.io/josh/faq.html)
Default: 8022
</td>
</tr>
<tr>
<td>
<code>JOSH_SSH_MAX_STARTUPS</code>
</td>
<td>
Maximum number of concurrent SSH authentication attempts. Default: 16
</td>
</tr>
<tr>
<td>
<code>JOSH_SSH_TIMEOUT</code>
</td>
<td>
Timeout, in seconds, for a single request when serving repos over SSH.
This time should cover fetch from upstream repo, filtering, and serving
repo to client. Default: 300
</td>
</tr>
<tr>
<td>
<code>JOSH_EXTRA_OPTS</code>
Expand Down
3 changes: 2 additions & 1 deletion docker/etc/ssh/sshd_config.template
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ X11Forwarding no
PrintMotd no

# Accepted environment variables

AcceptEnv GIT_PROTOCOL

# fail2ban-like features

PerSourceMaxStartups 10
PerSourceMaxStartups ${JOSH_SSH_MAX_STARTUPS}
PerSourceNetBlockSize 32:128

# Client management
Expand Down
2 changes: 2 additions & 0 deletions docker/s6-rc.d/sshd-generate-config/up
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/command/execlineb -P

importas -D 8022 josh_ssh_port JOSH_SSH_PORT
importas -D 16 josh_ssh_max_startups JOSH_SSH_MAX_STARTUPS
emptyenv -p
backtick JOSH_SSH_PORT { echo ${josh_ssh_port} }
backtick JOSH_SSH_MAX_STARTUPS { echo ${josh_ssh_max_startups} }
foreground
{
redirfd -r 0 /etc/ssh/sshd_config.template
Expand Down
10 changes: 9 additions & 1 deletion docker/s6-rc.d/sshd/run
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
#!/command/execlineb -P

/usr/sbin/sshd -e -D -h /data/keys/.ssh/id_ed25519
importas josh_http_port JOSH_HTTP_PORT
importas josh_ssh_timeout JOSH_SSH_TIMEOUT

/usr/sbin/sshd \
-e \
-D \
-h/data/keys/.ssh/id_ed25519 \
-oSetEnv=JOSH_SSH_SHELL_TIMEOUT=${josh_ssh_timeout} \
-oSetEnv=JOSH_SSH_SHELL_ENDPOINT_PORT=${josh_http_port}
28 changes: 23 additions & 5 deletions josh-ssh-shell/src/bin/josh-ssh-shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ struct Args {
command: String,
}

const HTTP_REQUEST_TIMEOUT: u64 = 120;
const HTTP_JOSH_SERVER_PORT: &str = "8000";
const HTTP_REQUEST_TIMEOUT: u64 = 300;
const HTTP_JOSH_SERVER_PORT: u16 = 8000;

fn die(message: &str) -> ! {
eprintln!("josh-ssh-shell: {}", message);
Expand Down Expand Up @@ -59,12 +59,30 @@ impl Display for CallError {
}
}

fn get_env_int<T: std::str::FromStr>(env_var: &str, default: T) -> T
where
<T as std::str::FromStr>::Err: Display,
{
let message = format!(
"Invalid {} value of env var {}",
std::any::type_name::<T>(),
env_var
);

env::var(env_var)
.map(|v| v.parse::<T>().unwrap_or_else(|_| die(&message)))
.unwrap_or(default)
}

fn get_endpoint() -> String {
let port =
std::env::var("JOSH_SSH_SHELL_ENDPOINT_PORT").unwrap_or(HTTP_JOSH_SERVER_PORT.to_string());
let port = get_env_int("JOSH_SSH_SHELL_ENDPOINT_PORT", HTTP_JOSH_SERVER_PORT);
format!("http://localhost:{}", port)
}

fn get_timeout() -> u64 {
get_env_int("JOSH_SSH_SHELL_TIMEOUT", HTTP_REQUEST_TIMEOUT)
}

async fn handle_command(
command: RequestedCommand,
ssh_socket: &Path,
Expand Down Expand Up @@ -151,7 +169,7 @@ async fn handle_command(
.post(format!("{}/serve_namespace", get_endpoint()))
.header(CONTENT_TYPE, "application/json")
.body(serde_json::to_string(&rpc_payload).unwrap())
.timeout(Duration::from_secs(HTTP_REQUEST_TIMEOUT))
.timeout(Duration::from_secs(get_timeout()))
.send()
.await?;

Expand Down

0 comments on commit 5c41263

Please sign in to comment.