Skip to content

Commit ef22972

Browse files
authored
Merge pull request #18 from michaelraney/master
Update default behavior for configuration
2 parents 6b199e1 + 36d3ac6 commit ef22972

File tree

5 files changed

+106
-8
lines changed

5 files changed

+106
-8
lines changed

pom.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,10 @@
228228
<!-- Generally, consumers of the shaded JAR
229229
will already have the driver if they want
230230
the plugin -->
231-
<excludes>com.datastax.oss</excludes>
231+
<excludes>
232+
<exclude>com.datastax.oss</exclude>
233+
<exclude>com.fasterxml.jackson.core</exclude>
234+
</excludes>
232235
</artifactSet>
233236
</configuration>
234237
<executions>

src/main/java/software/aws/mcs/auth/SigV4AuthProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public String getPath() {
130130
* Unused for this plugin.
131131
*/
132132
public SigV4AuthProvider(DriverContext driverContext) {
133-
this(driverContext.getConfig().getDefaultProfile().getString(REGION_OPTION));
133+
this(driverContext.getConfig().getDefaultProfile().getString(REGION_OPTION, null));
134134
}
135135

136136
/**

src/test/java/software/aws/mcs/auth/TestSigV4.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ public static void main(String[] args) throws Exception {
5555
// This class is thread-safe, you should create a single instance (per target Cassandra cluster), and share
5656
// it throughout your application.
5757
try (CqlSession session = CqlSession.builder()
58-
.addContactPoints(contactPoints)
59-
.withAuthProvider(new SigV4AuthProvider())
60-
.withSslContext(SSLContext.getDefault())
61-
.withLocalDatacenter("us-west-2")
62-
.build()) {
58+
.addContactPoints(contactPoints)
59+
.withAuthProvider(new SigV4AuthProvider())
60+
.withSslContext(SSLContext.getDefault())
61+
.withLocalDatacenter("us-west-2")
62+
.build()) {
6363

6464
// We use execute to send a query to Cassandra. This returns a ResultSet, which is essentially a collection
6565
// of Row objects.
@@ -72,4 +72,4 @@ public static void main(String[] args) throws Exception {
7272
System.out.printf("Cassandra version is: %s%n", releaseVersion);
7373
}
7474
}
75-
}
75+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package software.aws.mcs.auth;
2+
3+
/*-
4+
* #%L
5+
* AWS SigV4 Auth Java Driver 4.x Plugin
6+
* %%
7+
* Copyright (C) 2020-2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
8+
* %%
9+
* Licensed under the Apache License, Version 2.0 (the "License");
10+
* you may not use this file except in compliance with the License.
11+
* You may obtain a copy of the License at
12+
*
13+
* http://www.apache.org/licenses/LICENSE-2.0
14+
*
15+
* Unless required by applicable law or agreed to in writing, software
16+
* distributed under the License is distributed on an "AS IS" BASIS,
17+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18+
* See the License for the specific language governing permissions and
19+
* limitations under the License.
20+
* #L%
21+
*/
22+
23+
import java.io.File;
24+
import java.net.InetSocketAddress;
25+
import java.net.URL;
26+
import java.util.ArrayList;
27+
28+
import com.datastax.oss.driver.api.core.CqlSession;
29+
import com.datastax.oss.driver.api.core.config.DriverConfigLoader;
30+
import com.datastax.oss.driver.api.core.cql.ResultSet;
31+
import com.datastax.oss.driver.api.core.cql.Row;
32+
33+
public class TestSigV4Config {
34+
static String[] DEFAULT_CONTACT_POINTS = {"127.0.0.1:9042"};
35+
36+
public static void main(String[] args) throws Exception {
37+
String[] contactPointsRaw = DEFAULT_CONTACT_POINTS;
38+
39+
if (args.length == 1) {
40+
contactPointsRaw = args[0].split(",");
41+
} else if (args.length > 1) {
42+
System.out.println("Usage: TestSigV4 [<contact points, comma separated, 'IP:port' format>]");
43+
System.exit(-1);
44+
}
45+
46+
ArrayList<InetSocketAddress> contactPoints = new ArrayList<>(contactPointsRaw.length);
47+
48+
for (int i = 0; i < contactPointsRaw.length; i++) {
49+
String[] parts = contactPointsRaw[i].split(":");
50+
contactPoints.add(InetSocketAddress.createUnresolved(parts[0], Integer.parseInt(parts[1])));
51+
}
52+
53+
System.out.println("Using endpoints: " + contactPoints);
54+
55+
//By default the reference.conf is loaded by the driver which contains all defaults.
56+
//You can override this by providing reference.conf on the classpath
57+
//to isolate test you can load conf with a custom name
58+
URL url = TestSigV4Config.class.getClassLoader().getResource("keyspaces-reference.conf");
59+
60+
File file = new File(url.toURI());
61+
// The CqlSession object is the main entry point of the driver.
62+
// It holds the known state of the actual Cassandra cluster (notably the Metadata).
63+
// This class is thread-safe, you should create a single instance (per target Cassandra cluster), and share
64+
// it throughout your application.
65+
try (CqlSession session = CqlSession.builder()
66+
.withConfigLoader(DriverConfigLoader.fromFile(file))
67+
.addContactPoints(contactPoints)
68+
.withLocalDatacenter("us-west-2")
69+
.build()) {
70+
71+
// We use execute to send a query to Cassandra. This returns a ResultSet, which is essentially a collection
72+
// of Row objects.
73+
ResultSet rs = session.execute("select release_version from system.local");
74+
// Extract the first row (which is the only one in this case).
75+
Row row = rs.one();
76+
77+
// Extract the value of the first (and only) column from the row.
78+
String releaseVersion = row.getString("release_version");
79+
System.out.printf("Cassandra version is: %s%n", releaseVersion);
80+
}
81+
}
82+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
datastax-java-driver {
2+
advanced {
3+
reconnect-on-init=true
4+
5+
auth-provider {
6+
class = software.aws.mcs.auth.SigV4AuthProvider
7+
}
8+
ssl-engine-factory {
9+
class = DefaultSslEngineFactory
10+
hostname-validation = false
11+
}
12+
}
13+
}

0 commit comments

Comments
 (0)