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(server): Added validation for duplicated link config names #516

Merged
merged 3 commits into from
Dec 19, 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
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions crates/wadm-types/src/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,7 @@ pub async fn validate_manifest(manifest: &Manifest) -> Result<Vec<ValidationFail
failures.extend(ensure_no_custom_traits(manifest));
failures.extend(validate_component_properties(manifest));
failures.extend(check_duplicate_links(manifest));
failures.extend(validate_link_configs(manifest));
Ok(failures)
}

Expand Down Expand Up @@ -681,6 +682,56 @@ pub fn validate_component_properties(application: &Manifest) -> Vec<ValidationFa
failures
}

/// Validates link configs in a WADM application manifest.
///
/// At present this can check for:
/// - all config names are unique
///
pub fn validate_link_configs(manifest: &Manifest) -> Vec<ValidationFailure> {
let mut failures = Vec::new();
let mut link_config_names = HashSet::new();
for link_trait in manifest.links() {
if let TraitProperty::Link(LinkProperty {
target,
source,
..
}) = &link_trait.properties {
for config in target.config.iter() {
// Check if config name is unique
if !link_config_names.insert((
config.name.clone(),
)) {
failures.push(ValidationFailure::new(
ValidationFailureLevel::Error,
format!(
"Duplicate link config name found: '{}'",
config.name
),
));
}
}

if let Some(source) = source {
for config in source.config.iter() {
// Check if config name is unique
if !link_config_names.insert((
config.name.clone(),
)) {
failures.push(ValidationFailure::new(
ValidationFailureLevel::Error,
format!(
"Duplicate link config name found: '{}'",
config.name
),
));
}
}
}
}
};
failures
}

/// This function validates that a key/value pair is a valid OAM label. It's using fairly
/// basic validation rules to ensure that the manifest isn't doing anything horribly wrong. Keeping
/// this function free of regex is intentional to keep this code functional but simple.
Expand Down
78 changes: 78 additions & 0 deletions tests/fixtures/manifests/duplicate_link_config_names.wadm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: my-example-app
annotations:
description: "This is my app"
spec:
components:
- name: userinfo1
type: component
properties:
image: wasmcloud.azurecr.io/fake:1
traits:
- type: link
properties:
namespace: wasi
package: keyvalue
interfaces: [atomics, store]
target:
name: kvredis
config:
- name: redis-url

- name: userinfo2
type: component
properties:
image: wasmcloud.azurecr.io/fake:1
traits:
- type: link
properties:
namespace: wasi
package: keyvalue
interfaces: [atomics, store]
target:
name: kvredis
config:
- name: redis-url

- name: webcap1
type: capability
properties:
id: httpserver1
image: wasmcloud.azurecr.io/httpserver:0.13.1
traits:
- type: link
properties:
namespace: wasi
package: http
interfaces: ["incoming-handler"]
target:
name: userinfo1
source:
config:
- name: default-port
- name: alternate-port
- name: alternate-port

- name: webcap2
type: capability
properties:
id: httpserver2
image: wasmcloud.azurecr.io/httpserver:0.14.1
traits:
- type: link
properties:
target:
name: userinfo2
namespace: wasi
package: http
interfaces: ["incoming-handler"]
source:
config:
- name: default-port

- name: kvredis
type: capability
properties:
image: ghcr.io/wasmcloud/keyvalue-redis:0.28.1
23 changes: 23 additions & 0 deletions tests/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,26 @@ async fn validate_policy() -> Result<()> {
assert!(failures.valid(), "manifest is valid");
Ok(())
}

/// Ensure that we can detect duplicated link config names
#[tokio::test]
async fn validate_link_config_names() -> Result<()> {
let (_manifest, failures) =
validate_manifest_file("./tests/fixtures/manifests/duplicate_link_config_names.wadm.yaml")
.await
.context("failed to validate manifest")?;
let expected_errors = 3;
assert!(
!failures.is_empty()
&& failures
.iter()
.all(|f| f.level == ValidationFailureLevel::Error)
&& failures.len() == expected_errors,
"expected {} errors because manifest contains {} duplicated link config names, instead {} errors were found", expected_errors, expected_errors, failures.len().to_string()
);
assert!(
!failures.valid(),
"manifest should be invalid (duplicated link config names lead to a dead loop)"
);
Ok(())
}