Skip to content

Commit

Permalink
feat(aws-lambda) convert plugin to the PDK and new DB (#5)
Browse files Browse the repository at this point in the history
* feat(aws-lambda) convert plugin to the PDK and new DB

* docs(aws-lambda) add changelog

* fix(aws-lambda) clean up unused variables

* style(handler) remove debug statements

* feat(aws-lambda) replace old `singletons` API to PDK

* chore(aws-lambda) add CI support with Travis CI

* docs(aws-lambda) add README

* chore(aws-lambda) bump version to 0.2.0
  • Loading branch information
Murillo Paula authored Sep 20, 2019
1 parent 8c07647 commit 1365897
Show file tree
Hide file tree
Showing 10 changed files with 596 additions and 391 deletions.
59 changes: 59 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
dist: xenial
sudo: false

language: generic

jdk:
- oraclejdk8

notifications:
email: false

addons:
postgresql: "9.5"
apt:
packages:
- net-tools
- libpcre3-dev
- build-essential
hosts:
- grpcs_1.test
- grpcs_2.test

services:
- docker

env:
global:
- TEST_SUITE=integration
- INSTALL_CACHE=$HOME/install-cache
- DOWNLOAD_ROOT=$HOME/download-root
- PLUGIN_NAME=liamp
- KONG_PLUGINS=bundled,$PLUGIN_NAME
- KONG_TEST_PLUGINS=$KONG_PLUGINS
- TEST_FILE_PATH=$TRAVIS_BUILD_DIR/spec
- JOBS=2
matrix:
- KONG_TEST_DATABASE=cassandra CASSANDRA=2.2.12 KONG=master BUSTED_ARGS="-o gtest -v --exclude-tags=flaky,ipv6,postgres,off"
- KONG_TEST_DATABASE=cassandra CASSANDRA=3.9 KONG=master BUSTED_ARGS="-o gtest -v --exclude-tags=flaky,ipv6,postgres,off"
- KONG_TEST_DATABASE=postgres POSTGRES=9.5 KONG=master BUSTED_ARGS="-o gtest -v --exclude-tags=flaky,ipv6,cassandra,off"
matrix:
allow_failures:
- env: KONG_TEST_DATABASE=postgres POSTGRES=9.5 KONG=master BUSTED_ARGS="-o gtest -v --exclude-tags=flaky,ipv6,cassandra,off"

install:
- make setup-ci
- pushd kong-source && source .ci/setup_env.sh && popd
- pushd kong-source && make dev && popd
- cp -r kong-source/spec/fixtures spec
- luarocks make

script:
- pushd kong-source && bin/busted $BUSTED_ARGS ../spec && popd

cache:
apt: true
directories:
- $DOWNLOAD_CACHE
- $INSTALL_CACHE
- $HOME/.ccm/repository
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## 0.2.0

### Changed

- convert the plugin to the PDK and new DB (developed against Kong 1.x)

## 0.1.0

- Extended the `aws-lambda` plugin from the Kong/kong repository with added ECS IAM roles (developed against Kong 0.13)
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
ROOT_DIR:=$(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
KONG_SOURCE_LOCATION?=$(ROOT_DIR)/kong-source
KONG?=master

setup-kong:
-rm -rf $(KONG_SOURCE_LOCATION); \
git clone --branch $(KONG) https://github.com/Kong/kong.git $(KONG_SOURCE_LOCATION)

setup-ci: setup-kong
cd $(KONG_SOURCE_LOCATION); \
$(MAKE) setup-ci
152 changes: 117 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,56 +1,138 @@
# Introduction
[![Build Status][badge-travis-image]][badge-travis-url]

This is a custom version of the Lambda plugin.
# kong-plugin-aws-lambda

- allows for EC2 IAM roles for authorization, see https://github.com/Kong/kong/pull/2777
- has a modified version of https://github.com/Kong/kong/pull/3639
- added ECS IAM roles
Invoke an [AWS Lambda](https://aws.amazon.com/lambda/) function from Kong. It can be used in combination with other request plugins to secure, manage or extend the function.

## Configuration

## Installation
### Enabling the plugin on a Service

Since it is a custom version, it should be installed under its own name. To
facilitate this there is a rockspec file for use with LuaRocks.
#### With a database

Pack the rock (from `./kong/plugins/aws-lambda`):
Configure this plugin on a [Service](https://docs.konghq.com/latest/admin-api/#service-object) by making the following request:

```shell
> luarocks make
> luarocks pack kong-plugin-liamp
```
$ curl -X POST http://kong:8001/services/{service}/plugins \
--data name=aws-lambda \
--data "config.aws_region=AWS_REGION" \
--data "config.function_name=LAMBDA_FUNCTION_NAME"
```

This results in a `rock` file: `kong-plugin-liamp-0.1.0-1.all.rock`
#### Without a database

This file can be installed on any Kong system with:
Configure this plugin on a [Service](https://docs.konghq.com/latest/admin-api/#service-object) by adding this section do your declarative configuration file:

```shell
> luarocks install kong-plugin-liamp-0.1.0-1.all.rock
```
plugins:
- name: aws-lambda
service: {service}
config:
aws_region: AWS_REGION
function_name: LAMBDA_FUNCTION_NAME
```

In both cases, `{service}` is the `id` or `name` of the Service that this plugin configuration will target.


### Enabling the plugin on a Route

## Usage
#### With a database

Since it is renamed, it will not be enabled by default, hence it must be enabled
like other custom plugins:
Configure this plugin on a [Route](https://docs.konghq.com/latest/admin-api/#Route-object) with:

```shell
> export KONG_CUSTOM_PLUGINS=liamp
```
$ curl -X POST http://kong:8001/routes/{route}/plugins \
--data name=aws-lambda \
--data "config.aws_region=AWS_REGION" \
--data "config.function_name=LAMBDA_FUNCTION_NAME"
```

#### Without a database

Configure this plugin on a [Route](https://docs.konghq.com/latest/admin-api/#route-object) by adding this section do your declarative configuration file:

Once enabled, it differs slightly from the original Lambda plugin in that the
token and secret are no longer required when configuring the plugin.
The behaviour is now to default to IAM roles, unless the secret and token
are provided.
```
plugins:
- name: aws-lambda
route: {route}
config:
aws_region: AWS_REGION
function_name: LAMBDA_FUNCTION_NAME
```

* When the IAM roles are used (default, if no token/secret is provided), the plugin
will first try ECS metadata, and if not available it will fallback on EC2
metadata.
In both cases, `{route}` is the `id` or `name` of the Route that this plugin configuration will target.

* the ability was added to also send very large bodies (that where buffered to
disk) by Kong. To control this there is a new setting `skip_large_bodies` which
defaults to `true`. Set it to `false` to enable it, but be aware that those
very large bodies will have an impact on the system memory.
### Enabling the plugin on a Consumer

## Compatibility
#### With a database

You can use the `http://localhost:8001/plugins` endpoint to enable this plugin on specific [Consumers](https://docs.konghq.com/latest/admin-api/#Consumer-object):

```
$ curl -X POST http://kong:8001/consumers/{consumer}/plugins \
--data name=aws-lambda \
--data "config.aws_region=AWS_REGION" \
--data "config.function_name=LAMBDA_FUNCTION_NAME"
```

#### Without a database

Configure this plugin on a [Consumer](https://docs.konghq.com/latest/admin-api/#Consumer-object) by adding this section do your declarative configuration file:

```
plugins:
- name: aws-lambda
route: {route}
config:
aws_region: AWS_REGION
function_name: LAMBDA_FUNCTION_NAME
```

This plugin was developed against Kong `0.13`, and hence is compatible with
Kong Enterprise `0.33`
In both cases, `{consumer}` is the `id` or `username` of the Consumer that this plugin configuration will target.

You can combine `consumer_id` and `service_id`

In the same request, to furthermore narrow the scope of the plugin.

### Global plugins

- **Using a database**, all plugins can be configured using the `http://kong:8001/plugins/` endpoint.
- **Without a database**, all plugins can be configured via the `plugins:` entry on the declarative configuration file.

A plugin which is not associated to any Service, Route or Consumer (or API, if you are using an older version of Kong) is considered "global", and will be run on every request. Read the [Plugin Reference](https://docs.konghq.com/latest/admin-api/#add-plugin) and the [Plugin Precedence](https://docs.konghq.com/latest/admin-api/#precedence)sections for more information.

## Parameters

Here's a list of all the parameters which can be used in this plugin's configuration:

| Form Parameter | default | description
|----------------|---------|-------------
| `name`|| The name of the plugin to use, in this case: `aws-lambda`.
| `service_id`|| The id of the Service which this plugin will target.
| `route_id` || The id of the Route which this plugin will target.
| `enabled` | `true` | Whether this plugin will be applied.
| `consumer_id` || The id of the Consumer which this plugin will target.
|`config.aws_key` <br>*semi-optional* || The AWS key credential to be used when invoking the function. This value is required if `aws_secret` is defined.
|`config.aws_secret` <br>*semi-optional* ||The AWS secret credential to be used when invoking the function. This value is required if `aws_key` is defined.
|`config.aws_region` || The AWS region where the Lambda function is located. Regions supported are: `ap-northeast-1`, `ap-northeast-2`, `ap-south-1`, `ap-southeast-1`, `ap-southeast-2`, `ca-central-1`, `cn-north-1`, `cn-northwest-1`, `eu-central-1`, `eu-west-1`, `eu-west-2`, `sa-east-1`, `us-east-1`, `us-east-2`, `us-gov-west-1`, `us-west-1`, `us-west-2`.
|`config.function_name` || The AWS Lambda function name to invoke.
|`config.qualifier` <br>*optional* || The [`Qualifier`](http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_RequestSyntax) to use when invoking the function.
|`config.invocation_type` <br>*optional*| `RequestResponse` | The [`InvocationType`](http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_RequestSyntax) to use when invoking the function. Available types are `RequestResponse`, `Event`, `DryRun`.
|`config.log_type` <br>*optional* | `Tail`| The [`LogType`](http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_RequestSyntax) to use when invoking the function. By default `None` and `Tail` are supported.
|`config.port` <br>*optional* | `Tail`| The [`LogType`](http://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_RequestSyntax) to use when invoking the function. By default `None` and `Tail` are supported.
|`config.timeout`| `60000` | An optional timeout in milliseconds when invoking the function.
|`config.unhandled_status` <br>*optional* | `200`, `202` or `204` | The response status code to use (instead of the default `200`, `202`, or `204`) in the case of an [`Unhandled` Function Error](https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_ResponseSyntax)
|`config.forward_request_body` <br>*optional* | `false` | An optional value that defines whether the request body is to be sent in the `request_body` field of the JSON-encoded request. If the body arguments can be parsed, they will be sent in the separate `request_body_args` field of the request. The body arguments can be parsed for `application/json`, `application/x-www-form-urlencoded`, and `multipart/form-data` content types.
|`config.forward_request_headers` <br>*optional* | `false` | An optional value that defines whether the original HTTP request headers are to be sent as a map in the `request_headers` field of the JSON-encoded request.
|`config.forward_request_method` <br>*optional* | `false` | An optional value that defines whether the original HTTP request method verb is to be sent in the `request_method` field of the JSON-encoded request.
|`config.forward_request_uri` <br>*optional* |`false`|An optional value that defines whether the original HTTP request URI is to be sent in the `request_uri` field of the JSON-encoded request. Request URI arguments (if any) will be sent in the separate `request_uri_args` field of the JSON body.
|`config.is_proxy_integration` <br>*optional* | `false` | An optional value that defines whether the response format to receive from the Lambda to [this format](https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-output-format). Note that the parameter `isBase64Encoded` is not implemented.
|`config.awsgateway_compatible` <br>*optional* | `false` | An optional value that defines whether the plugin should wrap requests into the Amazon API gateway.
|`config.proxy_url` <br>*semi-optional* || An optional value that defines whether the plugin should connect through the given proxy server URL. This value is required if `proxy_scheme` is defined.
|`config.proxy_scheme` <br>*semi-optional* || An optional value that defines which HTTP protocol scheme to use in order to connect through the proxy server. The schemes supported are: `http` and `https`. This value is required if `proxy_url` is defined.
|`config.skip_large_bodies` <br>*optional* | `true` | An optional value that defines whether very large bodies (that are buffered to disk) should be sent by Kong. Note that sending very large bodies will have an impact on the system memory.

## Notes

When the IAM roles are used (default, if no `aws.key` / `aws.secret` is provided), the plugin will first try ECS metadata, and if not available it will fallback on EC2 metadata.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package = "kong-plugin-liamp" -- TODO: rename, must match the info in the filename of this rockspec!
-- as a convention; stick to the prefix: `kong-plugin-`
version = "0.1.0-1" -- TODO: renumber, must match the info in the filename of this rockspec!
-- The version '0.1.0' is the source code version, the trailing '1' is the version of this rockspec.
version = "0.2.0-1" -- TODO: renumber, must match the info in the filename of this rockspec!
-- The version '0.2.0' is the source code version, the trailing '1' is the version of this rockspec.
-- whenever the source version changes, the rockspec should be reset to 1. The rockspec version is only
-- updated (incremented) when this file changes, but the source remains the same.

Expand All @@ -12,7 +12,7 @@ local pluginName = package:match("^kong%-plugin%-(.+)$") -- "myPlugin"
supported_platforms = {"linux", "macosx"}
source = {
url = "http://github.com/Tieske/kong-plugin-liamp.git",
tag = "0.1.0"
tag = "0.2.0"
}

description = {
Expand Down
Loading

0 comments on commit 1365897

Please sign in to comment.