Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
carnei-ro committed May 28, 2021
0 parents commit 041df9e
Show file tree
Hide file tree
Showing 24 changed files with 1,272 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
return {
default = {
verbose = true,
coverage = false,
output = "gtest",
},
}
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true
charset = utf-8

[*.lua]
indent_style = space
indent_size = 2

[kong/templates/nginx*]
indent_style = space
indent_size = 4

[*.template]
indent_style = space
indent_size = 4

[Makefile]
indent_style = tab
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# servroot is typically the nginx/Kong workingdirectory when testing
servroot

# packed distribution format for LuaRocks
*.rock

*.rockspec
38 changes: 38 additions & 0 deletions .luacheckrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
-- Configuration file for LuaCheck
-- see: https://luacheck.readthedocs.io/en/stable/
--
-- To run do: `luacheck .` from the repo

std = "ngx_lua"
unused_args = false
redefined = false
max_line_length = false


globals = {
"_KONG",
"kong",
"ngx.IS_CLI",
}


not_globals = {
"string.len",
"table.getn",
}


ignore = {
"6.", -- ignore whitespace warnings
}


exclude_files = {
--"spec/fixtures/invalid-module.lua",
--"spec-old-api/fixtures/invalid-module.lua",
}


files["spec/**/*.lua"] = {
std = "ngx_lua+busted",
}
20 changes: 20 additions & 0 deletions .markdownlint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
default: true

commands-show-output: false
first-line-h1:
front_matter_title: "" # Ensure a H1 title exists even with a front matter
heading-style:
style: atx
hr-style:
style: "---"
line-length: false
no-trailing-punctuation:
punctuation: ".,;:。,;:!?" # Note: '!' and '?' are allowed
single-title:
front_matter_title: "" # Disabled if there's a front matter
ul-indent:
indent: 2
ul-style:
style: dash
no-inline-html: false
3 changes: 3 additions & 0 deletions .pongo/pongorc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--postgres
--cassandra

