Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove dependency on deprecated domain module #528

Merged
merged 5 commits into from
Nov 20, 2023
Merged
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
3 changes: 3 additions & 0 deletions CHANGES_NEXT_RELEASE
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
- Removed dependency on deprecated `domain` node module
rg2011 marked this conversation as resolved.
Show resolved Hide resolved
- Remove: `disableDomainMiddleware` config option
- Remove: `DISABLE_DOMAIN_MIDDLEWARE` environment variable
- Remove: operations no longer supported in CB API (aligned with Orion 3.10.1)
- Upgrade NodeJS version from 14-slim to 16-slim in Dockerfile
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ Right Attempt | ResponseStatus=200 | Token=860864fb6d1a4c8a8cb7d59d16daaa52 | Or
* `config.bypassRoleId`: ID of the role that will be considered to have administrative rights over the proxy (so being transparently proxied without validation). Valid values are Role UUIDs. E.g.: `db50362d5f264c8292bebdb5c5783741`.
* `config.dieOnRedirectError`: this flags changes the behavior of the PEP Proxy when an error is received when redirecting a request. If the flag is true, the PEP Proxy process is shut down immediately; if it is false, the behavior is the usual: generate a 501 Code error.
* `config.bodyLimit`: Controls the maximum request body size allowed, in bytes. Default is 1 Mb
* `config.disableDomainMiddleware`:Disable domain middleware used for logging. Disabled will reduce overhead, but loses info (transaction, correlator, service, subservice, etc) in logs. Default is false.

### Authentication configuration
* `config.authentication.checkHeaders`: when the proxy is working with the access control disabled (just user authentication), indicates whether the `fiware-service` and `fiware-servicepath` headers should be checked for existance and validity (checking: the headers exist, thy are not empty and the user is really part of the service and subservice mentioned in the header). This option is ignored when authorization is enabled, and considered to be `true` (as the headers constitute a mandatory part of the authorization process). Default value is `true`.
Expand Down Expand Up @@ -554,7 +553,6 @@ Some of the configuration values for the attributes above mentioned can be overr
| COMPONENT_NAME | config.componentName |
| COMPONENT_PLUGIN | config.middlewares and config.componentName if no COMPONENT_NAME provided |
| BODY_LIMIT | config.bodyLimit |
| DISABLE_DOMAIN_MIDDLEWARE | config.disableDomainMiddleware |

### Component configuration
A special environment variable, called `COMPONENT_PLUGIN` can be set with one of this values: `orion`, `perseo`, `keypass` and `rest`. This variable can be used to select what component plugin to load in order to determine the action of the incoming requests. This variable also rewrites `config.componentName` configuration paramenter.
Expand Down
3 changes: 0 additions & 3 deletions bin/pepProxy
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,6 @@ function loadConfiguration() {
if (process.env.BODY_LIMIT) {
config.bodyLimit = process.env.BODY_LIMIT;
}
if (process.env.DISABLE_DOMAIN_MIDDLEWARE) {
config.disableDomainMiddleware = process.env.DISABLE_DOMAIN_MIDDLEWARE == 'true';
}
}

loadConfiguration();
Expand Down
6 changes: 0 additions & 6 deletions config.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,4 @@ config.maxQueuedClients = 1000;
*/
config.bodyLimit = 1048576;

/**
* Disable domain middleware used for logging. Disabled will reduce overhead, but loses info (transaction, correlator, service,
* subservice, etc) in logs. Default is false.
*/
config.disableDomainMiddleware = false;

