From 80320193527d24c33ce67da99521a4cdd6631edc Mon Sep 17 00:00:00 2001 From: Hussein Ait-Lahcen Date: Wed, 24 Apr 2019 15:28:10 +0200 Subject: add(dhall): introduce pre-function plugin --- dhall/server/service/plugin/Config.dhall | 11 +++++------ dhall/server/service/plugin/functions.dhall | 6 ++++++ dhall/server/service/plugin/mkPreFunction.dhall | 9 +++++++++ dhall/server/service/plugin/package.dhall | 7 +------ 4 files changed, 21 insertions(+), 12 deletions(-) create mode 100644 dhall/server/service/plugin/functions.dhall create mode 100644 dhall/server/service/plugin/mkPreFunction.dhall diff --git a/dhall/server/service/plugin/Config.dhall b/dhall/server/service/plugin/Config.dhall index ea53361..1f60080 100644 --- a/dhall/server/service/plugin/Config.dhall +++ b/dhall/server/service/plugin/Config.dhall @@ -1,9 +1,8 @@ < CorrelationId : - { header_name : Text, echo_downstream : Bool, generator : Text } -| RequestTransformer : - { add : { headers : List Text } } + { header_name : Text, echo_downstream : Bool, generator : Text } +| RequestTransformer : { add : { headers : List Text } } | RequestTermination : - { status_code : Natural, content_type : Text, body : Text } -| IPRestriction : - { whitelist : Text } + { status_code : Natural, content_type : Text, body : Text } +| IPRestriction : { whitelist : Text } +| PreFunction : { functions : List Text } > diff --git a/dhall/server/service/plugin/functions.dhall b/dhall/server/service/plugin/functions.dhall new file mode 100644 index 0000000..a7b1295 --- /dev/null +++ b/dhall/server/service/plugin/functions.dhall @@ -0,0 +1,6 @@ +{ mkRequestTermination = ./mkRequestTermination.dhall +, mkRequestTransformer = ./mkRequestTransformer.dhall +, mkIPRestriction = ./mkIPRestriction.dhall +, correlationId = ./correlationId.dhall +, mkPreFunction = ./mkPreFunction.dhall +} diff --git a/dhall/server/service/plugin/mkPreFunction.dhall b/dhall/server/service/plugin/mkPreFunction.dhall new file mode 100644 index 0000000..ff4f734 --- /dev/null +++ b/dhall/server/service/plugin/mkPreFunction.dhall @@ -0,0 +1,9 @@ +let Plugin = ./Plugin.dhall + +let config = ./Config.dhall + +in λ(functions : List Text) + → { name = "pre-function" + , config = config.PreFunction { functions = functions } + } + : Plugin diff --git a/dhall/server/service/plugin/package.dhall b/dhall/server/service/plugin/package.dhall index 872c53b..c231bc6 100644 --- a/dhall/server/service/plugin/package.dhall +++ b/dhall/server/service/plugin/package.dhall @@ -1,6 +1 @@ -{ Type = ./Plugin.dhall -, mkRequestTermination = ./mkRequestTermination.dhall -, mkRequestTransformer = ./mkRequestTransformer.dhall -, mkIPRestriction = ./mkIPRestriction.dhall -, correlationId = ./correlationId.dhall -} +{ Type = ./Plugin.dhall, functions = ./functions.dhall } -- cgit v1.2.3 From 5f23d5909f18ecc5d0af12cdbb1aee44dccf89ca Mon Sep 17 00:00:00 2001 From: Paul Bonaud Date: Thu, 2 Jan 2020 13:44:05 +0100 Subject: config(dhall): add a pre-function lua definition for HTTPS redirects This PR fixes #12 and takes precedence over #13 (cherry-picked one of #13's commit) --- dhall/functions.dhall | 1 - dhall/server/service/plugin/functions.dhall | 1 + dhall/server/service/plugin/http2httpsRedirect.dhall | 15 +++++++++++++++ dhall/types.dhall | 13 ------------- 4 files changed, 16 insertions(+), 14 deletions(-) delete mode 100644 dhall/functions.dhall create mode 100644 dhall/server/service/plugin/http2httpsRedirect.dhall delete mode 100644 dhall/types.dhall diff --git a/dhall/functions.dhall b/dhall/functions.dhall deleted file mode 100644 index 6465760..0000000 --- a/dhall/functions.dhall +++ /dev/null @@ -1 +0,0 @@ -{ Server = ./server/functions.dhall } diff --git a/dhall/server/service/plugin/functions.dhall b/dhall/server/service/plugin/functions.dhall index a7b1295..098fba7 100644 --- a/dhall/server/service/plugin/functions.dhall +++ b/dhall/server/service/plugin/functions.dhall @@ -3,4 +3,5 @@ , mkIPRestriction = ./mkIPRestriction.dhall , correlationId = ./correlationId.dhall , mkPreFunction = ./mkPreFunction.dhall +, http2httpsRedirect = ./http2httpsRedirect.dhall } diff --git a/dhall/server/service/plugin/http2httpsRedirect.dhall b/dhall/server/service/plugin/http2httpsRedirect.dhall new file mode 100644 index 0000000..faa6d79 --- /dev/null +++ b/dhall/server/service/plugin/http2httpsRedirect.dhall @@ -0,0 +1,15 @@ +let mkPreFunction = ./mkPreFunction.dhall + +let luaRedirect = + '' + local scheme = kong.request.get_scheme() + if scheme == "http" then + local host = kong.request.get_host() + local query = kong.request.get_path_with_query() + local url = "https://" .. host ..query + kong.response.set_header("Location", url) + return kong.response.exit(301, url) + end + '' + +in mkPreFunction [ luaRedirect ] diff --git a/dhall/types.dhall b/dhall/types.dhall deleted file mode 100644 index 938dc74..0000000 --- a/dhall/types.dhall +++ /dev/null @@ -1,13 +0,0 @@ -{ Vault = - ./Vault.dhall -, Config = - ./Config.dhall -, Server = - ./server/Server.dhall -, Service = - ./server/service/Service.dhall -, Plugin = - ./server/service/plugin/Plugin.dhall -, Route = - ./server/service/route/Route.dhall -} -- cgit v1.2.3 From c3564f4773ec33b4d64bad5aeebe1f51c08c6d76 Mon Sep 17 00:00:00 2001 From: Paul Bonaud Date: Thu, 2 Jan 2020 13:48:25 +0100 Subject: ci: use latest version of Nix binaries and latest Ansible nix pkgs --- .travis.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 7bf8ed8..1deea24 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,16 +1,24 @@ --- language: nix +nix: 2.3.1 + # Use the new container infrastructure sudo: false install: + # Update nix channels + - nix-channel --add https://nixos.org/channels/nixpkgs-19.09-darwin nixpkgs + - nix-channel --remove nixpkgs-unstable + - nix-channel --update + # Install ansible - - nix-env -i python2.7-ansible python2.7-ansible-lint + - nix-env -i ansible ansible-lint - nix-env -if ./dhall-1.26.1.nix # Check ansible version - ansible --version + - ansible-lint --version # Create ansible.cfg with correct roles_path - printf '[defaults]\nroles_path=../' >ansible.cfg -- cgit v1.2.3 From 8dd292407aa6ab7879807120d5824993013e2eba Mon Sep 17 00:00:00 2001 From: Paul Bonaud Date: Thu, 2 Jan 2020 14:08:23 +0100 Subject: tests: fix variables in the test playbook --- tests/test.yml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/test.yml b/tests/test.yml index 490c967..36c81a2 100644 --- a/tests/test.yml +++ b/tests/test.yml @@ -3,9 +3,10 @@ remote_user: root roles: - role: ansible-kong-app - kong_servers: - - kong_app_admin_url: http://127.0.0.1:8000 - kong_app_admin_apikey: "" - services: - - url: http://example.com - name: test + vars: + kong_servers: + - kong_app_admin_url: http://127.0.0.1:8000 + kong_app_admin_apikey: "" + services: + - url: http://example.com + name: test -- cgit v1.2.3 From a3c4b3d5444e5d79afbd823a9acc12220a91f41f Mon Sep 17 00:00:00 2001 From: Paul Bonaud Date: Thu, 2 Jan 2020 15:25:06 +0100 Subject: tests: fake a Kong server --- .travis.yml | 7 +++++-- tests/kong.py | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100755 tests/kong.py diff --git a/.travis.yml b/.travis.yml index 1deea24..14ab868 100644 --- a/.travis.yml +++ b/.travis.yml @@ -13,7 +13,7 @@ install: - nix-channel --update # Install ansible - - nix-env -i ansible ansible-lint + - nix-env -i python3 ansible ansible-lint - nix-env -if ./dhall-1.26.1.nix # Check ansible version @@ -22,12 +22,15 @@ install: # Create ansible.cfg with correct roles_path - printf '[defaults]\nroles_path=../' >ansible.cfg + # Basic webserver to fake a Kong + - | + ./tests/kong.py& script: # Basic role syntax check - ansible-playbook tests/test.yml -i tests/inventory --syntax-check - ansible-lint . - - ansible-playbook tests/test.yml -i tests/inventory -C -D + - ansible-playbook tests/test.yml -i tests/inventory -D - scripts/dhall_check.sh notifications: diff --git a/tests/kong.py b/tests/kong.py new file mode 100755 index 0000000..32bafb2 --- /dev/null +++ b/tests/kong.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python + +from http.server import HTTPServer, CGIHTTPRequestHandler + +class SimpleHTTPRequestHandler(CGIHTTPRequestHandler): + def do_GET(self): + if self.path == '/services/test': + self.send_response(404) + else: + self.send_response(200) + self.send_header('Content-Type', 'application/json') + self.end_headers() + self.wfile.write(b'{ "data": [] }') + + def do_POST(self): + self.send_response(200) + self.end_headers() + self.wfile.write(b'Hello, world!') + + +httpd = HTTPServer(('127.0.0.1', 8000), SimpleHTTPRequestHandler) +httpd.serve_forever() -- cgit v1.2.3