Skip to content

Commit

Permalink
Migrate existing functionality to new os-config schema
Browse files Browse the repository at this point in the history
The response from querying /os/v1/config has changed slightly,
the changes to the response are reflected in the updated tests
in this commit.

See: https://balena.fibery.io/Work/Improvement/os-config-improving-the-interface-for-config.json-modification-901
See: balena-os/meta-balena#3227
See: balena-io/open-balena-api#1394
Signed-off-by: Christina Ying Wang <[email protected]>
Change-type: minor
  • Loading branch information
cywang117 committed Sep 13, 2023
1 parent a02bba2 commit f0882b9
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 68 deletions.
23 changes: 16 additions & 7 deletions src/remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ use std::collections::HashMap;
use std::thread;
use std::time::Duration;

use crate::schema::validate_schema_version;
use anyhow::{anyhow, Context, Result};

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct RemoteConfiguration {
pub services: HashMap<String, HashMap<String, String>>,
pub schema_version: String,
pub config: ConfigMigrationInstructions,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct ConfigMigrationInstructions {
pub overrides: HashMap<String, serde_json::Value>,
}

impl RemoteConfiguration {
Expand Down Expand Up @@ -66,8 +70,6 @@ fn fetch_configuration_impl(

info!("Service configuration retrieved");

validate_schema_version(&json_data)?;

Ok(serde_json::from_str(&json_data)?)
}

Expand Down Expand Up @@ -141,7 +143,6 @@ fn build_reqwest_client(
mod tests {

use super::*;
use crate::schema::SCHEMA_VERSION;

const JSON_DATA: &str = r#"{
"services": {
Expand All @@ -155,7 +156,11 @@ mod tests {
"authorized_keys": "authorized keys here"
}
},
"schema_version": "1.0.0"
"config": {
"overrides": {
"logsEndpoint": "https://logs.balenadev.io"
}
}
}"#;

#[test]
Expand All @@ -174,7 +179,11 @@ mod tests {
"authorized_keys".into() => "authorized keys here".into()
}
},
schema_version: SCHEMA_VERSION.into(),
config: ConfigMigrationInstructions {
overrides: hashmap! {
"logsEndpoint".into() => "https://logs.balenadev.io".into()
},
},
};

assert_eq!(parsed, expected);
Expand Down
50 changes: 16 additions & 34 deletions src/schema.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use crate::fs::read_file;
use anyhow::{Context, Result};
use std::collections::HashMap;
use std::path::Path;

use serde_json::Value;

use crate::fs::read_file;
use anyhow::{bail, Context, Result};

pub const SCHEMA_VERSION: &str = "1.0.0";

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct OsConfigSchema {
pub services: Vec<Service>,
// Fields that should be removed from config.json when leaving a cloud env (`balena leave`)
pub keys: Vec<String>,
pub schema_version: String,
pub config: ConfigJsonSchema,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
Expand All @@ -28,40 +24,22 @@ pub struct ConfigFile {
pub perm: String,
}

#[derive(Debug, Serialize, Deserialize, PartialEq)]
pub struct ConfigJsonSchema {
// Fields that may be modified in config.json
pub whitelist: Vec<String>,
}

pub fn read_os_config_schema(os_config_path: &Path) -> Result<OsConfigSchema> {
read_os_config_schema_impl(os_config_path).context("Reading `os-config.json` schema failed")
}

fn read_os_config_schema_impl(os_config_path: &Path) -> Result<OsConfigSchema> {
let json_data = read_file(os_config_path)?;

validate_schema_version(&json_data)?;

Ok(serde_json::from_str(&json_data)?)
}

pub fn validate_schema_version(json_data: &str) -> Result<()> {
let parsed: Value = serde_json::from_str(json_data)?;

match parsed.get("schema_version") {
Some(version_value) => match version_value.as_str() {
Some(schema_version) => {
if schema_version == SCHEMA_VERSION {
Ok(())
} else {
bail!(
"Expected schema version {}, got {}",
SCHEMA_VERSION,
schema_version
)
}
}
_ => bail!("`schema_version` should be a string"),
},
_ => bail!("Missing `schema_version`"),
}
}

#[cfg(test)]
mod tests {

Expand Down Expand Up @@ -98,7 +76,9 @@ mod tests {
}
],
"keys": ["apiKey", "apiEndpoint", "vpnEndpoint"],
"schema_version": "1.0.0"
"config": {
"whitelist": ["logsEndpoint"]
}
}"#;

#[test]
Expand Down Expand Up @@ -133,7 +113,9 @@ mod tests {
},
],
keys: vec!["apiKey".into(), "apiEndpoint".into(), "vpnEndpoint".into()],
schema_version: SCHEMA_VERSION.into(),
config: ConfigJsonSchema {
whitelist: vec!["logsEndpoint".into()],
},
};

assert_eq!(parsed, expected);
Expand Down
Loading

0 comments on commit f0882b9

Please sign in to comment.