Skip to content

Commit 77fcc8d

Browse files
author
Anish Cheraku
committed
code review 5
1 parent 97947f5 commit 77fcc8d

File tree

3 files changed

+59
-53
lines changed

3 files changed

+59
-53
lines changed

sources/api/apiclient/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ tokio-tungstenite = { workspace = true, features = ["connect"] }
5050
toml.workspace = true
5151
unindent.workspace = true
5252
url.workspace = true
53-
tempfile.workspace = true
5453

5554
[build-dependencies]
5655
generate-readme.workspace = true
5756

5857
[dev-dependencies]
58+
tempfile.workspace = true
5959
test-case.workspace = true

sources/api/apiclient/src/apply.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,13 @@ pub struct SettingsInput {
7474
impl SettingsInput {
7575
pub(crate) fn new(input: impl Into<String>) -> Self {
7676
let input = input.into();
77-
let parsed_url = Url::parse(&input).ok();
77+
let parsed_url = match Url::parse(&input) {
78+
Ok(url) => Some(url),
79+
Err(err) => {
80+
log::debug!("URL parse failed for '{}': {}", input, err);
81+
None
82+
}
83+
};
7884
SettingsInput { input, parsed_url }
7985
}
8086
}

sources/api/apiclient/src/uri_resolver.rs

Lines changed: 51 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ use std::any::Any;
1414
use std::convert::TryFrom;
1515
use std::path::PathBuf;
1616
use tokio::io::AsyncReadExt;
17-
const MAX_SIZE_BYTES: u64 = 100 * 1024 * 1024; /// Maximum allowed object size for S3 and HTTP(S) resolvers (100 MiB).
17+
const MAX_SIZE_BYTES: u64 = 100 * 1024 * 1024;
18+
/// Maximum allowed object size for S3 and HTTP(S) resolvers (100 MiB).
1819
1920
/// Anything that can fetch itself as a UTF-8 `String`.
2021
#[async_trait]
@@ -26,7 +27,7 @@ pub trait UriResolver: Any {
2627
// A minimal AWS ARN parser for our resolvers.
2728
struct Arn {
2829
service: String,
29-
region: String,
30+
region: String,
3031
parts: i8,
3132
}
3233

@@ -49,24 +50,20 @@ impl Arn {
4950
parts.len() > 4,
5051
InvalidArnFormatSnafu {
5152
input_source: input.to_string(),
52-
reason: format!(
53-
"expected at least 4 ':' separators, found {}",
54-
parts.len()
55-
),
53+
reason: format!("expected at least 4 ':' separators, found {}", parts.len()),
5654
}
5755
);
5856
let service = parts[2];
59-
let region = parts[3];
57+
let region = parts[3];
6058

6159
Ok(Arn {
62-
service: service.to_string(),
63-
region: region.to_string(),
64-
parts: parts.len() as i8,
60+
service: service.to_string(),
61+
region: region.to_string(),
62+
parts: parts.len() as i8,
6563
})
6664
}
6765
}
6866