module.exports = config;
7 changes: 5 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ RUN apt-get update && \
WORKDIR /opt/fiware-pep-steelskin
# hadolint ignore=DL3008,DL3009,DL3015
RUN \
# Ensure that Git is installed prior to running npm install
# Ensure that Git is installed prior to running npm install.
# curl is added here (again) because the RUN ... above is usually
# removed for local testing, and and curl is required for
# docker health-checks.
apt-get update && \
apt-get install -y git && \
apt-get install -y git curl && \
echo "INFO: npm install --production..." && \
npm install --production && \
# Remove Git and clean apt cache
Expand Down
26 changes: 4 additions & 22 deletions lib/fiware-pep-steelskin.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ var http = require('http'),
authorization,
async = require('async'),
logger = require('logops'),
domainMiddleware = require('./middleware/domain').requestDomain(),
loggingMiddleware = require('./middleware/logging').requestLogger('PEP' + (config.componentName || 'Proxy')),
cacheUtils = require('./services/cacheUtils'),
winston = require('winston'),
bodyParser = require('body-parser'),
Expand Down Expand Up @@ -197,6 +197,7 @@ function createDynamicMiddlewareExecutor(proxyObj) {
* @param {Function} next Call to the next error handler in the chain.
*/
function traceRequest(req, res, next) {
const logger = req.logger
logger.debug('Request for path [%s] from [%s]', req.path, req.get('host'));
logger.debug('Headers:\n%j\n', req.headers);

Expand Down Expand Up @@ -230,9 +231,7 @@ function initializeProxy(proxyObj, callback) {
proxyObj.proxy.use(xmlRawBody);
proxyObj.proxy.use(rawBody);
proxyObj.proxy.use(bodyParser.urlencoded({limit: config.bodyLimit ? config.bodyLimit : '1Mb', extended: true}));
if (!config.disableDomainMiddleware) {
proxyObj.proxy.use(domainMiddleware);
}
proxyObj.proxy.use(loggingMiddleware);

if (config.logLevel && config.logLevel.toUpperCase() === 'DEBUG') {
proxyObj.proxy.use(traceRequest);
Expand Down Expand Up @@ -316,9 +315,7 @@ function initializeAdmin(proxyObj, callback) {
proxyObj.administration.use(bodyParser.json({limit: config.bodyLimit ? config.bodyLimit : '1Mb'}));
proxyObj.administration.use(bodyParser.urlencoded({limit: config.bodyLimit ? config.bodyLimit : '1Mb',
extended: true}));
if (!config.disableDomainMiddleware) {
proxyObj.administration.use(domainMiddleware);
}
proxyObj.administration.use(loggingMiddleware);
proxyObj.administration.use(handleError);

adminMiddleware.loadContextRoutes(proxyObj.administration);
Expand Down Expand Up @@ -373,21 +370,6 @@ function startProxy(callback) {

logger.format = logger.formatters.pipe;

logger.getContext = function domainContext() {
var domainObj = require('domain').active || {};

return {
corr: domainObj.corr,
trans: domainObj.trans,
op: domainObj.op,
from: domainObj.from,
srv: domainObj.srv,
subsrv: domainObj.subsrv,
msg: domainObj.msg,
comp: 'PEP' + (config.componentName || 'Proxy')
};
};

logger.info('Creating proxy');

setAccessLogger();
Expand Down
113 changes: 0 additions & 113 deletions lib/middleware/domain.js

This file was deleted.

84 changes: 84 additions & 0 deletions lib/middleware/logging.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* Copyright 2014 Telefonica Investigación y Desarrollo, S.A.U
*
* This file is part of fiware-pep-steelskin
*
* fiware-pep-steelskin is free software: you can redistribute it and/or
* modify it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* fiware-pep-steelskin is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with fiware-pep-steelskin.
* If not, seehttp://www.gnu.org/licenses/.
*
* For those usages not covered by the GNU Affero General Public License
* please contact with::[[email protected]]
*/

'use strict';

var uuid = require('uuid'),
constants = require('../constants');

var logger = require('logops');

/**
* Express middleWare that creates a domain per request
* It also generates a unique request id that can be used to track requests in logs.
*
* @return {Function} Express middleWare.
*/

function requestLogger(componentName) {
return function(req, res, next) {
let contextSrv;
if (req.headers && req.headers[constants.ORGANIZATION_HEADER]) {
contextSrv = req.headers[constants.ORGANIZATION_HEADER];
}
let contextSubsrv;
if (req.headers && req.headers[constants.PATH_HEADER]) {
contextSubsrv = req.headers[constants.PATH_HEADER];
}
let contextFrom;
// x-forwarded-for/forwarded overwrites x-real-ip
if (req.headers[constants.X_REAL_IP_HEADER]) {
contextFrom = req.headers[constants.X_REAL_IP_HEADER];
}
if (req.headers[constants.X_FORWARDED_FOR_HEADER]) {
contextFrom = req.headers[constants.X_FORWARDED_FOR_HEADER];
}
if (req.headers[constants.FORWARDED_HEADER]) {
contextFrom = req.headers[constants.FORWARDED_HEADER];
}
let contextTrans = req.requestId = uuid.v4();
let contextCorr = req.get(constants.CORRELATOR_HEADER);
if (!contextCorr) {
contextCorr = contextTrans;
req.corr = contextCorr; // for propagate in FWD request
}
res.set(constants.CORRELATOR_HEADER, contextCorr); // for response
const contextStart = Date.now()
req.logger = logger.child({
corr: contextCorr,
trans: contextTrans,
op: req.url,
from: contextFrom,
srv: contextSrv,
subsrv: contextSubsrv,
comp: componentName
})
res.once('finish', function() {
const responseTime = Date.now() - contextStart;
req.logger.debug('response-time: ' + responseTime + ' statusCode: ' + res.statusCode);
});
next();
}
}

exports.requestLogger = requestLogger;
5 changes: 4 additions & 1 deletion lib/middleware/proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
var config = require('../../config'),
errors = require('../errors'),
request = require('request'),
logger = require('logops'),
constants = require('../constants'),
validationHeaders = [
'fiware-service',
Expand All @@ -47,6 +46,7 @@ var config = require('../../config'),
* @param {Function} next Call to the next middleware in the chain.
*/
function extractOrganization(req, res, next) {
const logger = req.logger;
if (req.headers[constants.ORGANIZATION_HEADER]) {
req.organization = req.headers[constants.ORGANIZATION_HEADER];
req.service = req.headers[constants.ORGANIZATION_HEADER];
Expand All @@ -66,6 +66,7 @@ function extractOrganization(req, res, next) {
* @param {Function} next Call to the next middleware in the chain.
*/
function extractUserId(req, res, next) {
const logger = req.logger;
if (req.headers[constants.AUTHORIZATION_HEADER]) {
req.userId = req.headers[constants.AUTHORIZATION_HEADER];
next();
Expand All @@ -84,6 +85,7 @@ function extractUserId(req, res, next) {
* @param {Function} next Invokes the next middleware in the chain.
*/
function generateFRN(req, res, next) {
const logger = req.logger;
var frn = config.resourceNamePrefix + config.componentName + ':';

if (req.organization) {
Expand Down Expand Up @@ -124,6 +126,7 @@ function generateFRN(req, res, next) {
* @param {Function} next Invokes the next middleware in the chain.
*/
function sendRequest(req, res, next) {
const logger = req.logger;
var options = {
uri: 'http://' + config.resource.original.host + ':' + config.resource.original.port + req.path,
qs: req.query,
Expand Down
4 changes: 2 additions & 2 deletions lib/plugins/keypassPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@

'use strict';

var logger = require('logops'),
errors = require('../errors');
var errors = require('../errors');

/**
* Keypass operation identification table. Each row of the table contains an operation with three fields:
Expand All @@ -47,6 +46,7 @@ var keypassOperations = [
* @param {Object} res Outgoing response.
*/
function extractAction(req, res, callback) {
const logger = req.logger;
logger.debug('Extracting action from URL [%s] and method [%s]', req.url, req.method);

if (req.path.match(/\/pdp\/v3.*/) && req.method === 'POST') {
Expand Down
Loading
Loading