Skip to content
Merged
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
4 changes: 2 additions & 2 deletions lambda-events/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ keywords = ["lambda", "aws", "amazon", "events", "S3"]
categories = ["api-bindings", "encoding", "web-programming"]

[dependencies]
base64 = "0.13"
base64 = "0.21"
http = "0.2"
http-body = "0.4"
http-serde = "^1"
serde = "^1"
serde_derive = "^1"
serde_with = { version = "^2", features = ["json"], optional = true }
serde_with = { version = "^3", features = ["json"], optional = true }
serde_json = "^1"
serde_dynamo = { version = "^4.1", optional = true }
bytes = { version = "1", features = ["serde"] }
Expand Down
8 changes: 5 additions & 3 deletions lambda-events/src/custom_serde/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[allow(unused)]
use base64::{decode, encode};
use base64::Engine;
use chrono::{DateTime, Duration, TimeZone, Utc};
use serde;
use serde::de::{Deserialize, Deserializer, Error as DeError};
Expand Down Expand Up @@ -122,14 +122,16 @@ where
D: Deserializer<'de>,
{
let s: String = String::deserialize(deserializer)?;
decode(s).map_err(DeError::custom)
base64::engine::general_purpose::STANDARD
.decode(s)
.map_err(DeError::custom)
}

pub(crate) fn serialize_base64<S>(value: &[u8], serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&encode(value))
serializer.serialize_str(&base64::engine::general_purpose::STANDARD.encode(value))
}

/// Deserializes `HashMap<_>`, mapping JSON `null` to an empty map.
Expand Down
12 changes: 10 additions & 2 deletions lambda-events/src/encodings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,14 @@ impl Body {
/// Panics when aws communicates to handler that request is base64 encoded but
/// it can not be base64 decoded
pub fn from_maybe_encoded(is_base64_encoded: bool, body: &str) -> Body {
use base64::Engine;

if is_base64_encoded {
Body::from(::base64::decode(body).expect("failed to decode aws base64 encoded body"))
Body::from(
::base64::engine::general_purpose::STANDARD
.decode(body)
.expect("failed to decode aws base64 encoded body"),
)
} else {
Body::from(body)
}
Expand Down Expand Up @@ -289,7 +295,9 @@ impl Serialize for Body {
Body::Text(data) => {
serializer.serialize_str(::std::str::from_utf8(data.as_ref()).map_err(S::Error::custom)?)
}
Body::Binary(data) => serializer.collect_str(&Base64Display::with_config(data, base64::STANDARD)),
Body::Binary(data) => {
serializer.collect_str(&Base64Display::new(data, &base64::engine::general_purpose::STANDARD))
}
Body::Empty => serializer.serialize_unit(),
}
}
Expand Down
17 changes: 11 additions & 6 deletions lambda-events/src/event/cloudwatch_logs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,17 @@ impl<'de> Deserialize<'de> for AwsLogs {
where
V: MapAccess<'de>,
{
use base64::Engine;

let mut data = None;
while let Some(key) = map.next_key()? {
match key {
"data" => {
let bytes = map
.next_value::<String>()
.and_then(|string| base64::decode(string).map_err(Error::custom))?;
let bytes = map.next_value::<String>().and_then(|string| {
base64::engine::general_purpose::STANDARD
.decode(string)
.map_err(Error::custom)
})?;

let bytes = flate2::read::GzDecoder::new(&bytes[..]);
let mut de = serde_json::Deserializer::from_reader(BufReader::new(bytes));
Expand All @@ -98,7 +102,7 @@ impl Serialize for AwsLogs {
where
S: Serializer,
{
let base = base64::write::EncoderWriter::new(Vec::new(), base64::STANDARD_NO_PAD);
let base = base64::write::EncoderWriter::new(Vec::new(), &base64::engine::general_purpose::STANDARD_NO_PAD);
let mut gzip = flate2::write::GzEncoder::new(base, flate2::Compression::default());

serde_json::to_writer(&mut gzip, &self.data).map_err(SeError::custom)?;
Expand All @@ -125,7 +129,8 @@ mod test {
}
}"#;
let event: LogsEvent = serde_json::from_str(json).expect("failed to deserialize");
let data = event.aws_logs.data.clone();

let data = event.clone().aws_logs.data;
assert_eq!("DATA_MESSAGE", data.message_type);
assert_eq!("123456789012", data.owner);
assert_eq!("/aws/lambda/echo-nodejs", data.log_group);
Expand All @@ -140,7 +145,7 @@ mod test {
assert_eq!(1552518348220, data.log_events[0].timestamp);
assert_eq!("REPORT RequestId: 6234bffe-149a-b642-81ff-2e8e376d8aff\tDuration: 46.84 ms\tBilled Duration: 47 ms \tMemory Size: 192 MB\tMax Memory Used: 72 MB\t\n", data.log_events[0].message);

let new_json = serde_json::to_string_pretty(&event).unwrap();
let new_json: String = serde_json::to_string_pretty(&event).unwrap();
let new_event: LogsEvent = serde_json::from_str(&new_json).expect("failed to deserialize");
assert_eq!(new_event, event);
}
Expand Down
7 changes: 5 additions & 2 deletions lambda-events/src/event/dynamodb/attributes.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use base64::Engine;
use event::serde_dynamo::AttributeValue;
use std::collections::HashMap;

Expand Down Expand Up @@ -62,7 +63,9 @@ mod test {
let attr: AttributeValue = serde_json::from_value(value.clone()).unwrap();
match attr {
AttributeValue::B(ref b) => {
let expected = base64::decode("dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk").unwrap();
let expected = base64::engine::general_purpose::STANDARD
.decode("dGhpcyB0ZXh0IGlzIGJhc2U2NC1lbmNvZGVk")
.unwrap();
assert_eq!(&expected, b)
}
other => panic!("unexpected value {:?}", other),
Expand Down Expand Up @@ -137,7 +140,7 @@ mod test {
AttributeValue::Bs(ref s) => {
let expected = vec!["U3Vubnk=", "UmFpbnk=", "U25vd3k="]
.into_iter()
.flat_map(base64::decode)
.flat_map(|s| base64::engine::general_purpose::STANDARD.decode(s))
.collect::<Vec<_>>();
assert_eq!(&expected, s);
}
Expand Down