Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

[WIP] Add Data Fixtures for Lambda Event Types #31

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
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
24 changes: 24 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
dist: trusty

language: rust

rust:
- stable
- beta
- nightly

cache: cargo

matrix:
allow_failures:
- rust: nightly
fast_finish: true

install: cargo build --release --verbose
script: cargo test --verbose

notifications:
email:
on_success: never
on_failure: never
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
[package]
name = "crowbar"
version = "0.2.0"
authors = ["Iliana Weller <ilianaw@buttslol.net>"]
authors = [
"Iliana Weller <ilianaw@buttslol.net>",
"Naftuli Kay <me@naftuli.wtf>"
]
description = "Wrapper to simplify writing AWS Lambda functions in Rust (using the Python execution environment)"
readme = "README.md"
repository = "https://github.com/ilianaw/rust-crowbar"
Expand All @@ -11,12 +14,15 @@ license = "MIT/Apache-2.0"
exclude = [".gitignore", "builder/**", "examples/**", "test/**"]

[dependencies]
chrono = { version = "0.4", features = ["serde"] }
serde = "1.0"
serde-aux = "0.5"
serde_derive = "1.0"
serde_json = "1.0"
serde_qs = "0.4"
cpython = { version = "0.1", default-features = false }
cpython-json = { version = "0.2", default-features = false }
error-chain = { version = "0.11.0", optional = true }

[features]
default = ["cpython/python3-sys"]

4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# rust-crowbar

