Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(wadm): ensure custom traits are not spread or link traits #396

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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