From 292c3389dacd4a70f5b550ec20ce1b0e1f025334 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 6 May 2024 15:14:23 +0200 Subject: [PATCH 1/4] allow change default express limit --- config.js | 3 ++- lib/commonConfig.js | 8 +++++++- lib/services/northBound/northboundServer.js | 4 ++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/config.js b/config.js index 3542fc7e2..88b4e610d 100644 --- a/config.js +++ b/config.js @@ -76,7 +76,8 @@ var config = { subservice: '/gardens', providerUrl: 'http://192.168.56.1:4041', deviceRegistrationDuration: 'P1M', - defaultType: 'Thing' + defaultType: 'Thing', + expressLimit: '1Mb' }; module.exports = config; diff --git a/lib/commonConfig.js b/lib/commonConfig.js index d7f4c2c40..d16908615 100644 --- a/lib/commonConfig.js +++ b/lib/commonConfig.js @@ -156,7 +156,8 @@ function processEnvironmentVariables() { 'IOTA_FALLBACK_TENANT', 'IOTA_FALLBACK_PATH', 'IOTA_LD_SUPPORT_NULL', - 'IOTA_LD_SUPPORT_DATASET_ID' + 'IOTA_LD_SUPPORT_DATASET_ID', + 'IOTA_EXPRESS_LIMIT' ]; const iotamVariables = [ 'IOTA_IOTAM_URL', @@ -468,6 +469,11 @@ function processEnvironmentVariables() { ? config.defaultEntityNameConjunction : ':'; } + if (process.env.IOTA_EXPRESS_LIMIT) { + config.expressLimit = process.env.IOTA_EXPRESS_LIMIT; + } else { + config.expressLimit = config.expressLimit ? config.expressLimit : '1mb'; + } } function setConfig(newConfig) { diff --git a/lib/services/northBound/northboundServer.js b/lib/services/northBound/northboundServer.js index 5ed44f8c4..bd22879d2 100644 --- a/lib/services/northBound/northboundServer.js +++ b/lib/services/northBound/northboundServer.js @@ -56,8 +56,8 @@ function start(config, callback) { northboundServer.app.set('port', config.server.port); northboundServer.app.set('host', config.server.host || '0.0.0.0'); northboundServer.app.use(domainUtils.requestDomain); - northboundServer.app.use(bodyParser.json()); - northboundServer.app.use(bodyParser.json({ type: 'application/*+json' })); + northboundServer.app.use(bodyParser.json({ limit: config.expressLimit })); + northboundServer.app.use(bodyParser.json({ type: 'application/*+json', limit: config.expressLimit })); if (config.logLevel && config.logLevel === 'DEBUG') { northboundServer.app.use(middlewares.traceRequest); From 9dca90510c52159fcfe57702cd4aafa22c34ee31 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 6 May 2024 15:23:55 +0200 Subject: [PATCH 2/4] update CNR --- CHANGES_NEXT_RELEASE | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index e69de29bb..bf9eeeba0 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -0,0 +1 @@ +- Fix default express limit to 1Mb instead default 100Kb and allow change it throught a conf env var 'IOTA_EXPRESS_LIMIT' (#telefonicaid/iotagent-json#827) From 62446dff48a199f4ff6c26941dfdc5085b9e72e3 Mon Sep 17 00:00:00 2001 From: Alvaro Vega Date: Mon, 6 May 2024 15:28:47 +0200 Subject: [PATCH 3/4] update doc --- doc/admin.md | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/doc/admin.md b/doc/admin.md index 413da4384..79939292d 100644 --- a/doc/admin.md +++ b/doc/admin.md @@ -260,13 +260,13 @@ the `mongob` section (as described bellow). E.g.: It configures the MongoDB driver for those repositories with 'mongodb' type. If the `host` parameter is a list of comma-separated IPs, they will be considered to be part of a Replica Set. In that case, the optional property -`replicaSet` should contain the Replica Set name. If the database requires authentication, username (`user`), -password (`password`) and authSource (`authSource`) can be set. If the database requires TLS/SSL connection but any -validation of the certificate chain is not mandatory, all you need is to set the ssl (`ssl`) option as `true` to connect -the database. If you need to add more complex option(s) such as `retryWrites=true` or `w=majority` when connection -database, extraArgs (`extraArgs`) can be used to perform it. For The MongoBD driver will retry the connection at startup -time `retries` times, waiting `retryTime` seconds between attempts, if those attributes are present (default values are -5 and 5 respectively). E.g.: +`replicaSet` should contain the Replica Set name. If the database requires authentication, username (`user`), password +(`password`) and authSource (`authSource`) can be set. If the database requires TLS/SSL connection but any validation of +the certificate chain is not mandatory, all you need is to set the ssl (`ssl`) option as `true` to connect the database. +If you need to add more complex option(s) such as `retryWrites=true` or `w=majority` when connection database, extraArgs +(`extraArgs`) can be used to perform it. For The MongoBD driver will retry the connection at startup time `retries` +times, waiting `retryTime` seconds between attempts, if those attributes are present (default values are 5 and 5 +respectively). E.g.: ```javascript { @@ -421,6 +421,12 @@ characters (such as semi-colons) which are specification. When provisioning devices, it is necessary that the developer provides valid `objectId`-`name` mappings whenever relaxed mode is used, to prevent the consumption of forbidden characters. +#### `expressLimit` + +IotAgents, as all Express applications that use the body-parser middleware, have a default limit to the request body +size that the application will handle. This default limit for ioiotagnets are 1Mb. So, if your IotAgent receives a +request with a body that exceeds this limit, the application will throw a “Error: Request entity too large”. + ### Configuration using environment variables Some of the configuration parameters can be overriden with environment variables, to ease the use of those parameters @@ -482,6 +488,7 @@ overrides. | IOTA_EXPLICIT_ATTRS | `explicitAttrs` | | IOTA_DEFAULT_ENTITY_NAME_CONJUNCTION | `defaultEntityNameConjunction` | | IOTA_RELAX_TEMPLATE_VALIDATION | `relaxTemplateValidation` | +| IOTA_EXPRESS_LIMIT | `expressLimit` | Note: From fd1a7d4d1d3c421e19c0a0aec82d3a8b8bae921b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ferm=C3=ADn=20Gal=C3=A1n=20M=C3=A1rquez?= Date: Mon, 6 May 2024 16:56:11 +0200 Subject: [PATCH 4/4] Apply suggestions from code review --- CHANGES_NEXT_RELEASE | 2 +- doc/admin.md | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/CHANGES_NEXT_RELEASE b/CHANGES_NEXT_RELEASE index bf9eeeba0..25e0be54d 100644 --- a/CHANGES_NEXT_RELEASE +++ b/CHANGES_NEXT_RELEASE @@ -1 +1 @@ -- Fix default express limit to 1Mb instead default 100Kb and allow change it throught a conf env var 'IOTA_EXPRESS_LIMIT' (#telefonicaid/iotagent-json#827) +- Fix: default express limit to 1Mb instead default 100Kb and allow change it throught a conf env var 'IOTA_EXPRESS_LIMIT' (telefonicaid/iotagent-json#827) diff --git a/doc/admin.md b/doc/admin.md index 79939292d..644e3ace7 100644 --- a/doc/admin.md +++ b/doc/admin.md @@ -427,6 +427,8 @@ IotAgents, as all Express applications that use the body-parser middleware, have size that the application will handle. This default limit for ioiotagnets are 1Mb. So, if your IotAgent receives a request with a body that exceeds this limit, the application will throw a “Error: Request entity too large”. +The 1Mb default can be changed setting the `expressLimit` configuration parameter (or equivalente `IOTA_EXPRESS_LIMIT` environment variable). + ### Configuration using environment variables Some of the configuration parameters can be overriden with environment variables, to ease the use of those parameters