Skip to content

Commit e72ae2a

Browse files
committed
tuftool: Remove KeySource enum, use tough-ssm and KeySource trait
This is a decent set of changes to tuftool. It fully removes the KeySource enum in source.rs and instead uses the KeySource trait from tough. This commit also removes all SSM related code in favor of the tough-ssm crate (which is a copy of this code). It also removes the deref.rs file, as it is no longer need because these features have stabilized in upstream Rust. See: rust-lang/rust#50264 and rust-lang/rust#64708
1 parent 3ea884e commit e72ae2a

File tree

12 files changed

+89
-322
lines changed

12 files changed

+89
-322
lines changed

tough-ssm/src/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ impl KeySource for SsmKeySource {
7272
&self,
7373
) -> std::result::Result<Box<dyn Sign>, Box<dyn std::error::Error + Send + Sync + 'static>>
7474
{
75-
//fn as_sign(&self) -> std::result::Result<Box<dyn Sign>, Self::Error> {
7675
let data = &self.read()?;
77-
//let sign: Box<dyn Sign> = Box::new(parse_keypair(&data).context(error::KeyPairParse)?);
7876
let sign = Box::new(parse_keypair(&data).context(error::KeyPairParse)?);
7977
Ok(sign)
8078
}

tuftool/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ tempfile = "3.1.0"
3434
url = "2.1.0"
3535
walkdir = "2.2.9"
3636
tough = { version = "0.5.0", path = "../tough", features = ["http"] }
37+
tough-ssm = { version = "0.1.0", path = "../tough-ssm" }
3738
tokio = "0.2.13"
3839

3940
[dev-dependencies]