69-
7067
/// Uri Resolver that reads from standard input.
7168
///
7269
/// This resolver accepts exactly "-" as its URI and will read all of stdin
@@ -111,15 +108,11 @@ impl TryFrom<&SettingsInput> for FileUri {
111108
fn try_from(input: &SettingsInput) -> ResolverResult<Self> {
112109
use resolver_error::*;
113110

114-
let url = {
115-
if let None = &input.parsed_url {
116-
log::debug!("Failed to parse HTTP URI '{}'", input.input);
117-
}
118-
input.parsed_url.clone()
119-
}.context(FileUriSnafu {
120-
input_source: input.input.as_str(),
111+
let url = input.parsed_url.clone().context(FileUriSnafu {
112+
input_source: input.input.clone(),
121113
})?;
122114

115+
// only accept file://
123116
ensure!(
124117
url.scheme() == "file",
125118
FileUriSnafu {
@@ -161,12 +154,7 @@ impl TryFrom<&SettingsInput> for HttpUri {
161154
type Error = ResolverError;
162155
fn try_from(input: &SettingsInput) -> ResolverResult<Self> {
163156
use resolver_error::*;
164-
let url = {
165-
if let None = &input.parsed_url {
166-
log::debug!("Failed to parse HTTP URI '{}'", input.input);
167-
}
168-
input.parsed_url.clone()
169-
}.context(InvalidHTTPUriSnafu {
157+
let url = input.parsed_url.clone().context(InvalidHTTPUriSnafu {
170158
input_source: input.input.clone(),
171159
})?;
172160
ensure!(
@@ -190,19 +178,24 @@ impl UriResolver for HttpUri {
190178
})?;
191179

192180
// 2) check status
193-
ensure!(resp.status().is_success(), HttpStatusSnafu {
181+
ensure!(
182+
resp.status().is_success(),
183+
HttpStatusSnafu {
194184
uri: self.url.to_string(),
195185
status: resp.status(),
196186
}
197187
);
198188

199189
// 3) check content length if available
200190
if let Some(content_length) = resp.content_length() {
201-
ensure!(content_length < MAX_SIZE_BYTES, HttpObjectTooLargeSnafu {
202-
size: content_length,
203-
max_size: MAX_SIZE_BYTES,
204-
uri: self.url.to_string(),
205-
});
191+
ensure!(
192+
content_length < MAX_SIZE_BYTES,
193+
HttpObjectTooLargeSnafu {
194+
size: content_length,
195+
max_size: MAX_SIZE_BYTES,
196+
uri: self.url.to_string(),
197+
}
198+
);
206199
}
207200

208201
// 4) read the body as bytes first to check size
@@ -310,8 +303,6 @@ impl UriResolver for S3Uri {
310303
String::from_utf8(bytes.to_vec()).context(Utf8DecodeSnafu {
311304
uri: format!("s3://{}/{}", self.bucket, self.key),
312305
})
313-
314-
315306
}
316307
}
317308

@@ -340,22 +331,21 @@ impl TryFrom<&SettingsInput> for SecretsManagerArn {
340331
arn.parts == 7,
341332
InvalidArnFormatSnafu {
342333
input_source: input.input.clone(),
343-
reason: format!(
344-
"expected 6 ':' separators (7 parts), found {}",
345-
arn.parts
346-
),
334+
reason: format!("expected 6 ':' separators (7 parts), found {}", arn.parts),
347335
}
348336
);
349337

350338
//enforce service name
351339
ensure!(
352340
arn.service == "secretsmanager",
353-
SecretsManagerArnSnafu { input_source: input.input.clone() }
341+
SecretsManagerArnSnafu {
342+
input_source: input.input.clone()
343+
}
354344
);
355345

356346
// 3) Construct
357347
Ok(SecretsManagerArn {
358-
region: arn.region,
348+
region: arn.region,
359349
full_arn: input.input.to_string(), // AWS SDK will accept the full ARN as the secret_id
360350
})
361351
}
@@ -478,23 +468,22 @@ impl TryFrom<&SettingsInput> for SsmArn {
478468
arn.parts == 6,
479469
InvalidArnFormatSnafu {
480470
input_source: input.input.clone(),
481-
reason: format!(
482-
"expected 5 ':' separators (6 parts), found {}",
483-
arn.parts
484-
),
471+
reason: format!("expected 5 ':' separators (6 parts), found {}", arn.parts),
485472
}
486473
);
487474

488475
// enforce service name
489476
ensure!(
490477
arn.service == "ssm",
491-
SsmArnSnafu { input_source: input.input.clone() }
478+
SsmArnSnafu {
479+
input_source: input.input.clone()
480+
}
492481
);
493482

494483
// 3) Construct
495484
Ok(SsmArn {
496-
region: arn.region,
497-
full_arn: input.input.to_string(),
485+
region: arn.region,
486+
full_arn: input.input.to_string(),
498487
})
499488
}
500489
}
@@ -598,7 +587,6 @@ impl UriResolver for SsmUri {
598587
#[derive(Debug, Snafu)]
599588
#[snafu(module)]
600589
pub enum ResolverError {
601-
602590
//Arn
603591
#[snafu(display("Invalid ARN '{}': {}", input_source, reason))]
604592
InvalidArnFormat {
@@ -752,7 +740,7 @@ pub enum ResolverError {
752740
#[snafu(display("SSM parameter '{}' did not return a string value", parameter_name))]
753741
SsmParameterMissing { parameter_name: String },
754742

755-
//UTF8Decode
743+
//UTF8Decode
756744
#[snafu(display("Failed to decode HTTP response as UTF-8 for {uri}"))]
757745
Utf8Decode {
758746
source: std::string::FromUtf8Error,
@@ -810,7 +798,10 @@ mod parse_uri_tests {
810798
#[test_case("--"; "double_dash")]
811799
fn parse_stdin_fail(input: &str) {
812800
let settings = SettingsInput::new(input);
813-
assert!(StdinUri::try_from(&settings).is_err(), "only `-` should parse as stdin");
801+
assert!(
802+
StdinUri::try_from(&settings).is_err(),
803+
"only `-` should parse as stdin"
804+
);
814805
}
815806

816807
//FileUri
@@ -830,7 +821,10 @@ mod parse_uri_tests {
830821
#[test_case("file://no/leading/slash"; "no_leading_slash")]
831822
fn parse_file_fail(input: &str) {
832823
let settings = SettingsInput::new(input);
833-
assert!(FileUri::try_from(&settings).is_err(), "invalid file URI should fail");
824+
assert!(
825+
FileUri::try_from(&settings).is_err(),
826+
"invalid file URI should fail"
827+
);
834828
}
835829

836830
//HttpUri
@@ -848,7 +842,10 @@ mod parse_uri_tests {
848842
#[test_case("https:// "; "space_after_scheme")]
849843
fn parse_http_fail(input: &str) {
850844
let settings = SettingsInput::new(input);
851-
assert!(HttpUri::try_from(&settings).is_err(), "invalid HTTP URI should fail");
845+
assert!(
846+
HttpUri::try_from(&settings).is_err(),
847+
"invalid HTTP URI should fail"
848+
);
852849
}
853850

854851
//S3Uri
@@ -866,7 +863,10 @@ mod parse_uri_tests {
866863
#[test_case("s3://"; "empty_bucket_and_key")]
867864
fn parse_s3_fail(input: &str) {
868865
let settings = SettingsInput::new(input);
869-
assert!(S3Uri::try_from(&settings).is_err(), "invalid S3 URI should fail");
866+
assert!(
867+
S3Uri::try_from(&settings).is_err(),
868+
"invalid S3 URI should fail"
869+
);
870870
}
871871

872872
// SecretsManagerArn

0 commit comments

Comments
 (0)