Skip to content

Commit 85423ec

Browse files
committed
git: Extract write_temporary_ssh_key() function
1 parent 910ac64 commit 85423ec

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

src/git.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::collections::HashMap;
2+
use std::io::Write;
23
use std::path::{Path, PathBuf};
34

45
use swirl::PerformError;
@@ -48,6 +49,25 @@ impl Credentials {
4849
}
4950
}
5051
}
52+
53+
pub fn write_temporary_ssh_key(&self) -> Result<tempfile::TempPath, PerformError> {
54+
let key = match self {
55+
Credentials::Ssh { key } => key,
56+
_ => return Err("SSH key not available".into()),
57+
};
58+
59+
// When running on production, ensure the file is created in tmpfs and not persisted to disk
60+
#[cfg(target_os = "linux")]
61+
let mut temp_key_file = tempfile::Builder::new().tempfile_in("/dev/shm")?;
62+
63+
// For other platforms, default to std::env::tempdir()
64+
#[cfg(not(target_os = "linux"))]
65+
let mut temp_key_file = tempfile::Builder::new().tempfile()?;
66+
67+
temp_key_file.write_all(key.as_bytes())?;
68+
69+
Ok(temp_key_file.into_temp_path())
70+
}
5171
}
5272

5373
#[derive(Serialize, Deserialize, Debug)]

src/worker/git.rs

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
use crate::background_jobs::Environment;
2-
use crate::git::{Crate, Credentials};
2+
use crate::git::Crate;
33
use crate::schema;
44
use anyhow::Context;
55
use chrono::Utc;
66
use diesel::prelude::*;
77
use std::fs::{self, OpenOptions};
8-
use std::io::prelude::*;
98
use swirl::PerformError;
109

1110
#[swirl::background_job]
@@ -99,23 +98,7 @@ pub fn squash_index(env: &Environment) -> Result<(), PerformError> {
9998

10099
// Shell out to git because libgit2 does not currently support push leases
101100

102-
let key = match &repo.credentials {
103-
Credentials::Ssh { key } => key,
104-
Credentials::Http { .. } => {
105-
return Err(String::from("squash_index: Password auth not supported").into())
106-
}
107-
_ => return Err(String::from("squash_index: Could not determine credentials").into()),
108-
};
109-
110-
// When running on production, ensure the file is created in tmpfs and not persisted to disk
111-
#[cfg(target_os = "linux")]
112-
let mut temp_key_file = tempfile::Builder::new().tempfile_in("/dev/shm")?;
113-
114-
// For other platforms, default to std::env::tempdir()
115-
#[cfg(not(target_os = "linux"))]
116-
let mut temp_key_file = tempfile::Builder::new().tempfile()?;
117-
118-
temp_key_file.write_all(key.as_bytes())?;
101+
let temp_key_path = repo.credentials.write_temporary_ssh_key()?;
119102

120103
let checkout_path = repo.checkout_path.path();
121104
let output = std::process::Command::new("git")
@@ -124,7 +107,7 @@ pub fn squash_index(env: &Environment) -> Result<(), PerformError> {
124107
"GIT_SSH_COMMAND",
125108
format!(
126109
"ssh -o StrictHostKeyChecking=accept-new -i {}",
127-
temp_key_file.path().display()
110+
temp_key_path.display()
128111
),
129112
)
130113
.args(&[

0 commit comments

Comments
 (0)