Skip to content

Commit

Permalink
Merge pull request rust-lang#2085 from kinnison/kinnison/pgp-keys
Browse files Browse the repository at this point in the history
Rework the PGP key configuration
  • Loading branch information
kinnison authored Oct 26, 2019
2 parents 38200a9 + 3ed8552 commit 3929244
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
66 changes: 57 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,41 @@ impl Display for OverrideReason {
}
}

#[derive(Debug)]
pub enum PgpPublicKey {
Builtin(&'static [u8]),
FromEnvironment(PathBuf, Vec<u8>),
FromConfiguration(PathBuf, Vec<u8>),
}

impl PgpPublicKey {
/// Retrieve the key data for this key
///
/// This key might be ASCII Armored or may not, we make no
/// guarantees.
pub fn key_data(&self) -> &[u8] {
match self {
Self::Builtin(k) => k,
Self::FromEnvironment(_, k) => &k,
Self::FromConfiguration(_, k) => &k,
}
}
}

impl Display for PgpPublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Builtin(_) => write!(f, "builtin Rust release key"),
Self::FromEnvironment(p, _) => {
write!(f, "key specified in RUST_PGP_KEY ({})", p.display())
}
Self::FromConfiguration(p, _) => {
write!(f, "key specified in configuration file ({})", p.display())
}
}
}
}

pub struct Cfg {
pub profile_override: Option<dist::Profile>,
pub rustup_dir: PathBuf,
Expand All @@ -41,7 +76,7 @@ pub struct Cfg {
pub update_hash_dir: PathBuf,
pub download_dir: PathBuf,
pub temp_cfg: temp::Cfg,
pub gpg_key: Cow<'static, str>,
pgp_keys: Vec<PgpPublicKey>,
pub toolchain_override: Option<String>,
pub env_override: Option<String>,
pub dist_root_url: String,
Expand All @@ -62,13 +97,22 @@ impl Cfg {
let update_hash_dir = rustup_dir.join("update-hashes");
let download_dir = rustup_dir.join("downloads");

// GPG key
let gpg_key =
if let Some(path) = env::var_os("RUSTUP_GPG_KEY").and_then(utils::if_not_empty) {
Cow::Owned(utils::read_file("public key", Path::new(&path))?)
} else {
Cow::Borrowed(include_str!("rust-key.gpg.ascii"))
};
// PGP keys
let mut pgp_keys: Vec<PgpPublicKey> =
vec![PgpPublicKey::Builtin(include_bytes!("rust-key.pgp.ascii"))];
if let Some(s_path) = env::var_os("RUSTUP_PGP_KEY") {
let path = PathBuf::from(s_path);
let content = utils::read_file_bytes("RUSTUP_PGP_KEY", &path)?;
pgp_keys.push(PgpPublicKey::FromEnvironment(path, content));
}
settings_file.with(|s| {
if let Some(s) = &s.pgp_keys {
let path = PathBuf::from(s);
let content = utils::read_file_bytes("PGP Key from config", &path)?;
pgp_keys.push(PgpPublicKey::FromConfiguration(path, content));
}
Ok(())
})?;

// Environment override
let env_override = env::var("RUSTUP_TOOLCHAIN")
Expand Down Expand Up @@ -105,7 +149,7 @@ impl Cfg {
update_hash_dir,
download_dir,
temp_cfg,
gpg_key,
pgp_keys,
notify_handler,
toolchain_override: None,
env_override,
Expand All @@ -122,6 +166,10 @@ impl Cfg {
Ok(cfg)
}

pub fn get_pgp_keys(&self) -> &[PgpPublicKey] {
&self.pgp_keys
}

pub fn set_profile_override(&mut self, profile: dist::Profile) {
self.profile_override = Some(profile);
}
Expand Down
File renamed without changes.
7 changes: 7 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub struct Settings {
pub default_toolchain: Option<String>,
pub profile: Option<String>,
pub overrides: BTreeMap<String, String>,
pub pgp_keys: Option<String>,
}

impl Default for Settings {
Expand All @@ -79,6 +80,7 @@ impl Default for Settings {
default_toolchain: None,
profile: Some("default".to_owned()),
overrides: BTreeMap::new(),
pgp_keys: None,
}
}
}
Expand Down Expand Up @@ -142,6 +144,7 @@ impl Settings {
default_toolchain: get_opt_string(&mut table, "default_toolchain", path)?,
profile: get_opt_string(&mut table, "profile", path)?,
overrides: Self::table_to_overrides(&mut table, path)?,
pgp_keys: get_opt_string(&mut table, "pgp_keys", path)?,
})
}
pub fn into_toml(self) -> toml::value::Table {
Expand All @@ -161,6 +164,10 @@ impl Settings {
result.insert("profile".to_owned(), toml::Value::String(v));
}

if let Some(v) = self.pgp_keys {
result.insert("pgp_keys".to_owned(), toml::Value::String(v));
}

let overrides = Self::overrides_to_table(self.overrides);
result.insert("overrides".to_owned(), toml::Value::Table(overrides));

Expand Down
7 changes: 7 additions & 0 deletions src/utils/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,13 @@ where
})
}

pub fn read_file_bytes(name: &'static str, path: &Path) -> Result<Vec<u8>> {
fs::read(path).chain_err(|| ErrorKind::ReadingFile {
name,
path: PathBuf::from(path),
})
}

pub fn read_file(name: &'static str, path: &Path) -> Result<String> {
fs::read_to_string(path).chain_err(|| ErrorKind::ReadingFile {
name,
Expand Down

0 comments on commit 3929244

Please sign in to comment.