Skip to content
This repository was archived by the owner on May 17, 2024. It is now read-only.

feat: support on templating env for route inputs #8

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,48 @@
'use strict';

const fs = require('fs');
const path = require('path');
const Logger = require('./lib/logger.js');
const SetupApp = require('./lib/setupApp');
const { version } = require('./package.json');

const serverPromise = SetupApp();

const cwd = process.cwd();
const NODE_ENV = process.env.NODE_ENV || 'development';
const CONFIG_BASE_PATH = process.env.CONFIG_BASE_PATH || './config';
const configFileName = `${NODE_ENV}.json`;

let configPath;
if (path.isAbsolute(CONFIG_BASE_PATH)) {
configPath = path.normalize(CONFIG_BASE_PATH) + path.sep + configFileName;
} else {
configPath = CONFIG_BASE_PATH.replace(/\/$/, '/') + '/' + configFileName;
}

module.exports = serverPromise;
module.exports.start = start;

if (require.main === module) {
validateConfig();
start();
}

function validateConfig() {
try {
fs.statSync(path.relative(cwd, configPath));
require(configPath);
} catch (err) {
if (err.code === 'ENOENT') {
const msg = `Could not find configuration file for environment ${NODE_ENV}`;
console.log('FATAL', msg);
console.log('FATAL', 'Set NODE_ENV to production or development or add', configPath);
throw new Error(msg);
}
throw new Error(err.message);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
throw new Error(err.message);
throw err;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To remove block. We won't be using config files instead as per discussion.

}
}

function start() {
serverPromise.then((server) => {
process.once('SIGTERM', terminate);
Expand Down
6 changes: 6 additions & 0 deletions config/development.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ldap": {
"databaseUser": "OVERRIDE_IN_ENV",
"databasePassword": "OVERRIDE_IN_ENV"
}
}
6 changes: 6 additions & 0 deletions config/production.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"ldap": {
"databaseUser": "OVERRIDE_IN_ENV",
"databasePassword": "OVERRIDE_IN_ENV"
}
}
7 changes: 7 additions & 0 deletions config/test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"ldap": {
"databaseUser": "cn=read-only-admin,dc=example,dc=com",
"databasePassword": "password",
"url": "ldap%3A%2F%2Fldap.forumsys.com"
}
}
4 changes: 4 additions & 0 deletions functions/ldap.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ const LDAP = require('ldapjs');
const Logger = require('../lib/logger.js');
const Utils = require('../lib/utils.js');
const Qs = require('qs');
const config = require('exp-config');
const helpers = require("../lib/helpers.js")

/**
*
Expand All @@ -14,6 +16,8 @@ exports.plugin = {
name: 'ldap',
register: async function (server) {
server.ext('onRequest', (request, h) => {
helpers.setQueryParameterTemplateValues(request.query, config.ldap);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
helpers.setQueryParameterTemplateValues(request.query, config.ldap);
helpers.setQueryParameterTemplateValues(request.query, config.ldap);

Is request.query at this point an object or still the serialized string value?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

request.query is already an object at this point


let { tlsOptions, attributes } = request.query;

if (tlsOptions) {
Expand Down
14 changes: 14 additions & 0 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
'use strict';

const Handlebars = require("handlebars");

Handlebars.registerHelper('ENV', (value) => {
return new Handlebars.SafeString(value);
});

exports.setQueryParameterTemplateValues = (query, context) => {
for (const key of Object.keys(query)) {
const template = Handlebars.compile(query[key]);
query[key] = template(context);
}
}
9 changes: 9 additions & 0 deletions nodemon.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"ignore": ["tmp/", "node_modules/", "logs/", "test/"],
"ext": "js json env",
"watch": ["*", ".env"],
"env": {
"ENV_PREFIX": "ONIFY_",
"INTERPRET_CHAR_AS_DOT": "_"
}
}
43 changes: 37 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
"@hapi/inert": "^7.0.0",
"@hapi/vision": "^7.0.0",
"activedirectory2": "^2.1.0",
"exp-config": "^4.2.1",
"fast-xml-parser": "^4.2.4",
"handlebars": "^4.7.7",
"hapi-swagger": "^15.0.0",
"joi": "^17.6.3",
"ldapjs": "^3.0.2",
Expand Down
24 changes: 24 additions & 0 deletions test/functions/ldap.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 200`, 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);
});
});