Skip to content
This repository was archived by the owner on Sep 25, 2019. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
use std::process::Command;

fn main() {
let output = Command::new("sh").arg("./scripts/build-version.sh").output().unwrap();
println!("cargo:rustc-env=BUILD_VERSION={}", String::from_utf8(output.stdout).unwrap());
}
let output = Command::new("sh")
.arg("./scripts/build-version.sh")
.output()
.unwrap();
println!(
"cargo:rustc-env=BUILD_VERSION={}",
String::from_utf8(output.stdout).unwrap()
);
}
28 changes: 14 additions & 14 deletions create_snapshot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,22 @@ pub unsafe extern "C" fn c_get_next_stream_id() -> c_uint {
}

fn main() {
unsafe { js_init() };
unsafe { js_init() };

let args: Vec<String> = env::args().collect();
let filename = args[1].as_str();
let mut file = File::open(filename).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let cfilename = CString::new(filename).unwrap();
let ccontents = CString::new(contents).unwrap();
let snap = unsafe { js_create_snapshot(cfilename.as_ptr(), ccontents.as_ptr()) };
let args: Vec<String> = env::args().collect();
let filename = args[1].as_str();
let mut file = File::open(filename).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
let cfilename = CString::new(filename).unwrap();
let ccontents = CString::new(contents).unwrap();
let snap = unsafe { js_create_snapshot(cfilename.as_ptr(), ccontents.as_ptr()) };

let bytes: Vec<u8> =
unsafe { slice::from_raw_parts(snap.ptr as *const u8, snap.len as usize) }.to_vec();
let bytes: Vec<u8> =
unsafe { slice::from_raw_parts(snap.ptr as *const u8, snap.len as usize) }.to_vec();

let out = &args[2];
let mut outfile = File::create(&Path::new(out.as_str())).unwrap();
let out = &args[2];
let mut outfile = File::create(&Path::new(out.as_str())).unwrap();

outfile.write_all(bytes.as_slice()).unwrap();
outfile.write_all(bytes.as_slice()).unwrap();
}
90 changes: 45 additions & 45 deletions distributed-fly/src/kms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,67 @@ use r2d2_redis::redis;
use sha2::{Digest, Sha256};

lazy_static! {
static ref KMS_CLIENT: KmsClient = KmsClient::new(Region::UsEast1);
static ref KMS_CLIENT: KmsClient = KmsClient::new(Region::UsEast1);
}

static CACHE_PREFIX: &str = "local:kms:";

pub fn decrypt(blob: Vec<u8>) -> Result<Option<Vec<u8>>, rusoto_kms::DecryptError> {
debug!("decrypting value via KMS");
if let Some(plaintext) = plaintext_cache(&blob) {
debug!("found in cache, aborting decryption");
return Ok(Some(plaintext));
}
let req = DecryptRequest {
ciphertext_blob: blob.clone(),
encryption_context: None,
grant_tokens: None,
};
let res = KMS_CLIENT.decrypt(req).sync()?;
if let Some(plaintext) = res.plaintext {
set_plaintext_cache(&blob, &plaintext);
Ok(Some(plaintext))
} else {
Ok(None)
}
debug!("decrypting value via KMS");
if let Some(plaintext) = plaintext_cache(&blob) {
debug!("found in cache, aborting decryption");
return Ok(Some(plaintext));
}
let req = DecryptRequest {
ciphertext_blob: blob.clone(),
encryption_context: None,
grant_tokens: None,
};
let res = KMS_CLIENT.decrypt(req).sync()?;
if let Some(plaintext) = res.plaintext {
set_plaintext_cache(&blob, &plaintext);
Ok(Some(plaintext))
} else {
Ok(None)
}
}

fn format_cache_key(blob: &Vec<u8>) -> String {
format!("{}{:x}", CACHE_PREFIX, Sha256::digest(blob.as_slice()))
format!("{}{:x}", CACHE_PREFIX, Sha256::digest(blob.as_slice()))
}

fn set_plaintext_cache(blob: &Vec<u8>, plaintext: &Vec<u8>) {
if let Ok(conn) = REDIS_POOL.get() {
if let Err(e) = redis::cmd("SET")
.arg(format_cache_key(blob))
.arg(plaintext.as_slice())
.query::<()>(&*conn)
{
error!("error setting kms cache key: {}", e);
if let Ok(conn) = REDIS_POOL.get() {
if let Err(e) = redis::cmd("SET")
.arg(format_cache_key(blob))
.arg(plaintext.as_slice())
.query::<()>(&*conn)
{
error!("error setting kms cache key: {}", e);
}
}
}
}

fn plaintext_cache(blob: &Vec<u8>) -> Option<Vec<u8>> {
match REDIS_POOL.get() {
Err(e) => {
error!("error getting redis conn for kms: {}", e);
None
}
Ok(conn) => {
match redis::cmd("GET")
.arg(format_cache_key(blob))
.query::<Option<Vec<u8>>>(&*conn)
{
match REDIS_POOL.get() {
Err(e) => {
error!("error querying redis for kms: {}", e);
None
error!("error getting redis conn for kms: {}", e);
None
}
Ok(conn) => {
match redis::cmd("GET")
.arg(format_cache_key(blob))
.query::<Option<Vec<u8>>>(&*conn)
{
Err(e) => {
error!("error querying redis for kms: {}", e);
None
}
Ok(maybe_vec) => match maybe_vec {
Some(vec) => Some(vec),
None => None,
},
}
}
Ok(maybe_vec) => match maybe_vec {
Some(vec) => Some(vec),
None => None,
},
}
}
}
}
3 changes: 1 addition & 2 deletions distributed-fly/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,7 @@ fn main() {
Conn::Tcp(c) => (c.peer_addr(), false),
Conn::Tls(c) => (c.get_ref().get_ref().peer_addr(), true),
};
let remote_addr = remote_addr
.unwrap_or_else(|_| "0.0.0.0:0".parse().unwrap());
let remote_addr = remote_addr.unwrap_or_else(|_| "0.0.0.0:0".parse().unwrap());
service_fn(move |req| {
serve_http(tls, req, unsafe { SELECTOR.as_ref().unwrap() }, remote_addr)
})
Expand Down
Loading