Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,27 @@ jobs:
--export-url-as TESTING_INFLUXDB_URL \
--export-db-as TESTING_INFLUXDB_DATABASE \
--export-token-as TESTING_INFLUXDB_TOKEN
- run:
name: Generate self-signed certificates
command: bash ./scripts/generate-self-signed-cert.sh
- run:
name: "Running tests"
command: |
mvn -B -U clean install -DargLine="@{argLine} << parameters.arg-line >>"
- run:
name: Remove self-signed certificates
command: |
rm -f test/java/com/influxdb/v3/client/testdata/certificates/influxdb.crt \
test/java/com/influxdb/v3/client/testdata/certificates/influxdb.key \
test/java/com/influxdb/v3/client/testdata/certificates/influxdb.pem \
test/java/com/influxdb/v3/client/testdata/certificates/other-server.crt \
test/java/com/influxdb/v3/client/testdata/certificates/other-server.key \
test/java/com/influxdb/v3/client/testdata/certificates/other-server.pem \
test/java/com/influxdb/v3/client/testdata/certificates/client.crt \
test/java/com/influxdb/v3/client/testdata/certificates/client.key \
test/java/com/influxdb/v3/client/testdata/certificates/client.pem \
test/java/com/influxdb/v3/client/testdata/certificates/client-encrypted.key
when: always
- save_cache:
name: Saving Maven Cache
key: *cache-key
Expand Down
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
## 1.12.0 [unreleased]

### Features

1. [#470](https://github.com/InfluxCommunity/influxdb3-java/pull/470):
- Add support for mTLS.
- Support separated .crt and .key files or bundled certificate and key in one .p12 file.

### CI

1. [#446](https://github.com/InfluxCommunity/influxdb3-java/pull/446): Sets up automated releasing to Maven Central
Expand Down
111 changes: 111 additions & 0 deletions examples/src/main/java/com/influxdb/v3/TlsConfigExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* The MIT License
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.influxdb.v3;

import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import com.influxdb.v3.client.InfluxDBClient;
import com.influxdb.v3.client.PointValues;
import com.influxdb.v3.client.config.ClientConfig;

public final class TlsConfigExample {

public static String resolveProperty(final String property, final String fallback) {
return System.getProperty(property, System.getenv(property)) == null
? fallback : System.getProperty(property, System.getenv(property));
}

private TlsConfigExample() { }

public static void main(final String[] args) {
String host = resolveProperty("INFLUX_HOST", "https://localhost:8181");
String token = resolveProperty("INFLUX_TOKEN", "my-token");
String database = resolveProperty("INFLUX_DATABASE", "my-database");

String measurement = "tls_example";

// ClientConfig config = configTls(host, token, database);

// ClientConfig config = configMutualTls(host, token, database);

try (InfluxDBClient client = InfluxDBClient.getInstance(config)) {
client.writeRecord(String.format("%s,id=0001 temp=30.14,ticks=42i", measurement));

TimeUnit.SECONDS.sleep(1);
String sql = String.format("SELECT * FROM %s ORDER BY time DESC", measurement);
try (Stream<PointValues> values = client.queryPoints(sql)) {
values.forEach(pv -> {
String sv = measurement + ","
+ " id: " + pv.getTag("id") + ","
+ " fVal: " + pv.getFloatField("temp") + ","
+ " iVal: " + pv.getIntegerField("ticks") + ","
+ " " + pv.getTimestamp();
System.out.println(sv);
});
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}

public ClientConfig configTls(
@Nonnull final String host,
@Nonnull final String token,
@Nonnull final String database) {
ClientConfig.Builder configBuilder = new ClientConfig.Builder()
.host(host)
.token(token.toCharArray())
.database(database)
.sslRootsFilePath("path/to/server.crt");

// Optional, set this if client certificate is password protected.
configBuilder.sslRootFilePassword("password".toCharArray());
return configBuilder.build();
}

public ClientConfig configMutualTls(
@Nonnull final String host,
@Nonnull final String token,
@Nonnull final String database) {
ClientConfig.Builder configBuilder = new ClientConfig.Builder()
.host(host)
.token(token.toCharArray())
.database(database)
.sslRootsFilePath("path/to/server.crt");

// Optional, set this if server certificate is password-protected.
configBuilder.sslRootFilePassword("password".toCharArray());

// Config client certificate send to the server by using a bundled certificate and key in one .p12 file
// or separated .crt and .key files format, but NOT both.

// Using separated .crt and .key files format.
configBuilder.certificateFilePath("path/to/client.crt", "path/to/client.key");

// Using .p12 file format.
// configBuilder.certificateP12FilePath("path/to/client.p12", "password".toCharArray());


return configBuilder.build();
}
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@
**/target/**, **/*.jar, **/.git/**, **/.*, **/*.png, **/*.iml, **/*.bolt, .idea/**,
**/*nightly*/**, **/.m2/**, LICENSE, **/*.md, **/.github/**, license_header.txt,
release.properties/, **/pom.xml.releaseBackup, **/pom.xml.tag, **/semantic.yml,
.circleci/config.yml, **/*.pem
.circleci/config.yml, **/*.pem, **/*.p12, **/*.crt, **/*.key
</excludes>
</licenseSet>
</licenseSets>
Expand Down
59 changes: 59 additions & 0 deletions scripts/generate-self-signed-cert.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#!/bin/bash
#
# The MIT License
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#

set -euo pipefail

SCRIPT_DIR="$(cd -- "$(dirname -- "${BASH_SOURCE[0]}")" && pwd)"
CERTS_DIR="${CERTS_DIR:-${SCRIPT_DIR}/../src/test/java/com/influxdb/v3/client/testdata/certificates}"
mkdir -p "$CERTS_DIR"
CERTS_DIR="$(cd -- "$CERTS_DIR" && pwd)"
cd "$CERTS_DIR"

PKCS12_PASSWORD="changeit"
INFLUXDB_SERVER_NAME="influxdb"
OTHER_SERVER_NAME="other-server"

# Gen Server .crt and .key with SAN (Subject Alternative Names) then convert to .p12 file
for SERVER_NAME in "$INFLUXDB_SERVER_NAME" "$OTHER_SERVER_NAME"; do
echo "### Generate server key and certificate for $SERVER_NAME..."
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 | openssl pkcs8 -topk8 -nocrypt -out "${SERVER_NAME}.key"

openssl req -new -x509 -key "${SERVER_NAME}.key" -out "${SERVER_NAME}.crt" -days 3650 \
-subj "/CN=localhost/O=Development/C=US" \
-addext "subjectAltName = DNS:localhost,IP:127.0.0.1"

openssl pkcs12 -export -in "${SERVER_NAME}.crt" -inkey "${SERVER_NAME}.key" \
-out "${SERVER_NAME}.p12" -name "myalias" -password "pass:$PKCS12_PASSWORD"
done

# Gen Client .crt and .key then convert to .p12 file
echo "### Generate client key..."
openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:2048 | openssl pkcs8 -topk8 -nocrypt -out client.key
openssl req -new -x509 -key client.key -out client.crt -days 3650 \
-subj "/CN=test-client/O=Development/C=US"
openssl pkcs12 -export -in client.crt -inkey client.key \
-out client.p12 -name "myalias" -password "pass:$PKCS12_PASSWORD"

#Encrypt pkcs8 for client.key for testing
openssl pkcs8 -topk8 -v2 aes-256-cbc -iter 1000000 -in client.key -out client_pkcs8.key \
-passout "pass:$PKCS12_PASSWORD"
Loading
Loading