Skip to content

config::load_credentials: Support credentials script on Unix #5899

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 30 additions & 9 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ use std::fs::{self, File};
use std::io::prelude::*;
use std::io::SeekFrom;
use std::mem;
#[cfg(unix)] use std::os::unix::fs::MetadataExt;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::str::FromStr;
use std::sync::{Once, ONCE_INIT};
use std::time::Instant;
Expand Down Expand Up @@ -659,18 +661,25 @@ impl Config {
fn load_credentials(&self, cfg: &mut ConfigValue) -> CargoResult<()> {
let home_path = self.home_path.clone().into_path_unlocked();
let credentials = home_path.join("credentials");
if fs::metadata(&credentials).is_err() {
let metadata = fs::metadata(&credentials);
if metadata.is_err() {
return Ok(());
}

let metadata = metadata?;
let mut contents = String::new();
let mut file = File::open(&credentials)?;
file.read_to_string(&mut contents).chain_err(|| {
format!(
"failed to read configuration file `{}`",
credentials.display()
)
})?;

if ! is_executable(metadata) {
let mut file = File::open(&credentials)?;
file.read_to_string(&mut contents).chain_err(|| {
format!(
"failed to read configuration file `{}`",
credentials.display()
)
})?;
} else {
let output = Command::new(credentials.clone()).output()?;
contents = String::from_utf8(output.stdout)?;
}

let toml = cargo_toml::parse(&contents, &credentials, self).chain_err(|| {
format!(
Expand Down Expand Up @@ -787,6 +796,18 @@ impl Config {
}
}

/// Check whether the credentials file is executable
#[cfg(unix)]
fn is_executable(m: fs::Metadata) -> bool {
return m.mode() & 0o100 == 0;
}

#[cfg(not(unix))]
fn is_executable(m: fs::Metadata) -> bool {
return false;
}


/// A segment of a config key.
///
/// Config keys are split on dots for regular keys, or underscores for
Expand Down