You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@openwhisk.apache.org by ra...@apache.org on 2020/03/04 17:33:42 UTC

[openwhisk-package-alarms] branch master updated: Update database authentication method to resolve issue with special characters (#155)

This is an automated email from the ASF dual-hosted git repository.

rabbah pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/openwhisk-package-alarms.git


The following commit(s) were added to refs/heads/master by this push:
     new e248a56  Update database authentication method to resolve issue with special characters (#155)
e248a56 is described below

commit e248a564f4f163d0cb4cca0afd3c56c63a4ed71b
Author: 김건희 <ki...@gmail.com>
AuthorDate: Wed Mar 4 09:32:11 2020 -0800

    Update database authentication method to resolve issue with special characters (#155)
    
    * Add intellij to .gitginore
    * Fix database authentication issue with special characters
    * Update script for deploying catalog
---
 .gitignore               |  6 ++++++
 action/alarmWebAction.js |  8 ++++----
 action/lib/Database.js   |  4 ++--
 action/lib/common.js     | 11 ++++++++++-
 installCatalog.sh        | 34 ++++++++++++++++++++++++++--------
 provider/app.js          |  2 +-
 6 files changed, 49 insertions(+), 16 deletions(-)

diff --git a/.gitignore b/.gitignore
index c848c58..97fa149 100644
--- a/.gitignore
+++ b/.gitignore
@@ -7,6 +7,12 @@ action/*.zip
 action/package.json
 package-lock.json
 
+# IntelliJ
+.idea
+*.class
+*.iml
+out/
+
 # Eclipse
 bin/
 **/.project
diff --git a/action/alarmWebAction.js b/action/alarmWebAction.js
index 97826e1..b2e9bf2 100644
--- a/action/alarmWebAction.js
+++ b/action/alarmWebAction.js
@@ -148,7 +148,7 @@ function main(params) {
         return new Promise(function (resolve, reject) {
             common.verifyTriggerAuth(triggerData, false)
             .then(() => {
-                db = new Database(params.DB_URL, params.DB_NAME);
+                db = new Database(params.DB_URL, params.DB_NAME, params.DB_USERNAME, params.DB_PASSWORD);
                 return db.getWorkerID(workers);
             })
             .then((worker) => {
@@ -174,7 +174,7 @@ function main(params) {
         return new Promise(function (resolve, reject) {
             common.verifyTriggerAuth(triggerData, false)
             .then(() => {
-                db = new Database(params.DB_URL, params.DB_NAME);
+                db = new Database(params.DB_URL, params.DB_NAME, params.DB_USERNAME, params.DB_PASSWORD);
                 return db.getTrigger(triggerID);
             })
             .then(doc => {
@@ -224,7 +224,7 @@ function main(params) {
 
             common.verifyTriggerAuth(triggerData, false)
             .then(() => {
-                db = new Database(params.DB_URL, params.DB_NAME);
+                db = new Database(params.DB_URL, params.DB_NAME, params.DB_USERNAME, params.DB_PASSWORD);
                 return db.getTrigger(triggerID);
             })
             .then(trigger => {
@@ -342,7 +342,7 @@ function main(params) {
         return new Promise(function (resolve, reject) {
             common.verifyTriggerAuth(triggerData, true)
             .then(() => {
-                db = new Database(params.DB_URL, params.DB_NAME);
+                db = new Database(params.DB_URL, params.DB_NAME, params.DB_USERNAME, params.DB_PASSWORD);
                 return db.getTrigger(triggerID);
             })
             .then(trigger => {
diff --git a/action/lib/Database.js b/action/lib/Database.js
index cbbdb3c..6f91440 100644
--- a/action/lib/Database.js
+++ b/action/lib/Database.js
@@ -18,8 +18,8 @@
 const common = require('./common');
 
 // constructor for DB object - a thin, promise-loving wrapper around nano
-module.exports = function(dbURL, dbName) {
-    var nano = require('nano')(dbURL);
+module.exports = function(dbURL, dbName, dbUsername, dbPassword) {
+    var nano = require('nano')(common.createUrl(dbURL, dbUsername, dbPassword));
     this.db = nano.db.use(dbName);
     var utilsDB = this;
 
diff --git a/action/lib/common.js b/action/lib/common.js
index 0f74130..32644ea 100644
--- a/action/lib/common.js
+++ b/action/lib/common.js
@@ -138,6 +138,14 @@ function constructObject(data, isPayload) {
     return jsonObject;
 }
 
+function createUrl(url, username, password) {
+    if (username && password) {
+      var urlComponents = url.split('://');
+      return urlComponents[0] + '://' + encodeURIComponent(username) + ':' + encodeURIComponent(password) + '@' + urlComponents[1];
+    } else {
+      return url;
+    }
+}
 
 module.exports = {
     'requestHelper': requestHelper,
@@ -145,5 +153,6 @@ module.exports = {
     'verifyTriggerAuth': verifyTriggerAuth,
     'parseQName': parseQName,
     'sendError': sendError,
-    'constructObject': constructObject
+    'constructObject': constructObject,
+    'createUrl': createUrl
 };
diff --git a/installCatalog.sh b/installCatalog.sh
index fb51232..73ef5e0 100755
--- a/installCatalog.sh
+++ b/installCatalog.sh
@@ -20,8 +20,12 @@
 # use the command line interface to install standard actions deployed
 # automatically
 #
-# To run this command
-# ./installCatalog.sh <authkey> <edgehost> <dburl> <dbprefix> <apihost> <workers>
+# If the authentication information in your database contains special characters that require encoding,
+# run the script in the following format:
+# ./installCatalog.sh <authkey> <edgehost> <apihost> <workers> <dburl> <dbprefix> <dbusername> <dbpassword>
+#
+# Otherwise, run it in the following format:
+# ./installCatalog.sh <authkey> <edgehost> <apihost> <workers> <dburl> <dbprefix>
 
 set -e
 set -x
@@ -29,16 +33,25 @@ set -x
 : ${OPENWHISK_HOME:?"OPENWHISK_HOME must be set and non-empty"}
 WSK_CLI="$OPENWHISK_HOME/bin/wsk"
 
-if [ $# -eq 0 ]; then
-    echo "Usage: ./installCatalog.sh <authkey> <edgehost> <dburl> <dbprefix> <apihost> <workers>"
+
+if [ $# -ne 6 ] && [ $# -ne 8 ]; then
+    echo "
+If the authentication information in your database contains special characters that require encoding,
+run the script in the following format:
+./installCatalog.sh <authkey> <edgehost> <apihost> <workers> <dburl> <dbprefix> <dbusername> <dbpassword>
+ Otherwise, run it in the following format:
+./installCatalog.sh <authkey> <edgehost> <apihost> <workers> <dburl> <dbprefix>"
+    exit 1
 fi
 
 AUTH="$1"
 EDGEHOST="$2"
-DB_URL="$3"
-DB_NAME="${4}alarmservice"
-APIHOST="$5"
-WORKERS="$6"
+APIHOST="$3"
+WORKERS="$4"
+DB_URL="$5"
+DB_NAME="${6}alarmservice"
+DB_USERNAME="$7"
+DB_PASSWORD="$8"
 LIMIT_CRON_FIELDS="${LIMIT_CRON_FIELDS}"
 ACTION_RUNTIME_VERSION=${ACTION_RUNTIME_VERSION:="nodejs:10"}
 
@@ -104,6 +117,11 @@ COMMAND=" -i --apihost $EDGEHOST package update --auth $AUTH --shared no alarmsW
     -p DB_NAME $DB_NAME \
     -p apihost $APIHOST"
 
+if [ -n "$DB_USERNAME" ] && [ -n "$DB_PASSWORD" ]; then
+    COMMAND+=" -p DB_USERNAME $DB_USERNAME"
+    COMMAND+=" -p DB_PASSWORD $DB_PASSWORD"
+fi
+
 if [ -n "$WORKERS" ]; then
     COMMAND+=" -p workers $WORKERS"
 fi
diff --git a/provider/app.js b/provider/app.js
index 485580f..1719dad 100644
--- a/provider/app.js
+++ b/provider/app.js
@@ -66,7 +66,7 @@ function createDatabase() {
     var method = 'createDatabase';
     logger.info(method, 'creating the trigger database');
 
-    var nano = require('nano')(dbProtocol + '://' + dbUsername + ':' + dbPassword + '@' + dbHost);
+    var nano = require('nano')(dbProtocol + '://' + encodeURIComponent(dbUsername) + ':' + encodeURIComponent(dbPassword) + '@' + dbHost);
 
     if (nano !== null) {
         return new Promise(function (resolve, reject) {