Skip to content

Commit 3929244

Browse files
authored
Merge pull request #2085 from kinnison/kinnison/pgp-keys
Rework the PGP key configuration
2 parents 38200a9 + 3ed8552 commit 3929244

File tree

4 files changed

+71
-9
lines changed

4 files changed

+71
-9
lines changed

src/config.rs

Lines changed: 57 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,41 @@ impl Display for OverrideReason {
3333
}
3434
}
3535

36+
#[derive(Debug)]
37+
pub enum PgpPublicKey {
38+
Builtin(&'static [u8]),
39+
FromEnvironment(PathBuf, Vec<u8>),
40+
FromConfiguration(PathBuf, Vec<u8>),
41+
}
42+
43+
impl PgpPublicKey {
44+
/// Retrieve the key data for this key
45+
///
46+
/// This key might be ASCII Armored or may not, we make no
47+
/// guarantees.
48+
pub fn key_data(&self) -> &[u8] {
49+
match self {
50+
Self::Builtin(k) => k,
51+
Self::FromEnvironment(_, k) => &k,
52+
Self::FromConfiguration(_, k) => &k,
53+
}
54+
}
55+
}
56+
57+
impl Display for PgpPublicKey {
58+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
59+
match self {
60+
Self::Builtin(_) => write!(f, "builtin Rust release key"),
61+
Self::FromEnvironment(p, _) => {
62+
write!(f, "key specified in RUST_PGP_KEY ({})", p.display())
63+
}
64+
Self::FromConfiguration(p, _) => {
65+
write!(f, "key specified in configuration file ({})", p.display())
66+
}
67+
}
68+
}
69+
}
70+
3671
pub struct Cfg {
3772
pub profile_override: Option<dist::Profile>,
3873
pub rustup_dir: PathBuf,
@@ -41,7 +76,7 @@ pub struct Cfg {
4176
pub update_hash_dir: PathBuf,
4277
pub download_dir: PathBuf,
4378
pub temp_cfg: temp::Cfg,
44-
pub gpg_key: Cow<'static, str>,
79+
pgp_keys: Vec<PgpPublicKey>,
4580
pub toolchain_override: Option<String>,
4681
pub env_override: Option<String>,
4782
pub dist_root_url: String,
@@ -62,13 +97,22 @@ impl Cfg {
6297
let update_hash_dir = rustup_dir.join("update-hashes");
6398
let download_dir = rustup_dir.join("downloads");
6499

65-
// GPG key
66-
let gpg_key =
67-
if let Some(path) = env::var_os("RUSTUP_GPG_KEY").and_then(utils::if_not_empty) {
68-
Cow::Owned(utils::read_file("public key", Path::new(&path))?)
69-
} else {
70-
Cow::Borrowed(include_str!("rust-key.gpg.ascii"))
71-
};
100+
// PGP keys
101+
let mut pgp_keys: Vec<PgpPublicKey> =
102+
vec![PgpPublicKey::Builtin(include_bytes!("rust-key.pgp.ascii"))];
103+
if let Some(s_path) = env::var_os("RUSTUP_PGP_KEY") {
104+
let path = PathBuf::from(s_path);
105+
let content = utils::read_file_bytes("RUSTUP_PGP_KEY", &path)?;
106+
pgp_keys.push(PgpPublicKey::FromEnvironment(path, content));
107+
}
108+
settings_file.with(|s| {
109+
if let Some(s) = &s.pgp_keys {
110+
let path = PathBuf::from(s);
111+
let content = utils::read_file_bytes("PGP Key from config", &path)?;
112+
pgp_keys.push(PgpPublicKey::FromConfiguration(path, content));
113+
}
114+
Ok(())
115+
})?;
72116

73117
// Environment override
74118
let env_override = env::var("RUSTUP_TOOLCHAIN")
@@ -105,7 +149,7 @@ impl Cfg {
105149
update_hash_dir,
106150
download_dir,
107151
temp_cfg,
108-
gpg_key,
152+
pgp_keys,
109153
notify_handler,
110154
toolchain_override: None,
111155
env_override,
@@ -122,6 +166,10 @@ impl Cfg {
122166
Ok(cfg)
123167
}
124168

169+
pub fn get_pgp_keys(&self) -> &[PgpPublicKey] {
170+
&self.pgp_keys
171+
}
172+
125173
pub fn set_profile_override(&mut self, profile: dist::Profile) {
126174
self.profile_override = Some(profile);
127175
}
File renamed without changes.

src/settings.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ pub struct Settings {
6969
pub default_toolchain: Option<String>,
7070
pub profile: Option<String>,
7171
pub overrides: BTreeMap<String, String>,
72+
pub pgp_keys: Option<String>,
7273
}
7374

7475
impl Default for Settings {
@@ -79,6 +80,7 @@ impl Default for Settings {
7980
default_toolchain: None,
8081
profile: Some("default".to_owned()),
8182
overrides: BTreeMap::new(),
83+
pgp_keys: None,
8284
}
8385
}
8486
}
@@ -142,6 +144,7 @@ impl Settings {
142144
default_toolchain: get_opt_string(&mut table, "default_toolchain", path)?,
143145
profile: get_opt_string(&mut table, "profile", path)?,
144146
overrides: Self::table_to_overrides(&mut table, path)?,
147+
pgp_keys: get_opt_string(&mut table, "pgp_keys", path)?,
145148
})
146149
}
147150
pub fn into_toml(self) -> toml::value::Table {
@@ -161,6 +164,10 @@ impl Settings {
161164
result.insert("profile".to_owned(), toml::Value::String(v));
162165
}
163166

167+
if let Some(v) = self.pgp_keys {
168+
result.insert("pgp_keys".to_owned(), toml::Value::String(v));
169+
}
170+
164171
let overrides = Self::overrides_to_table(self.overrides);
165172
result.insert("overrides".to_owned(), toml::Value::Table(overrides));
166173

src/utils/utils.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,13 @@ where
3737
})
3838
}
3939

40+
pub fn read_file_bytes(name: &'static str, path: &Path) -> Result<Vec<u8>> {
41+
fs::read(path).chain_err(|| ErrorKind::ReadingFile {
42+
name,
43+
path: PathBuf::from(path),
44+
})
45+
}
46+
4047
pub fn read_file(name: &'static str, path: &Path) -> Result<String> {
4148
fs::read_to_string(path).chain_err(|| ErrorKind::ReadingFile {
4249
name,

0 commit comments

Comments
 (0)