diff options
Diffstat (limited to 'tedge_config')
-rw-r--r-- | tedge_config/src/config_setting.rs | 6 | ||||
-rw-r--r-- | tedge_config/src/settings.rs | 6 | ||||
-rw-r--r-- | tedge_config/src/tedge_config.rs | 7 | ||||
-rw-r--r-- | tedge_config/tests/test_tedge_config.rs | 28 |
4 files changed, 30 insertions, 17 deletions
diff --git a/tedge_config/src/config_setting.rs b/tedge_config/src/config_setting.rs index 3dcbfdab..aa68d66a 100644 --- a/tedge_config/src/config_setting.rs +++ b/tedge_config/src/config_setting.rs @@ -66,4 +66,10 @@ pub enum ConfigSettingError { #[error("Derivation for `{key}` failed: {cause}")] DerivationFailed { key: &'static str, cause: String }, + + #[error("Config value {key}, cannot be configured: {message} ")] + SettingIsNotConfigurable { + key: &'static str, + message: &'static str, + }, } diff --git a/tedge_config/src/settings.rs b/tedge_config/src/settings.rs index e5cea255..b1a312e2 100644 --- a/tedge_config/src/settings.rs +++ b/tedge_config/src/settings.rs @@ -3,6 +3,7 @@ use crate::{config_setting::*, models::*}; /// /// Identifier of the device within the fleet. It must be globally /// unique and the same one used in the device certificate. +/// NOTE: This setting is derived from the device certificate and therefore is read only. /// /// Example: Raspberrypi-4d18303a-6d3a-11eb-b1a6-175f6bb72665") /// @@ -14,8 +15,9 @@ impl ConfigSetting for DeviceIdSetting { const DESCRIPTION: &'static str = concat!( "Identifier of the device within the fleet. It must be ", - "globally unique and the same one used in the device certificate. ", - "Example: Raspberrypi-4d18303a-6d3a-11eb-b1a6-175f6bb72665" + "globally unique and is derived from the device certificate. ", + "Example: Raspberrypi-4d18303a-6d3a-11eb-b1a6-175f6bb72665", + "NOTE: This setting is derived from the device certificate and therefore is read only." ); type Value = String; diff --git a/tedge_config/src/tedge_config.rs b/tedge_config/src/tedge_config.rs index dcc1ccb0..b4e88ce9 100644 --- a/tedge_config/src/tedge_config.rs +++ b/tedge_config/src/tedge_config.rs @@ -45,7 +45,12 @@ fn device_id_read_only_error() -> ConfigSettingError { fn cert_error_into_config_error(key: &'static str, err: CertificateError) -> ConfigSettingError { match &err { CertificateError::IoError(io_err) => match io_err.kind() { - std::io::ErrorKind::NotFound => ConfigSettingError::ConfigNotSet { key }, + std::io::ErrorKind::NotFound => ConfigSettingError::SettingIsNotConfigurable { key, + message: concat!( + "The device id is read from the device certificate.\n", + "To set 'device.id' to some <id>, you can use `tedge cert create --device-id <id>`.", + ), + }, _ => ConfigSettingError::DerivationFailed { key, cause: format!("{}", err), diff --git a/tedge_config/tests/test_tedge_config.rs b/tedge_config/tests/test_tedge_config.rs index a4f772e1..0cf4c396 100644 --- a/tedge_config/tests/test_tedge_config.rs +++ b/tedge_config/tests/test_tedge_config.rs @@ -38,7 +38,7 @@ external_keyfile = "key.pem" let config = TEdgeConfigRepository::new_with_defaults(config_location, config_defaults).load()?; - assert!(config.query_optional(DeviceIdSetting)?.is_none()); + assert!(config.query_optional(DeviceIdSetting).is_err()); assert_eq!( config.query(DeviceKeyPathSetting)?, FilePath::from("/path/to/key") @@ -140,7 +140,7 @@ port = 1883 { let mut config = config_repo.load()?; - assert!(config.query_optional(DeviceIdSetting)?.is_none()); + assert!(config.query_optional(DeviceIdSetting).is_err()); assert_eq!( config.query(DeviceKeyPathSetting)?, FilePath::from("/path/to/key") @@ -204,7 +204,7 @@ port = 1883 { let config = config_repo.load()?; - assert!(config.query_optional(DeviceIdSetting)?.is_none()); + assert!(config.query_optional(DeviceIdSetting).is_err()); assert_eq!( config.query(DeviceKeyPathSetting)?, FilePath::from("/path/to/key") @@ -275,7 +275,7 @@ fn test_parse_config_with_only_device_configuration() -> Result<(), TEdgeConfigE let config = TEdgeConfigRepository::new_with_defaults(config_location, config_defaults).load()?; - assert!(config.query_optional(DeviceIdSetting)?.is_none()); + assert!(config.query_optional(DeviceIdSetting).is_err()); assert_eq!( config.query(DeviceCertPathSetting)?, FilePath::from("/etc/ssl/certs/tedge-certificate.pem") @@ -320,7 +320,7 @@ url = "your-tenant.cumulocity.com" let config = TEdgeConfigRepository::new_with_defaults(config_location, config_defaults).load()?; - assert!(config.query_optional(DeviceIdSetting)?.is_none()); + assert!(config.query_optional(DeviceIdSetting).is_err()); assert_eq!( config.query(DeviceCertPathSetting)?, FilePath::from("/etc/ssl/certs/tedge-certificate.pem") @@ -368,7 +368,7 @@ url = "MyAzure.azure-devices.net" let config = TEdgeConfigRepository::new_with_defaults(config_location, config_defaults).load()?; - assert!(config.query_optional(DeviceIdSetting)?.is_none()); + assert!(config.query_optional(DeviceIdSetting).is_err()); assert_eq!( config.query(DeviceCertPathSetting)?, FilePath::from("/etc/ssl/certs/tedge-certificate.pem") @@ -416,7 +416,7 @@ port = 2222 let config = TEdgeConfigRepository::new_with_defaults(config_location, config_defaults).load()?; - assert!(config.query_optional(DeviceIdSetting)?.is_none()); + assert!(config.query_optional(DeviceIdSetting).is_err()); assert_eq!( config.query(DeviceCertPathSetting)?, FilePath::from("/etc/ssl/certs/tedge-certificate.pem") @@ -532,7 +532,7 @@ fn test_parse_config_empty_file() -> Result<(), TEdgeConfigError> { let config = TEdgeConfigRepository::new_with_defaults(config_location, config_defaults).load()?; - assert!(config.query_optional(DeviceIdSetting)?.is_none()); + assert!(config.query_optional(DeviceIdSetting).is_err()); assert_eq!( config.query(DeviceCertPathSetting)?, @@ -565,7 +565,7 @@ fn test_parse_config_no_config_file() -> Result<(), TEdgeConfigError> { let config_location = TEdgeConfigLocation::from_custom_root("/non/existent/path"); let config = TEdgeConfigRepository::new(config_location).load()?; - assert!(config.query_optional(DeviceIdSetting)?.is_none()); + assert!(config.query_optional(DeviceIdSetting).is_err()); assert_eq!( config.query(DeviceCertPathSetting)?, FilePath::from("/non/existent/path/device-certs/tedge-certificate.pem") @@ -684,7 +684,7 @@ port = 1024 let original_device_key_path = FilePath::from("/path/to/key"); let original_device_cert_path = FilePath::from("/path/to/cert"); - assert!(config.query_optional(DeviceIdSetting)?.is_none()); + assert!(config.query_optional(DeviceIdSetting).is_err()); assert_eq!( config.query(DeviceKeyPathSetting)?, original_device_key_path @@ -723,7 +723,7 @@ port = 1024 config.unset(AzureMapperTimestamp)?; let updated_mqtt_port = Port(2048); - config.update(MqttPortSetting, updated_mqtt_port.clone())?; + config.update(MqttPortSetting, updated_mqtt_port)?; assert_eq!( config.query(DeviceKeyPathSetting)?, @@ -764,7 +764,7 @@ cert_path = "/path/to/cert" TEdgeConfigRepository::new_with_defaults(config_location, dummy_tedge_config_defaults()) .load()?; - assert!(config.query_optional(DeviceIdSetting)?.is_none()); + assert!(config.query_optional(DeviceIdSetting).is_err()); Ok(()) } @@ -780,7 +780,7 @@ cert_path = "/path/to/cert" TEdgeConfigRepository::new_with_defaults(config_location, dummy_tedge_config_defaults()) .load()?; - assert!(config.query_optional(DeviceIdSetting)?.is_none()); + assert!(config.query_optional(DeviceIdSetting).is_err()); Ok(()) } @@ -861,7 +861,7 @@ fn create_certificate( device_id, )?; let pem = keypair.certificate_pem_string()?; - let mut file = std::fs::File::create(path.clone())?; + let mut file = std::fs::File::create(path)?; file.write_all(pem.as_bytes())?; Ok(()) } |