26 changes: 26 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
repos:
- repo: local
hooks:
- id: update-readme
name: Update README.md file
description: Update README.md file.
entry: ./update_readme.sh "" ""
language: script
pass_filenames: false
files: '.*\.lua'
- repo: git://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
hooks:
- id: check-added-large-files
- id: check-byte-order-marker
- id: check-executables-have-shebangs
- id: check-merge-conflict
- id: end-of-file-fixer
- id: mixed-line-ending
- id: trailing-whitespace
args: [--markdown-linebreak-ext=md]
- repo: git://github.com/igorshubovych/markdownlint-cli
rev: v0.27.1
hooks:
- id: markdownlint
154 changes: 154 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
VERSION := $(shell sed -n "s/.*VERSION.*= \"\{1,\}\(.*\)\"/\1/p;" kong/plugins/*/handler.lua | tr -d ',')
NAME := $(shell ls kong/plugins)
DIR_NAME=$(shell basename $${PWD})
UID := $(shell id -u)
GID := $(shell id -g)
SUMMARY := $(shell sed -n '/^summary: /s/^summary: //p' README.md)
export UID GID NAME VERSION

.DEFAULT_GOAL:=help

ifeq ($(origin DOCKER_COMPOSE_FILE),undefined)
DOCKER_COMPOSE_FILE := docker-compose-dbless.yaml
endif

ifeq ($(origin KONG_VERSION),undefined)
KONG_VERSION := 2.1.2
endif

help: ## Display this help
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z0-9_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

.PHONY: build
build: rockspec validate ## Build / Pack the plugin. Output at the ./dist directory
@find kong/plugins/${NAME}/ -type f -iname "*lua~" -exec rm -f {} \;
@docker run --rm -u 0 -v ${PWD}:/plugin \
--entrypoint /bin/bash kong:2.0.3-centos \
-c "cd /plugin ; yum install -q -y zip; luarocks make > /dev/null 2>&1 ; luarocks pack kong-plugin-${NAME} 2> /dev/null ; chown ${UID}:${GID} *.rock"
@mkdir -p dist
@mv *.rock dist/
@printf '\n\n Check "dist" folder \n\n'

.PHONY: validate
validate: ## Check plugin version, summary and create rockspec if not exists.
@if [ -z "$${VERSION}" ]; then \
printf "\n\nNo VERSION found in handler.lua;\nPlease set it in your object that extends the base_plugin.\nEx: plugin.VERSION = \"0.1.0\"\n\n"; \
exit 1 ;\
else \
echo ${VERSION} | egrep '^[0-9]\.[0-9]\.[0-9]$$' > /dev/null 2>&1 ; \
if [ $${?} -ne 0 ]; then \
printf "\n\nVERSION must follow the Semantic Version pattern without additional labels for pre-releases.\nWhich means: Major.Minor.Patch, eg 0.1.0 or ...\nReceived: $${VERSION} \n\n"; \
exit 2 ; \
fi ; \
fi
@if [ -z "${SUMMARY}" ]; then \
printf "\n\nNo SUMMARY found.\nPlease, create a 'README.md' file and place your summary there.\nFollow the pattern '^summary: '\nDo not use double quotes"; \
printf "\nExample:\nsummary: this is my summary\n\n\n" ;\
exit 4 ;\
fi
@if [ ! -f kong-plugin-${NAME}-${VERSION}-1.rockspec ]; then \
make rockspec; \
fi

.PHONY: rockspec
rockspec: ## Create the RockSpec file, parsing the Plugin Name, Version, Dependencies and Summary.
@printf 'package = "kong-plugin-%s"\nversion = "%s"\n\nsource = {\n url = "%s",\n branch = "main"\n}\n\ndescription = {\n summary = "%s",\n}\n\ndependencies = {\n' "${NAME}" "${VERSION}-1" "$(shell git remote -v | grep -E '^origin.*push.$$' | awk '{print $$2}')" "${SUMMARY}" > kong-plugin-${NAME}-${VERSION}-1.rockspec
@grep -Ev '^#|^ *$$' dependencies.conf | sed -e 's/$$/",/g' -e 's/^/\ \ "/g' >> kong-plugin-${NAME}-${VERSION}-1.rockspec
@printf '}\n\nbuild = {\n type = "builtin",\n modules = {\n' >> kong-plugin-${NAME}-${VERSION}-1.rockspec
@find kong/plugins/${NAME} -type f -iname "*.lua" -exec bash -c 'printf " [\"%s\"] = \"%s\",\n" "$$(tr '/' '.' <<< $${1/\.lua})" "{}"' _ {} \; >> kong-plugin-${NAME}-${VERSION}-1.rockspec
@printf " }\n}\n" >> kong-plugin-${NAME}-${VERSION}-1.rockspec

.PHONY: clean
clean: ## Remove artifactory files and take down docker stack.
@rm -rf *.rock *.rockspec dist shm kong/plugins/${NAME}/${NAME}
@find kong/plugins/${NAME} -type f -iname "*lua~" -exec rm -f {} \;
@docker-compose -f ${DOCKER_COMPOSE_FILE} down -v

.PHONY: clear
clear: clean ## Same as clean.

.PHONY: start
start: validate ## Exec start the docker-compose stack.
@docker-compose -f ${DOCKER_COMPOSE_FILE} up -d

.PHONY: stop
stop: ## Stop the containers.
@docker-compose -f ${DOCKER_COMPOSE_FILE} down

.PHONY: logs
logs: kong-logs ## Show Kong container logs.
.PHONY: kong-logs
kong-logs: ## Same as logs.
@docker logs -f $$(docker ps -qf name=${DIR_NAME}_kong_1) 2>&1 || true

.PHONY: shell
shell: kong-bash ## Docker exec into Kong container shell.
.PHONY: kong-bash
kong-bash: ## Same as shell.
@docker exec -it $$(docker ps -qf name=${DIR_NAME}_kong_1) bash || true

.PHONY: reload
reload: kong-reload ## Perform Kong Reload into Kong container.
.PHONY: kong-reload
kong-reload: ## Same as reload.
@docker exec -it $$(docker ps -qf name=${DIR_NAME}_kong_1) bash -c "/usr/local/bin/kong reload"

.PHONY: restart
restart: ## Remove Kong container and recreate it.
@docker rm -vf $$(docker ps -qf name=${DIR_NAME}_kong_1)
@docker-compose -f ${DOCKER_COMPOSE_FILE} up -d

.PHONY: truncate-logs
truncate-logs: ## Needs sudo privileges: truncate Kong Container logs.
@sudo truncate -s 0 $$(docker inspect --format='{{.LogPath}}' ${DIR_NAME}_kong_1)

.PHONY: reconfigure
reconfigure: clean start kong-logs ## Shortcut to clean, start, logs.

.PHONY: config-aux
config-aux: ## Works only with Database: Creates 'aux.lua' file and post it as a pre-function plugin to /aux route.
@[ ! -f aux.lua ] && echo -e 'ngx.say("hello from aux - edit aux.lua and run make patch-aux")\nngx.exit(200)' > aux.lua || printf ''
@curl -s -X POST http://localhost:8001/services/ -d 'name=aux' -d url=http://localhost
@curl -s -X POST http://localhost:8001/services/aux/routes -d 'paths[]=/aux' -d 'name=aux'
@curl -i -X POST http://localhost:8001/services/aux/plugins -F "name=pre-function" -F "[email protected]"

.PHONY: patch-aux
patch-aux: ## Works only with Database: Updates /aux pre-function plugin.
@curl -i -X PATCH http://localhost:8001/plugins/$$(curl -s http://localhost:8001/plugins/ | jq -r ".data[] | select (.name|test(\"pre-function\")) .id") -F "name=pre-function" -F "[email protected]"
@echo " "

.PHONY: req-aux
req-aux: ## GET /aux endpoint.
@curl -s http://localhost:8000/aux

.PHONY: resty-script
resty-script: ## Execute inside Kong Container the 'resty-script.lua' file.
@docker exec -it $$(docker ps -qf name=${DIR_NAME}_kong_1) /usr/local/openresty/bin/resty /plugin-development/resty-script.lua || true

.PHONY: config
config: ## Works only with Database: Create a 'httpbin' service and '/' route. Add the custom-plugin to the '/' route.
@curl -s -X POST http://localhost:8001/services/ -d 'name=httpbin' -d url=http://httpbin.org/anything
@curl -s -X POST http://localhost:8001/services/httpbin/routes -d 'paths[]=/' -d 'name=root'
@curl -i -X POST http://localhost:8001/routes/root/plugins -F "name=${NAME}"

.PHONY: config-plugin-remove
config-plugin-remove: ## Works only with Database: Remove the custom-plugin.
@curl -i -X DELETE http://localhost:8001/plugins/$$(curl -s http://localhost:8001/plugins/ | jq -r ".data[] | select (.name|test(\"${NAME}\")) .id")

.PHONY: remove-all
remove-all: ## Works only with Database: Remove all configurations for plugins, consumers, routes, services, and upstreams
@for i in plugins consumers routes services upstreams; do for j in $$(curl -s --url http://127.0.0.1:8001/$${i} | jq -r ".data[].id"); do curl -s -i -X DELETE --url http://127.0.0.1:8001/$${i}/$${j}; done; done

.PHONY: test
test: rockspec ## Execute 'pongo' tests with Kong Version 2.0.x.
@sed 's/\(local PLUGIN_NAME\).*/\1 = "${NAME}"/g' -i spec/*/*.lua
@KONG_VERSION=${KONG_VERSION} pongo run -v -o gtest ./spec

.PHONY: lint
lint: rockspec ## Execute 'pongo' lint
@sed 's/\(local PLUGIN_NAME\).*/\1 = "${NAME}"/g' -i spec/*/*.lua
@KONG_VERSION=${KONG_VERSION} pongo lint

.PHONY: update-readme
update-readme: ## Depends on Kong up and running: Updates 'Plugin Priority', 'Plugin Version', 'Configs' and 'Usage' sections from README.md file.
@./update_readme.sh ${NAME}
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Kong Plugin S3

summary: Get AWS S3 Objects with AWS V4 signed request

<!-- BEGINNING OF KONG-PLUGIN DOCS HOOK -->
## Plugin Priority

Priority: **750**

## Plugin Version

Version: **0.1.0**

## Configs

| name | type | required | default | validations |
| ---- | ---- | -------- | ------- | ----------- |
| config.aws_key | **string** | false | | |
| config.aws_secret | **string** | false | | |
| config.aws_region | **string** | true | | |
| config.bucket_name | **string** | true | | |
| config.rewrites | **map[string][string]** (*check `'config.rewrites' object`) | false | <pre>"/": "/index.html"</pre> | |
| config.host | **string** | false | | |
| config.port | **integer** | false | <pre>443</pre> | <pre>- between:<br/> - 0<br/> - 65535</pre> |

### 'config.rewrites' object

| keys_type | keys_validations | values_type | values_required | values_default | values_validations |
| --------- | ---------------- | ----------- | --------------- | -------------- | ------------------ |
| **string** | | **string** | true | | |

## Usage

```yaml
---
plugins:
- name: s3
enabled: true
config:
aws_key: ''
aws_secret: ''
aws_region: ''
bucket_name: ''
rewrites: {}
host: ''
port: 443
```
<!-- END OF KONG-PLUGIN DOCS HOOK -->
6 changes: 6 additions & 0 deletions dependencies.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
lua ~> 5.1

## Examples:
# lua-cjson
# lua-resty-http >= 0.11
# lua-resty-validation ~> 2.7-1
43 changes: 43 additions & 0 deletions docker-compose-dbless.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
version: '3'

services:

kong:
image: kong:2.1.2-centos
environment:
- KONG_LUA_SSL_VERIFY_DEPTH=3
- KONG_LUA_SSL_TRUSTED_CERTIFICATE=/etc/pki/ca-trust/extracted/openssl/ca-bundle.trust.crt
- KONG_PROXY_ACCESS_LOG=/dev/stdout
- KONG_ADMIN_ACCESS_LOG=/dev/stdout
- KONG_PROXY_ERROR_LOG=/dev/stderr
- KONG_ADMIN_ERROR_LOG=/dev/stderr
- KONG_ADMIN_LISTEN=0.0.0.0:8001
- KONG_LOG_LEVEL=debug
- KONG_DATABASE=off
- KONG_DECLARATIVE_CONFIG=/plugin-development/kong.yml
- KONG_NGINX_HTTP_INCLUDE=/plugin-development/kong-http-include.conf
- KONG_PLUGINS=bundled,${NAME}
volumes:
- plugin-development:/plugin-development
user: "0:0"
command:
- /bin/bash
- -c
- |
mkdir -p /usr/local/lib/luarocks/rocks-5.1/kong-plugin-${NAME}/${VERSION}-1/
ln -s /plugin-development/kong-plugin-${NAME}-${VERSION}-1.rockspec /usr/local/lib/luarocks/rocks-5.1/kong-plugin-${NAME}/${VERSION}-1/kong-plugin-${NAME}-${VERSION}-1.rockspec
ln -s /plugin-development/kong/plugins/${NAME} /usr/local/share/lua/5.1/kong/plugins/${NAME}
/docker-entrypoint.sh kong start --vv
ports:
- "7999:7999"
- "8000:8000"
- "8001:8001"


volumes:
plugin-development:
driver: local
driver_opts:
type: none
o: bind
device: ${PWD}/
Loading

0 comments on commit 041df9e

Please sign in to comment.