-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathNeptuneRdf4JSigV4Example.java
211 lines (166 loc) · 7.35 KB
/
NeptuneRdf4JSigV4Example.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/
package com.amazonaws.neptune.client.rdf4j;
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import com.amazonaws.neptune.auth.NeptuneSigV4SignerException;
import software.amazon.awssdk.utils.StringUtils;
import org.eclipse.rdf4j.query.BindingSet;
import org.eclipse.rdf4j.query.TupleQuery;
import org.eclipse.rdf4j.query.TupleQueryResult;
import org.eclipse.rdf4j.query.Update;
import org.eclipse.rdf4j.repository.Repository;
import org.eclipse.rdf4j.repository.RepositoryConnection;
/**
* Small example demonstrating how to use NeptuneSparqlRepository with SignatureV4 in combination
* with the RDF4J library (see http://rdf4j.org/). The example uses the {@link NeptuneSparqlRepository}
* class contained in this package, which extends RDF4J's SparqlRepository class by IAM authentication.
* <p>
* Before running this code, make sure you've got everything setup properly, in particular:
* <ol>
* <li> Make sure that your AWS credentials are available in the provider chain, see
* <a href="https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/auth/DefaultAWSCredentialsProviderChain.html">
* DefaultAWSCredentialsProviderChain</a> for more information.</li>
* <li> Start the main method by passing in your endpoint, e.g. "http://<my_neptune_host>:<my_neptune_port>".
* The server will send a <code>"SELECT * WHERE { ?s ?p ?o } LIMIT 10"</code> query against your endpoint.</li>
* </ol>
*
* @author schmdtm
*/
public final class NeptuneRdf4JSigV4Example {
/**
* Region in which the Neptune instance runs.
*/
private static final String TEST_REGION = "us-east-1";
/**
* Sample select query, limited to ten results.
*/
private static final String SAMPLE_QUERY = "SELECT * WHERE { ?s ?p ?o } LIMIT 10";
/**
* Sample SPARQL UPDATE query.
*/
private static final String SAMPLE_UPDATE = "INSERT DATA { <http://Alice> <http://knows> <http://Bob> }";
/**
* Expected exception when sending an unsigned request to an auth enabled neptune server.
*/
static final String ACCESS_DENIED_MSG = "{\"status\":\"403 Forbidden\",\"message\":\"Access Denied!\"}";
/**
* Main method. Expecting the endpoint as the first argument.
*
* @param args arguments
* @throws Exception in case there are problems
*/
public static void main(final String[] args) throws Exception {
if (args.length == 0 || StringUtils.isEmpty(args[0])) {
System.err.println("Please specify your endpoint as program argument "
+ "(e.g.: http://<my_neptune_host>:<my_neptune_port>)");
System.exit(1);
}
final String endpoint = args[0];
// example of sending a signed query against the SPARQL endpoint
// use default SAMPLE_QUERY if not specified from input args
final String query = (args.length > 1 && !StringUtils.isEmpty(args[1])) ? args[1] : SAMPLE_QUERY;
executeSignedQueryRequest(endpoint, query);
}
/**
* Example for signed request.
*
* @param endpointUrl of the endpoint to which to send the request
* @throws NeptuneSigV4SignerException in case there's a problem signing the request
*/
protected static void executeSignedQueryRequest(final String endpointUrl, final String query)
throws NeptuneSigV4SignerException {
final AwsCredentialsProvider awsCredentialsProvider = DefaultCredentialsProvider.create();
final NeptuneSparqlRepository neptuneSparqlRepo =
new NeptuneSparqlRepository(endpointUrl, awsCredentialsProvider, TEST_REGION);
try {
neptuneSparqlRepo.init();
evaluateAndPrintQueryResult(query, neptuneSparqlRepo);
} finally {
neptuneSparqlRepo.shutDown();
}
}
/**
* Example for signed request.
*
* @param endpointUrl of the endpoint to which to send the request
* @throws NeptuneSigV4SignerException in case there's a problem signing the request
*/
protected static void executeSignedInsertRequest(final String endpointUrl)
throws NeptuneSigV4SignerException {
final AwsCredentialsProvider awsCredentialsProvider = DefaultCredentialsProvider.create();
final NeptuneSparqlRepository neptuneSparqlRepo =
new NeptuneSparqlRepository(endpointUrl, awsCredentialsProvider, TEST_REGION);
try {
neptuneSparqlRepo.init();
try (RepositoryConnection conn = neptuneSparqlRepo.getConnection()) {
final Update update = conn.prepareUpdate(SAMPLE_UPDATE);
update.execute();
System.out.println("Update query executed!");
}
} finally {
neptuneSparqlRepo.shutDown();
}
}
/**
* Example for unsigned request.
*
* @param endpointUrl of the endpoint to which to send the request
*/
protected static void executeUnsignedQueryRequest(final String endpointUrl) {
// use the simple constructor version which skips auth initialization
final NeptuneSparqlRepository neptuneSparqlRepo = new NeptuneSparqlRepository(endpointUrl);
try {
neptuneSparqlRepo.init();
evaluateAndPrintQueryResult(SAMPLE_QUERY, neptuneSparqlRepo);
} finally {
neptuneSparqlRepo.shutDown();
}
}
/**
* Evaluate the query and print the query result.
*
* @param queryString the query string to evaluate
* @param repo the repository over which to evaluate the query
*/
protected static void evaluateAndPrintQueryResult(final String queryString, final Repository repo) {
try (RepositoryConnection conn = repo.getConnection()) {
final TupleQuery query = conn.prepareTupleQuery(queryString);
System.out.println("> Printing query result: ");
final TupleQueryResult res = query.evaluate();
while (res.hasNext()) {
System.err.println("{");
final BindingSet bs = res.next();
boolean first = true;
for (final String varName : bs.getBindingNames()) {
if (first) {
System.out.print(" { ");
} else {
System.out.print(", ");
}
System.out.print("?" + varName + " -> " + bs.getBinding(varName));
first = false;
}
System.out.println("}");
System.out.println("}");
}
}
}
/**
* Constructor.
*/
private NeptuneRdf4JSigV4Example() {
}
}