Validate devices
This commit is contained in:
@ -1,4 +1,5 @@
|
||||
use crate::app_config::AppConfig;
|
||||
use crate::crypto::pki;
|
||||
use crate::devices::device::{Device, DeviceId, DeviceInfo};
|
||||
use crate::utils::time_utils::time_secs;
|
||||
use openssl::x509::X509Req;
|
||||
@ -10,6 +11,10 @@ pub enum DevicesListError {
|
||||
EnrollFailedDeviceAlreadyExists,
|
||||
#[error("Persist device config failed: the configuration of the device was not found!")]
|
||||
PersistFailedDeviceNotFound,
|
||||
#[error("Validated device failed: the device does not exists!")]
|
||||
ValidateDeviceFailedDeviceNotFound,
|
||||
#[error("Validated device failed: the device is already validated!")]
|
||||
ValidateDeviceFailedDeviceAlreadyValidated,
|
||||
}
|
||||
|
||||
pub struct DevicesList(HashMap<DeviceId, Device>);
|
||||
@ -96,6 +101,29 @@ impl DevicesList {
|
||||
self.0.clone().into_values().collect()
|
||||
}
|
||||
|
||||
/// Validate a device
|
||||
pub fn validate(&mut self, id: &DeviceId) -> anyhow::Result<()> {
|
||||
let dev = self
|
||||
.0
|
||||
.get_mut(id)
|
||||
.ok_or(DevicesListError::ValidateDeviceFailedDeviceNotFound)?;
|
||||
|
||||
if dev.validated {
|
||||
return Err(DevicesListError::ValidateDeviceFailedDeviceAlreadyValidated.into());
|
||||
}
|
||||
|
||||
// Issue certificate
|
||||
let csr = X509Req::from_pem(&std::fs::read(AppConfig::get().device_csr_path(id))?)?;
|
||||
let cert = pki::gen_certificate_for_device(&csr)?;
|
||||
std::fs::write(AppConfig::get().device_cert_path(id), cert)?;
|
||||
|
||||
// Mark device as validated
|
||||
dev.validated = true;
|
||||
self.persist_dev_config(id)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Delete a device
|
||||
pub fn delete(&mut self, id: &DeviceId) -> anyhow::Result<()> {
|
||||
let crt_path = AppConfig::get().device_cert_path(id);
|
||||
|
Reference in New Issue
Block a user