Skip to content

Commit 215adad

Browse files
committed
apiclient: enable all URI schemes for network configure
Move SettingsInput and select_resolver to uri_resolver, so that both apply and network depend on uri_resolver Signed-off-by: Kyle Sessions <kssessio@amazon.com>
1 parent 923faca commit 215adad

File tree

4 files changed

+84
-277
lines changed

4 files changed

+84
-277
lines changed

sources/api/apiclient/src/apply.rs

Lines changed: 5 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
//! This module allows application of settings from URIs or stdin. The inputs are expected to be
22
//! TOML settings files, in the same format as user data, or the JSON equivalent. The inputs are
33
//! pulled and applied to the API server in a single transaction.
4-
use crate::apply::error::ResolverFailureSnafu;
54
use crate::rando;
5+
use crate::uri_resolver::{select_resolver, SettingsInput};
66
use futures::future::{join, ready};
77
use futures::stream::{self, StreamExt};
8-
use reqwest::Url;
98
use serde::de::{Deserialize, IntoDeserializer};
109
use snafu::{OptionExt, ResultExt};
11-
use std::convert::TryFrom;
1210
use std::path::Path;
1311

1412
/// Reads settings in TOML or JSON format from files at the requested URIs (or from stdin, if given
@@ -66,70 +64,14 @@ where
6664
Ok(())
6765
}
6866

69-
/// Holds the raw input string and the URL (if it parses).
70-
pub struct SettingsInput {
71-
pub input: String,
72-
pub parsed_url: Option<Url>,
73-
}
74-
impl SettingsInput {
75-
pub(crate) fn new(input: impl Into<String>) -> Self {
76-
let input = input.into();
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-
};
84-
SettingsInput { input, parsed_url }
85-
}
86-
}
87-
8867
/// Retrieves the given source location and returns the result in a String.
8968
async fn get<S>(input_source: S) -> Result<String>
9069
where
9170
S: AsRef<str>,
9271
{
9372
let settings = SettingsInput::new(input_source.as_ref());
94-
let resolver = select_resolver(&settings)?;
95-
resolver.resolve().await.context(ResolverFailureSnafu)
96-
}
97-
98-
/// Macro to try multiple settings resolver types in sequence, returning the first one that succeeds.
99-
macro_rules! try_resolvers {
100-
($input:expr, $($resolver_type:ty),+ $(,)?) => {
101-
$(
102-
if let Ok(r) = <$resolver_type>::try_from($input) {
103-
log::debug!("select_resolver: picked {}", stringify!($resolver_type));
104-
return Ok(Box::new(r));
105-
}
106-
)+
107-
};
108-
}
109-
110-
/// Choose which UriResolver applies to `input` (stdin, base64:, file://, http(s)://, s3://, secretsmanager://, and ssm://).
111-
fn select_resolver(input: &SettingsInput) -> Result<Box<dyn crate::uri_resolver::UriResolver>> {
112-
use crate::uri_resolver::*;
113-
114-
// NOTE: Order matters! More specific resolvers must come before general ones.
115-
// - StdinUri first (exact match for "-")
116-
// - ARN resolvers before URI resolvers (ARNs are more specific)
117-
try_resolvers!(input, StdinUri, Base64Uri, FileUri, HttpUri,);
118-
119-
#[cfg(feature = "tls")]
120-
try_resolvers!(
121-
input,
122-
S3Uri,
123-
SecretsManagerArn,
124-
SecretsManagerUri,
125-
SsmArn,
126-
SsmUri,
127-
);
128-
129-
error::NoResolverSnafu {
130-
input_source: input.input.clone(),
131-
}
132-
.fail()
73+
let resolver = select_resolver(&settings).context(error::ResolverFailureSnafu)?;
74+
resolver.resolve().await.context(error::ResolverFailureSnafu)
13375
}
13476

13577
/// Takes a string of TOML or JSON settings data and reserializes
@@ -178,9 +120,6 @@ mod error {
178120
source: Box<crate::Error>,
179121
},
180122

181-
#[snafu(display("No URI resolver found for '{}'", input_source))]
182-
NoResolver { input_source: String },
183-
184123
#[snafu(display(
185124
"Input '{}' is not valid TOML or JSON. (TOML error: {}) (JSON error: {})",
186125
input_source,
@@ -264,8 +203,7 @@ pub type Result<T> = std::result::Result<T, error::Error>;
264203

265204
#[cfg(test)]
266205
mod resolver_selection_tests {
267-
use super::select_resolver;
268-
use crate::apply::SettingsInput;
206+
use crate::uri_resolver::{select_resolver, SettingsInput};
269207
use std::any::{Any, TypeId};
270208
use test_case::test_case;
271209

@@ -315,7 +253,7 @@ mod format_change_tests {
315253

316254
#[cfg(test)]
317255
mod resolver_error_tests {
318-
use super::{select_resolver, SettingsInput};
256+
use crate::uri_resolver::{select_resolver, SettingsInput};
319257

320258
#[test]
321259
fn unsupported_scheme() {

sources/api/apiclient/src/cloud_resolvers.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//!
33
//! This module is only compiled when the `tls` feature is enabled.
44
5-
use crate::apply::SettingsInput;
5+
use crate::uri_resolver::SettingsInput;
66
use crate::uri_resolver::{ResolverResult, UriResolver, MAX_SIZE_BYTES};
77
use async_trait::async_trait;
88
use snafu::{ensure, OptionExt, ResultExt, Snafu};

0 commit comments

Comments
 (0)