From b3e83f45862be097b23a8ca423350c2fd235f7e7 Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Thu, 11 May 2023 13:35:10 -0400 Subject: [PATCH] code cleanup --- trifid-api/src/config.rs | 4 +-- trifid-api/src/routes/v1/hosts.rs | 34 ++++++++++++------- ...0511_create_table_host_enrollment_codes.rs | 19 ++++++++--- 3 files changed, 37 insertions(+), 20 deletions(-) diff --git a/trifid-api/src/config.rs b/trifid-api/src/config.rs index 5f26f5e..9d06334 100644 --- a/trifid-api/src/config.rs +++ b/trifid-api/src/config.rs @@ -82,7 +82,7 @@ pub struct TrifidConfigTokens { #[serde(default = "mfa_tokens_expiry_time")] pub mfa_tokens_expiry_time_seconds: u64, #[serde(default = "enrollment_tokens_expiry_time")] - pub enrollment_tokens_expiry_time: u64 + pub enrollment_tokens_expiry_time: u64, } #[derive(Serialize, Deserialize, Debug)] @@ -119,4 +119,4 @@ fn mfa_tokens_expiry_time() -> u64 { } // 10 minutes fn enrollment_tokens_expiry_time() -> u64 { 600 -} // 10 minutes \ No newline at end of file +} // 10 minutes diff --git a/trifid-api/src/routes/v1/hosts.rs b/trifid-api/src/routes/v1/hosts.rs index a0978e2..b195133 100644 --- a/trifid-api/src/routes/v1/hosts.rs +++ b/trifid-api/src/routes/v1/hosts.rs @@ -56,6 +56,7 @@ // This endpoint requires the `definednetworking` extension to be enabled to be used. use crate::auth_tokens::{enforce_2fa, enforce_api_token, TokenInfo}; +use crate::config::CONFIG; use crate::cursor::Cursor; use crate::error::{APIError, APIErrorsResponse}; use crate::routes::v1::trifid::SUPPORTED_EXTENSIONS; @@ -76,7 +77,6 @@ use std::net::{Ipv4Addr, SocketAddrV4}; use std::str::FromStr; use std::time::{SystemTime, UNIX_EPOCH}; use trifid_api_entities::entity::{host, host_static_address, network, organization}; -use crate::config::CONFIG; #[derive(Serialize, Deserialize)] pub struct ListHostsRequestOpts { @@ -1801,26 +1801,30 @@ pub async fn block_host( pub struct CodeResponse { pub code: String, #[serde(rename = "lifetimeSeconds")] - pub lifetime_seconds: u64 + pub lifetime_seconds: u64, } #[derive(Serialize, Deserialize)] pub struct EnrollmentCodeResponse { pub data: EnrollmentCodeResponseData, - pub metadata: EnrollmentCodeResponseMetadata + pub metadata: EnrollmentCodeResponseMetadata, } #[derive(Serialize, Deserialize)] pub struct EnrollmentCodeResponseData { #[serde(rename = "enrollmentCode")] - pub enrollment_code: CodeResponse + pub enrollment_code: CodeResponse, } #[derive(Serialize, Deserialize)] pub struct EnrollmentCodeResponseMetadata {} #[post("/v1/hosts/{host_id}/enrollment-code")] -pub async fn enroll_host(id: Path, req_info: HttpRequest, db: Data) -> HttpResponse { +pub async fn enroll_host( + id: Path, + req_info: HttpRequest, + db: Data, +) -> HttpResponse { let session_info = enforce_2fa(&req_info, &db.conn) .await .unwrap_or(TokenInfo::NotPresent); @@ -1964,8 +1968,8 @@ pub async fn enroll_host(id: Path, req_info: HttpRequest, db: Data, req_info: HttpRequest, db: Data, req_info: HttpRequest, db: Data, @@ -2307,8 +2315,8 @@ pub async fn create_host_and_enrollment_code( enrollment_code: CodeResponse { code: code.id, lifetime_seconds: CONFIG.tokens.enrollment_tokens_expiry_time, - } + }, }, metadata: CreateHostAndCodeResponseMetadata {}, }) -} \ No newline at end of file +} diff --git a/trifid-api/trifid_api_migration/src/m20230511_120511_create_table_host_enrollment_codes.rs b/trifid-api/trifid_api_migration/src/m20230511_120511_create_table_host_enrollment_codes.rs index aca855f..8d8fd7e 100644 --- a/trifid-api/trifid_api_migration/src/m20230511_120511_create_table_host_enrollment_codes.rs +++ b/trifid-api/trifid_api_migration/src/m20230511_120511_create_table_host_enrollment_codes.rs @@ -1,5 +1,5 @@ -use sea_orm_migration::prelude::*; use crate::m20230427_170037_create_table_hosts::Host; +use sea_orm_migration::prelude::*; #[derive(DeriveMigrationName)] pub struct Migration; @@ -11,15 +11,24 @@ impl MigrationTrait for Migration { .create_table( Table::create() .table(HostEnrollmentCode::Table) - .col(ColumnDef::new(HostEnrollmentCode::Id).string().not_null().primary_key()) + .col( + ColumnDef::new(HostEnrollmentCode::Id) + .string() + .not_null() + .primary_key(), + ) .col(ColumnDef::new(HostEnrollmentCode::Host).string().not_null()) - .col(ColumnDef::new(HostEnrollmentCode::ExpiresOn).big_integer().not_null()) + .col( + ColumnDef::new(HostEnrollmentCode::ExpiresOn) + .big_integer() + .not_null(), + ) .foreign_key( ForeignKey::create() .from(HostEnrollmentCode::Table, HostEnrollmentCode::Host) .to(Host::Table, Host::Id) .on_update(ForeignKeyAction::Cascade) - .on_delete(ForeignKeyAction::Cascade) + .on_delete(ForeignKeyAction::Cascade), ) .to_owned(), ) @@ -39,5 +48,5 @@ pub enum HostEnrollmentCode { Table, Id, Host, - ExpiresOn + ExpiresOn, }