]> git.immae.eu Git - github/fretlink/ansible-kong-app.git/commitdiff
Merge pull request #18 from paulrbr-fl/pre-function-plugins v0.30
authorpaulrbr-fl <43074087+paulrbr-fl@users.noreply.github.com>
Thu, 2 Jan 2020 15:25:37 +0000 (16:25 +0100)
committerGitHub <noreply@github.com>
Thu, 2 Jan 2020 15:25:37 +0000 (16:25 +0100)
config(dhall): add a pre-function lua definition for HTTPS redirects

.travis.yml
dhall/functions.dhall [deleted file]
dhall/server/service/plugin/Config.dhall
dhall/server/service/plugin/functions.dhall [new file with mode: 0644]
dhall/server/service/plugin/http2httpsRedirect.dhall [new file with mode: 0644]
dhall/server/service/plugin/mkPreFunction.dhall [new file with mode: 0644]
dhall/server/service/plugin/package.dhall
dhall/types.dhall [deleted file]
tests/kong.py [new file with mode: 0755]
tests/test.yml

index 7bf8ed8ea8cfcec8fb2e3ef469765b234ce4a920..14ab868b529eae5894a46235d4d8934a6757c3b3 100644 (file)
@@ -1,25 +1,36 @@
 ---
 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 python3 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
+  # 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/dhall/functions.dhall b/dhall/functions.dhall
deleted file mode 100644 (file)
index 6465760..0000000
+++ /dev/null
@@ -1 +0,0 @@
-{ Server = ./server/functions.dhall }
index ea53361976a12621e75edcecc4f94c6a5f133d5e..1f600806feb823d3d58a439fc29fb88949c006c2 100644 (file)
@@ -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 (file)
index 0000000..098fba7
--- /dev/null
@@ -0,0 +1,7 @@
+{ mkRequestTermination = ./mkRequestTermination.dhall
+, mkRequestTransformer = ./mkRequestTransformer.dhall
+, 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 (file)
index 0000000..faa6d79
--- /dev/null
@@ -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/server/service/plugin/mkPreFunction.dhall b/dhall/server/service/plugin/mkPreFunction.dhall
new file mode 100644 (file)
index 0000000..ff4f734
--- /dev/null
@@ -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
index 872c53b620fb0c36bebc687e9da498f5052cc3b4..c231bc631434839d405e88a8803c65d3a8425c65 100644 (file)
@@ -1,6 +1 @@
-{ Type = ./Plugin.dhall
-, mkRequestTermination = ./mkRequestTermination.dhall
-, mkRequestTransformer = ./mkRequestTransformer.dhall
-, mkIPRestriction = ./mkIPRestriction.dhall
-, correlationId = ./correlationId.dhall
-}
+{ Type = ./Plugin.dhall, functions = ./functions.dhall }
diff --git a/dhall/types.dhall b/dhall/types.dhall
deleted file mode 100644 (file)
index 938dc74..0000000
+++ /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
-}
diff --git a/tests/kong.py b/tests/kong.py
new file mode 100755 (executable)
index 0000000..32bafb2
--- /dev/null
@@ -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()
index 490c967d11ac143ccbc6a7960e00f130e892ad1f..36c81a28f611f46dc1dbefb80a93ae16b02e042f 100644 (file)
@@ -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