Skip to content

Commit 3ecb5a0

Browse files
Improve SSH cache
commit-id:b4c3585d
1 parent 160f097 commit 3ecb5a0

File tree

6 files changed

+212
-57
lines changed

6 files changed

+212
-57
lines changed

Cargo.lock

Lines changed: 54 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

josh-proxy/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,4 @@ url = "2.3.1"
4444
uuid = { version = "1.2.2", features = ["v4"] }
4545
josh-rpc = { path = "../josh-rpc" }
4646
tokio-util = "0.7.4"
47+
tempdir = "0.3.7"

josh-proxy/src/auth.rs

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,6 @@ pub async fn check_auth(url: &str, auth: &Handle, required: bool) -> josh::JoshR
7373
return Ok(false);
7474
}
7575

76-
// If the upsteam is ssh we don't really handle authentication here.
77-
// All we need is a username, the private key is expected to available localy.
78-
// This is really not secure at all and should never be used in a production deployment.
79-
if url.starts_with("ssh") {
80-
return Ok(auth.hash != "");
81-
}
82-
8376
if let Some(last) = AUTH_TIMERS.lock()?.get(&(url.to_string(), auth.clone())) {
8477
let since = std::time::Instant::now().duration_since(*last);
8578
tracing::trace!("last: {:?}, since: {:?}", last, since);
@@ -99,47 +92,50 @@ pub async fn check_auth(url: &str, auth: &Handle, required: bool) -> josh::JoshR
9992
.get(auth)
10093
.unwrap_or(&Header { header: None })
10194
.to_owned();
102-
let nurl = format!("{}/info/refs?service=git-upload-pack", url);
95+
let refs_url = format!("{}/info/refs?service=git-upload-pack", url);
10396

104-
let builder = hyper::Request::builder().method("GET").uri(&nurl);
97+
let builder = hyper::Request::builder()
98+
.method(hyper::Method::GET)
99+
.uri(&refs_url);
105100

106-
let builder = if let Some(h) = password.header {
107-
builder.header("authorization", h)
101+
let builder = if let Some(value) = password.header {
102+
builder.header(hyper::header::AUTHORIZATION, value)
108103
} else {
109104
builder
110105
};
111106

112-
let r = builder.body(hyper::Body::empty())?;
113-
let resp = client.request(r).await?;
107+
let request = builder.body(hyper::Body::empty())?;
108+
let resp = client.request(request).await?;
114109

115110
let status = resp.status();
116111

117112
tracing::trace!("http resp.status {:?}", resp.status());
118113

119-
let msg = format!("got http response: {} {:?}", nurl, resp);
114+
let err_msg = format!("got http response: {} {:?}", refs_url, resp);
120115

121-
if status == 200 {
116+
if status == hyper::StatusCode::OK {
122117
AUTH_TIMERS
123118
.lock()?
124119
.insert((url.to_string(), auth.clone()), std::time::Instant::now());
125120
Ok(true)
126-
} else if status == 401 {
127-
tracing::warn!("resp.status == 401: {:?}", &msg);
121+
} else if status == hyper::StatusCode::UNAUTHORIZED {
122+
tracing::warn!("resp.status == 401: {:?}", &err_msg);
128123
tracing::trace!(
129124
"body: {:?}",
130125
std::str::from_utf8(&hyper::body::to_bytes(resp.into_body()).await?)
131126
);
132127
Ok(false)
133128
} else {
134-
return Err(josh::josh_error(&msg));
129+
return Err(josh::josh_error(&err_msg));
135130
}
136131
}
137132

138133
pub fn strip_auth(
139134
req: hyper::Request<hyper::Body>,
140135
) -> josh::JoshResult<(Handle, hyper::Request<hyper::Body>)> {
141136
let mut req = req;
142-
let header: Option<hyper::header::HeaderValue> = req.headers_mut().remove("authorization");
137+
let header: Option<hyper::header::HeaderValue> =
138+
req.headers_mut().remove(hyper::header::AUTHORIZATION);
143139

144140
if let Some(header) = header {
145141
let hp = Handle {

0 commit comments

Comments
 (0)