]> git.proxmox.com Git - pve-installer.git/commitdiff
answer: perform basic input validation for keyboard
authorChristian Ebner <c.ebner@proxmox.com>
Wed, 24 Apr 2024 08:48:50 +0000 (10:48 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Wed, 24 Apr 2024 09:27:56 +0000 (11:27 +0200)
Currently it is possible to validate and create an iso with an
invalid keyboad layout, only failing later during installation.

Add a basic check for correct keyboard layout by defining an enum
with allowed variants.

Signed-off-by: Christian Ebner <c.ebner@proxmox.com>
Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
proxmox-auto-installer/src/answer.rs
proxmox-auto-installer/src/utils.rs

index a6cf8b73eb2d9b1a73a255abebd6c48821f00cfa..af7485aa8741f71c5ca576c7f2ba081faffc2fd2 100644 (file)
@@ -23,7 +23,7 @@ pub struct Answer {
 pub struct Global {
     pub country: String,
     pub fqdn: Fqdn,
-    pub keyboard: String,
+    pub keyboard: KeyboardLayout,
     pub mailto: String,
     pub timezone: String,
     pub root_password: String,
@@ -270,3 +270,40 @@ pub struct BtrfsOptions {
     pub hdsize: Option<f64>,
     pub raid: Option<BtrfsRaidLevel>,
 }
+
+#[derive(Clone, Deserialize, Serialize, Debug, PartialEq)]
+#[serde(rename_all = "kebab-case", deny_unknown_fields)]
+pub enum KeyboardLayout {
+    De,
+    DeCh,
+    Dk,
+    EnGb,
+    EnUs,
+    Es,
+    Fi,
+    Fr,
+    FrBe,
+    FrCa,
+    FrCh,
+    Hu,
+    Is,
+    It,
+    Jp,
+    Lt,
+    Mk,
+    Nl,
+    No,
+    Pl,
+    Pt,
+    PtBr,
+    Se,
+    Si,
+    Tr,
+}
+
+impl std::fmt::Display for KeyboardLayout {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        let keyboard_layout = serde_json::to_value(self).unwrap().to_string();
+        write!(f, "{}", keyboard_layout.trim_matches('\"'))
+    }
+}
index 7e1366c87b7c598c36abef49a758d25cd816cef7..202ad41516998cc7fe82c736ad91b66d34f66576 100644 (file)
@@ -281,7 +281,11 @@ pub fn verify_locale_settings(answer: &Answer, locales: &LocaleInfo) -> Result<(
     {
         bail!("country code '{}' is not valid", &answer.global.country);
     }
-    if !locales.kmap.keys().any(|i| i == &answer.global.keyboard) {
+    if !locales
+        .kmap
+        .keys()
+        .any(|i| i == &answer.global.keyboard.to_string())
+    {
         bail!("keyboard layout '{}' is not valid", &answer.global.keyboard);
     }
 
@@ -328,7 +332,7 @@ pub fn parse_answer(
 
         country: answer.global.country.clone(),
         timezone: answer.global.timezone.clone(),
-        keymap: answer.global.keyboard.clone(),
+        keymap: answer.global.keyboard.to_string(),
 
         password: answer.global.root_password.clone(),
         mailto: answer.global.mailto.clone(),