Skip to content

Commit f49af69

Browse files
authored
Merge pull request #189 from nats-io/2.3.0
2.3.0
2 parents 6c5aca7 + daf45c0 commit f49af69

17 files changed

Lines changed: 308 additions & 128 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11

22
# Change Log
33

4+
## Version 2.3.0
5+
6+
* [BREAKING CHANGE] Replaced use of strings for seeds and public keys in NKey code to use char[] to allow better memory security.
7+
48
## Version 2.2.0
59

610
* [ADDED] Support for NKeys, and nonce based auth

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ This is version 2.1 of the java-nats library. This version is a ground up rewrit
1616

1717
The API is [simple to use](#listening-for-incoming-messages) and highly [performant](#Benchmarking).
1818

19-
Version 2+ uses a simplified versioning scheme. Any issues will be fixed in the incremental version number. As a major release, the major version has been updated to 2 to allow clients to limit there use of this new API. With the addition of drain() we updated to 2.1, nkey support moved us to 2.2.
19+
Version 2+ uses a simplified versioning scheme. Any issues will be fixed in the incremental version number. As a major release, the major version has been updated to 2 to allow clients to limit there use of this new API. With the addition of drain() we updated to 2.1, NKey support moved us to 2.2.
2020

2121
Previous versions are still available in the repo.
2222

@@ -27,15 +27,20 @@ The client protocol spec doesn't explicitly state the encoding on subjects. Some
2727
### NKey-based Challenge Response Authentication
2828

2929
The NATS server is adding support for a challenge response authentication scheme based on [NKeys](https://github.com/nats-io/nkeys). Version 2.2.0 of
30-
the Java client supports this scheme via an AuthHandler interface.
30+
the Java client supports this scheme via an AuthHandler interface. *Version 2.3.0 replaced several NKey methods that used strings with methods using char[]
31+
to improve security. Since NKeys are new, and not available in the server we have not bumped the major version and are just bumping the minor version.*
3132

3233
## Installation
3334

3435
The java-nats client is provided in a single jar file, with a single external dependency for the encryption in NKey support. See [Building From Source](#building-from-source) for details on building the library.
3536

3637
### Downloading the Jar
3738

38-
You can download the latest jar at [https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.2.0/jnats-2.2.0.jar](https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.2.0/jnats-2.2.0.jar).
39+
You can download the latest jar at [https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.3.0/jnats-2.3.0.jar](https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.3.0/jnats-2.3.0.jar).
40+
41+
The examples are available at [https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.3.0/jnats-2.3.0-examples.jar](https://search.maven.org/remotecontent?filepath=io/nats/jnats/2.3.0/jnats-2.3.0-examples.jar).
42+
43+
To use NKeys, you will need the ed25519 library, which can be downloaded at [https://repo1.maven.org/maven2/net/i2p/crypto/eddsa/0.3.0/eddsa-0.3.0.jar](https://repo1.maven.org/maven2/net/i2p/crypto/eddsa/0.3.0/eddsa-0.3.0.jar).
3944

4045
### Using Gradle
4146

build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ plugins {
1212

1313
// Update version here, repeated check-ins not into master will have snapshot on them
1414
def versionMajor = 2
15-
def versionMinor = 2
15+
def versionMinor = 3
1616
def versionPatch = 0
1717
def versionModifier = ""
18-
def jarVersion = "2.2.0"
18+
def jarVersion = "2.3.0"
1919
def branch = System.getenv("TRAVIS_BRANCH");
2020

2121
def getVersionName = { ->
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// Copyright 2018 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package io.nats.examples;
15+
16+
import java.io.BufferedReader;
17+
import java.io.File;
18+
import java.io.FileReader;
19+
import java.io.IOException;
20+
import java.security.GeneralSecurityException;
21+
22+
import io.nats.client.AuthHandler;
23+
import io.nats.client.NKey;
24+
25+
public class ExampleAuthHandler implements AuthHandler {
26+
private NKey nkey;
27+
28+
public ExampleAuthHandler(String path) throws Exception {
29+
BufferedReader in = null;
30+
31+
try {
32+
in = new BufferedReader((new FileReader(new File(path))));
33+
34+
char[] buffer = new char[2048];
35+
int len = in.read(buffer);
36+
char[] seed = new char[len];
37+
38+
System.arraycopy(buffer, 0, seed, 0, len);
39+
40+
this.nkey = NKey.fromSeed(seed);
41+
42+
for (int i=0;i<buffer.length;i++) {
43+
buffer[i] = '\0';
44+
}
45+
} finally {
46+
if (in != null) {
47+
in.close();
48+
}
49+
}
50+
}
51+
52+
public NKey getNKey() {
53+
return this.nkey;
54+
}
55+
56+
public char[] getID() {
57+
try {
58+
return this.nkey.getPublicKey();
59+
} catch (GeneralSecurityException|IOException|NullPointerException ex) {
60+
return null;
61+
}
62+
}
63+
64+
public byte[] sign(byte[] nonce) {
65+
try {
66+
return this.nkey.sign(nonce);
67+
} catch (GeneralSecurityException|IOException|NullPointerException ex) {
68+
return null;
69+
}
70+
}
71+
}

src/examples/java/io/nats/examples/NatsDispatch.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.time.Duration;
1818
import java.util.concurrent.CountDownLatch;
1919

20+
import io.nats.client.AuthHandler;
2021
import io.nats.client.Connection;
2122
import io.nats.client.Dispatcher;
2223
import io.nats.client.Nats;
@@ -26,7 +27,9 @@ public class NatsDispatch {
2627

2728
static final String usageString =
2829
"\nUsage: java NatsDispatch [server] <subject> <msgCount>"
29-
+ "\n\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n";
30+
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
31+
+ "Set the environment variable NATS_NKEY to use challenge resposne authentication by setting a file containing your seed.\n"
32+
+ "Use the URL for user/pass/token authentication.\n";
3033

3134
public static void main(String args[]) {
3235
String subject;
@@ -48,8 +51,15 @@ public static void main(String args[]) {
4851

4952
try {
5053

51-
Options options = new Options.Builder().server(server).noReconnect().build();
52-
Connection nc = Nats.connect(options);
54+
Options.Builder builder = new Options.Builder().server(server).noReconnect();
55+
56+
if (System.getenv("NATS_NKEY") != null) {
57+
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
58+
builder.authHandler(handler);
59+
}
60+
61+
System.out.printf("Trying to connect to %s, and listen to %s for %d messages.\n", server, subject, msgCount);
62+
Connection nc = Nats.connect(builder.build());
5363
CountDownLatch latch = new CountDownLatch(msgCount); // dispatcher runs callback in another thread
5464

5565
Dispatcher d = nc.createDispatcher((msg) -> {

src/examples/java/io/nats/examples/NatsPub.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.time.Duration;
1818

19+
import io.nats.client.AuthHandler;
1920
import io.nats.client.Connection;
2021
import io.nats.client.Nats;
2122
import io.nats.client.Options;
@@ -24,7 +25,9 @@ public class NatsPub {
2425

2526
static final String usageString =
2627
"\nUsage: java NatsPub [server] <subject> <text message>"
27-
+ "\n\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n";
28+
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
29+
+ "Set the environment variable NATS_NKEY to use challenge resposne authentication by setting a file containing your seed.\n"
30+
+ "Use the URL for user/pass/token authentication.\n";
2831

2932
public static void main(String args[]) {
3033
String subject;
@@ -46,8 +49,14 @@ public static void main(String args[]) {
4649

4750
try {
4851

49-
Options options = new Options.Builder().server(server).noReconnect().build();
50-
Connection nc = Nats.connect(options);
52+
Options.Builder builder = new Options.Builder().server(server).noReconnect();
53+
54+
if (System.getenv("NATS_NKEY") != null) {
55+
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
56+
builder.authHandler(handler);
57+
}
58+
59+
Connection nc = Nats.connect(builder.build());
5160
nc.publish(subject, message.getBytes(StandardCharsets.UTF_8));
5261
nc.flush(Duration.ZERO);
5362
nc.close();

src/examples/java/io/nats/examples/NatsQSub.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.time.Duration;
1818

19+
import io.nats.client.AuthHandler;
1920
import io.nats.client.Connection;
2021
import io.nats.client.Message;
2122
import io.nats.client.Nats;
@@ -26,7 +27,9 @@ public class NatsQSub {
2627

2728
static final String usageString =
2829
"\nUsage: java NatsQSub [server] <subject> <queue> <msgCount>"
29-
+ "\n\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n";
30+
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
31+
+ "Set the environment variable NATS_NKEY to use challenge resposne authentication by setting a file containing your seed.\n"
32+
+ "Use the URL for user/pass/token authentication.\n";
3033

3134
public static void main(String args[]) {
3235
String subject;
@@ -51,8 +54,14 @@ public static void main(String args[]) {
5154

5255
try {
5356

54-
Options options = new Options.Builder().server(server).noReconnect().build();
55-
Connection nc = Nats.connect(options);
57+
Options.Builder builder = new Options.Builder().server(server).noReconnect();
58+
59+
if (System.getenv("NATS_NKEY") != null) {
60+
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
61+
builder.authHandler(handler);
62+
}
63+
64+
Connection nc = Nats.connect(builder.build());
5665
Subscription sub = nc.subscribe(subject, queue);
5766
nc.flush(Duration.ZERO);
5867

src/examples/java/io/nats/examples/NatsReply.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.time.Duration;
1818
import java.util.concurrent.CountDownLatch;
1919

20+
import io.nats.client.AuthHandler;
2021
import io.nats.client.Connection;
2122
import io.nats.client.Dispatcher;
2223
import io.nats.client.Nats;
@@ -26,7 +27,9 @@ public class NatsReply {
2627

2728
static final String usageString =
2829
"\nUsage: java NatsReply [server] <subject> <msgCount>"
29-
+ "\n\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n";
30+
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
31+
+ "Set the environment variable NATS_NKEY to use challenge resposne authentication by setting a file containing your seed.\n"
32+
+ "Use the URL for user/pass/token authentication.\n";
3033

3134
public static void main(String args[]) {
3235
String subject;
@@ -48,8 +51,14 @@ public static void main(String args[]) {
4851

4952
try {
5053

51-
Options options = new Options.Builder().server(server).noReconnect().build();
52-
Connection nc = Nats.connect(options);
54+
Options.Builder builder = new Options.Builder().server(server).noReconnect();
55+
56+
if (System.getenv("NATS_NKEY") != null) {
57+
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
58+
builder.authHandler(handler);
59+
}
60+
61+
Connection nc = Nats.connect(builder.build());
5362
CountDownLatch latch = new CountDownLatch(msgCount); // dispatcher runs callback in another thread
5463

5564
Dispatcher d = nc.createDispatcher((msg) -> {

src/examples/java/io/nats/examples/NatsReq.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.util.concurrent.Future;
1818

19+
import io.nats.client.AuthHandler;
1920
import io.nats.client.Connection;
2021
import io.nats.client.Message;
2122
import io.nats.client.Nats;
@@ -25,7 +26,9 @@ public class NatsReq {
2526

2627
static final String usageString =
2728
"\nUsage: java NatsReq [server] <subject> <text message>"
28-
+ "\n\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n";
29+
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
30+
+ "Set the environment variable NATS_NKEY to use challenge resposne authentication by setting a file containing your seed.\n"
31+
+ "Use the URL for user/pass/token authentication.\n";
2932

3033
public static void main(String args[]) {
3134
String subject;
@@ -47,8 +50,14 @@ public static void main(String args[]) {
4750

4851
try {
4952

50-
Options options = new Options.Builder().server(server).noReconnect().build();
51-
Connection nc = Nats.connect(options);
53+
Options.Builder builder = new Options.Builder().server(server).noReconnect();
54+
55+
if (System.getenv("NATS_NKEY") != null) {
56+
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
57+
builder.authHandler(handler);
58+
}
59+
60+
Connection nc = Nats.connect(builder.build());
5261
Future<Message> replyFuture = nc.request(subject, message.getBytes(StandardCharsets.UTF_8));
5362
Message reply = replyFuture.get();
5463

src/examples/java/io/nats/examples/NatsSub.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import java.nio.charset.StandardCharsets;
1717
import java.time.Duration;
1818

19+
import io.nats.client.AuthHandler;
1920
import io.nats.client.Connection;
2021
import io.nats.client.Message;
2122
import io.nats.client.Nats;
@@ -26,7 +27,9 @@ public class NatsSub {
2627

2728
static final String usageString =
2829
"\nUsage: java NatsSub [server] <subject> <msgCount>"
29-
+ "\n\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n";
30+
+ "\nUse tls:// or opentls:// to require tls, via the Default SSLContext\n"
31+
+ "Set the environment variable NATS_NKEY to use challenge resposne authentication by setting a file containing your seed.\n"
32+
+ "Use the URL for user/pass/token authentication.\n";
3033

3134
public static void main(String args[]) {
3235
String subject;
@@ -48,8 +51,15 @@ public static void main(String args[]) {
4851

4952
try {
5053

51-
Options options = new Options.Builder().server(server).noReconnect().build();
52-
Connection nc = Nats.connect(options);
54+
Options.Builder builder = new Options.Builder().server(server).noReconnect();
55+
56+
if (System.getenv("NATS_NKEY") != null) {
57+
AuthHandler handler = new ExampleAuthHandler(System.getenv("NATS_NKEY"));
58+
builder.authHandler(handler);
59+
}
60+
61+
System.out.printf("Trying to connect to %s, and listen to %s for %d messages.\n", server, subject, msgCount);
62+
Connection nc = Nats.connect(builder.build());
5363
Subscription sub = nc.subscribe(subject);
5464
nc.flush(Duration.ZERO);
5565

0 commit comments

Comments
 (0)