Skip to content

Commit

Permalink
proxmoxve: ignore user-data file if header is not present
Browse files Browse the repository at this point in the history
  • Loading branch information
arcln committed Apr 16, 2024
1 parent 8d88a45 commit 09b602c
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 9 deletions.
42 changes: 33 additions & 9 deletions src/providers/proxmoxve/cloudconfig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use slog_scope::warn;
use std::{
collections::HashMap,
fs::File,
io::{BufRead, BufReader},
net::{AddrParseError, IpAddr},
path::Path,
str::FromStr,
Expand All @@ -19,7 +20,7 @@ use std::{
#[derive(Debug)]
pub struct ProxmoxVECloudConfig {
pub meta_data: ProxmoxVECloudMetaData,
pub user_data: ProxmoxVECloudUserData,
pub user_data: Option<ProxmoxVECloudUserData>,
pub vendor_data: ProxmoxVECloudVendorData,
pub network_config: ProxmoxVECloudNetworkConfig,
}
Expand Down Expand Up @@ -81,9 +82,26 @@ pub struct ProxmoxVECloudNetworkConfigSubnet {

impl ProxmoxVECloudConfig {
pub fn try_new(path: &Path) -> Result<Self> {
let user_data_file = BufReader::new(File::open(path.join("user-data"))?);
let mut user_data = None;

if let Some(first_line) = user_data_file.lines().next() {
if let Ok(first_line) = first_line {
if first_line.starts_with("#cloud-config") {
user_data = serde_yaml::from_reader(File::open(path.join("user-data"))?)?;
}
}
}

if user_data.is_none() {
warn!(
"user-data does not have the expected header `#cloud-config`, ignoring this file"
);
}

Ok(Self {
user_data,
meta_data: serde_yaml::from_reader(File::open(path.join("meta-data"))?)?,
user_data: serde_yaml::from_reader(File::open(path.join("user-data"))?)?,
vendor_data: serde_yaml::from_reader(File::open(path.join("vendor-data"))?)?,
network_config: serde_yaml::from_reader(File::open(path.join("network-config"))?)?,
})
Expand Down Expand Up @@ -119,16 +137,22 @@ impl MetadataProvider for ProxmoxVECloudConfig {
}

fn hostname(&self) -> Result<Option<String>> {
Ok(Some(self.user_data.hostname.clone()))
Ok(self
.user_data
.as_ref()
.map(|user_data| user_data.hostname.clone()))
}

fn ssh_keys(&self) -> Result<Vec<PublicKey>> {
Ok(self
.user_data
.ssh_authorized_keys
.iter()
.map(|key| PublicKey::from_str(key))
.collect::<Result<Vec<_>, _>>()?)
if let Some(user_data) = &self.user_data {
return Ok(user_data
.ssh_authorized_keys
.iter()
.map(|key| PublicKey::from_str(key))
.collect::<Result<Vec<_>, _>>()?);
}

Ok(vec![])
}

fn networks(&self) -> Result<Vec<network::Interface>> {
Expand Down
10 changes: 10 additions & 0 deletions src/providers/proxmoxve/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,13 @@ fn test_network_static() {
]
);
}

#[test]
fn test_invalid_user_data() {
let config =
ProxmoxVECloudConfig::try_new(Path::new("tests/fixtures/proxmoxve/invalid-user-data"))
.expect("cannot parse config");

assert_eq!(config.hostname().unwrap().is_none(), true);
assert_eq!(config.ssh_keys().unwrap(), vec![]);
}
1 change: 1 addition & 0 deletions tests/fixtures/proxmoxve/invalid-user-data/meta-data
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
instance-id: 15a9919cb91024fbd1d70fa07f0efa749cbba03b
30 changes: 30 additions & 0 deletions tests/fixtures/proxmoxve/invalid-user-data/network-config
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
version: 1
config:
- type: physical
name: eth0
mac_address: '01:23:45:67:89:00'
subnets:
- type: static
address: '192.168.1.1'
netmask: '255.255.255.0'
gateway: '192.168.1.254'
- type: static6
address: '2001:0db8:85a3:0000:0000:8a2e:0370:0/24'
gateway: '2001:0db8:85a3:0000:0000:8a2e:0370:9999'
- type: physical
name: eth1
mac_address: '01:23:45:67:89:99'
subnets:
- type: static
address: '192.168.42.1'
netmask: '255.255.255.0'
gateway: '192.168.42.254'
- type: static6
address: '2001:0db8:85a3:0000:0000:8a2e:4242:0/24'
gateway: '2001:0db8:85a3:0000:0000:8a2e:4242:9999'
- type: nameserver
address:
- '1.1.1.1'
- '8.8.8.8'
search:
- 'local.com'
5 changes: 5 additions & 0 deletions tests/fixtures/proxmoxve/invalid-user-data/user-data
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"some": {
"ignition": "config"
}
}
Empty file.

0 comments on commit 09b602c

Please sign in to comment.