diff --git a/.github/workflows/audit.yml b/.github/workflows/audit.yml index d2d3c0e..4e78f8a 100644 --- a/.github/workflows/audit.yml +++ b/.github/workflows/audit.yml @@ -13,9 +13,9 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - - name: Run audit - uses: actions-rs/audit-check@v1 - with: - token: ${{ secrets.GITHUB_TOKEN }} + - uses: actions/checkout@v2 + + - name: Run audit + uses: actions-rs/audit-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index a61dfff..93eb48a 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -13,16 +13,13 @@ jobs: - name: Copy Repo Files uses: actions/checkout@master - - - name: Set up QEMU + - name: Set up QEMU uses: docker/setup-qemu-action@v1 - - - name: Set up Docker Buildx + - name: Set up Docker Buildx uses: docker/setup-buildx-action@v1 - - - name: Login to GitHub Container Registry + - name: Login to GitHub Container Registry uses: docker/login-action@v1 with: registry: ghcr.io @@ -36,8 +33,7 @@ jobs: username: ${{ secrets.PORTUS_USERNAME }} password: ${{ secrets.PORTUS_PASSWORD }} - - - name: Build and push + - name: Build and push uses: docker/build-push-action@v2 with: context: . diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 7fdcafb..44e0c09 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -15,20 +15,20 @@ jobs: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v2 - - - name: Cache build data - uses: actions/cache@v2 - with: - path: | - target - ~/.cargo/ - key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }} - restore-keys: | - ${{ runner.os }}-cargo- - - - name: Build - run: cargo build --verbose - - - name: Run tests - run: cargo test --verbose + - uses: actions/checkout@v2 + + - name: Cache build data + uses: actions/cache@v2 + with: + path: | + target + ~/.cargo/ + key: ${{ runner.os }}-cargo-${{ hashFiles('Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-cargo- + + - name: Build + run: cargo build --verbose + + - name: Run tests + run: cargo test --verbose diff --git a/Cargo.toml b/Cargo.toml index 0936796..5d3d8b7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,10 +12,10 @@ msgrpc = "0.1.0" postgres = "0.17.5" serde_postgres = "0.2.0" dotenv = "0.15.0" -serde = {version = "1.0.115", features = ["serde_derive"]} +serde = { version = "1.0.115", features = ["serde_derive"] } rand = "0.7.3" bcrypt = "0.8.2" -zeroize = {version = "1.1.0", features = ["zeroize_derive"]} +zeroize = { version = "1.1.0", features = ["zeroize_derive"] } byteorder = "1.3.4" rmp-serde = "0.14.4" rmp = "0.8.9" diff --git a/src/database/mod.rs b/src/database/mod.rs index b037ffb..f55dbba 100644 --- a/src/database/mod.rs +++ b/src/database/mod.rs @@ -1,3 +1,12 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + +use dotenv; +use postgres::NoTls; +use r2d2::Pool; +use r2d2_postgres::PostgresConnectionManager; + use crate::database::models::CreatePermissionsEntry; use crate::database::permissions::{Permissions, DEFAULT_PERMISSIONS}; use crate::database::role_permissions::RolePermissions; @@ -5,10 +14,6 @@ use crate::database::roles::Roles; use crate::database::user_roles::UserRoles; use crate::database::users::Users; use crate::utils::error::DatabaseResult; -use dotenv; -use postgres::NoTls; -use r2d2::Pool; -use r2d2_postgres::PostgresConnectionManager; pub mod models; pub mod permissions; diff --git a/src/database/models.rs b/src/database/models.rs index c89ab31..6014bca 100644 --- a/src/database/models.rs +++ b/src/database/models.rs @@ -1,3 +1,7 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + use postgres::Row; use serde::{Deserialize, Serialize}; use zeroize::Zeroize; diff --git a/src/database/permissions.rs b/src/database/permissions.rs index 4cdcb5b..60a8c81 100644 --- a/src/database/permissions.rs +++ b/src/database/permissions.rs @@ -1,3 +1,7 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + use crate::database::models::{CreatePermissionsEntry, Permission}; use crate::database::{DatabaseResult, PostgresPool, Table, ADMIN_ROLE_NAME}; diff --git a/src/database/role_permissions.rs b/src/database/role_permissions.rs index 52030fa..3c2361a 100644 --- a/src/database/role_permissions.rs +++ b/src/database/role_permissions.rs @@ -1,3 +1,7 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + use crate::database::models::Permission; use crate::database::{DatabaseResult, PostgresPool, Table}; use crate::utils::error::DBError; @@ -34,7 +38,7 @@ impl RolePermissions { pub fn by_role(&self, role_id: i32) -> DatabaseResult> { let mut connection = self.pool.get()?; let rows = connection.query( - "SELECT * FROM role_permissions, permissions WHERE role_id = $1 AND role_permissions.permission_id = permissions.id", + "SELECT * FROM role_permissions, permissions WHERE role_id = $1 AND role_permissions.permission_id = permissions.id", &[&role_id])?; serde_postgres::from_rows(&rows).map_err(DBError::from) diff --git a/src/database/roles.rs b/src/database/roles.rs index d8f49bf..e723046 100644 --- a/src/database/roles.rs +++ b/src/database/roles.rs @@ -1,3 +1,7 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + use crate::database::models::Role; use crate::database::role_permissions::RolePermissions; use crate::database::{DatabaseResult, PostgresPool, Table, DEFAULT_ADMIN_EMAIL, ENV_ADMIN_EMAIL}; diff --git a/src/database/tokens.rs b/src/database/tokens.rs index d613ab7..1317353 100644 --- a/src/database/tokens.rs +++ b/src/database/tokens.rs @@ -1,10 +1,16 @@ -use crate::utils::{create_user_token, get_user_id_from_token, TOKEN_LENGTH}; -use serde::Serialize; +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + use std::cmp::{max, min}; use std::collections::HashMap; use std::time::Instant; + +use serde::Serialize; use zeroize::Zeroize; +use crate::utils::{create_user_token, get_user_id_from_token, TOKEN_LENGTH}; + const REQUEST_TOKEN_EXPIRE_SECONDS: u32 = 60 * 10; const REFRESH_TOKEN_EXPIRE_SECONDS: u32 = 60 * 60 * 24; diff --git a/src/database/user_roles.rs b/src/database/user_roles.rs index 188fa4a..e8916a7 100644 --- a/src/database/user_roles.rs +++ b/src/database/user_roles.rs @@ -1,3 +1,7 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + use crate::database::models::Role; use crate::database::{DatabaseResult, PostgresPool, Table}; use crate::utils::error::DBError; diff --git a/src/database/users.rs b/src/database/users.rs index 40d00e1..0c67622 100644 --- a/src/database/users.rs +++ b/src/database/users.rs @@ -1,3 +1,12 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + +use std::sync::Arc; + +use parking_lot::Mutex; +use zeroize::{Zeroize, Zeroizing}; + use crate::database::models::UserRecord; use crate::database::tokens::{SessionTokens, TokenStore}; use crate::database::user_roles::UserRoles; @@ -5,10 +14,6 @@ use crate::database::{DatabaseResult, PostgresPool, Table}; use crate::utils::error::DBError; use crate::utils::{create_salt, hash_password}; -use parking_lot::Mutex; -use std::sync::Arc; -use zeroize::{Zeroize, Zeroizing}; - /// Table that stores users with their email addresses and hashed passwords #[derive(Clone)] pub struct Users { diff --git a/src/lib.rs b/src/lib.rs index 6496c66..351018c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,7 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + #[macro_use] extern crate rouille; diff --git a/src/main.rs b/src/main.rs index 2753d68..0d27340 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,13 +1,19 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + +use std::thread; +use std::thread::Builder; + use chrono::Local; use colored::Colorize; use crossbeam_utils::sync::WaitGroup; use env_logger::Env; +use log::Level; + use flotte_user_management::database::Database; use flotte_user_management::server::http_server::UserHttpServer; use flotte_user_management::server::user_rpc::UserRpcServer; -use log::Level; -use std::thread; -use std::thread::Builder; fn main() { init_logger(); diff --git a/src/server/http_server.rs b/src/server/http_server.rs index c06f634..d8524ae 100644 --- a/src/server/http_server.rs +++ b/src/server/http_server.rs @@ -1,3 +1,16 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + +use std::error::Error; +use std::fmt::{self, Display}; +use std::io::Read; + +use regex::Regex; +use rouille::{Request, Response, Server}; +use serde::export::Formatter; +use serde::Serialize; + use crate::database::permissions::{CREATE_ROLE_PERMISSION, VIEW_ROLE_PERMISSION}; use crate::database::Database; use crate::server::messages::{ @@ -5,13 +18,6 @@ use crate::server::messages::{ }; use crate::utils::error::DBError; use crate::utils::get_user_id_from_token; -use regex::Regex; -use rouille::{Request, Response, Server}; -use serde::export::Formatter; -use serde::Serialize; -use std::error::Error; -use std::fmt::{self, Display}; -use std::io::Read; macro_rules! require_permission { ($database:expr,$request:expr,$permission:expr) => { @@ -43,6 +49,7 @@ impl Display for HTTPError { write!(f, "{}", self.message) } } + impl Error for HTTPError {} impl From for HTTPError { diff --git a/src/server/messages.rs b/src/server/messages.rs index 2fdcdea..98c9f51 100644 --- a/src/server/messages.rs +++ b/src/server/messages.rs @@ -1,12 +1,18 @@ -use crate::database::models::{CreatePermissionsEntry, Permission}; -use crate::utils::error::DBError; -use serde::export::Formatter; -use serde::{Deserialize, Serialize}; +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + use std::error::Error; use std::fmt; use std::fmt::Display; + +use serde::export::Formatter; +use serde::{Deserialize, Serialize}; use zeroize::Zeroize; +use crate::database::models::{CreatePermissionsEntry, Permission}; +use crate::utils::error::DBError; + #[derive(Deserialize)] pub struct TokenRequest { pub token: String, @@ -28,6 +34,7 @@ impl Display for ErrorMessage { write!(f, "{}", self.message) } } + impl Error for ErrorMessage {} impl From for ErrorMessage { diff --git a/src/server/mod.rs b/src/server/mod.rs index 7044df8..ba9fc5d 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -1,3 +1,7 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + pub mod http_server; pub mod messages; pub mod rpc_methods; diff --git a/src/server/rpc_methods.rs b/src/server/rpc_methods.rs index fe45e05..92d19a4 100644 --- a/src/server/rpc_methods.rs +++ b/src/server/rpc_methods.rs @@ -1,4 +1,9 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + #![allow(dead_code)] + pub(crate) const NULL: [u8; 4] = [0x00, 0x00, 0x00, 0x00]; pub(crate) const ERROR: [u8; 4] = [0x0F, 0x0F, 0x0F, 0x0F]; pub(crate) const INFO: [u8; 4] = [0x49, 0x4e, 0x46, 0x4f]; diff --git a/src/server/user_rpc.rs b/src/server/user_rpc.rs index 71864cb..1168852 100644 --- a/src/server/user_rpc.rs +++ b/src/server/user_rpc.rs @@ -1,18 +1,25 @@ -use super::rpc_methods::*; +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + +use std::collections::HashMap; +use std::sync::Arc; +use std::thread::Builder; + +use msgrpc::message::Message; +use msgrpc::server::RpcServer; +use rmp_serde::Deserializer; +use scheduled_thread_pool::ScheduledThreadPool; +use serde::Deserialize; + use crate::database::Database; use crate::server::messages::{ CreatePermissionsRequest, CreateRoleRequest, ErrorMessage, GetPermissionsRequest, InfoEntry, TokenRequest, }; use crate::utils::get_user_id_from_token; -use msgrpc::message::Message; -use msgrpc::server::RpcServer; -use rmp_serde::Deserializer; -use scheduled_thread_pool::ScheduledThreadPool; -use serde::Deserialize; -use std::collections::HashMap; -use std::sync::Arc; -use std::thread::Builder; + +use super::rpc_methods::*; const RPC_SERVER_ADDRESS: &str = "RPC_SERVER_ADDRESS"; const DEFAULT_SERVER_ADDRESS: &str = "127.0.0.1:5555"; diff --git a/src/utils/error.rs b/src/utils/error.rs index ad0e375..ea84ea7 100644 --- a/src/utils/error.rs +++ b/src/utils/error.rs @@ -1,8 +1,13 @@ -use r2d2::Error; -use serde_postgres::DeError; +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + use std::error; use std::fmt::{self, Display, Formatter}; +use r2d2::Error; +use serde_postgres::DeError; + #[derive(Debug)] pub enum DBError { Postgres(PostgresError), diff --git a/src/utils/mod.rs b/src/utils/mod.rs index c31a509..7a81780 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -1,7 +1,12 @@ +// flotte-user-management server for managing users, roles and permissions +// Copyright (C) 2020 trivernis +// See LICENSE for more information + +use std::panic; + use bcrypt::DEFAULT_COST; use byteorder::{BigEndian, ByteOrder}; use rand::Rng; -use std::panic; pub mod error;