Skip to content
Permalink
Browse files

Ensure that app selector matches the full application name.

  • Loading branch information...
schrieveslaach committed May 24, 2019
1 parent 9912491 commit f2e1d9e8394d5bf034e8c67616189a803c8d6d1d
Showing with 83 additions and 2 deletions.
  1. +33 −0 api/src/services/apps_service.rs
  2. +50 −2 api/src/services/config_service.rs
@@ -555,4 +555,37 @@ mod tests {

Ok(())
}

#[test]
fn should_create_app_for_master_without_secrets_because_of_none_matching_app_selector(
) -> Result<(), AppsServiceError> {
let config = config_from_str!(
r#"
[services.mariadb]
[[services.mariadb.secrets]]
name = "user"
data = "SGVsbG8="
appSelector = "master"
"#
);

let infrastructure = Box::new(DummyInfrastructure::new());
let apps = AppsService::new(config, infrastructure)?;

apps.create_or_update(
&String::from("master-1.x"),
None,
&service_configs!("mariadb"),
)?;

let configs = apps
.infrastructure
.get_configs_of_app(&String::from("master-1.x"))?;
assert_eq!(configs.len(), 1);

let volumes = configs.get(0).unwrap().volumes();
assert_eq!(volumes, None);

Ok(())
}
}
@@ -159,7 +159,7 @@ impl Config {

for (_, companion) in companions_map
.iter()
.filter(|(_, companion)| companion.app_selector.is_match(app_name))
.filter(|(_, companion)| is_full_match(app_name, &companion.app_selector))
.filter(|(_, companion)| predicate(*companion))
{
let mut config = ServiceConfig::try_from(companion)?;
@@ -244,9 +244,16 @@ impl Service {
}
}

fn is_full_match(s: &str, r: &Regex) -> bool {
match r.captures(s) {
None => false,
Some(captures) => captures.get(0).map_or("", |m| m.as_str()) == s
}
}

impl Secret {
pub fn matches(&self, app_name: &str) -> bool {
self.app_selector.is_match(app_name)
is_full_match(app_name, &self.app_selector)
}

fn parse_secstr<'de, D>(deserializer: D) -> Result<SecUtf8, D::Error>
@@ -568,6 +575,29 @@ mod tests {
assert_eq!(secret_file_content, "Hello");
}

#[test]
fn should_set_service_secrets_with_regex_app_selector() {
let config = config_from_str!(
r#"
[services.mariadb]
[[services.mariadb.secrets]]
name = "user"
data = "SGVsbG8="
appSelector = "master(-.+)?"
"#
);

let mut service_config = service_config!("mariadb");
config.add_secrets_to(&mut service_config, &String::from("master-1.x"));

let secret_file_content = service_config
.volumes()
.expect("File content is missing")
.get("/run/secrets/user")
.expect("No file for /run/secrets/user");
assert_eq!(secret_file_content, "Hello");
}

#[test]
fn should_not_set_service_secrets_with_specific_app_selector() {
let config = config_from_str!(
@@ -586,6 +616,24 @@ mod tests {
assert!(service_config.volumes().is_none());
}

#[test]
fn should_not_set_service_secrets_with_partially_specific_app_selector() {
let config = config_from_str!(
r#"
[services.mariadb]
[[services.mariadb.secrets]]
name = "user"
data = "SGVsbG8="
appSelector = "master"
"#
);

let mut service_config = service_config!("mariadb");
config.add_secrets_to(&mut service_config, &String::from("master-1.x"));

assert_eq!(service_config.volumes(), None);
}

#[test]
fn should_not_parse_config_because_of_invalid_secret_data() {
let config_str = r#"

0 comments on commit f2e1d9e

Please sign in to comment.
You can’t perform that action at this time.