diff --git a/CHANGELOG.md b/CHANGELOG.md index 732c66fde..da1cdc53e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,9 @@ ## 2.13 [unreleased] +### Features + +- Support for Basic Authentication [PR #492](https://github.com/influxdata/influxdb-java/pull/492) ## 2.12 [2018-07-31] diff --git a/src/main/java/org/influxdb/impl/BasicAuthInterceptor.java b/src/main/java/org/influxdb/impl/BasicAuthInterceptor.java new file mode 100644 index 000000000..ffa75af61 --- /dev/null +++ b/src/main/java/org/influxdb/impl/BasicAuthInterceptor.java @@ -0,0 +1,24 @@ +package org.influxdb.impl; + +import java.io.IOException; + +import okhttp3.Credentials; +import okhttp3.Interceptor; +import okhttp3.Request; +import okhttp3.Response; + +public class BasicAuthInterceptor implements Interceptor { + + private String credentials; + + public BasicAuthInterceptor(final String user, final String password) { + credentials = Credentials.basic(user, password); + } + + @Override + public Response intercept(final Chain chain) throws IOException { + Request request = chain.request(); + Request authenticatedRequest = request.newBuilder().header("Authorization", credentials).build(); + return chain.proceed(authenticatedRequest); + } +} diff --git a/src/main/java/org/influxdb/impl/InfluxDBImpl.java b/src/main/java/org/influxdb/impl/InfluxDBImpl.java index 980184d93..a2f1019b6 100644 --- a/src/main/java/org/influxdb/impl/InfluxDBImpl.java +++ b/src/main/java/org/influxdb/impl/InfluxDBImpl.java @@ -79,8 +79,6 @@ public class InfluxDBImpl implements InfluxDB { private static final LogLevel LOG_LEVEL = LogLevel.parseLogLevel(System.getProperty(LOG_LEVEL_PROPERTY)); private final InetAddress hostAddress; - private final String username; - private final String password; private String version; private final Retrofit retrofit; private final InfluxDBService influxDBService; @@ -119,16 +117,14 @@ public InfluxDBImpl(final String url, final String username, final String passwo final ResponseFormat responseFormat) { this.messagePack = ResponseFormat.MSGPACK.equals(responseFormat); this.hostAddress = parseHostAddress(url); - this.username = username; - this.password = password; this.loggingInterceptor = new HttpLoggingInterceptor(); setLogLevel(LOG_LEVEL); this.gzipRequestInterceptor = new GzipRequestInterceptor(); OkHttpClient.Builder clonedBuilder = client.build().newBuilder(); - clonedBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor); - + clonedBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor). + addInterceptor(new BasicAuthInterceptor(username, password)); Factory converterFactory = null; switch (responseFormat) { case MSGPACK: @@ -167,8 +163,6 @@ public InfluxDBImpl(final String url, final String username, final String passwo super(); this.messagePack = false; this.hostAddress = parseHostAddress(url); - this.username = username; - this.password = password; this.loggingInterceptor = new HttpLoggingInterceptor(); setLogLevel(LOG_LEVEL); @@ -176,7 +170,8 @@ public InfluxDBImpl(final String url, final String username, final String passwo this.gzipRequestInterceptor = new GzipRequestInterceptor(); OkHttpClient.Builder clonedBuilder = client.build().newBuilder(); this.retrofit = new Retrofit.Builder().baseUrl(url) - .client(clonedBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor).build()) + .client(clonedBuilder.addInterceptor(loggingInterceptor).addInterceptor(gzipRequestInterceptor). + addInterceptor(new BasicAuthInterceptor(username, password)).build()) .addConverterFactory(MoshiConverterFactory.create()).build(); this.influxDBService = influxDBService; @@ -423,8 +418,6 @@ public void write(final BatchPoints batchPoints) { this.batchedCount.add(batchPoints.getPoints().size()); RequestBody lineProtocol = RequestBody.create(MEDIA_TYPE_STRING, batchPoints.lineProtocol()); execute(this.influxDBService.writePoints( - this.username, - this.password, batchPoints.getDatabase(), batchPoints.getRetentionPolicy(), TimeUtil.toTimePrecision(batchPoints.getPrecision()), @@ -437,8 +430,6 @@ public void write(final BatchPoints batchPoints) { public void write(final String database, final String retentionPolicy, final ConsistencyLevel consistency, final TimeUnit precision, final String records) { execute(this.influxDBService.writePoints( - this.username, - this.password, database, retentionPolicy, TimeUtil.toTimePrecision(precision), @@ -537,12 +528,10 @@ public void query(final Query query, final int chunkSize, final Consumer call = null; if (query instanceof BoundParameterQuery) { BoundParameterQuery boundParameterQuery = (BoundParameterQuery) query; - call = this.influxDBService.query(this.username, this.password, - query.getDatabase(), query.getCommandWithUrlEncoded(), chunkSize, + call = this.influxDBService.query(query.getDatabase(), query.getCommandWithUrlEncoded(), chunkSize, boundParameterQuery.getParameterJsonWithUrlEncoded()); } else { - call = this.influxDBService.query(this.username, this.password, - query.getDatabase(), query.getCommandWithUrlEncoded(), chunkSize); + call = this.influxDBService.query(query.getDatabase(), query.getCommandWithUrlEncoded(), chunkSize); } call.enqueue(new Callback() { @@ -581,11 +570,11 @@ public QueryResult query(final Query query, final TimeUnit timeUnit) { Call call = null; if (query instanceof BoundParameterQuery) { BoundParameterQuery boundParameterQuery = (BoundParameterQuery) query; - call = this.influxDBService.query(this.username, this.password, query.getDatabase(), + call = this.influxDBService.query(query.getDatabase(), TimeUtil.toTimePrecision(timeUnit), query.getCommandWithUrlEncoded(), boundParameterQuery.getParameterJsonWithUrlEncoded()); } else { - call = this.influxDBService.query(this.username, this.password, query.getDatabase(), + call = this.influxDBService.query(query.getDatabase(), TimeUtil.toTimePrecision(timeUnit), query.getCommandWithUrlEncoded()); } return executeQuery(call); @@ -598,7 +587,7 @@ public QueryResult query(final Query query, final TimeUnit timeUnit) { public void createDatabase(final String name) { Preconditions.checkNonEmptyString(name, "name"); String createDatabaseQueryString = String.format("CREATE DATABASE \"%s\"", name); - executeQuery(this.influxDBService.postQuery(this.username, this.password, Query.encode(createDatabaseQueryString))); + executeQuery(this.influxDBService.postQuery(Query.encode(createDatabaseQueryString))); } /** @@ -606,8 +595,7 @@ public void createDatabase(final String name) { */ @Override public void deleteDatabase(final String name) { - executeQuery(this.influxDBService.postQuery(this.username, this.password, - Query.encode("DROP DATABASE \"" + name + "\""))); + executeQuery(this.influxDBService.postQuery(Query.encode("DROP DATABASE \"" + name + "\""))); } /** @@ -615,8 +603,7 @@ public void deleteDatabase(final String name) { */ @Override public List describeDatabases() { - QueryResult result = executeQuery(this.influxDBService.query(this.username, - this.password, SHOW_DATABASE_COMMAND_ENCODED)); + QueryResult result = executeQuery(this.influxDBService.query(SHOW_DATABASE_COMMAND_ENCODED)); // {"results":[{"series":[{"name":"databases","columns":["name"],"values":[["mydb"]]}]}]} // Series [name=databases, columns=[name], values=[[mydb], [unittest_1433605300968]]] List> databaseNames = result.getResults().get(0).getSeries().get(0).getValues(); @@ -650,16 +637,13 @@ private Call callQuery(final Query query) { Call call; if (query instanceof BoundParameterQuery) { BoundParameterQuery boundParameterQuery = (BoundParameterQuery) query; - call = this.influxDBService.postQuery(this.username, - this.password, query.getDatabase(), query.getCommandWithUrlEncoded(), + call = this.influxDBService.postQuery(query.getDatabase(), query.getCommandWithUrlEncoded(), boundParameterQuery.getParameterJsonWithUrlEncoded()); } else { if (query.requiresPost()) { - call = this.influxDBService.postQuery(this.username, - this.password, query.getDatabase(), query.getCommandWithUrlEncoded()); + call = this.influxDBService.postQuery(query.getDatabase(), query.getCommandWithUrlEncoded()); } else { - call = this.influxDBService.query(this.username, - this.password, query.getDatabase(), query.getCommandWithUrlEncoded()); + call = this.influxDBService.query(query.getDatabase(), query.getCommandWithUrlEncoded()); } } return call; @@ -783,7 +767,7 @@ public void createRetentionPolicy(final String rpName, final String database, fi if (isDefault) { queryBuilder.append(" DEFAULT"); } - executeQuery(this.influxDBService.postQuery(this.username, this.password, Query.encode(queryBuilder.toString()))); + executeQuery(this.influxDBService.postQuery(Query.encode(queryBuilder.toString()))); } /** @@ -818,8 +802,7 @@ public void dropRetentionPolicy(final String rpName, final String database) { .append("\" ON \"") .append(database) .append("\""); - executeQuery(this.influxDBService.postQuery(this.username, this.password, - Query.encode(queryBuilder.toString()))); + executeQuery(this.influxDBService.postQuery(Query.encode(queryBuilder.toString()))); } private interface ChunkProccesor { diff --git a/src/main/java/org/influxdb/impl/InfluxDBService.java b/src/main/java/org/influxdb/impl/InfluxDBService.java index 074e9b004..dfe897257 100644 --- a/src/main/java/org/influxdb/impl/InfluxDBService.java +++ b/src/main/java/org/influxdb/impl/InfluxDBService.java @@ -39,49 +39,44 @@ interface InfluxDBService { * Can be one of one, any, all, quorum. Defaults to all. */ @POST("write") - public Call writePoints(@Query(U) String username, - @Query(P) String password, @Query(DB) String database, + public Call writePoints(@Query(DB) String database, @Query(RP) String retentionPolicy, @Query(PRECISION) String precision, @Query(CONSISTENCY) String consistency, @Body RequestBody batchPoints); @GET("query") - public Call query(@Query(U) String username, @Query(P) String password, @Query(DB) String db, + public Call query(@Query(DB) String db, @Query(EPOCH) String epoch, @Query(value = Q, encoded = true) String query); @POST("query") - public Call query(@Query(U) String username, @Query(P) String password, @Query(DB) String db, + public Call query(@Query(DB) String db, @Query(EPOCH) String epoch, @Query(value = Q, encoded = true) String query, @Query(value = PARAMS, encoded = true) String params); @GET("query") - public Call query(@Query(U) String username, @Query(P) String password, @Query(DB) String db, + public Call query(@Query(DB) String db, @Query(value = Q, encoded = true) String query); @POST("query") - public Call postQuery(@Query(U) String username, @Query(P) String password, @Query(DB) String db, + public Call postQuery(@Query(DB) String db, @Query(value = Q, encoded = true) String query); @POST("query") - public Call postQuery(@Query(U) String username, @Query(P) String password, @Query(DB) String db, + public Call postQuery(@Query(DB) String db, @Query(value = Q, encoded = true) String query, @Query(value = PARAMS, encoded = true) String params); @GET("query") - public Call query(@Query(U) String username, @Query(P) String password, - @Query(value = Q, encoded = true) String query); + public Call query(@Query(value = Q, encoded = true) String query); @POST("query") - public Call postQuery(@Query(U) String username, - @Query(P) String password, @Query(value = Q, encoded = true) String query); + public Call postQuery(@Query(value = Q, encoded = true) String query); @Streaming @GET("query?chunked=true") - public Call query(@Query(U) String username, - @Query(P) String password, @Query(DB) String db, @Query(value = Q, encoded = true) String query, + public Call query(@Query(DB) String db, @Query(value = Q, encoded = true) String query, @Query(CHUNK_SIZE) int chunkSize); @Streaming @POST("query?chunked=true") - public Call query(@Query(U) String username, - @Query(P) String password, @Query(DB) String db, @Query(value = Q, encoded = true) String query, + public Call query(@Query(DB) String db, @Query(value = Q, encoded = true) String query, @Query(CHUNK_SIZE) int chunkSize, @Query(value = PARAMS, encoded = true) String params); } diff --git a/src/test/java/org/influxdb/impl/ChunkingExceptionTest.java b/src/test/java/org/influxdb/impl/ChunkingExceptionTest.java index c81189b92..b18c657f4 100644 --- a/src/test/java/org/influxdb/impl/ChunkingExceptionTest.java +++ b/src/test/java/org/influxdb/impl/ChunkingExceptionTest.java @@ -56,7 +56,7 @@ public void testChunkingException(Exception ex, String message) throws IOExcepti Call call = mock(Call.class); ResponseBody responseBody = mock(ResponseBody.class); - when(influxDBService.query(any(String.class), any(String.class), any(String.class), any(String.class), anyInt())).thenReturn(call); + when(influxDBService.query(any(String.class), any(String.class), anyInt())).thenReturn(call); when(responseBody.source()).thenReturn(new Buffer()); doThrow(ex).when(adapter).fromJson(any(JsonReader.class));