Skip to content

Commit

Permalink
cargo fmt and bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
publicarray committed Jul 17, 2023
1 parent bce5a1a commit b19702e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 42 deletions.
2 changes: 1 addition & 1 deletion server/src/api/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub async fn validator(req: ServiceRequest, credentials: BearerAuth) -> Result<S
Ok(req)
}
// required by `actix-web-httpauth` validator signature
Err(e) => Err((e, req))
Err(e) => Err((e, req)),
}
}

Expand Down
9 changes: 6 additions & 3 deletions server/src/api/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ use openpgp::parse::Parse;
use openpgp::serialize::stream::{Armorer, Message, Signer};
extern crate serde_derive;
extern crate serde_qs as qs;
use crate::utils;
use crate::filestorage;
use crate::utils;
use crate::PGP_KEY_PATH;

use actix_web_grants::proc_macro::has_any_role;
use async_std::path::Path;
use async_std::{prelude::*};
use async_std::prelude::*;
use async_tar::Archive;
use futures::StreamExt;
use regex::Regex;
Expand Down Expand Up @@ -268,7 +268,10 @@ pub async fn post(
// move file
let fs_response = match filestorage::store_file(&info, filepath, icon256path).await {
Ok(_) => Ok(()),
Err(e) => Err(e),
Err(e) => {
error!("Could not save file: {:?}", e);
Err(e)
}
};
debug!("File saved! {:?}", fs_response);

Expand Down
24 changes: 16 additions & 8 deletions server/src/filestorage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::models::*;
use anyhow::{Result};
use anyhow::Result;
use s3::Region;
// use async_std::path::PathBuf;
use std::path::PathBuf;
Expand All @@ -15,7 +15,6 @@ use s3::creds::Credentials;
// filepath: temp file path
// icon256path: iconfile
pub async fn store_file(info: &Info, filepath: PathBuf, icon256path: PathBuf) -> Result<()> {

let new_filename = format!(
"{}.v{}.f{}[{}].spk",
info.package,
Expand All @@ -24,7 +23,6 @@ pub async fn store_file(info: &Info, filepath: PathBuf, icon256path: PathBuf) ->
info.arch.replace(' ', "-")
);


if *STORAGE_TYPE == "filesystem" && !STORAGE_PATH.is_empty() {
// path / package name / package revision
trace!("Using filesystem");
Expand All @@ -48,16 +46,25 @@ pub async fn store_file(info: &Info, filepath: PathBuf, icon256path: PathBuf) ->
async_std::fs::copy(filepath, new_filepath).await?;
async_std::fs::copy(icon256path, file_path.join("icon_256.png")).await?;
// S3 API
} else if *STORAGE_TYPE == "s3" && !STORAGE_S3_API.is_empty() && STORAGE_S3_ID.is_empty() && !STORAGE_S3_REGION.is_empty() && !STORAGE_S3_SECRET_KEY.is_empty() && !STORAGE_S3_BUCKET.is_empty() {
} else if *STORAGE_TYPE == "s3"
&& !STORAGE_S3_API.is_empty()
&& !STORAGE_S3_ID.is_empty()
&& !STORAGE_S3_REGION.is_empty()
&& !STORAGE_S3_SECRET_KEY.is_empty()
&& !STORAGE_S3_BUCKET.is_empty()
{
trace!("Using s3 api");
let bucket_name = &**STORAGE_S3_BUCKET;
let region_name = (*STORAGE_S3_REGION).clone();
let endpoint = (*STORAGE_S3_API).clone();
let region = Region::Custom { region: region_name, endpoint };
let region = Region::Custom {
region: region_name,
endpoint,
};
let credentials = Credentials::new(Some(&**STORAGE_S3_ID), Some(&**STORAGE_S3_SECRET_KEY), None, None, None)?;
//let credentials = Credentials::default()?;
let bucket = Bucket::new(bucket_name, region, credentials)?;

let contents = async_std::fs::read(filepath.clone()).await?;
let content = contents.as_slice();
//let content = "I want to go to S3".as_bytes();
Expand All @@ -72,10 +79,11 @@ pub async fn store_file(info: &Info, filepath: PathBuf, icon256path: PathBuf) ->
let s3_response = bucket.put_object(new_filepath, content).await?;
// debug!("s3: api response: {}", s3_response.to_string());
debug!("s3: api response code: {}", s3_response.status_code());
if s3_response.status_code() == 200 {
if s3_response.status_code() != 200 {
//
}

} else {
error!("No storage for files configured!")
}
Ok(())
}
44 changes: 15 additions & 29 deletions server/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ extern crate chrono;
extern crate regex;

extern crate sequoia_openpgp as openpgp;
use openpgp::Cert;
use openpgp::{parse::Parse, serialize::SerializeInto};
use rustls::{Certificate, PrivateKey, ServerConfig};
use rustls_pemfile::{certs, pkcs8_private_keys, rsa_private_keys, ec_private_keys};
use actix_cors::Cors;
use actix_files as fs;
use actix_web::{middleware, web, App, HttpServer};
use actix_web_httpauth::middleware::HttpAuthentication;
use openpgp::Cert;
use openpgp::{parse::Parse, serialize::SerializeInto};
use rustls::{Certificate, PrivateKey, ServerConfig};
use rustls_pemfile::{certs, ec_private_keys, pkcs8_private_keys, rsa_private_keys};
mod claims;

use diesel::r2d2::{self, ConnectionManager};
Expand All @@ -33,8 +33,8 @@ use evmap_derive::ShallowCopy;

use crate::api::*;

pub mod utils;
pub mod filestorage;
pub mod utils;

pub mod api;
pub mod models;
Expand Down Expand Up @@ -158,7 +158,7 @@ async fn main() -> std::io::Result<()> {
let pool = r2d2::Pool::builder()
.build(manager)
.expect("Failed to create database connection pool.");
let mut tls_config:Option<rustls::ServerConfig> = None;
let mut tls_config: Option<rustls::ServerConfig> = None;
if std::path::Path::new(&tls_key).exists() {
tls_config = Some(load_rustls_config(&tls_key, &tls_cert));
}
Expand All @@ -168,7 +168,7 @@ async fn main() -> std::io::Result<()> {

// OpenPGP
//let mut tsk:Option<Cert> = None;
let mut pgp_key:Option<String> = None;
let mut pgp_key: Option<String> = None;
// if file at &PGP_KEY_PATH exists
if std::path::Path::new(&*PGP_KEY_PATH).exists() {
let temp_tsk = Cert::from_file(&*PGP_KEY_PATH)
Expand Down Expand Up @@ -267,52 +267,38 @@ async fn main() -> std::io::Result<()> {

if let Some(tls_config) = tls_config {
info!("TLS=Enabled");
server.bind_rustls(&bind, tls_config)?
.run()
.await
server.bind_rustls(&bind, tls_config)?.run().await
} else {
server.bind(&bind)?
.run()
.await
server.bind(&bind)?.run().await
}
}

use std::fs::File;
use std::io::BufReader;
fn load_rustls_config(key: &String, cert: &String) -> rustls::ServerConfig {
// init server config builder with safe defaults
let config = ServerConfig::builder()
.with_safe_defaults()
.with_no_client_auth();
let config = ServerConfig::builder().with_safe_defaults().with_no_client_auth();

// load TLS key/cert files
let cert_file = &mut BufReader::new(File::open(cert).unwrap());
let key_file = &mut BufReader::new(File::open(key).unwrap());

// convert files to key/cert objects
let cert_chain = certs(cert_file)
.unwrap()
.into_iter()
.map(Certificate)
.collect();
let mut keys: Vec<PrivateKey> = ec_private_keys(key_file)
.unwrap()
.into_iter()
.map(PrivateKey)
.collect();
let cert_chain = certs(cert_file).unwrap().into_iter().map(Certificate).collect();
let mut keys: Vec<PrivateKey> = ec_private_keys(key_file).unwrap().into_iter().map(PrivateKey).collect();
if keys.is_empty() {
keys = pkcs8_private_keys(key_file)
.unwrap()
.into_iter()
.map(PrivateKey)
.collect();
.collect();
}
if keys.is_empty() {
keys = rsa_private_keys(key_file)
.unwrap()
.into_iter()
.map(PrivateKey)
.collect();
.collect();
}
// exit if no keys could be parsed
if keys.is_empty() {
Expand All @@ -321,4 +307,4 @@ fn load_rustls_config(key: &String, cert: &String) -> rustls::ServerConfig {
}

config.with_single_cert(cert_chain, keys.remove(0)).unwrap()
}
}
2 changes: 1 addition & 1 deletion server/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@ pub fn send_email(message: String, subject: String, to: String) {
Ok(_) => debug!("Email sent successfully! {}", subject),
Err(e) => warn!("Could not send email: {:?}", e),
}
}
}

0 comments on commit b19702e

Please sign in to comment.