Skip to content

Commit

Permalink
Fixes #15456: Handle ttl parameter correctly in shared-files API
Browse files Browse the repository at this point in the history
  • Loading branch information
bernsteining committed Aug 14, 2019
1 parent b086bf9 commit a11cb79
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 17 deletions.
23 changes: 11 additions & 12 deletions relay/sources/relayd/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ use crate::{
error::Error,
remote_run::{RemoteRun, RemoteRunTarget},
shared_files::{
file_writer, metadata_hash_checker, metadata_parser, metadata_writer,
parse_parameter_from_raw, parse_ttl, shared_folder_head,
metadata_hash_checker, metadata_parser, parse_parameter_from_raw, put_handler,
shared_folder_head,
},
{stats::Stats, status::Status, JobConfig},
};
Expand Down Expand Up @@ -122,17 +122,16 @@ pub fn api(
.and(warp::body::concat())
.map(
move |ttl: String, peek: filters::path::Peek, mut buf: warp::body::FullBody| {
metadata_writer(
format!(
"{}expires={}\n",
metadata_parser(buf.by_ref()).unwrap(),
parse_ttl(parse_parameter_from_raw(ttl)).unwrap()
reply::with_status(
"".to_string(),
put_handler(
format!("{}", metadata_parser(buf.by_ref()).unwrap()),
peek.as_str(),
parse_parameter_from_raw(ttl),
job_config.clone(),
buf,
),
peek.as_str(),
);

file_writer(buf.by_ref(), peek.as_str());
reply()
)
},
);

Expand Down
60 changes: 55 additions & 5 deletions relay/sources/relayd/src/shared_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
// You should have received a copy of the GNU General Public License
// along with Rudder. If not, see <http://www.gnu.org/licenses/>.

use crate::error::Error;
use crate::{error::Error, JobConfig};
use chrono::prelude::DateTime;
use chrono::{Duration, Utc};
use hyper::StatusCode;
Expand All @@ -46,6 +46,7 @@ use std::io::BufRead;
use std::io::Read;
use std::path::Path;
use std::str::FromStr;
use std::sync::Arc;
use warp::Buf;

pub enum HashType {
Expand Down Expand Up @@ -181,7 +182,13 @@ pub fn validate_signature(
}

pub fn parse_ttl(ttl: String) -> Result<i64, Error> {
let regex_numbers = Regex::new(r"^(?:(?P<days>\d+)(?:d|days|day))?\s*(?:(?P<hours>\d+)(?:h|hours|hour))?\s*(?:(?P<minutes>\d+)(?:m|minutes|minute))?\s*(?:(?P<seconds>\d+)(?:s|seconds|second))?").unwrap();
let regex_timestamp = Regex::new(r"^([0-9]+)$").unwrap();

if regex_timestamp.is_match(&ttl) {
return Ok(ttl.parse::<i64>().unwrap());
};

let regex_numbers = Regex::new(r"^(?:(?P<days>\d+)(?:d|days|day))?\s*(?:(?P<hours>\d+)(?:h|hours|hour))?\s*(?:(?P<minutes>\d+)(?:m|minutes|minute))?\s*(?:(?P<seconds>\d+)(?:s|seconds|second))?$").unwrap();

fn parse_time<'t>(cap: &regex::Captures<'t>, n: &str) -> Result<i64, Error> {
Ok(match cap.name(n) {
Expand Down Expand Up @@ -235,13 +242,39 @@ pub fn file_writer(buf: &mut warp::body::FullBody, path: &str) {
fs::write(format!("shared-files/{}", path), myvec).expect("Unable to write file");
}

pub fn metadata_writer(metadata_string: String, peek: &str) {
pub fn put_handler(
metadata_string: String,
peek: &str,
ttl: String,
job_config: Arc<JobConfig>,
mut buf: warp::body::FullBody,
) -> hyper::StatusCode {
let timestamp = match parse_ttl(ttl) {
Ok(ttl) => ttl,
Err(_x) => return StatusCode::from_u16(500).unwrap(),
};

let myvec: Vec<String> = peek.split('/').map(|s| s.to_string()).collect();
let (target_uuid, source_uuid, _file_id) = (&myvec[0], &myvec[1], &myvec[2]);

if !job_config
.nodes
.read()
.expect("Cannot read nodes list")
.is_subnode(source_uuid)
{
return StatusCode::from_u16(404).unwrap();
}

let _ = fs::create_dir_all(format!("shared-files/{}/{}/", target_uuid, source_uuid)); // on cree les folders s'ils existent pas
//fs::create_dir_all(format!("/var/rudder/configuration-repository/shared-files/{}/{}/", target_uuid, source_uuid)); // real path
fs::write(format!("shared-files/{}.metadata", peek), metadata_string)
.expect("Unable to write file");
fs::write(
format!("shared-files/{}.metadata", peek),
format!("{}expires={}\n", metadata_string, timestamp),
)
.expect("Unable to write file");
file_writer(buf.by_ref(), peek);
StatusCode::from_u16(200).unwrap()
}

pub fn metadata_hash_checker(path: String, hash: String) -> hyper::StatusCode {
Expand Down Expand Up @@ -354,4 +387,21 @@ z5VEb9yx2KikbWyChM1Akp82AV5BzqE80QIBIw==".to_string()).unwrap(),
assert!(validate_signature(data, keypub, HashType::Sha512, &signature).unwrap());
}

#[test]
pub fn it_parses_ttl() {
assert!(parse_ttl("1h 7s".to_string()).is_ok());

assert!(parse_ttl("1hour 2minutes".to_string()).is_ok());

assert!(parse_ttl("1d 7seconds".to_string()).is_ok());

assert_eq!(parse_ttl("9136".to_string()).unwrap(), 9136);

assert!(parse_ttl("913j".to_string()).is_err());

assert!(parse_ttl("913b83".to_string()).is_err());

assert!(parse_ttl("913h 89j".to_string()).is_err());
}

}

0 comments on commit a11cb79

Please sign in to comment.