Skip to content

Commit 68101e2

Browse files
committed
using links, successfully tested with pgsql ms server
1 parent cf2b7e7 commit 68101e2

File tree

7 files changed

+70
-33
lines changed

7 files changed

+70
-33
lines changed

.vscode/launch.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"type": "java",
9+
"name": "Launch Current File",
10+
"request": "launch",
11+
"mainClass": "${file}"
12+
},
13+
{
14+
"type": "java",
15+
"name": "Launch BinaryHeap",
16+
"request": "launch",
17+
"mainClass": "com.cosmian.rest.abe.policy.BinaryHeap",
18+
"projectName": "cosmian_java_lib"
19+
}
20+
]
21+
}

src/main/java/com/cosmian/RestClient.java

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.nio.charset.StandardCharsets;
1212
import java.security.KeyManagementException;
1313
import java.security.NoSuchAlgorithmException;
14+
import java.util.Optional;
1415
import java.util.logging.Logger;
1516

1617
import javax.net.ssl.HttpsURLConnection;
@@ -24,7 +25,7 @@ public class RestClient {
2425
public static final int DEFAULT_READ_TIMEOUT = 45000;
2526

2627
private final String server_url;
27-
private final String api_key;
28+
private final Optional<String> api_key;
2829
private final int connection_timeout;
2930
private final int read_timeout;
3031
// cache the socket factory for kee-alive
@@ -44,7 +45,7 @@ public class RestClient {
4445
* milliseconds.
4546
* @param api_key The API Key to use to authenticate
4647
*/
47-
public RestClient(String server_url, String api_key, int connection_timeout, int read_timeout) {
48+
public RestClient(String server_url, Optional<String> api_key, int connection_timeout, int read_timeout) {
4849
if (server_url.endsWith("/")) {
4950
this.server_url = server_url.substring(0, server_url.length() - 1);
5051
} else {
@@ -74,20 +75,22 @@ public RestClient(String server_url, String api_key, int connection_timeout, int
7475
* DEFAULT_READ_TIMEOUT
7576
*
7677
* @param server_url the REST Server URL e.g. http://localhost:9000
77-
* @param api_key API Key to use to authenticate
78+
* @param api_key he optional API Key to use to authenticate
7879
*/
79-
public RestClient(String server_url, String api_key) {
80+
public RestClient(String server_url, Optional<String> api_key) {
8081
this(server_url, api_key, DEFAULT_CONNECT_TIMEOUT, DEFAULT_READ_TIMEOUT);
8182
}
8283

8384
private HttpURLConnection get_connection(String path) throws MalformedURLException, IOException {
8485
HttpURLConnection cnx = (HttpURLConnection) new URL(this.server_url + path).openConnection();
8586
if (cnx instanceof HttpsURLConnection) {
86-
((HttpsURLConnection)cnx).setSSLSocketFactory(this.ssl_socket_factory);
87+
((HttpsURLConnection) cnx).setSSLSocketFactory(this.ssl_socket_factory);
8788
}
8889
cnx.setConnectTimeout(this.connection_timeout);
8990
cnx.setReadTimeout(this.read_timeout);
90-
cnx.setRequestProperty("Authorization", this.api_key);
91+
if (this.api_key.isPresent()) {
92+
cnx.setRequestProperty("Authorization", "Bearer " + this.api_key.get());
93+
}
9194
return cnx;
9295
}
9396

@@ -156,13 +159,19 @@ private static RestException handle_throwable(String method, Throwable t, HttpUR
156159
// see
157160
// https://docs.oracle.com/javase/8/docs/technotes/guides/net/http-keepalive.html
158161
int code = cnx.getResponseCode();
159-
byte[] bytes;
162+
byte[] bytes = null;
160163
try (InputStream es = cnx.getErrorStream()) {
161-
bytes = read_all_bytes(es);
164+
if (es != null) {
165+
bytes = read_all_bytes(es);
166+
}
162167
}
163168
String body;
164169
try {
165-
body = new String(bytes, StandardCharsets.UTF_8);
170+
if (bytes == null) {
171+
body = "";
172+
} else {
173+
body = new String(bytes, StandardCharsets.UTF_8);
174+
}
166175
} catch (Exception _e) {
167176
body = "N/A";
168177
}

src/main/java/com/cosmian/rest/Cosmian.java

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cosmian.rest;
22

3+
import java.util.Optional;
34
import java.util.logging.Logger;
45

56
import com.cosmian.CosmianException;
@@ -20,29 +21,32 @@ public class Cosmian {
2021
* Instantiate a new Cosmian Server REST Client
2122
*
2223
* @param server_url
23-
* the REST Server URL e.g. http://localhost:9000
24+
* the REST Server URL e.g. http://localhost:9000
2425
* @param api_key
25-
* the Cosmian API KEY
26+
* the Cosmian API KEY
2627
* @param connection_timeout
27-
* Sets a specified timeout value, in milliseconds, to be used when opening a communications link to the
28-
* resource referenced by this URLConnection.
28+
* Sets a specified timeout value, in milliseconds, to
29+
* be used when opening a communications link to the
30+
* resource referenced by this URLConnection.
2931
* @param read_timeout
30-
* Sets the read timeout to a specified timeout, in milliseconds.
32+
* Sets the read timeout to a specified timeout, in
33+
* milliseconds.
3134
*/
32-
Cosmian(String server_url, String api_key, int connection_timeout, int read_timeout) {
35+
Cosmian(String server_url, Optional<String> api_key, int connection_timeout, int read_timeout) {
3336
this.rest_client = new RestClient(server_url, api_key, connection_timeout, read_timeout);
3437
}
3538

3639
/**
37-
* Instantiate a new Cosmian Server REST Client with DEFAULT_CONNECT_TIMEOUT and DEFAULT_READ_TIMEOUT
40+
* Instantiate a new Cosmian Server REST Client with DEFAULT_CONNECT_TIMEOUT and
41+
* DEFAULT_READ_TIMEOUT
3842
*
3943
* @param server_url
40-
* the REST Server URL e.g. http://localhost:9000
44+
* the REST Server URL e.g. http://localhost:9000
4145
* @param api_key
42-
* the Cosmian API KEY
46+
* the Cosmian API KEY
4347
* @see RestClient
4448
*/
45-
public Cosmian(String server_url, String api_key) {
49+
public Cosmian(String server_url, Optional<String> api_key) {
4650
this.rest_client = new RestClient(server_url, api_key);
4751
}
4852

@@ -77,7 +81,7 @@ public Abe abe() {
7781
* Hex Encode an array of bytes
7882
*
7983
* @param bytes
80-
* the bytes to encode
84+
* the bytes to encode
8185
* @return the hex encoded String
8286
*/
8387
public static String hex_encode(byte[] bytes) {
@@ -88,10 +92,10 @@ public static String hex_encode(byte[] bytes) {
8892
* Decode an hex encoded String to bytes
8993
*
9094
* @param hex_encoded_string
91-
* the hex encoded String
95+
* the hex encoded String
9296
* @return the decoded bytes
9397
* @throws CosmianException
94-
* if the hex String is invalid
98+
* if the hex String is invalid
9599
*/
96100
public static byte[] hex_decode(String hex_encoded_string) throws CosmianException {
97101
try {
@@ -107,9 +111,9 @@ public static byte[] hex_decode(String hex_encoded_string) throws CosmianExcepti
107111
* Concat 2 byte-arrays
108112
*
109113
* @param a
110-
* first byte array
114+
* first byte array
111115
* @param b
112-
* second byte array
116+
* second byte array
113117
* @return the merged byte array
114118
*/
115119
public static byte[] concat(byte[] a, byte[] b) {

src/main/java/com/cosmian/rest/abe/Abe.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.cosmian.rest.abe;
22

3-
import java.nio.charset.StandardCharsets;
43
import java.util.Optional;
54
import java.util.logging.Logger;
65

@@ -32,6 +31,9 @@
3231
import com.cosmian.rest.kmip.types.Attributes;
3332
import com.cosmian.rest.kmip.types.CryptographicAlgorithm;
3433
import com.cosmian.rest.kmip.types.KeyFormatType;
34+
import com.cosmian.rest.kmip.types.Link;
35+
import com.cosmian.rest.kmip.types.LinkType;
36+
import com.cosmian.rest.kmip.types.LinkedObjectIdentifier;
3537
import com.cosmian.rest.kmip.types.ObjectType;
3638
import com.cosmian.rest.kmip.types.RevocationReason;
3739
import com.cosmian.rest.kmip.types.VendorAttribute;
@@ -214,10 +216,11 @@ public String createUserDecryptionKey(AccessPolicy accessPolicy, String privateM
214216
// convert the Access Policy to attributes and attach it to the common
215217
// attributes
216218
VendorAttribute accessPolicyAttribute = accessPolicy.toVendorAttribute();
217-
VendorAttribute masterPrivateKeyId = new VendorAttribute("cosmian", "abe_master_private_key_id",
218-
privateMasterKeyUniqueIdentifier.getBytes(StandardCharsets.UTF_8));
219219
commonAttributes.setVendorAttributes(
220-
Optional.of(new VendorAttribute[] { accessPolicyAttribute, masterPrivateKeyId }));
220+
Optional.of(new VendorAttribute[] { accessPolicyAttribute }));
221+
// link to the master private key
222+
commonAttributes.setLink(new Link[] {
223+
new Link(LinkType.Parent_Link, new LinkedObjectIdentifier(privateMasterKeyUniqueIdentifier)) });
221224

222225
Create request = new Create(ObjectType.Private_Key, commonAttributes, Optional.empty());
223226
CreateResponse response = this.kmip.create(request);

src/main/java/com/cosmian/rest/kmip/types/VendorAttribute.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ public class VendorAttribute implements KmipStruct {
2727
public static final String VENDOR_ATTR_ABE_ATTR = "abe_attributes";
2828
public static final String VENDOR_ATTR_ABE_POLICY = "abe_policy";
2929
public static final String VENDOR_ATTR_ABE_ACCESS_POLICY = "abe_access_policy";
30+
@Deprecated
3031
public static final String VENDOR_ATTR_ABE_HEADER_UID = "abe_header_uid";
31-
public static final String VENDOR_ATTR_ABE_MASTER_PRIV_KEY_ID = "abe_master_private_key_id";
32-
public static final String VENDOR_ATTR_ABE_MASTER_PUB_KEY_ID = "abe_master_public_key_id";
3332

3433
/**
3534
* Text String (with usage limited to alphanumeric, underscore and period – i.e.
4.15 MB
Binary file not shown.

src/test/java/com/cosmian/TestUtils.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.cosmian;
22

3+
import java.util.Optional;
34
import java.util.logging.ConsoleHandler;
45
import java.util.logging.Level;
56
import java.util.logging.Logger;
@@ -26,11 +27,11 @@ public static String kmsServerUrl() {
2627
return v;
2728
}
2829

29-
public static String apiKey() {
30+
public static Optional<String> apiKey() {
3031
String v = System.getenv("COSMIAN_API_KEY");
3132
if (v == null) {
32-
return "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IlRWdk5xTEtoUHhUSGdhYUNGRGRoSSJ9.eyJnaXZlbl9uYW1lIjoiTGFldGl0aWEiLCJmYW1pbHlfbmFtZSI6Ikxhbmdsb2lzIiwibmlja25hbWUiOiJsYWV0aXRpYS5sYW5nbG9pcyIsIm5hbWUiOiJMYW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