wise-coders / influxdb-jdbc-driver

InfluxDB JDBC Driver | DbSchema Influx Management GUI
https://dbschema.com
Other
6 stars 3 forks source link

Token tailing equal mark (`=`) missing when passing by connection URL. #1

Closed VergeDX closed 11 months ago

VergeDX commented 11 months ago

If the token contains =, the = part will miss in HTTP(s) request. It seems method Connection connect(String, Properties) parse HTTP query string by =.

VergeDX commented 11 months ago
diff --git a/build.gradle b/build.gradle
index 4f08853..867824c 100644
--- a/build.gradle
+++ b/build.gradle
@@ -2,9 +2,11 @@ buildscript {
     repositories {
         mavenCentral()
         maven { url "https://plugins.gradle.org/m2/" }
+        gradlePluginPortal()
     }
     dependencies {
         classpath "gradle.plugin.install4j.install4j:gradle_plugin:7.0.8"
+        classpath 'com.github.johnrengelman:shadow:8.1.1'
     }
 }
 plugins{
@@ -12,7 +14,8 @@ plugins{
 }
 apply plugin: 'application'
 apply plugin: 'distribution'
-
+apply plugin: 'com.github.johnrengelman.shadow'
+apply plugin: 'java'

 repositories {
     mavenCentral()
@@ -21,7 +24,14 @@ repositories {
 dependencies {
     implementation 'com.influxdb:influxdb-client-java:6.9.0'
     implementation 'com.influxdb:influxdb-client-flux:6.9.0'
+    implementation 'org.apache.httpcomponents.client5:httpclient5:5.2.1'
+
+    compileOnly 'org.projectlombok:lombok:1.18.30'
+    annotationProcessor 'org.projectlombok:lombok:1.18.30'
+
     testImplementation 'junit:junit:4.13.2'
+    testCompileOnly 'org.projectlombok:lombok:1.18.30'
+    testAnnotationProcessor 'org.projectlombok:lombok:1.18.30'
 }

 compileJava{
@@ -34,8 +44,10 @@ compileJava{
     }
 }

+// https://stackoverflow.com/questions/53853474/shadowjar-no-value-has-been-specified-for-property-mainclassname
+project.setProperty("mainClassName", "com.wisecoders.dbschema.influxdb.JdbcDriver")
+
 jar {
-    archiveName ="influxjdbc${version}.jar"
     manifest {
         attributes 'Main-Class': 'com.wisecoders.dbschema.influxdb.JdbcDriver'
         attributes 'Class-Path': configurations.runtimeClasspath.files.collect { it.getName() }.join(' ')
@@ -56,7 +68,6 @@ task zip(type: Zip, dependsOn:['clean','jar']) {
 }

 task tar(type: Tar, dependsOn:['clean','jar']) {
-    archiveName 'InfluxDBJdbcDriver.tar'
     from configurations.runtimeClasspath.allArtifacts.files
     from configurations.runtimeClasspath
 }
diff --git a/src/main/java/com/wisecoders/dbschema/influxdb/JdbcDriver.java b/src/main/java/com/wisecoders/dbschema/influxdb/JdbcDriver.java
index 1ec58e5..b235b02 100644
--- a/src/main/java/com/wisecoders/dbschema/influxdb/JdbcDriver.java
+++ b/src/main/java/com/wisecoders/dbschema/influxdb/JdbcDriver.java
@@ -3,12 +3,16 @@ package com.wisecoders.dbschema.influxdb;

 import com.influxdb.client.InfluxDBClient;
 import com.influxdb.client.InfluxDBClientFactory;
+import lombok.SneakyThrows;
+import org.apache.hc.core5.http.NameValuePair;
+import org.apache.hc.core5.net.URIBuilder;

-import java.net.URLDecoder;
-import java.nio.charset.StandardCharsets;
+import java.net.URI;
 import java.sql.*;
+import java.util.Map;
 import java.util.Properties;
 import java.util.logging.*;
+import java.util.stream.Collectors;

 /**
  * Copyright Wise Coders GmbH https://wisecoders.com
@@ -49,29 +53,18 @@ public class JdbcDriver implements Driver
      * https://{HOST}:{PORT}?org={DB}&token={PARAM}&days={PARAM2}
      */
     @Override
+    @SneakyThrows
     public Connection connect(String url, Properties info) throws SQLException {
         if ( url != null && acceptsURL( url )){

-            int idx;
-            if ( ( idx = url.lastIndexOf("?") ) > -1 ){
-                for ( String pair : url.substring( idx + 1 ).split("&")){
-                    String[] keyVal = pair.split("=");
-                    if ( keyVal.length == 2 ) {
-                        String key = keyVal[0];
-                        String val = URLDecoder.decode( keyVal[1], StandardCharsets.UTF_8 );
-                        if (!info.containsKey(keyVal[0])) {
-                            info.put(key, val);
-                            LOGGER.log(Level.INFO, "Param " + key + "=" + val );
-                        }
-                    }
-                }
-            }
+            Map<String, String> map = new URIBuilder(new URI(url)).getQueryParams().stream()
+                    .collect(Collectors.toMap(NameValuePair::getName, NameValuePair::getValue));

-            String userName = ( info != null ? (String)info.get("user") : null );
-            String password = ( info != null ? (String)info.get("password") : null );
-            String token = ( info != null ? (String)info.get("token") : null );
-            String org = ( info != null ? (String)info.get("org") : null );
-            String startDaysStr = ( info != null ? (String)info.get(DAYS) : null );
+            String userName = map.getOrDefault("user", null);
+            String password = map.getOrDefault("password", null);
+            String token = map.getOrDefault("token", null);
+            String org = map.getOrDefault("org", null);
+            String startDaysStr = map.getOrDefault("password", null);

             LOGGER.log( Level.INFO, "Connection URL=" + url + " user=" + userName  + " password=" + password + " org=" + org +" token=" + token  + " days=" + startDaysStr );
wise-coders commented 11 months ago

I didn't understand what is wrong. The connect splits first by &, and then by =, only after the ? in the URL.

if ( ( idx = url.lastIndexOf("?") ) > -1 ){
                for ( String pair : url.substring( idx + 1 ).split("&")){
                    String[] keyVal = pair.split("=");
                    if ( keyVal.length == 2 ) {
VergeDX commented 11 months ago

If the token has tailing equal mark, Java's String#split(String) will strip them:

jshell> "token=AAAAAAAAAAAAA==".split("=")
$1 ==> String[2] { "token", "AAAAAAAAAAAAA" }
wise-coders commented 11 months ago

Thank you for your reply. I got it now. We added a fix and deployed a new version. If you use the driver in DbSchema, please drop the folder .DbSchema from the user home directory. The latest driver will be downloaded.