diff --git a/Cargo.lock b/Cargo.lock index a7285a5..4fa7427 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -880,6 +880,7 @@ dependencies = [ "byteorder", "diesel_derives", "itoa", + "serde_json", ] [[package]] diff --git a/trifid-api/Cargo.toml b/trifid-api/Cargo.toml index 1d56761..5e8cdb8 100644 --- a/trifid-api/Cargo.toml +++ b/trifid-api/Cargo.toml @@ -19,7 +19,7 @@ serde_json = "1" toml = "0.8" log = "0.4" env_logger = "0.10" -diesel = { version = "2" } +diesel = { version = "2", features = ["serde_json"] } diesel-async = { version = "0.4", features = ["postgres", "bb8", "async-connection-wrapper"] } diesel_migrations = "2" bb8 = "0.8" diff --git a/trifid-api/migrations/2023-11-24-182032_create_organizations/down.sql b/trifid-api/migrations/2023-11-24-182032_create_organizations/down.sql new file mode 100644 index 0000000..c1631f2 --- /dev/null +++ b/trifid-api/migrations/2023-11-24-182032_create_organizations/down.sql @@ -0,0 +1 @@ +DROP TABLE organizations; \ No newline at end of file diff --git a/trifid-api/migrations/2023-11-24-182032_create_organizations/up.sql b/trifid-api/migrations/2023-11-24-182032_create_organizations/up.sql new file mode 100644 index 0000000..933ad41 --- /dev/null +++ b/trifid-api/migrations/2023-11-24-182032_create_organizations/up.sql @@ -0,0 +1,5 @@ +CREATE TABLE organizations ( + id VARCHAR NOT NULL PRIMARY KEY, + owner_id VARCHAR NOT NULL UNIQUE REFERENCES users(id), + name VARCHAR NOT NULL +); \ No newline at end of file diff --git a/trifid-api/migrations/2023-11-24-184336_create_signing_cas/down.sql b/trifid-api/migrations/2023-11-24-184336_create_signing_cas/down.sql new file mode 100644 index 0000000..0653479 --- /dev/null +++ b/trifid-api/migrations/2023-11-24-184336_create_signing_cas/down.sql @@ -0,0 +1 @@ +DROP TABLE signing_cas; \ No newline at end of file diff --git a/trifid-api/migrations/2023-11-24-184336_create_signing_cas/up.sql b/trifid-api/migrations/2023-11-24-184336_create_signing_cas/up.sql new file mode 100644 index 0000000..629a10c --- /dev/null +++ b/trifid-api/migrations/2023-11-24-184336_create_signing_cas/up.sql @@ -0,0 +1,11 @@ +CREATE TABLE signing_cas +( + id VARCHAR NOT NULL PRIMARY KEY, + pem VARCHAR NOT NULL, + cert jsonb NOT NULL, + expires_at TIMESTAMP NOT NULL, + organization_id VARCHAR NOT NULL REFERENCES organizations(id), + salt bytea NOT NULL, + info bytea NOT NULL, + private_key bytea NOT NULL +); \ No newline at end of file diff --git a/trifid-api/migrations/2023-11-24-191527_create_networks/down.sql b/trifid-api/migrations/2023-11-24-191527_create_networks/down.sql new file mode 100644 index 0000000..109187f --- /dev/null +++ b/trifid-api/migrations/2023-11-24-191527_create_networks/down.sql @@ -0,0 +1 @@ +DROP TABLE networks; \ No newline at end of file diff --git a/trifid-api/migrations/2023-11-24-191527_create_networks/up.sql b/trifid-api/migrations/2023-11-24-191527_create_networks/up.sql new file mode 100644 index 0000000..f9278ff --- /dev/null +++ b/trifid-api/migrations/2023-11-24-191527_create_networks/up.sql @@ -0,0 +1,10 @@ +CREATE TABLE networks +( + id VARCHAR NOT NULL PRIMARY KEY, + cidr VARCHAR NOT NULL, + organization_id VARCHAR NOT NULL REFERENCES organizations(id), + signing_ca_id VARCHAR NOT NULL REFERENCES signing_cas(id), + created_at TIMESTAMP NOT NULL, + name VARCHAR NOT NULL, + lighthouses_as_relays BOOLEAN NOT NULL +); \ No newline at end of file diff --git a/trifid-api/src/models.rs b/trifid-api/src/models.rs index 8045031..adad667 100644 --- a/trifid-api/src/models.rs +++ b/trifid-api/src/models.rs @@ -1,5 +1,6 @@ use diesel::{Associations, Identifiable, Insertable, Queryable, Selectable}; use std::time::SystemTime; +use serde_json::Value; #[derive(Queryable, Selectable, Insertable, Identifiable, Debug, PartialEq, Clone)] #[diesel(table_name = crate::schema::users)] @@ -60,3 +61,70 @@ pub struct AuthToken { pub user_id: String, pub expires: SystemTime, } + +#[derive( +Queryable, Selectable, Insertable, Identifiable, Associations, Debug, PartialEq, Clone, +)] +#[diesel(belongs_to(User, foreign_key = owner_id))] +#[diesel(table_name = crate::schema::organizations)] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct Organization { + pub id: String, + pub owner_id: String, + pub name: String +} + +/* +id -> Varchar, + pem -> Varchar, + cert -> Jsonb, + expires_at -> Timestamp, + organization_id -> Varchar, + salt -> Bytea, + info -> Bytea, + private_key -> Bytea, + */ + +#[derive( +Queryable, Selectable, Insertable, Identifiable, Associations, Debug, PartialEq, Clone, +)] +#[diesel(belongs_to(Organization))] +#[diesel(table_name = crate::schema::signing_cas)] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct SigningCA { + pub id: String, + pub pem: String, + pub cert: Value, + pub expires_at: SystemTime, + pub organization_id: String, + pub salt: Vec, + pub info: Vec, + pub private_key: Vec +} + +/* +id VARCHAR NOT NULL PRIMARY KEY, + cidr VARCHAR NOT NULL, + organization_id VARCHAR NOT NULL REFERENCES organizations(id), + signing_ca_id VARCHAR NOT NULL REFERENCES signing_cas(id), + created_at TIMESTAMP NOT NULL, + name VARCHAR NOT NULL, + lighthouses_as_relays BOOLEAN + */ + +#[derive( +Queryable, Selectable, Insertable, Identifiable, Associations, Debug, PartialEq, Clone, +)] +#[diesel(belongs_to(Organization))] +#[diesel(belongs_to(SigningCA, foreign_key = signing_ca_id))] +#[diesel(table_name = crate::schema::networks)] +#[diesel(check_for_backend(diesel::pg::Pg))] +pub struct Network { + pub id: String, + pub cidr: String, + pub organization_id: String, + pub signing_ca_id: String, + pub created_at: SystemTime, + pub name: String, + pub lighthouses_as_relays: bool +} \ No newline at end of file diff --git a/trifid-api/src/routes/v1/mod.rs b/trifid-api/src/routes/v1/mod.rs index 1abc840..8a4c1cc 100644 --- a/trifid-api/src/routes/v1/mod.rs +++ b/trifid-api/src/routes/v1/mod.rs @@ -2,3 +2,4 @@ pub mod auth; pub mod signup; pub mod totp_authenticators; pub mod verify_totp_authenticator; +pub mod networks; diff --git a/trifid-api/src/routes/v1/networks.rs b/trifid-api/src/routes/v1/networks.rs new file mode 100644 index 0000000..f550861 --- /dev/null +++ b/trifid-api/src/routes/v1/networks.rs @@ -0,0 +1,16 @@ +use actix_web::HttpRequest; +use actix_web::web::Json; +use serde::Deserialize; +use crate::AppState; + +#[derive(Deserialize, Debug)] +pub struct CreateNetworkReq { + pub cidr: String, + pub name: String +} + +pub struct CreateNetworkResp { + +} + +pub async fn create_network_req(req: Json, state: Data, req_info: HttpRequest) \ No newline at end of file diff --git a/trifid-api/src/schema.rs b/trifid-api/src/schema.rs index d6cc5b6..be252bc 100644 --- a/trifid-api/src/schema.rs +++ b/trifid-api/src/schema.rs @@ -16,6 +16,26 @@ diesel::table! { } } +diesel::table! { + networks (id) { + id -> Varchar, + cidr -> Varchar, + organization_id -> Varchar, + signing_ca_id -> Varchar, + created_at -> Timestamp, + name -> Varchar, + lighthouses_as_relays -> Bool, + } +} + +diesel::table! { + organizations (id) { + id -> Varchar, + owner_id -> Varchar, + name -> Varchar, + } +} + diesel::table! { session_tokens (id) { id -> Varchar, @@ -24,6 +44,19 @@ diesel::table! { } } +diesel::table! { + signing_cas (id) { + id -> Varchar, + pem -> Varchar, + cert -> Jsonb, + expires_at -> Timestamp, + organization_id -> Varchar, + salt -> Bytea, + info -> Bytea, + private_key -> Bytea, + } +} + diesel::table! { totp_authenticators (id) { id -> Varchar, @@ -45,13 +78,20 @@ diesel::table! { diesel::joinable!(auth_tokens -> users (user_id)); diesel::joinable!(magic_links -> users (user_id)); +diesel::joinable!(networks -> organizations (organization_id)); +diesel::joinable!(networks -> signing_cas (signing_ca_id)); +diesel::joinable!(organizations -> users (owner_id)); diesel::joinable!(session_tokens -> users (user_id)); +diesel::joinable!(signing_cas -> organizations (organization_id)); diesel::joinable!(totp_authenticators -> users (user_id)); diesel::allow_tables_to_appear_in_same_query!( auth_tokens, magic_links, + networks, + organizations, session_tokens, + signing_cas, totp_authenticators, users, );