Browse Source

SQL: Refactor Tableau connector to make use of the connection properties (#69169)

* Refactor to make use of the connection propreties

This refactors the way the connection URL is being built, to make use of
frameworks' ability to construct the URL based on a set of connection
propreties.
This also ensures that the URI attributes are properly escaped.
Bogdan Pintea 4 years ago
parent
commit
3c1e0a94b1

+ 1 - 29
x-pack/plugin/sql/connectors/tableau/connector/connectionBuilder.js

@@ -6,35 +6,7 @@
     } else{
         urlBuilder += "http://";
     }
-    urlBuilder += attr["server"] + ":" + attr["port"];
+    urlBuilder += attr["server"] + ":" + attr["port"] + "?";
 
-    var params = [];
-    params["user"] = attr["username"];
-    params["password"] = attr["password"];
-
-    var formattedParams = [];
-
-    for (var key in params) {
-        if (params[key]) {
-            var param = encodeURIComponent(params[key]);
-            formattedParams.push(connectionHelper.formatKeyValuePair(key, param));
-        }
-    }
-
-    if (formattedParams.length > 0) {
-        urlBuilder += "?" + formattedParams.join("&")
-    }
-
-    // logging visible in log.txt if -DLogLevel=Debug is added in Tableau command line
-    logging.log("ES JDBC URL before adding additional parameters: " + urlBuilder);
-
-    // TODO: wrap error-prone "free form"
-    var additionalConnectionParameters = attr[connectionHelper.attributeWarehouse];
-    if (additionalConnectionParameters != null && additionalConnectionParameters.trim().length > 0) {
-        urlBuilder += (formattedParams.length == 0) ? "?" : "&";
-        urlBuilder += additionalConnectionParameters;
-    }
-
-    logging.log("ES JDBC final URL: " + urlBuilder);
     return [urlBuilder];
 })

+ 24 - 0
x-pack/plugin/sql/connectors/tableau/connector/connectionProperties.js

@@ -0,0 +1,24 @@
+(function propertiesbuilder(attr) {
+    var props = {};
+
+    props["user"] = attr["username"];
+    props["password"] = attr["password"];
+
+    var extraProps = attr[connectionHelper.attributeWarehouse];
+    if (extraProps != null && extraProps.trim().length > 0) {
+        // allow `&` and white-space as attribue-value pair delimiters
+        var avps = extraProps.trim().split(/[\s&]/);
+        for (var i = 0; i < avps.length; i++) {
+            var tokens = avps[i].split("=");
+            if (tokens.length != 2 || tokens[0].length == 0 || tokens[1].length == 0) {
+                var errMessage = "Invalid additional settings property `" + avps[i] + "`: " +
+                    "not conforming to the attribute=value format."
+                return connectionHelper.ThrowTableauException(errMessage);
+            } else {
+                props[tokens[0]] = tokens[1];
+            }
+        }
+    }
+
+    return props;
+})

+ 3 - 0
x-pack/plugin/sql/connectors/tableau/connector/connectionResolver.tdr

@@ -17,5 +17,8 @@
         </attribute-list>
       </required-attributes>
     </connection-normalizer>
+    <connection-properties>
+      <script file='connectionProperties.js'/>
+    </connection-properties>
     </connection-resolver>
 </tdr>