[![Build Status][travis.svg]][travis]
[![crates.io](https://img.shields.io/crates/v/crowbar.svg)](https://crates.io/crates/crowbar)
[![docs.rs](https://docs.rs/crowbar/badge.svg)](https://docs.rs/crowbar)

Expand Down Expand Up @@ -62,3 +63,6 @@ crowbar welcomes your contributions:
* Please submit non-trivial changes as an issue first; send a pull request when the implementation is agreed on

crowbar follows a [code of conduct](https://github.com/ilianaw/rust-crowbar/blob/master/CODE_OF_CONDUCT.md); please read it.

[travis]: https://travis-ci.org/ilianaw/rust-crowbar
[travis.svg]: https://travis-ci.org/ilianaw/rust-crowbar.svg?branch=master
38 changes: 38 additions & 0 deletions src/data/apicall/fixtures/default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"version": "0",
"id": "36eb8523-97d0-4518-b33d-ee3579ff19f0",
"detail-type": "AWS API Call via CloudTrail",
"source": "aws.s3",
"account": "123456789012",
"time": "2016-02-20T01:09:13Z",
"region": "us-east-1",
"resources": [],
"detail": {
"eventVersion": "1.03",
"userIdentity": {
"type": "Root",
"principalId": "123456789012",
"arn": "arn:aws:iam::123456789012:root",
"accountId": "123456789012",
"sessionContext": {
"attributes": {
"mfaAuthenticated": "false",
"creationDate": "2016-02-20T01:05:59Z"
}
}
},
"eventTime": "2016-02-20T01:09:13Z",
"eventSource": "s3.amazonaws.com",
"eventName": "CreateBucket",
"awsRegion": "us-east-1",
"sourceIPAddress": "100.100.100.100",
"userAgent": "[S3Console/0.4]",
"requestParameters": {
"bucketName": "bucket-test-iad"
},
"responseElements": null,
"requestID": "9D767BCC3B4E7487",
"eventID": "24ba271e-d595-4e66-a7fd-9c16cbf8abae",
"eventType": "AwsApiCall"
}
}
59 changes: 59 additions & 0 deletions src/data/apicall/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#[cfg(test)]
mod tests;

use super::*;

#[derive(Serialize,Deserialize)]
pub struct APICall {
pub version: String,
pub id: String,
pub account: String,
pub time: DateTime<Utc>,
pub region: String,
pub resources: Vec<String>,
pub detail: APICallDetail,
}

#[derive(Serialize,Deserialize)]
#[serde(rename_all="camelCase")]
pub struct APICallDetail {
pub event_version: String,
pub user_identity: UserIdentity,
pub event_time: DateTime<Utc>,
pub event_source: String,
pub event_name: String,
pub aws_region: String,
#[serde(rename="sourceIPAddress")]
pub source_ip_address: String,
pub user_agent: String,
pub request_parameters: Option<BTreeMap<String, String>>,
pub response_elements: Option<Value>,
#[serde(rename="requestID")]
pub request_id: String,
#[serde(rename="eventID")]
pub event_id: String,
pub event_type: String,
}

#[derive(Serialize,Deserialize)]
#[serde(rename_all="camelCase")]
pub struct UserIdentity {
#[serde(rename="type")]
pub user_type: String,
pub principal_id: String,
pub arn: String,
pub account_id: String,
pub session_context: SessionContext,
}

#[derive(Serialize,Deserialize)]
pub struct SessionContext {
pub attributes: SessionContextAttributes,
}

#[derive(Serialize,Deserialize)]
#[serde(rename_all="camelCase")]
pub struct SessionContextAttributes {
pub mfa_authenticated: String,
pub creation_date: DateTime<Utc>,
}
8 changes: 8 additions & 0 deletions src/data/apicall/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
use super::*;

use serde_json;

#[test]
fn test_deserialize() {
let _call: APICall = serde_json::from_str(include_str!("fixtures/default.json")).unwrap();
}
43 changes: 43 additions & 0 deletions src/data/apigateway/auth.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/// API Gateway Custom Authenticator Events
use data::apigateway::HttpEventRequestContext;

use std::collections::BTreeMap;
use std::fmt;

#[serde(rename_all="SCREAMING_SNAKE_CASE")]
#[derive(Debug,Eq,PartialEq,Serialize,Deserialize)]
pub enum EventType {
Request,
Token
}

#[serde(rename_all="camelCase")]
#[derive(Serialize,Deserialize)]
pub struct Event {
pub headers: Option<BTreeMap<String, String>>,
pub http_method: String,
pub method_arn: String,
pub path: String,
pub path_parameters: Option<BTreeMap<String, String>>,
pub query_string_parameters: Option<BTreeMap<String, String>>,
pub resource: String,
pub request_context: HttpEventRequestContext,
pub stage_variables: Option<BTreeMap<String, String>>,
#[serde(rename="type")]
pub event_type: EventType,
}

#[derive(Serialize,Deserialize)]
pub enum Effect {
Allow,
Deny
}

impl fmt::Display for Effect {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Effect::Allow => write!(f, "Allow"),
Effect::Deny => write!(f, "Deny")
}
}
}
26 changes: 26 additions & 0 deletions src/data/apigateway/fixtures/authorize.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"headers": null,
"httpMethod": "GET",
"methodArn": "arn:aws:execute-api:us-east-1:111111111111:rest-api-id/null/GET/",
"path": "/",
"pathParameters": {},
"queryStringParameters": {},
"requestContext": {
"accountId": "111111111111",
"apiId": "rest-api-id",
"httpMethod": "GET",
"identity": {
"apiKey": "test-invoke-api-key",
"apiKeyId": "test-invoke-api-key-id",
"sourceIp": "test-invoke-source-ip"
},
"path": "/",
"requestId": "test-invoke-request",
"resourceId": "test-invoke-resource-id",
"resourcePath": "/",
"stage": "test-invoke-stage"
},
"resource": "/",
"stageVariables": {},
"type": "REQUEST"
}
53 changes: 53 additions & 0 deletions src/data/apigateway/fixtures/request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"body": null,
"headers": {
"Accept": "*/*",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Host": "example.com",
"User-Agent": "curl/7.47.0",
"Via": "1.1 deadbeefcafebabebeefbeefdeaddead.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "J0YMU-brgNv9hzadv_rjnICwfLCd4-IYjVz55KdZS6fuGB6xkU65WA==",
"X-Amzn-Trace-Id": "Root=1-5a9095d0-2459dc8c2eead5b445840ca0",
"X-Forwarded-For": "127.0.0.1, 127.0.0.1",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"httpMethod": "GET",
"isBase64Encoded": false,
"path": "/echo.json",
"pathParameters": null,
"queryStringParameters": null,
"requestContext": {
"accountId": "123456789012",
"apiId": "smddwihyy9",
"httpMethod": "GET",
"identity": {
"accessKey": null,
"accountId": null,
"caller": null,
"cognitoAuthenticationProvider": null,
"cognitoAuthenticationType": null,
"cognitoIdentityId": null,
"cognitoIdentityPoolId": null,
"sourceIp": "127.0.0.1",
"user": null,
"userAgent": "curl/7.47.0",
"userArn": null
},
"path": "/echo.json",
"protocol": "HTTP/1.1",
"requestId": "30e9177b-7253-43c4-95b4-b7c275ad037f",
"requestTime": "1/Jan/1970:00:00:00 +0000",
"requestTimeEpoch": 0,
"resourceId": "abcdef",
"resourcePath": "/echo.json",
"stage": "production"
},
"resource": "/echo.json",
"stageVariables": null
}
36 changes: 36 additions & 0 deletions src/data/apigateway/fixtures/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"body": null,
"headers": null,
"httpMethod": "DELETE",
"isBase64Encoded": false,
"path": "/user/session.json",
"pathParameters": null,
"queryStringParameters": null,
"requestContext": {
"accountId": "123456789012",
"apiId": "rest-api-id",
"httpMethod": "DELETE",
"identity": {
"accessKey": "AWS_ACCESS_KEY_ID",
"accountId": "123456789012",
"apiKey": "test-invoke-api-key",
"apiKeyId": "test-invoke-api-key-id",
"caller": "AWS_CALLER_ID",
"cognitoAuthenticationProvider": null,
"cognitoAuthenticationType": null,
"cognitoIdentityId": null,
"cognitoIdentityPoolId": null,
"sourceIp": "test-invoke-source-ip",
"user": "AIDAJDXSYAWOHYRSW7IPO",
"userAgent": "Apache-HttpClient/4.5.x (Java/1.8.0_144)",
"userArn": "arn:aws:iam::123456789012:user/example"
},
"path": "/user/session.json",
"requestId": "test-invoke-request",
"resourceId": "abcdef",
"resourcePath": "/user/session.json",
"stage": "test-invoke-stage"
},
"resource": "/user/session.json",
"stageVariables": null
}
Loading