From 77e27a11c31fee82c2ff30613bc832f5171fa845 Mon Sep 17 00:00:00 2001 From: Hussein Ait-Lahcen Date: Mon, 19 Nov 2018 15:51:21 +0100 Subject: [PATCH] add dhall interface --- .travis.yml | 2 ++ dhall-1.18.0.nix | 16 ++++++++++++ dhall/Config.dhall | 1 + dhall/Vault.dhall | 1 + dhall/functions.dhall | 1 + dhall/server/Server.dhall | 9 +++++++ dhall/server/functions.dhall | 1 + dhall/server/mkServer.dhall | 17 ++++++++++++ dhall/server/service/Service.dhall | 5 ++++ dhall/server/service/functions.dhall | 1 + dhall/server/service/plugin/Config.dhall | 9 +++++++ .../server/service/plugin/CorrelationId.dhall | 11 ++++++++ dhall/server/service/plugin/Plugin.dhall | 1 + dhall/server/service/plugin/functions.dhall | 9 +++++++ .../service/plugin/mkIPRestriction.dhall | 11 ++++++++ .../service/plugin/mkRequestTermination.dhall | 20 ++++++++++++++ .../service/plugin/mkRequestTransformer.dhall | 11 ++++++++ dhall/server/service/route/Route.dhall | 1 + dhall/types.dhall | 13 ++++++++++ scripts/dhall_check.sh | 26 +++++++++++++++++++ 20 files changed, 166 insertions(+) create mode 100644 dhall-1.18.0.nix create mode 100644 dhall/Config.dhall create mode 100644 dhall/Vault.dhall create mode 100644 dhall/functions.dhall create mode 100644 dhall/server/Server.dhall create mode 100644 dhall/server/functions.dhall create mode 100644 dhall/server/mkServer.dhall create mode 100644 dhall/server/service/Service.dhall create mode 100644 dhall/server/service/functions.dhall create mode 100644 dhall/server/service/plugin/Config.dhall create mode 100644 dhall/server/service/plugin/CorrelationId.dhall create mode 100644 dhall/server/service/plugin/Plugin.dhall create mode 100644 dhall/server/service/plugin/functions.dhall create mode 100644 dhall/server/service/plugin/mkIPRestriction.dhall create mode 100644 dhall/server/service/plugin/mkRequestTermination.dhall create mode 100644 dhall/server/service/plugin/mkRequestTransformer.dhall create mode 100644 dhall/server/service/route/Route.dhall create mode 100644 dhall/types.dhall create mode 100755 scripts/dhall_check.sh diff --git a/.travis.yml b/.travis.yml index de15287..f18b9b3 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ sudo: false install: # Install ansible - nix-env -i python2.7-ansible python2.7-ansible-lint + - nix-env -if ./dhall-1.18.0.nix # Check ansible version - ansible --version @@ -19,6 +20,7 @@ script: - ansible-playbook tests/test.yml -i tests/inventory --syntax-check - ansible-lint . - ansible-playbook tests/test.yml -i tests/inventory -C -D + - scripts/dhall_check.sh notifications: webhooks: https://galaxy.ansible.com/api/v1/notifications/ diff --git a/dhall-1.18.0.nix b/dhall-1.18.0.nix new file mode 100644 index 0000000..d6522bd --- /dev/null +++ b/dhall-1.18.0.nix @@ -0,0 +1,16 @@ +{ pkgs ? import {} }: with pkgs; + +stdenv.mkDerivation rec { + name = "dhall-${version}"; + version = "1.18.0"; + phases = [ "installPhase "]; + src = fetchurl { + url = "https://github.com/dhall-lang/dhall-haskell/releases/download/${version}/dhall-${version}-x86_64-linux.tar.bz2"; + sha256 = "0jvw6ss96xifb21mzpvfjzvaffcnpj0jhpc4rd36cl2r22800qgx"; + }; + installPhase = '' + mkdir -p $out/bin + tar -xjf $src + mv bin/dhall $out/bin/ + ''; +} diff --git a/dhall/Config.dhall b/dhall/Config.dhall new file mode 100644 index 0000000..f1a28fc --- /dev/null +++ b/dhall/Config.dhall @@ -0,0 +1 @@ +let Server = ./server/Server.dhall in { kong_servers : List Server } diff --git a/dhall/Vault.dhall b/dhall/Vault.dhall new file mode 100644 index 0000000..56cd0e5 --- /dev/null +++ b/dhall/Vault.dhall @@ -0,0 +1 @@ +{ appAdminApiKey : Text, accessKey : Text } diff --git a/dhall/functions.dhall b/dhall/functions.dhall new file mode 100644 index 0000000..6465760 --- /dev/null +++ b/dhall/functions.dhall @@ -0,0 +1 @@ +{ Server = ./server/functions.dhall } diff --git a/dhall/server/Server.dhall b/dhall/server/Server.dhall new file mode 100644 index 0000000..beddd7d --- /dev/null +++ b/dhall/server/Server.dhall @@ -0,0 +1,9 @@ + let Service = ./service/Service.dhall + +in { kong_app_admin_url : + Text + , kong_app_admin_apikey : + Text + , services : + List Service + } diff --git a/dhall/server/functions.dhall b/dhall/server/functions.dhall new file mode 100644 index 0000000..2775fb9 --- /dev/null +++ b/dhall/server/functions.dhall @@ -0,0 +1 @@ +{ Service = ./service/functions.dhall, mkServer = ./mkServer.dhall } diff --git a/dhall/server/mkServer.dhall b/dhall/server/mkServer.dhall new file mode 100644 index 0000000..f02055b --- /dev/null +++ b/dhall/server/mkServer.dhall @@ -0,0 +1,17 @@ + let Server = ./Server.dhall + +in let Service = ./service/Service.dhall + +in let Vault = ./../Vault.dhall + +in λ(vault : Vault) + → λ(adminUrl : Text) + → λ(services : List Service) + → { kong_app_admin_url = + adminUrl + , kong_app_admin_apikey = + vault.appAdminApiKey + , services = + services + } + : Server diff --git a/dhall/server/service/Service.dhall b/dhall/server/service/Service.dhall new file mode 100644 index 0000000..f238553 --- /dev/null +++ b/dhall/server/service/Service.dhall @@ -0,0 +1,5 @@ + let Plugin = ./plugin/Plugin.dhall + +in let Route = ./route/Route.dhall + +in { name : Text, url : Text, plugins : List Plugin, routes : List Route } diff --git a/dhall/server/service/functions.dhall b/dhall/server/service/functions.dhall new file mode 100644 index 0000000..08f25c0 --- /dev/null +++ b/dhall/server/service/functions.dhall @@ -0,0 +1 @@ +{ Plugin = ./plugin/functions.dhall } diff --git a/dhall/server/service/plugin/Config.dhall b/dhall/server/service/plugin/Config.dhall new file mode 100644 index 0000000..73a333f --- /dev/null +++ b/dhall/server/service/plugin/Config.dhall @@ -0,0 +1,9 @@ +< CorrelationId : + { header_name : Text, echo_downstream : Bool } +| RequestTransformer : + { `add.headers` : Text } +| RequestTermination : + { status_code : Natural, content_type : Text, body : Text } +| IPRestriction : + { whitelist : Text } +> diff --git a/dhall/server/service/plugin/CorrelationId.dhall b/dhall/server/service/plugin/CorrelationId.dhall new file mode 100644 index 0000000..d96bfc9 --- /dev/null +++ b/dhall/server/service/plugin/CorrelationId.dhall @@ -0,0 +1,11 @@ + let Plugin = ./Plugin.dhall + +in let config = constructors ./Config.dhall + +in { name = + "correlation-id" + , config = + config.CorrelationId + { header_name = "X-correl", echo_downstream = True } + } + : Plugin diff --git a/dhall/server/service/plugin/Plugin.dhall b/dhall/server/service/plugin/Plugin.dhall new file mode 100644 index 0000000..548061c --- /dev/null +++ b/dhall/server/service/plugin/Plugin.dhall @@ -0,0 +1 @@ +let Config = ./Config.dhall in { name : Text, config : Config } diff --git a/dhall/server/service/plugin/functions.dhall b/dhall/server/service/plugin/functions.dhall new file mode 100644 index 0000000..dcf2c9a --- /dev/null +++ b/dhall/server/service/plugin/functions.dhall @@ -0,0 +1,9 @@ +{ mkRequestTermination = + ./mkRequestTermination.dhall +, mkRequestTransformer = + ./mkRequestTransformer.dhall +, mkIPRestriction = + ./mkIPRestriction.dhall +, CorrelationId = + ./CorrelationId.dhall +} diff --git a/dhall/server/service/plugin/mkIPRestriction.dhall b/dhall/server/service/plugin/mkIPRestriction.dhall new file mode 100644 index 0000000..f803769 --- /dev/null +++ b/dhall/server/service/plugin/mkIPRestriction.dhall @@ -0,0 +1,11 @@ + let Plugin = ./Plugin.dhall + +in let config = constructors ./Config.dhall + +in λ(whiteList : Text) + → { name = + "ip-restriction" + , config = + config.IPRestriction { whitelist = whiteList } + } + : Plugin diff --git a/dhall/server/service/plugin/mkRequestTermination.dhall b/dhall/server/service/plugin/mkRequestTermination.dhall new file mode 100644 index 0000000..aff538f --- /dev/null +++ b/dhall/server/service/plugin/mkRequestTermination.dhall @@ -0,0 +1,20 @@ + let Plugin = ./Plugin.dhall + +in let config = constructors ./Config.dhall + +in λ(statusCode : Natural) + → λ(contentType : Text) + → λ(body : Text) + → { name = + "request-termination" + , config = + config.RequestTermination + { status_code = + statusCode + , content_type = + contentType + , body = + body + } + } + : Plugin diff --git a/dhall/server/service/plugin/mkRequestTransformer.dhall b/dhall/server/service/plugin/mkRequestTransformer.dhall new file mode 100644 index 0000000..90789c8 --- /dev/null +++ b/dhall/server/service/plugin/mkRequestTransformer.dhall @@ -0,0 +1,11 @@ + let Plugin = ./Plugin.dhall + +in let config = constructors ./Config.dhall + +in λ(header : Text) + → { name = + "request-transformer" + , config = + config.RequestTransformer { `add.headers` = header } + } + : Plugin diff --git a/dhall/server/service/route/Route.dhall b/dhall/server/service/route/Route.dhall new file mode 100644 index 0000000..c0fa1a9 --- /dev/null +++ b/dhall/server/service/route/Route.dhall @@ -0,0 +1 @@ +{ paths : List Text, protocols : List Text, hosts : List Text } diff --git a/dhall/types.dhall b/dhall/types.dhall new file mode 100644 index 0000000..938dc74 --- /dev/null +++ b/dhall/types.dhall @@ -0,0 +1,13 @@ +{ 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/scripts/dhall_check.sh b/scripts/dhall_check.sh new file mode 100755 index 0000000..6fc7fd1 --- /dev/null +++ b/scripts/dhall_check.sh @@ -0,0 +1,26 @@ +#!/usr/bin/env bash + +pushd () { + command pushd "$@" > /dev/null +} + +popd () { + command popd "$@" > /dev/null +} + +go() { + local ERROR=0; + for file in $(find -type f -name "*.dhall"); do + pushd $(dirname $file); + cat $(basename $file) | dhall --explain resolve > /dev/null; + echo "Type checking ${file}" + if [ "$?" -ne "0" ]; then + echo "Failed to resolve $file" + ERROR=1; + fi; + popd; + done; + exit $ERROR; +} + +go -- 2.41.0