Validate devices
This commit is contained in:
parent
2502ed6bcf
commit
e97ef6fe45
@ -1,4 +1,5 @@
|
|||||||
use crate::app_config::AppConfig;
|
use crate::app_config::AppConfig;
|
||||||
|
use crate::crypto::pki;
|
||||||
use crate::devices::device::{Device, DeviceId, DeviceInfo};
|
use crate::devices::device::{Device, DeviceId, DeviceInfo};
|
||||||
use crate::utils::time_utils::time_secs;
|
use crate::utils::time_utils::time_secs;
|
||||||
use openssl::x509::X509Req;
|
use openssl::x509::X509Req;
|
||||||
@ -10,6 +11,10 @@ pub enum DevicesListError {
|
|||||||
EnrollFailedDeviceAlreadyExists,
|
EnrollFailedDeviceAlreadyExists,
|
||||||
#[error("Persist device config failed: the configuration of the device was not found!")]
|
#[error("Persist device config failed: the configuration of the device was not found!")]
|
||||||
PersistFailedDeviceNotFound,
|
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>);
|
pub struct DevicesList(HashMap<DeviceId, Device>);
|
||||||
@ -96,6 +101,29 @@ impl DevicesList {
|
|||||||
self.0.clone().into_values().collect()
|
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
|
/// Delete a device
|
||||||
pub fn delete(&mut self, id: &DeviceId) -> anyhow::Result<()> {
|
pub fn delete(&mut self, id: &DeviceId) -> anyhow::Result<()> {
|
||||||
let crt_path = AppConfig::get().device_cert_path(id);
|
let crt_path = AppConfig::get().device_cert_path(id);
|
||||||
|
@ -94,6 +94,21 @@ impl Handler<EnrollDevice> for EnergyActor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Validate a device
|
||||||
|
#[derive(Message)]
|
||||||
|
#[rtype(result = "anyhow::Result<()>")]
|
||||||
|
pub struct ValidateDevice(pub DeviceId);
|
||||||
|
|
||||||
|
impl Handler<ValidateDevice> for EnergyActor {
|
||||||
|
type Result = anyhow::Result<()>;
|
||||||
|
|
||||||
|
fn handle(&mut self, msg: ValidateDevice, _ctx: &mut Context<Self>) -> Self::Result {
|
||||||
|
log::info!("Requested to validate device {:?}...", &msg.0);
|
||||||
|
self.devices.validate(&msg.0)?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Delete a device
|
/// Delete a device
|
||||||
#[derive(Message)]
|
#[derive(Message)]
|
||||||
#[rtype(result = "anyhow::Result<()>")]
|
#[rtype(result = "anyhow::Result<()>")]
|
||||||
|
@ -139,6 +139,10 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
|
|||||||
"/web_api/devices/list_validated",
|
"/web_api/devices/list_validated",
|
||||||
web::get().to(devices_controller::list_validated),
|
web::get().to(devices_controller::list_validated),
|
||||||
)
|
)
|
||||||
|
.route(
|
||||||
|
"/web_api/device/{id}/validate",
|
||||||
|
web::post().to(devices_controller::validate_device),
|
||||||
|
)
|
||||||
.route(
|
.route(
|
||||||
"/web_api/device/{id}",
|
"/web_api/device/{id}",
|
||||||
web::delete().to(devices_controller::delete_device),
|
web::delete().to(devices_controller::delete_device),
|
||||||
@ -152,6 +156,7 @@ pub async fn secure_server(energy_actor: EnergyActorAddr) -> anyhow::Result<()>
|
|||||||
"/devices_api/mgmt/enroll",
|
"/devices_api/mgmt/enroll",
|
||||||
web::post().to(mgmt_controller::enroll),
|
web::post().to(mgmt_controller::enroll),
|
||||||
)
|
)
|
||||||
|
// TODO : check device status
|
||||||
})
|
})
|
||||||
.bind_openssl(&AppConfig::get().listen_address, builder)?
|
.bind_openssl(&AppConfig::get().listen_address, builder)?
|
||||||
.run()
|
.run()
|
||||||
|
@ -33,6 +33,15 @@ pub struct DeviceInPath {
|
|||||||
id: DeviceId,
|
id: DeviceId,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Validate a device
|
||||||
|
pub async fn validate_device(actor: WebEnergyActor, id: web::Path<DeviceInPath>) -> HttpResult {
|
||||||
|
actor
|
||||||
|
.send(energy_actor::ValidateDevice(id.id.clone()))
|
||||||
|
.await??;
|
||||||
|
|
||||||
|
Ok(HttpResponse::Accepted().finish())
|
||||||
|
}
|
||||||
|
|
||||||
/// Delete a device
|
/// Delete a device
|
||||||
pub async fn delete_device(actor: WebEnergyActor, id: web::Path<DeviceInPath>) -> HttpResult {
|
pub async fn delete_device(actor: WebEnergyActor, id: web::Path<DeviceInPath>) -> HttpResult {
|
||||||
actor
|
actor
|
||||||
|
@ -49,6 +49,15 @@ export class DeviceApi {
|
|||||||
})
|
})
|
||||||
).data;
|
).data;
|
||||||
}
|
}
|
||||||
|
/**
|
||||||
|
* Validate a device
|
||||||
|
*/
|
||||||
|
static async Validate(d: Device): Promise<void> {
|
||||||
|
await APIClient.exec({
|
||||||
|
uri: `/device/${encodeURIComponent(d.id)}/validate`,
|
||||||
|
method: "POST",
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete a device
|
* Delete a device
|
||||||
|
@ -8,6 +8,7 @@ import {
|
|||||||
TableContainer,
|
TableContainer,
|
||||||
TableHead,
|
TableHead,
|
||||||
TableRow,
|
TableRow,
|
||||||
|
Tooltip,
|
||||||
} from "@mui/material";
|
} from "@mui/material";
|
||||||
import React from "react";
|
import React from "react";
|
||||||
import { Device, DeviceApi } from "../api/DeviceApi";
|
import { Device, DeviceApi } from "../api/DeviceApi";
|
||||||
@ -18,6 +19,7 @@ import { useSnackbar } from "../hooks/context_providers/SnackbarProvider";
|
|||||||
import { AsyncWidget } from "../widgets/AsyncWidget";
|
import { AsyncWidget } from "../widgets/AsyncWidget";
|
||||||
import { SolarEnergyRouteContainer } from "../widgets/SolarEnergyRouteContainer";
|
import { SolarEnergyRouteContainer } from "../widgets/SolarEnergyRouteContainer";
|
||||||
import { TimeWidget } from "../widgets/TimeWidget";
|
import { TimeWidget } from "../widgets/TimeWidget";
|
||||||
|
import CheckIcon from "@mui/icons-material/Check";
|
||||||
|
|
||||||
export function PendingDevicesRoute(): React.ReactElement {
|
export function PendingDevicesRoute(): React.ReactElement {
|
||||||
const loadKey = React.useRef(1);
|
const loadKey = React.useRef(1);
|
||||||
@ -57,6 +59,21 @@ function PendingDevicesList(p: {
|
|||||||
const snackbar = useSnackbar();
|
const snackbar = useSnackbar();
|
||||||
const loadingMessage = useLoadingMessage();
|
const loadingMessage = useLoadingMessage();
|
||||||
|
|
||||||
|
const validateDevice = async (d: Device) => {
|
||||||
|
try {
|
||||||
|
loadingMessage.show("Validating device...");
|
||||||
|
await DeviceApi.Validate(d);
|
||||||
|
|
||||||
|
snackbar("The device has been successfully validated!");
|
||||||
|
p.onReload();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(`Failed to validate device! ${e})`);
|
||||||
|
alert("Failed to validate device!");
|
||||||
|
} finally {
|
||||||
|
loadingMessage.hide();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const deleteDevice = async (d: Device) => {
|
const deleteDevice = async (d: Device) => {
|
||||||
try {
|
try {
|
||||||
if (
|
if (
|
||||||
@ -109,9 +126,16 @@ function PendingDevicesList(p: {
|
|||||||
<TimeWidget time={dev.time_create} />
|
<TimeWidget time={dev.time_create} />
|
||||||
</TableCell>
|
</TableCell>
|
||||||
<TableCell>
|
<TableCell>
|
||||||
<IconButton onClick={() => deleteDevice(dev)}>
|
<Tooltip title="Validate device">
|
||||||
<DeleteIcon />
|
<IconButton onClick={() => validateDevice(dev)}>
|
||||||
</IconButton>
|
<CheckIcon />
|
||||||
|
</IconButton>
|
||||||
|
</Tooltip>
|
||||||
|
<Tooltip title="Delete device">
|
||||||
|
<IconButton onClick={() => deleteDevice(dev)}>
|
||||||
|
<DeleteIcon />
|
||||||
|
</IconButton>
|
||||||
|
</Tooltip>
|
||||||
</TableCell>
|
</TableCell>
|
||||||
</TableRow>
|
</TableRow>
|
||||||
))}
|
))}
|
||||||
|
Loading…
Reference in New Issue
Block a user