Skip to content

Commit 785cbd2

Browse files
Update ValidatorFn to use string slices
Signed-off-by: Kate Goldenring <[email protected]>
1 parent b01be64 commit 785cbd2

File tree

3 files changed

+20
-28
lines changed

3 files changed

+20
-28
lines changed

crates/app/src/lib.rs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub const OCI_IMAGE_DIGEST_KEY: MetadataKey = MetadataKey::new("oci_image_digest
3131

3232
/// Validation function type for ensuring that applications meet requirements
3333
/// even with components filtered out.
34-
pub type ValidatorFn = dyn Fn(&App, &[String]) -> anyhow::Result<()>;
34+
pub type ValidatorFn = dyn Fn(&App, &[&str]) -> anyhow::Result<()>;
3535

3636
/// An `App` holds loaded configuration for a Spin application.
3737
#[derive(Debug, Clone)]
@@ -171,7 +171,7 @@ impl App {
171171
/// Introspects the LockedApp to find and selectively retain the triggers that correspond to those components
172172
fn retain_components(
173173
mut self,
174-
retained_components: &[String],
174+
retained_components: &[&str],
175175
validators: &[&ValidatorFn],
176176
) -> Result<LockedApp> {
177177
self.validate_retained_components_exist(retained_components)?;
@@ -181,7 +181,7 @@ impl App {
181181
let (component_ids, trigger_ids): (HashSet<String>, HashSet<String>) = self
182182
.triggers()
183183
.filter_map(|t| match t.component() {
184-
Ok(comp) if retained_components.contains(&comp.id().to_string()) => {
184+
Ok(comp) if retained_components.contains(&comp.id()) => {
185185
Some((comp.id().to_owned(), t.id().to_owned()))
186186
}
187187
_ => None,
@@ -195,13 +195,13 @@ impl App {
195195
}
196196

197197
/// Validates that all components specified to be retained actually exist in the app
198-
fn validate_retained_components_exist(&self, retained_components: &[String]) -> Result<()> {
198+
fn validate_retained_components_exist(&self, retained_components: &[&str]) -> Result<()> {
199199
let app_components = self
200200
.components()
201201
.map(|c| c.id().to_string())
202202
.collect::<HashSet<_>>();
203203
for c in retained_components {
204-
if !app_components.contains(c) {
204+
if !app_components.contains(*c) {
205205
return Err(Error::ValidationError(anyhow::anyhow!(
206206
"Specified component \"{c}\" not found in application"
207207
)));
@@ -320,7 +320,7 @@ struct CommonTriggerConfig {
320320
/// Introspects the LockedApp to find and selectively retain the triggers that correspond to those components
321321
pub fn retain_components(
322322
locked: LockedApp,
323-
components: &[String],
323+
components: &[&str],
324324
validators: &[&ValidatorFn],
325325
) -> Result<LockedApp> {
326326
App::new("unused", locked).retain_components(components, validators)
@@ -332,7 +332,7 @@ mod test {
332332

333333
use super::*;
334334

335-
fn does_nothing_validator(_: &App, _: &[String]) -> anyhow::Result<()> {
335+
fn does_nothing_validator(_: &App, _: &[&str]) -> anyhow::Result<()> {
336336
Ok(())
337337
}
338338

@@ -351,12 +351,7 @@ mod test {
351351
source = "does-not-exist.wasm"
352352
};
353353
let mut locked_app = build_locked_app(&manifest).await.unwrap();
354-
locked_app = retain_components(
355-
locked_app,
356-
&["empty".to_string()],
357-
&[&does_nothing_validator],
358-
)
359-
.unwrap();
354+
locked_app = retain_components(locked_app, &["empty"], &[&does_nothing_validator]).unwrap();
360355
let components = locked_app
361356
.components
362357
.iter()

crates/factor-outbound-networking/src/config.rs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,18 @@ pub fn allowed_outbound_hosts(component: &AppComponent) -> anyhow::Result<Vec<St
4444
/// are ignored.
4545
pub fn validate_service_chaining_for_components(
4646
app: &App,
47-
retained_components: &[String],
47+
retained_components: &[&str],
4848
) -> anyhow::Result<()> {
4949
app
5050
.triggers().try_for_each(|t| {
5151
let Ok(component) = t.component() else { return Ok(()) };
52-
if retained_components.contains(&component.id().to_string()) {
52+
if retained_components.contains(&component.id()) {
5353
let allowed_hosts = allowed_outbound_hosts(&component).context("failed to get allowed hosts")?;
5454
for host in allowed_hosts {
5555
// Templated URLs are not yet resolved at this point, so ignore unresolvable URIs
5656
if let Ok(uri) = host.parse::<http::Uri>() {
5757
if let Some(chaining_target) = parse_service_chaining_target(&uri) {
58-
if !retained_components.contains(&chaining_target) {
58+
if !retained_components.contains(&chaining_target.as_ref()) {
5959
if chaining_target == "*" {
6060
return Err(anyhow::anyhow!("Selected component '{}' cannot use wildcard service chaining: allowed_outbound_hosts = [\"http://*.spin.internal\"]", component.id()));
6161
}
@@ -891,24 +891,21 @@ mod test {
891891
.await
892892
.expect("could not build locked app");
893893
let app = App::new("unused", locked_app);
894-
let Err(e) = validate_service_chaining_for_components(&app, &["empty".to_string()]) else {
894+
let Err(e) = validate_service_chaining_for_components(&app, &["empty"]) else {
895895
panic!("Expected service chaining to non-retained component error");
896896
};
897897
assert_eq!(
898898
e.to_string(),
899899
"Selected component 'empty' cannot use service chaining to unselected component: allowed_outbound_hosts = [\"http://another.spin.internal\"]"
900900
);
901-
let Err(e) = validate_service_chaining_for_components(
902-
&app,
903-
&["third".to_string(), "another".to_string()],
904-
) else {
901+
let Err(e) = validate_service_chaining_for_components(&app, &["third", "another"]) else {
905902
panic!("Expected wildcard service chaining error");
906903
};
907904
assert_eq!(
908905
e.to_string(),
909906
"Selected component 'third' cannot use wildcard service chaining: allowed_outbound_hosts = [\"http://*.spin.internal\"]"
910907
);
911-
assert!(validate_service_chaining_for_components(&app, &["another".to_string()]).is_ok());
908+
assert!(validate_service_chaining_for_components(&app, &["another"]).is_ok());
912909
}
913910

914911
#[tokio::test]
@@ -945,10 +942,6 @@ mod test {
945942
.await
946943
.expect("could not build locked app");
947944
let app = App::new("unused", locked_app);
948-
assert!(validate_service_chaining_for_components(
949-
&app,
950-
&["empty".to_string(), "third".to_string()]
951-
)
952-
.is_ok());
945+
assert!(validate_service_chaining_for_components(&app, &["empty", "third"]).is_ok());
953946
}
954947
}

src/commands/up.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,11 @@ impl UpCommand {
201201
if !self.components.is_empty() {
202202
locked_app = spin_app::retain_components(
203203
locked_app,
204-
&self.components,
204+
&self
205+
.components
206+
.iter()
207+
.map(|s| s.as_str())
208+
.collect::<Vec<&str>>(),
205209
&[&validate_service_chaining_for_components],
206210
)
207211
.context(

0 commit comments

Comments
 (0)