Skip to content

Commit

Permalink
fix(wadm): ensure custom traits are not spread or link traits
Browse files Browse the repository at this point in the history
Signed-off-by: Brooks Townsend <[email protected]>
  • Loading branch information
brooksmtownsend committed Aug 28, 2024
1 parent b1fb889 commit 5a22fd1
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 14 deletions.
15 changes: 10 additions & 5 deletions crates/wadm-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,11 @@ impl Trait {
self.trait_type == LINK_TRAIT
}

/// Check if a trait is a scaler
pub fn is_scaler(&self) -> bool {
self.trait_type == SPREADSCALER_TRAIT || self.trait_type == DAEMONSCALER_TRAIT
}

/// Helper that creates a new spreadscaler type trait with the given properties
pub fn new_spreadscaler(props: SpreadScalerProperty) -> Trait {
Trait {
Expand Down Expand Up @@ -348,11 +353,11 @@ impl From<SpreadScalerProperty> for TraitProperty {
}
}

impl From<serde_json::Value> for TraitProperty {
fn from(value: serde_json::Value) -> Self {
Self::Custom(value)
}
}
// impl From<serde_json::Value> for TraitProperty {
// fn from(value: serde_json::Value) -> Self {
// Self::Custom(value)
// }
// }

/// Properties for the config list associated with components, providers, and links
///
Expand Down
27 changes: 27 additions & 0 deletions crates/wadm-types/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@ pub async fn validate_manifest(manifest: &Manifest) -> Result<Vec<ValidationFail
failures.extend(check_misnamed_interfaces(manifest));
failures.extend(check_dangling_links(manifest));
failures.extend(validate_policies(manifest));
failures.extend(ensure_no_custom_traits(manifest));
Ok(failures)
}

Expand Down Expand Up @@ -461,6 +462,32 @@ fn check_misnamed_interfaces(manifest: &Manifest) -> Vec<ValidationFailure> {
failures
}

/// This validation rule should eventually be removed, but at this time (as of wadm 0.14.0)
/// custom traits are not supported. We technically deserialize the custom trait, but 99%
/// of the time this is just a poorly formatted spread or link scaler which is incredibly
/// frustrating to debug.
fn ensure_no_custom_traits(manifest: &Manifest) -> Vec<ValidationFailure> {
let mut failures = Vec::new();
for component in manifest.components() {
if let Some(traits) = &component.traits {
for trait_item in traits {
match &trait_item.properties {
TraitProperty::Custom(trt) if trait_item.is_link() => failures.push(ValidationFailure::new(
ValidationFailureLevel::Error,
format!("Link trait deserialized as custom trait, ensure fields are correct: {}", trt),
)),
TraitProperty::Custom(trt) if trait_item.is_scaler() => failures.push(ValidationFailure::new(
ValidationFailureLevel::Error,
format!("Scaler trait deserialized as custom trait, ensure fields are correct: {}", trt),
)),
_ => (),
}
}
}
}
failures
}

/// Check for "dangling" links, which contain targets that are not specified elsewhere in the
/// WADM manifest.
///
Expand Down
7 changes: 4 additions & 3 deletions tests/fixtures/manifests/dangling-link.wadm.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ spec:
instances: 1
- type: link
properties:
namespace: wasi
package: http
interfaces: [outgoing-handler]
target:
name: httpserver
values:
address: 0.0.0.0:8080
name: httpclient
10 changes: 4 additions & 6 deletions tests/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,11 @@ async fn validate_dangling_links() -> Result<()> {
!failures.is_empty()
&& failures
.iter()
.all(|f| f.level == ValidationFailureLevel::Warning),
"failures present, all warnings"
);
assert!(
failures.valid(),
"manifest should be valid (missing targets could be managed externally)"
.all(|f| f.level == ValidationFailureLevel::Warning
|| f.level == ValidationFailureLevel::Error),
"failures present, all warnings or errors"
);
assert!(!failures.valid(), "manifest should not be valid");
Ok(())
}

Expand Down

0 comments on commit 5a22fd1

Please sign in to comment.