diff --git a/README.md b/README.md index 6ba9ab9..3417322 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,10 @@ ## Changelog +### latest + +* feat: support on templating env for route inputs + ### 1.4.2 * chore: bump fast-xml-parser from 4.0.11 to 4.2.4 diff --git a/functions/ldap.js b/functions/ldap.js index 8f84755..f050e93 100644 --- a/functions/ldap.js +++ b/functions/ldap.js @@ -5,6 +5,7 @@ const LDAP = require('ldapjs'); const Logger = require('../lib/logger.js'); const Utils = require('../lib/utils.js'); const Qs = require('qs'); +const helpers = require("../lib/helpers.js") /** * @@ -14,6 +15,8 @@ exports.plugin = { name: 'ldap', register: async function (server) { server.ext('onRequest', (request, h) => { + helpers.setQueryParameterTemplateValues(request.query); + let { tlsOptions, attributes } = request.query; if (tlsOptions) { diff --git a/lib/helpers.js b/lib/helpers.js new file mode 100644 index 0000000..baaeae5 --- /dev/null +++ b/lib/helpers.js @@ -0,0 +1,16 @@ +'use strict'; + +require("dotenv").config(); + +const Handlebars = require("handlebars"); + +Handlebars.registerHelper('ENV', (value) => { + return new Handlebars.SafeString(value); +}); + +exports.setQueryParameterTemplateValues = (query) => { + for (const key of Object.keys(query)) { + const template = Handlebars.compile(query[key]); + query[key] = template(process.env); + } +} \ No newline at end of file diff --git a/nodemon.json b/nodemon.json new file mode 100644 index 0000000..db4ce61 --- /dev/null +++ b/nodemon.json @@ -0,0 +1,8 @@ +{ + "ignore": ["tmp/", "node_modules/", "logs/", "test/"], + "ext": "js json env", + "watch": [ + "*", + ".env" + ] +} \ No newline at end of file diff --git a/package-lock.json b/package-lock.json index 76d3d80..debbb9c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hub-functions", - "version": "1.4.1", + "version": "1.4.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "hub-functions", - "version": "1.4.1", + "version": "1.4.2", "license": "MIT", "dependencies": { "@hapi/glue": "^8.0.0", @@ -14,7 +14,9 @@ "@hapi/inert": "^7.0.0", "@hapi/vision": "^7.0.0", "activedirectory2": "^2.1.0", + "dotenv": "^16.3.1", "fast-xml-parser": "^4.2.4", + "handlebars": "^4.7.7", "hapi-swagger": "^15.0.0", "joi": "^17.6.3", "ldapjs": "^3.0.2", @@ -2299,6 +2301,17 @@ "node": ">=6.0.0" } }, + "node_modules/dotenv": { + "version": "16.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", + "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==", + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/motdotla/dotenv?sponsor=1" + } + }, "node_modules/ecdsa-sig-formatter": { "version": "1.0.11", "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", @@ -7102,6 +7115,11 @@ "esutils": "^2.0.2" } }, + "dotenv": { + "version": "16.3.1", + "resolved": "https://registry.npmjs.org/dotenv/-/dotenv-16.3.1.tgz", + "integrity": "sha512-IPzF4w4/Rd94bA9imS68tZBaYyBWSCE47V1RGuMrB94iyTOIEwRmVL2x/4An+6mETpLrKJ5hQkB8W4kFAadeIQ==" + }, "ecdsa-sig-formatter": { "version": "1.0.11", "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.11.tgz", diff --git a/package.json b/package.json index 9ab40f5..e2527a8 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,9 @@ "@hapi/inert": "^7.0.0", "@hapi/vision": "^7.0.0", "activedirectory2": "^2.1.0", + "dotenv": "^16.3.1", "fast-xml-parser": "^4.2.4", + "handlebars": "^4.7.7", "hapi-swagger": "^15.0.0", "joi": "^17.6.3", "ldapjs": "^3.0.2", diff --git a/test/functions/ldap.js b/test/functions/ldap.js index dc6da1c..2d50723 100644 --- a/test/functions/ldap.js +++ b/test/functions/ldap.js @@ -128,4 +128,28 @@ describe('ldap:', () => { expect(res.statusCode).to.equal(200); expect(res.result.length === 5).to.equal(true); }); + + it(`GET ${FUNCTION_ENDPOINT}/search - search result for query with environment variable placeholder in parameters - returns 200`, async () => { + const username = '%7B%7BENV databaseUser%7D%7D'; + const password = '%7B%7BENV databasePassword%7D%7D'; + + const res = await server.inject({ + method: 'GET', + url: `${FUNCTION_ENDPOINT}/search?url=${url}&username=${username}&password=${password}&base=${base}&filter=${filter}&scope=${scope}&paged=true&pageSize=5`, + }); + + expect(res.statusCode).to.equal(200); + }); + + it(`GET ${FUNCTION_ENDPOINT}/search - search result for query without using helper function on environment variable placeholder in parameters - returns 401`, async () => { + const username = '%7B%7BdatabaseUser%7D%7D'; + const password = '%7B%7BdatabasePassword%7D%7D'; + + const res = await server.inject({ + method: 'GET', + url: `${FUNCTION_ENDPOINT}/search?url=${url}&username=${username}&password=${password}&base=${base}&filter=${filter}&scope=${scope}&paged=true&pageSize=5`, + }); + + expect(res.statusCode).to.equal(401); + }); });