dropwizard-metrics-influxdb icon indicating copy to clipboard operation
dropwizard-metrics-influxdb copied to clipboard

InfluxDbHttpSender encodes auth string with newline symbol in the end

Open e-vrvr opened this issue 7 years ago • 7 comments

I'm trying to add authorization to my http(s) reporter, so I set config entry to auth: user:password. Later, here it gets encrypted to dXNlcjpwYXNzd29yZA==\r\n -note the newline symbols in the end. And then during connection construction, it fails with Illegal character(s) in message header field here .

Full stack trace:

java.lang.IllegalArgumentException: Illegal character(s) in message header value: Basic dXNlcjpwYXNzd29yZA==(newline symbol)

	at sun.net.www.protocol.http.HttpURLConnection.checkMessageHeader(HttpURLConnection.java:507)
	at sun.net.www.protocol.http.HttpURLConnection.isExternalMessageHeaderAllowed(HttpURLConnection.java:459)
	at sun.net.www.protocol.http.HttpURLConnection.setRequestProperty(HttpURLConnection.java:3017)
	at sun.net.www.protocol.https.HttpsURLConnectionImpl.setRequestProperty(HttpsURLConnectionImpl.java:316)
	at com.izettle.metrics.influxdb.InfluxDbHttpSender.writeData(InfluxDbHttpSender.java:68)
	at com.izettle.metrics.influxdb.InfluxDbBaseSender.writeData(InfluxDbBaseSender.java:46)
	at com.izettle.metrics.influxdb.InfluxDbHttpSender.writeData(InfluxDbHttpSender.java:15)
	at com.izettle.metrics.influxdb.InfluxDbReporter.report(InfluxDbReporter.java:240)
	at com.codahale.metrics.ScheduledReporter.report(ScheduledReporter.java:162)
	at com.codahale.metrics.ScheduledReporter$1.run(ScheduledReporter.java:117)
	at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
	at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
	at java.lang.Thread.run(Thread.java:745)

Sample config

metrics:
  frequency: 1m
  reporters:
    - type: influxdb
      protocol: https
      host: influxdb.example.com
      port: 443
      database: test

Is there any way to make this working?

e-vrvr avatar Nov 23 '17 08:11 e-vrvr

Could you give an example of you config with auth where you see this?

rickard-von-essen-iz avatar Nov 23 '17 10:11 rickard-von-essen-iz

Sure

metrics:
  frequency: 1m
  reporters:
    - type: influxdb
      protocol: https
      host: influxdb.example.com
      port: 443
      database: test
      auth: user:password
      tags:
        host: dev
      prefix: mymetrics_
      precision: 10s
      frequency: 10s
      groupGauges: yes
      includes:
        - memory.heap.usage

UPD. latest version of library, dropwizard 1.2.0

e-vrvr avatar Nov 23 '17 11:11 e-vrvr

I have the same issue.

I am using the influxdb docker without any configuration.

I basically call the reporter like this :

new InfluxDbHttpSender(influxUrl.getProtocol(),
                influxUrl.getHost(),
                influxUrl.getPort(),
                db, influxUrl.getUserInfo(), TimeUnit.SECONDS,
                1000, 1000, "")

and influxUrl is http://root:root@influx:8086/dbname

then I get the error :

api-service_1  | 13:47:37.650 [metrics-influxDb-reporter-1-thread-1] WARN com.izettle.metrics.influxdb.InfluxDbReporter - Unable to report to InfluxDB with error 'Illegal character(s) in message header value: Basic cm9vdDpyb290
api-service_1  | 13:48:37.643 [metrics-influxDb-reporter-1-thread-1] WARN com.izettle.metrics.influxdb.InfluxDbReporter - Unable to report to InfluxDB with error 'Illegal character(s) in message header value: Basic cm9vdDpyb290

I'm using the 1.2.2 version.

artragis avatar Mar 29 '18 13:03 artragis

I think I have the fix, I try a PR soon. Found from stackoverflow

artragis avatar Mar 29 '18 14:03 artragis

what I did was extending InfluxDbHttpSender with this code

if (authString != null && !authString.isEmpty()) {
            this.authStringEncoded = Base64.encodeBase64String(authString.getBytes(Charsets.UTF_8)).trim(); // this is the one single difference from parent class
        } else {
            this.authStringEncoded = "";
        }

e-vrvr avatar Mar 29 '18 14:03 e-vrvr

How can you extend as the properties are private final?

artragis avatar Mar 30 '18 07:03 artragis

I found the reason of this bug : I was using another dependency that requires commons-condecs at version 1.4, and this version... adds a \n\r. Once I forced in my own pom the common-codecs to be 1.9 or later (we are at 1.11 today) no problem occures.

@rickard-von-essen-iz To ensure my fix, I have a unittest, do you want me to make a PR for that?

artragis avatar Mar 30 '18 07:03 artragis