forked from awslabs/aws-lambda-rust-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcloudtrail.rs
115 lines (106 loc) · 4.08 KB
/
cloudtrail.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct AWSAPICall<I = Value, O = Value> {
pub event_version: String,
pub user_identity: UserIdentity,
pub event_time: String,
pub event_source: String,
pub event_name: String,
pub aws_region: String,
#[serde(rename = "sourceIPAddress")]
pub source_ipaddress: String,
pub user_agent: String,
pub request_parameters: I,
pub response_elements: Option<O>,
#[serde(default)]
pub additional_event_data: Option<Value>,
#[serde(rename = "requestID")]
pub request_id: String,
#[serde(rename = "eventID")]
pub event_id: String,
pub event_type: String,
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SessionIssuer {
pub r#type: String,
pub user_name: Option<String>,
pub principal_id: String,
pub arn: String,
pub account_id: String,
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct WebIdFederationData {
pub federated_provider: Option<String>,
pub attributes: Option<String>,
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct Attributes {
pub mfa_authenticated: String,
pub creation_date: DateTime<Utc>,
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SessionContext {
pub session_issuer: Option<SessionIssuer>,
pub web_id_federation_data: Option<WebIdFederationData>,
pub attributes: Attributes,
pub source_identity: Option<String>,
pub ec2_role_delivery: Option<String>,
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct OnBehalfOf {
pub user_id: String,
pub identity_store_arn: String,
}
// https://docs.aws.amazon.com/awscloudtrail/latest/userguide/cloudtrail-event-reference-user-identity.html
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct UserIdentity {
pub r#type: String,
pub account_id: Option<String>,
pub arn: Option<String>,
pub credential_id: Option<String>,
pub invoked_by: Option<String>,
pub principal_id: Option<String>,
pub session_context: Option<SessionContext>,
pub user_name: Option<String>,
pub on_behalf_of: Option<OnBehalfOf>,
}
#[cfg(test)]
mod tests {
use super::AWSAPICall;
#[test]
#[cfg(feature = "cloudwatch_events")]
fn example_cloudwatch_cloudtrail_unknown_assumed_role() {
let data = include_bytes!("../../fixtures/example-cloudwatch-cloudtrail-assumed-role.json");
let parsed: AWSAPICall = serde_json::from_slice(data).unwrap();
let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: AWSAPICall = serde_json::from_slice(&output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}
#[test]
#[cfg(feature = "cloudwatch_events")]
fn example_cloudwatch_cloudtrail_unknown_federate() {
let data = include_bytes!("../../fixtures/example-cloudwatch-cloudtrail-unknown-federate.json");
let parsed: AWSAPICall = serde_json::from_slice(data).unwrap();
let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: AWSAPICall = serde_json::from_slice(&output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}
#[test]
#[cfg(feature = "cloudwatch_events")]
fn example_cloudwatch_cloudtrail_assumed_role() {
let data = include_bytes!("../../fixtures/example-cloudwatch-cloudtrail-unknown-user-auth.json");
let parsed: AWSAPICall = serde_json::from_slice(data).unwrap();
let output: String = serde_json::to_string(&parsed).unwrap();
let reparsed: AWSAPICall = serde_json::from_slice(&output.as_bytes()).unwrap();
assert_eq!(parsed, reparsed);
}
}