code cleanup

This commit is contained in:
c0repwn3r 2023-05-11 13:35:10 -04:00
parent 68c120a5ab
commit b3e83f4586
Signed by: core
GPG Key ID: FDBF740DADDCEECF
3 changed files with 37 additions and 20 deletions

View File

@ -82,7 +82,7 @@ pub struct TrifidConfigTokens {
#[serde(default = "mfa_tokens_expiry_time")] #[serde(default = "mfa_tokens_expiry_time")]
pub mfa_tokens_expiry_time_seconds: u64, pub mfa_tokens_expiry_time_seconds: u64,
#[serde(default = "enrollment_tokens_expiry_time")] #[serde(default = "enrollment_tokens_expiry_time")]
pub enrollment_tokens_expiry_time: u64 pub enrollment_tokens_expiry_time: u64,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]

View File

@ -56,6 +56,7 @@
// This endpoint requires the `definednetworking` extension to be enabled to be used. // This endpoint requires the `definednetworking` extension to be enabled to be used.
use crate::auth_tokens::{enforce_2fa, enforce_api_token, TokenInfo}; use crate::auth_tokens::{enforce_2fa, enforce_api_token, TokenInfo};
use crate::config::CONFIG;
use crate::cursor::Cursor; use crate::cursor::Cursor;
use crate::error::{APIError, APIErrorsResponse}; use crate::error::{APIError, APIErrorsResponse};
use crate::routes::v1::trifid::SUPPORTED_EXTENSIONS; use crate::routes::v1::trifid::SUPPORTED_EXTENSIONS;
@ -76,7 +77,6 @@ use std::net::{Ipv4Addr, SocketAddrV4};
use std::str::FromStr; use std::str::FromStr;
use std::time::{SystemTime, UNIX_EPOCH}; use std::time::{SystemTime, UNIX_EPOCH};
use trifid_api_entities::entity::{host, host_static_address, network, organization}; use trifid_api_entities::entity::{host, host_static_address, network, organization};
use crate::config::CONFIG;
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct ListHostsRequestOpts { pub struct ListHostsRequestOpts {
@ -1801,26 +1801,30 @@ pub async fn block_host(
pub struct CodeResponse { pub struct CodeResponse {
pub code: String, pub code: String,
#[serde(rename = "lifetimeSeconds")] #[serde(rename = "lifetimeSeconds")]
pub lifetime_seconds: u64 pub lifetime_seconds: u64,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct EnrollmentCodeResponse { pub struct EnrollmentCodeResponse {
pub data: EnrollmentCodeResponseData, pub data: EnrollmentCodeResponseData,
pub metadata: EnrollmentCodeResponseMetadata pub metadata: EnrollmentCodeResponseMetadata,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct EnrollmentCodeResponseData { pub struct EnrollmentCodeResponseData {
#[serde(rename = "enrollmentCode")] #[serde(rename = "enrollmentCode")]
pub enrollment_code: CodeResponse pub enrollment_code: CodeResponse,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct EnrollmentCodeResponseMetadata {} pub struct EnrollmentCodeResponseMetadata {}
#[post("/v1/hosts/{host_id}/enrollment-code")] #[post("/v1/hosts/{host_id}/enrollment-code")]
pub async fn enroll_host(id: Path<String>, req_info: HttpRequest, db: Data<AppState>) -> HttpResponse { pub async fn enroll_host(
id: Path<String>,
req_info: HttpRequest,
db: Data<AppState>,
) -> HttpResponse {
let session_info = enforce_2fa(&req_info, &db.conn) let session_info = enforce_2fa(&req_info, &db.conn)
.await .await
.unwrap_or(TokenInfo::NotPresent); .unwrap_or(TokenInfo::NotPresent);
@ -1964,8 +1968,8 @@ pub async fn enroll_host(id: Path<String>, req_info: HttpRequest, db: Data<AppSt
errors: vec![APIError { errors: vec![APIError {
code: "ERR_UNAUTHORIZED".to_string(), code: "ERR_UNAUTHORIZED".to_string(),
message: message:
"This resource does not exist or you do not have permission to access it." "This resource does not exist or you do not have permission to access it."
.to_string(), .to_string(),
path: None, path: None,
}], }],
}) })
@ -2006,7 +2010,12 @@ pub async fn enroll_host(id: Path<String>, req_info: HttpRequest, db: Data<AppSt
}; };
HttpResponse::Ok().json(EnrollmentCodeResponse { HttpResponse::Ok().json(EnrollmentCodeResponse {
data: EnrollmentCodeResponseData { enrollment_code: CodeResponse { code: code.id, lifetime_seconds: CONFIG.tokens.enrollment_tokens_expiry_time } }, data: EnrollmentCodeResponseData {
enrollment_code: CodeResponse {
code: code.id,
lifetime_seconds: CONFIG.tokens.enrollment_tokens_expiry_time,
},
},
metadata: EnrollmentCodeResponseMetadata {}, metadata: EnrollmentCodeResponseMetadata {},
}) })
} }
@ -2014,20 +2023,19 @@ pub async fn enroll_host(id: Path<String>, req_info: HttpRequest, db: Data<AppSt
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct CreateHostAndCodeResponse { pub struct CreateHostAndCodeResponse {
pub data: CreateHostAndCodeResponseData, pub data: CreateHostAndCodeResponseData,
pub metadata: CreateHostAndCodeResponseMetadata pub metadata: CreateHostAndCodeResponseMetadata,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct CreateHostAndCodeResponseData { pub struct CreateHostAndCodeResponseData {
pub host: HostResponse, pub host: HostResponse,
#[serde(rename = "enrollmentCode")] #[serde(rename = "enrollmentCode")]
pub enrollment_code: CodeResponse pub enrollment_code: CodeResponse,
} }
#[derive(Serialize, Deserialize)] #[derive(Serialize, Deserialize)]
pub struct CreateHostAndCodeResponseMetadata {} pub struct CreateHostAndCodeResponseMetadata {}
#[post("/v1/host-and-enrollment-code")] #[post("/v1/host-and-enrollment-code")]
pub async fn create_host_and_enrollment_code( pub async fn create_host_and_enrollment_code(
req: Json<CreateHostRequest>, req: Json<CreateHostRequest>,
@ -2307,7 +2315,7 @@ pub async fn create_host_and_enrollment_code(
enrollment_code: CodeResponse { enrollment_code: CodeResponse {
code: code.id, code: code.id,
lifetime_seconds: CONFIG.tokens.enrollment_tokens_expiry_time, lifetime_seconds: CONFIG.tokens.enrollment_tokens_expiry_time,
} },
}, },
metadata: CreateHostAndCodeResponseMetadata {}, metadata: CreateHostAndCodeResponseMetadata {},
}) })

View File

@ -1,5 +1,5 @@
use sea_orm_migration::prelude::*;
use crate::m20230427_170037_create_table_hosts::Host; use crate::m20230427_170037_create_table_hosts::Host;
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)] #[derive(DeriveMigrationName)]
pub struct Migration; pub struct Migration;
@ -11,15 +11,24 @@ impl MigrationTrait for Migration {
.create_table( .create_table(
Table::create() Table::create()
.table(HostEnrollmentCode::Table) .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::Host).string().not_null())
.col(ColumnDef::new(HostEnrollmentCode::ExpiresOn).big_integer().not_null()) .col(
ColumnDef::new(HostEnrollmentCode::ExpiresOn)
.big_integer()
.not_null(),
)
.foreign_key( .foreign_key(
ForeignKey::create() ForeignKey::create()
.from(HostEnrollmentCode::Table, HostEnrollmentCode::Host) .from(HostEnrollmentCode::Table, HostEnrollmentCode::Host)
.to(Host::Table, Host::Id) .to(Host::Table, Host::Id)
.on_update(ForeignKeyAction::Cascade) .on_update(ForeignKeyAction::Cascade)
.on_delete(ForeignKeyAction::Cascade) .on_delete(ForeignKeyAction::Cascade),
) )
.to_owned(), .to_owned(),
) )
@ -39,5 +48,5 @@ pub enum HostEnrollmentCode {
Table, Table,
Id, Id,
Host, Host,
ExpiresOn ExpiresOn,
} }