tuftool/src/create.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::error::{self, Result};
66
use crate::key::RootKeys;
77
use crate::metadata;
88
use crate::root_digest::RootDigest;
9-
use crate::source::KeySource;
9+
use crate::source::parse_key_source;
1010
use chrono::{DateTime, Utc};
1111
use maplit::hashmap;
1212
use rayon::prelude::*;
@@ -20,6 +20,7 @@ use std::io::Read;
2020
use std::num::{NonZeroU64, NonZeroUsize};
2121
use std::path::{Path, PathBuf};
2222
use structopt::StructOpt;
23+
use tough::key_source::KeySource;
2324
use tough::schema::{
2425
decoded::Decoded, Hashes, Role, Snapshot, SnapshotMeta, Target, Targets, Timestamp,
2526
TimestampMeta,
@@ -37,8 +38,8 @@ pub(crate) struct CreateArgs {
3738
jobs: Option<NonZeroUsize>,
3839

3940
/// Key files to sign with
40-
#[structopt(short = "k", long = "key", required = true)]
41-
keys: Vec<KeySource>,
41+
#[structopt(short = "k", long = "key", required = true, parse(try_from_str = parse_key_source))]
42+
keys: Vec<Box<dyn KeySource>>,
4243

4344
/// Version of snapshot.json file
4445
#[structopt(long = "snapshot-version")]

tuftool/src/deref.rs

-18
This file was deleted.

tuftool/src/error.rs

+12-68
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
use snafu::{Backtrace, Snafu};
77
use std::path::PathBuf;
88

9-
#[cfg(any(feature = "rusoto-native-tls", feature = "rusoto-rustls"))]
10-
use crate::deref::OptionDeref;
11-
129
pub(crate) type Result<T> = std::result::Result<T, Error>;
1310

1411
#[derive(Debug, Snafu)]
@@ -124,14 +121,6 @@ pub(crate) enum Error {
124121
backtrace: Backtrace,
125122
},
126123

127-
#[snafu(display("{}: {}", path.display(), source))]
128-
Key {
129-
path: PathBuf,
130-
#[snafu(source(from(Error, Box::new)))]
131-
#[snafu(backtrace)]
132-
source: Box<Self>,
133-
},
134-
135124
#[snafu(display("Duplicate key ID: {}", key_id))]
136125
KeyDuplicate {
137126
key_id: String,
@@ -150,6 +139,12 @@ pub(crate) enum Error {
150139
backtrace: Backtrace,
151140
},
152141

142+
#[snafu(display("Unable to parse keypair: {}", source))]
143+
KeyPairFromKeySource {
144+
source: Box<dyn std::error::Error + Send + Sync + 'static>,
145+
backtrace: Backtrace,
146+
},
147+
153148
#[snafu(display("Unable to match any of the provided keys with root.json"))]
154149
KeysNotFoundInRoot { backtrace: Backtrace },
155150

@@ -200,28 +195,6 @@ pub(crate) enum Error {
200195
backtrace: Backtrace,
201196
},
202197

203-
#[cfg(any(feature = "rusoto-native-tls", feature = "rusoto-rustls"))]
204-
#[snafu(display("Error creating AWS credentials provider: {}", source))]
205-
RusotoCreds {
206-
source: rusoto_credential::CredentialsError,
207-
backtrace: Backtrace,
208-
},
209-
210-
#[cfg(any(feature = "rusoto-native-tls", feature = "rusoto-rustls"))]
211-
#[snafu(display("Unknown AWS region \"{}\": {}", region, source))]
212-
RusotoRegion {
213-
region: String,
214-
source: rusoto_core::region::ParseRegionError,
215-
backtrace: Backtrace,
216-
},
217-
218-
#[cfg(any(feature = "rusoto-native-tls", feature = "rusoto-rustls"))]
219-
#[snafu(display("Error creating AWS request dispatcher: {}", source))]
220-
RusotoTls {
221-
source: rusoto_core::request::TlsError,
222-
backtrace: Backtrace,
223-
},
224-
225198
#[snafu(display("Failed to sign message"))]
226199
Sign {
227200
source: tough::error::Error,
@@ -234,41 +207,6 @@ pub(crate) enum Error {
234207
backtrace: Backtrace,
235208
},
236209

237-
#[cfg(any(feature = "rusoto-native-tls", feature = "rusoto-rustls"))]
238-
#[snafu(display(
239-
"Failed to get aws-ssm://{}{}: {}",
240-
profile.deref_shim().unwrap_or(""),
241-
parameter_name,
242-
source,
243-
))]
244-
SsmGetParameter {
245-
profile: Option<String>,
246-
parameter_name: String,
247-
source: rusoto_core::RusotoError<rusoto_ssm::GetParameterError>,
248-
backtrace: Backtrace,
249-
},
250-
251-
#[cfg(any(feature = "rusoto-native-tls", feature = "rusoto-rustls"))]
252-
#[snafu(display(
253-
"Failed to put aws-ssm://{}{}: {}",
254-
profile.deref_shim().unwrap_or(""),
255-
parameter_name,
256-
source,
257-
))]
258-
SsmPutParameter {
259-
profile: Option<String>,
260-
parameter_name: String,
261-
source: rusoto_core::RusotoError<rusoto_ssm::PutParameterError>,
262-
backtrace: Backtrace,
263-
},
264-
265-
#[cfg(any(feature = "rusoto-native-tls", feature = "rusoto-rustls"))]
266-
#[snafu(display("Missing field in SSM response: {}", field))]
267-
SsmMissingField {
268-
field: &'static str,
269-
backtrace: Backtrace,
270-
},
271-
272210
#[snafu(display("Target not found: {}", target))]
273211
TargetNotFound {
274212
target: String,
@@ -309,6 +247,12 @@ pub(crate) enum Error {
309247
backtrace: Backtrace,
310248
},
311249

250+
#[snafu(display("Failed write: {}", source))]
251+
WriteKeySource {
252+
source: Box<dyn std::error::Error + Send + Sync + 'static>,
253+
backtrace: Backtrace,
254+
},
255+
312256
#[snafu(display("Failed writing target data to disk: {}", source))]
313257
WriteTarget {
314258
source: std::io::Error,

tuftool/src/main.rs

-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313

1414
mod create;
1515
mod datetime;
16-
mod deref;
1716
mod download;
1817
mod error;
1918
mod key;
@@ -23,7 +22,6 @@ mod root;
2322
mod root_digest;
2423
mod sign;
2524
mod source;
26-
mod ssm;
2725

2826
use crate::error::Result;
2927
use snafu::{ErrorCompat, OptionExt, ResultExt};

tuftool/src/refresh.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::datetime::parse_datetime;
55
use crate::error::{self, Result};
66
use crate::metadata;
77
use crate::root_digest::RootDigest;
8-
use crate::source::KeySource;
8+
use crate::source::parse_key_source;
99
use chrono::{DateTime, Utc};
1010
use maplit::hashmap;
1111
use ring::rand::SystemRandom;
@@ -15,6 +15,7 @@ use std::fs::File;
1515
use std::num::{NonZeroU64, NonZeroUsize};
1616
use std::path::PathBuf;
1717
use structopt::StructOpt;
18+
use tough::key_source::KeySource;
1819
use tough::schema::{Hashes, Snapshot, SnapshotMeta, Targets, Timestamp, TimestampMeta};
1920
use tough::{FilesystemTransport, HttpTransport, Limits, Repository, Transport};
2021
use url::Url;
@@ -42,8 +43,8 @@ pub(crate) struct RefreshArgs {
4243
jobs: Option<NonZeroUsize>,
4344

4445
/// Key files to sign with
45-
#[structopt(short = "k", long = "key", required = true)]
46-
keys: Vec<KeySource>,
46+
#[structopt(short = "k", long = "key", required = true, parse(try_from_str = parse_key_source))]
47+
keys: Vec<Box<dyn KeySource>>,
4748

4849
/// Version of snapshot.json file
4950
#[structopt(long = "snapshot-version")]

tuftool/src/root.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
use crate::datetime::parse_datetime;
55
use crate::error::{self, Result};
6-
use crate::source::KeySource;
6+
use crate::source::parse_key_source;
77
use crate::{load_file, write_file};
88
use chrono::{DateTime, Timelike, Utc};
99
use maplit::hashmap;
@@ -12,6 +12,7 @@ use std::collections::HashMap;
1212
use std::num::NonZeroU64;
1313
use std::path::PathBuf;
1414
use structopt::StructOpt;
15+
use tough::key_source::KeySource;
1516
use tough::schema::decoded::{Decoded, Hex};
1617
use tough::schema::{key::Key, RoleKeys, RoleType, Root, Signed};
1718
use tough::sign::{parse_keypair, Sign};
@@ -51,7 +52,8 @@ pub(crate) enum Command {
5152
/// Path to root.json
5253
path: PathBuf,
5354
/// The new key
54-
key_path: KeySource,
55+
#[structopt(parse(try_from_str = parse_key_source))]
56+
key_path: Box<dyn KeySource>,
5557
/// The role to add the key to
5658
#[structopt(short = "r", long = "role")]
5759
roles: Vec<RoleType>,
@@ -71,7 +73,8 @@ pub(crate) enum Command {
7173
/// Path to root.json
7274
path: PathBuf,
7375
/// Where to write the new key
74-
key_path: KeySource,
76+
#[structopt(parse(try_from_str = parse_key_source))]
77+
key_path: Box<dyn KeySource>,
7578
/// Bit length of new key
7679
#[structopt(short = "b", long = "bits", default_value = "2048")]
7780
bits: u16,
@@ -181,9 +184,13 @@ impl Command {
181184
write_file(path, &root)
182185
}
183186

184-
fn add_key(path: &PathBuf, roles: &[RoleType], key_path: &KeySource) -> Result<()> {
187+
#[allow(clippy::borrowed_box)]
188+
fn add_key(path: &PathBuf, roles: &[RoleType], key_path: &Box<dyn KeySource>) -> Result<()> {
185189
let mut root: Signed<Root> = load_file(path)?;
186-
let key_pair = key_path.as_public_key()?;
190+
let key_pair = key_path
191+
.as_sign()
192+
.context(error::KeyPairFromKeySource)?
193+
.tuf_key();
187194
let key_id = hex::encode(add_key(&mut root.signed, roles, key_pair)?);
188195
clear_sigs(&mut root);
189196
println!("{}", key_id);
@@ -214,10 +221,11 @@ impl Command {
214221
write_file(path, &root)
215222
}
216223

224+
#[allow(clippy::borrowed_box)]
217225
fn gen_rsa_key(
218226
path: &PathBuf,
219227
roles: &[RoleType],
220-
key_path: &KeySource,
228+
key_path: &Box<dyn KeySource>,
221229
bits: u16,
222230
exponent: u32,
223231
) -> Result<()> {
@@ -247,7 +255,9 @@ impl Command {
247255

248256
let key_pair = parse_keypair(stdout.as_bytes()).context(error::KeyPairParse)?;
249257
let key_id = hex::encode(add_key(&mut root.signed, roles, key_pair.tuf_key())?);
250-
key_path.write(&stdout, &key_id)?;
258+
key_path
259+
.write(&stdout, &key_id)
260+
.context(error::WriteKeySource)?;
251261
clear_sigs(&mut root);
252262
println!("{}", key_id);
253263
write_file(path, &root)

tuftool/src/root_digest.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::error;
22
use crate::error::Result;
33
use crate::key::RootKeys;
4-
use crate::source::KeySource;
54
use ring::digest::{SHA256, SHA256_OUTPUT_LEN};
65
use snafu::ensure;
76
use snafu::ResultExt;
87
use std::collections::HashMap;
98
use std::path::PathBuf;
9+
use tough::key_source::KeySource;
1010
use tough::schema::{Root, Signed};
1111

1212
/// Represents a loaded root.json file along with its sha256 digest and size in bytes
@@ -56,10 +56,10 @@ impl RootDigest {
5656
///
5757
/// * An error can occur for io reasons
5858
///
59-
pub(crate) fn load_keys(&self, keys: &[KeySource]) -> Result<RootKeys> {
59+
pub(crate) fn load_keys(&self, keys: &[Box<dyn KeySource>]) -> Result<RootKeys> {
6060
let mut map = HashMap::new();
6161
for source in keys {
62-
let key_pair = source.as_sign()?;
62+
let key_pair = source.as_sign().context(error::KeyPairFromKeySource)?;
6363
if let Some((keyid, _)) = self
6464
.root
6565
.keys

tuftool/src/sign.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44
use crate::error::Result;
55
use crate::key::sign_metadata;
66
use crate::root_digest::RootDigest;
7-
use crate::source::KeySource;
7+
use crate::source::parse_key_source;
88
use crate::{load_file, write_file};
99
use ring::rand::SystemRandom;
1010
use serde::{Deserialize, Serialize};
1111
use std::collections::HashMap;
1212
use std::path::PathBuf;
1313
use structopt::StructOpt;
14+
use tough::key_source::KeySource;
1415
use tough::schema::{RoleType, Signed};
1516

1617
#[derive(Debug, StructOpt)]
@@ -20,8 +21,8 @@ pub(crate) struct SignArgs {
2021
root: PathBuf,
2122

2223
/// Key files to sign with
23-
#[structopt(short = "k", long = "key")]
24-
keys: Vec<KeySource>,
24+
#[structopt(short = "k", long = "key", parse(try_from_str = parse_key_source))]
25+
keys: Vec<Box<dyn KeySource>>,
2526

2627
/// Metadata file to sign
2728
metadata_file: PathBuf,

0 commit comments

Comments
 (0)