Skip to content

Commit e248a56

Browse files
authored
Update database authentication method to resolve issue with special characters (apache#155)
* Add intellij to .gitginore * Fix database authentication issue with special characters * Update script for deploying catalog
1 parent 238d6f9 commit e248a56

File tree

6 files changed

+49
-16
lines changed

6 files changed

+49
-16
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ action/*.zip
77
action/package.json
88
package-lock.json
99

10+
# IntelliJ
11+
.idea
12+
*.class
13+
*.iml
14+
out/
15+
1016
# Eclipse
1117
bin/
1218
**/.project

action/alarmWebAction.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ function main(params) {
148148
return new Promise(function (resolve, reject) {
149149
common.verifyTriggerAuth(triggerData, false)
150150
.then(() => {
151-
db = new Database(params.DB_URL, params.DB_NAME);
151+
db = new Database(params.DB_URL, params.DB_NAME, params.DB_USERNAME, params.DB_PASSWORD);
152152
return db.getWorkerID(workers);
153153
})
154154
.then((worker) => {
@@ -174,7 +174,7 @@ function main(params) {
174174
return new Promise(function (resolve, reject) {
175175
common.verifyTriggerAuth(triggerData, false)
176176
.then(() => {
177-
db = new Database(params.DB_URL, params.DB_NAME);
177+
db = new Database(params.DB_URL, params.DB_NAME, params.DB_USERNAME, params.DB_PASSWORD);
178178
return db.getTrigger(triggerID);
179179
})
180180
.then(doc => {
@@ -224,7 +224,7 @@ function main(params) {
224224

225225
common.verifyTriggerAuth(triggerData, false)
226226
.then(() => {
227-
db = new Database(params.DB_URL, params.DB_NAME);
227+
db = new Database(params.DB_URL, params.DB_NAME, params.DB_USERNAME, params.DB_PASSWORD);
228228
return db.getTrigger(triggerID);
229229
})
230230
.then(trigger => {
@@ -342,7 +342,7 @@ function main(params) {
342342
return new Promise(function (resolve, reject) {
343343
common.verifyTriggerAuth(triggerData, true)
344344
.then(() => {
345-
db = new Database(params.DB_URL, params.DB_NAME);
345+
db = new Database(params.DB_URL, params.DB_NAME, params.DB_USERNAME, params.DB_PASSWORD);
346346
return db.getTrigger(triggerID);
347347
})
348348
.then(trigger => {

action/lib/Database.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
const common = require('./common');
1919

2020
// constructor for DB object - a thin, promise-loving wrapper around nano
21-
module.exports = function(dbURL, dbName) {
22-
var nano = require('nano')(dbURL);
21+
module.exports = function(dbURL, dbName, dbUsername, dbPassword) {
22+
var nano = require('nano')(common.createUrl(dbURL, dbUsername, dbPassword));
2323
this.db = nano.db.use(dbName);
2424
var utilsDB = this;
2525

action/lib/common.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,21 @@ function constructObject(data, isPayload) {
138138
return jsonObject;
139139
}
140140

141+
function createUrl(url, username, password) {
142+
if (username && password) {
143+
var urlComponents = url.split('://');
144+
return urlComponents[0] + '://' + encodeURIComponent(username) + ':' + encodeURIComponent(password) + '@' + urlComponents[1];
145+
} else {
146+
return url;
147+
}
148+
}
141149

142150
module.exports = {
143151
'requestHelper': requestHelper,
144152
'createWebParams': createWebParams,
145153
'verifyTriggerAuth': verifyTriggerAuth,
146154
'parseQName': parseQName,
147155
'sendError': sendError,
148-
'constructObject': constructObject
156+
'constructObject': constructObject,
157+
'createUrl': createUrl
149158
};

installCatalog.sh

+26-8
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,38 @@
2020
# use the command line interface to install standard actions deployed
2121
# automatically
2222
#
23-
# To run this command
24-
# ./installCatalog.sh <authkey> <edgehost> <dburl> <dbprefix> <apihost> <workers>
23+
# If the authentication information in your database contains special characters that require encoding,
24+
# run the script in the following format:
25+
# ./installCatalog.sh <authkey> <edgehost> <apihost> <workers> <dburl> <dbprefix> <dbusername> <dbpassword>
26+
#
27+
# Otherwise, run it in the following format:
28+
# ./installCatalog.sh <authkey> <edgehost> <apihost> <workers> <dburl> <dbprefix>
2529

2630
set -e
2731
set -x
2832

2933
: ${OPENWHISK_HOME:?"OPENWHISK_HOME must be set and non-empty"}
3034
WSK_CLI="$OPENWHISK_HOME/bin/wsk"
3135

32-
if [ $# -eq 0 ]; then
33-
echo "Usage: ./installCatalog.sh <authkey> <edgehost> <dburl> <dbprefix> <apihost> <workers>"
36+
37+
if [ $# -ne 6 ] && [ $# -ne 8 ]; then
38+
echo "
39+
If the authentication information in your database contains special characters that require encoding,
40+
run the script in the following format:
41+
./installCatalog.sh <authkey> <edgehost> <apihost> <workers> <dburl> <dbprefix> <dbusername> <dbpassword>
42+
Otherwise, run it in the following format:
43+
./installCatalog.sh <authkey> <edgehost> <apihost> <workers> <dburl> <dbprefix>"
44+
exit 1
3445
fi
3546

3647
AUTH="$1"
3748
EDGEHOST="$2"
38-
DB_URL="$3"
39-
DB_NAME="${4}alarmservice"
40-
APIHOST="$5"
41-
WORKERS="$6"
49+
APIHOST="$3"
50+
WORKERS="$4"
51+
DB_URL="$5"
52+
DB_NAME="${6}alarmservice"
53+
DB_USERNAME="$7"
54+
DB_PASSWORD="$8"
4255
LIMIT_CRON_FIELDS="${LIMIT_CRON_FIELDS}"
4356
ACTION_RUNTIME_VERSION=${ACTION_RUNTIME_VERSION:="nodejs:10"}
4457

@@ -104,6 +117,11 @@ COMMAND=" -i --apihost $EDGEHOST package update --auth $AUTH --shared no alarmsW
104117
-p DB_NAME $DB_NAME \
105118
-p apihost $APIHOST"
106119

120+
if [ -n "$DB_USERNAME" ] && [ -n "$DB_PASSWORD" ]; then
121+
COMMAND+=" -p DB_USERNAME $DB_USERNAME"
122+
COMMAND+=" -p DB_PASSWORD $DB_PASSWORD"
123+
fi
124+
107125
if [ -n "$WORKERS" ]; then
108126
COMMAND+=" -p workers $WORKERS"
109127
fi

provider/app.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ function createDatabase() {
6666
var method = 'createDatabase';
6767
logger.info(method, 'creating the trigger database');
6868

69-
var nano = require('nano')(dbProtocol + '://' + dbUsername + ':' + dbPassword + '@' + dbHost);
69+
var nano = require('nano')(dbProtocol + '://' + encodeURIComponent(dbUsername) + ':' + encodeURIComponent(dbPassword) + '@' + dbHost);
7070

7171
if (nano !== null) {
7272
return new Promise(function (resolve, reject) {

0 commit comments

Comments
 (0)