-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support Direct Import of Certificates via URL Feature req #3350
Adding the import-cert-url command to import a cert with a url
- Loading branch information
1 parent
3d58409
commit 17d41b8
Showing
6 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
use anyhow::{anyhow, Result}; | ||
use std::path::{Path, PathBuf}; | ||
use url::Url; | ||
use std::fs; | ||
|
||
pub fn download_cert_if_url(cert_source: &PathBuf, temp_dir: &Path) -> Result<PathBuf> { | ||
let source_str = cert_source.to_string_lossy(); | ||
if let Ok(url) = Url::parse(&source_str) { | ||
if url.scheme() == "http" || url.scheme() == "https" { | ||
download_cert(&source_str, temp_dir) | ||
} else { | ||
Ok(cert_source.clone()) | ||
} | ||
} else { | ||
Ok(cert_source.clone()) | ||
} | ||
} | ||
|
||
fn download_cert(url: &str, temp_dir: &Path) -> Result<PathBuf> { | ||
// Create temp directory if it doesn't exist | ||
fs::create_dir_all(temp_dir)?; | ||
|
||
// Generate a unique filename based on the URL | ||
let file_name = url | ||
.split('/') | ||
.last() | ||
.ok_or_else(|| anyhow!("Invalid URL format"))?; | ||
let temp_path = temp_dir.join(file_name); | ||
|
||
// Download the certificate | ||
let response = reqwest::blocking::get(url)?; | ||
if !response.status().is_success() { | ||
return Err(anyhow!( | ||
"Failed to download certificate. Status: {}", | ||
response.status() | ||
)); | ||
} | ||
|
||
// Save the certificate to a temporary file | ||
let content = response.bytes()?; | ||
fs::write(&temp_path, content)?; | ||
|
||
Ok(temp_path) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters