Skip to content

Commit ac10d60

Browse files
Optimize HCP Vault integration and enhance Javadocs for HCP Vault Secret
1 parent 49d620b commit ac10d60

10 files changed

+554
-110
lines changed

ojdbc-provider-hashicorp/src/main/java/oracle/jdbc/provider/hashicorp/hcpvault/HcpVaultResourceFactory.java

+41-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
/*
2+
** Copyright (c) 2024 Oracle and/or its affiliates.
3+
**
4+
** The Universal Permissive License (UPL), Version 1.0
5+
**
6+
** Subject to the condition set forth below, permission is hereby granted to any
7+
** person obtaining a copy of this software, associated documentation and/or data
8+
** (collectively the "Software"), free of charge and under any and all copyright
9+
** rights in the Software, and any and all patent rights owned or freely
10+
** licensable by each licensor hereunder covering either (i) the unmodified
11+
** Software as contributed to or provided by such licensor, or (ii) the Larger
12+
** Works (as defined below), to deal in both
13+
**
14+
** (a) the Software, and
15+
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
** one is included with the Software (each a "Larger Work" to which the Software
17+
** is contributed by such licensors),
18+
**
19+
** without restriction, including without limitation the rights to copy, create
20+
** derivative works of, display, perform, and distribute the Software and make,
21+
** use, sell, offer for sale, import, export, have made, and have sold the
22+
** Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
** either these or other terms.
24+
**
25+
** This license is subject to the following condition:
26+
** The above copyright notice and either this complete permission notice or at
27+
** a minimum a reference to the UPL must be included in all copies or
28+
** substantial portions of the Software.
29+
**
30+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
** SOFTWARE.
37+
*/
38+
139
package oracle.jdbc.provider.hashicorp.hcpvault;
240

341
import oracle.jdbc.provider.factory.Resource;
@@ -10,11 +48,10 @@ public abstract class HcpVaultResourceFactory<T> implements ResourceFactory<T> {
1048

1149
@Override
1250
public final Resource<T> request(ParameterSet parameterSet) {
13-
// Retrieve the HCP credentials (token) from the credentials factory
1451
HcpVaultCredentials credentials = HcpVaultCredentialsFactory
15-
.getInstance()
16-
.request(parameterSet)
17-
.getContent();
52+
.getInstance()
53+
.request(parameterSet)
54+
.getContent();
1855

1956
try {
2057
return request(credentials, parameterSet);
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,54 @@
1-
package oracle.jdbc.provider.hashicorp.hcpvault.authentication;
1+
/*
2+
** Copyright (c) 2024 Oracle and/or its affiliates.
3+
**
4+
** The Universal Permissive License (UPL), Version 1.0
5+
**
6+
** Subject to the condition set forth below, permission is hereby granted to any
7+
** person obtaining a copy of this software, associated documentation and/or data
8+
** (collectively the "Software"), free of charge and under any and all copyright
9+
** rights in the Software, and any and all patent rights owned or freely
10+
** licensable by each licensor hereunder covering either (i) the unmodified
11+
** Software as contributed to or provided by such licensor, or (ii) the Larger
12+
** Works (as defined below), to deal in both
13+
**
14+
** (a) the Software, and
15+
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
** one is included with the Software (each a "Larger Work" to which the Software
17+
** is contributed by such licensors),
18+
**
19+
** without restriction, including without limitation the rights to copy, create
20+
** derivative works of, display, perform, and distribute the Software and make,
21+
** use, sell, offer for sale, import, export, have made, and have sold the
22+
** Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
** either these or other terms.
24+
**
25+
** This license is subject to the following condition:
26+
** The above copyright notice and either this complete permission notice or at
27+
** a minimum a reference to the UPL must be included in all copies or
28+
** substantial portions of the Software.
29+
**
30+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
** SOFTWARE.
37+
*/
238

39+
package oracle.jdbc.provider.hashicorp.hcpvault.authentication;
340

41+
/**
42+
* Enumeration of authentication methods supported by HCP Vault Secrets.
43+
* <p>
44+
* This represents the different ways to authenticate with the HCP Vault Secrets API.
45+
* </p>
46+
*/
447
public enum HcpVaultAuthenticationMethod {
48+
49+
/**
50+
* Authentication using client credentials via the OAuth2 client_credentials flow.
51+
* This method retrieves an API token by providing a client ID and client secret.
52+
*/
553
CLIENT_CREDENTIALS
654
}

ojdbc-provider-hashicorp/src/main/java/oracle/jdbc/provider/hashicorp/hcpvault/authentication/HcpVaultCredentials.java

+42-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,48 @@
1+
/*
2+
** Copyright (c) 2024 Oracle and/or its affiliates.
3+
**
4+
** The Universal Permissive License (UPL), Version 1.0
5+
**
6+
** Subject to the condition set forth below, permission is hereby granted to any
7+
** person obtaining a copy of this software, associated documentation and/or data
8+
** (collectively the "Software"), free of charge and under any and all copyright
9+
** rights in the Software, and any and all patent rights owned or freely
10+
** licensable by each licensor hereunder covering either (i) the unmodified
11+
** Software as contributed to or provided by such licensor, or (ii) the Larger
12+
** Works (as defined below), to deal in both
13+
**
14+
** (a) the Software, and
15+
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
** one is included with the Software (each a "Larger Work" to which the Software
17+
** is contributed by such licensors),
18+
**
19+
** without restriction, including without limitation the rights to copy, create
20+
** derivative works of, display, perform, and distribute the Software and make,
21+
** use, sell, offer for sale, import, export, have made, and have sold the
22+
** Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
** either these or other terms.
24+
**
25+
** This license is subject to the following condition:
26+
** The above copyright notice and either this complete permission notice or at
27+
** a minimum a reference to the UPL must be included in all copies or
28+
** substantial portions of the Software.
29+
**
30+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
** SOFTWARE.
37+
*/
38+
139
package oracle.jdbc.provider.hashicorp.hcpvault.authentication;
240

341
/**
4-
* Holds the HCP API token obtained from the client_credentials flow.
42+
* Represents the credentials required to authenticate with HCP Vault Secrets.
43+
* <p>
44+
* This class holds the API token obtained from the client_credentials OAuth2 flow.
45+
* </p>
546
*/
647
public final class HcpVaultCredentials {
748
private final String hcpApiToken;

ojdbc-provider-hashicorp/src/main/java/oracle/jdbc/provider/hashicorp/hcpvault/authentication/HcpVaultCredentialsFactory.java

+53-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
/*
2+
** Copyright (c) 2024 Oracle and/or its affiliates.
3+
**
4+
** The Universal Permissive License (UPL), Version 1.0
5+
**
6+
** Subject to the condition set forth below, permission is hereby granted to any
7+
** person obtaining a copy of this software, associated documentation and/or data
8+
** (collectively the "Software"), free of charge and under any and all copyright
9+
** rights in the Software, and any and all patent rights owned or freely
10+
** licensable by each licensor hereunder covering either (i) the unmodified
11+
** Software as contributed to or provided by such licensor, or (ii) the Larger
12+
** Works (as defined below), to deal in both
13+
**
14+
** (a) the Software, and
15+
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
** one is included with the Software (each a "Larger Work" to which the Software
17+
** is contributed by such licensors),
18+
**
19+
** without restriction, including without limitation the rights to copy, create
20+
** derivative works of, display, perform, and distribute the Software and make,
21+
** use, sell, offer for sale, import, export, have made, and have sold the
22+
** Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
** either these or other terms.
24+
**
25+
** This license is subject to the following condition:
26+
** The above copyright notice and either this complete permission notice or at
27+
** a minimum a reference to the UPL must be included in all copies or
28+
** substantial portions of the Software.
29+
**
30+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
** SOFTWARE.
37+
*/
38+
139
package oracle.jdbc.provider.hashicorp.hcpvault.authentication;
240

341
import oracle.jdbc.provider.factory.Resource;
@@ -9,15 +47,27 @@
947
import static oracle.jdbc.provider.util.ParameterUtil.getRequiredOrFallback;
1048

1149
/**
12-
* A factory for creating {@link HcpVaultCredentials} objects for HCP.
50+
* A factory for creating {@link HcpVaultCredentials} objects for HCP Vault Secrets.
51+
* <p>
52+
* This class implements the client_credentials flow for OAuth2 authentication, retrieving
53+
* an API token to interact with the HCP Vault Secrets API.
54+
* </p>
1355
*/
1456
public final class HcpVaultCredentialsFactory implements ResourceFactory<HcpVaultCredentials> {
1557

16-
// The param that indicates which HCP auth method to use (only one for now).
58+
/**
59+
* Parameter indicating the authentication method to use for HCP Vault Secrets.
60+
*/
1761
public static final Parameter<HcpVaultAuthenticationMethod> AUTHENTICATION_METHOD = Parameter.create(REQUIRED);
1862

19-
// The OAuth2 client_id and client_secret
63+
/**
64+
* Parameter for the OAuth2 client ID. Required.
65+
*/
2066
public static final Parameter<String> CLIENT_ID = Parameter.create(REQUIRED);
67+
68+
/**
69+
* Parameter for the OAuth2 client secret. Required.
70+
*/
2171
public static final Parameter<String> CLIENT_SECRET = Parameter.create(REQUIRED);
2272

2373
private static final HcpVaultCredentialsFactory INSTANCE = new HcpVaultCredentialsFactory();

ojdbc-provider-hashicorp/src/main/java/oracle/jdbc/provider/hashicorp/hcpvault/authentication/HcpVaultOAuthClient.java

+61-13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,41 @@
1+
/*
2+
** Copyright (c) 2024 Oracle and/or its affiliates.
3+
**
4+
** The Universal Permissive License (UPL), Version 1.0
5+
**
6+
** Subject to the condition set forth below, permission is hereby granted to any
7+
** person obtaining a copy of this software, associated documentation and/or data
8+
** (collectively the "Software"), free of charge and under any and all copyright
9+
** rights in the Software, and any and all patent rights owned or freely
10+
** licensable by each licensor hereunder covering either (i) the unmodified
11+
** Software as contributed to or provided by such licensor, or (ii) the Larger
12+
** Works (as defined below), to deal in both
13+
**
14+
** (a) the Software, and
15+
** (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
16+
** one is included with the Software (each a "Larger Work" to which the Software
17+
** is contributed by such licensors),
18+
**
19+
** without restriction, including without limitation the rights to copy, create
20+
** derivative works of, display, perform, and distribute the Software and make,
21+
** use, sell, offer for sale, import, export, have made, and have sold the
22+
** Software and the Larger Work(s), and to sublicense the foregoing rights on
23+
** either these or other terms.
24+
**
25+
** This license is subject to the following condition:
26+
** The above copyright notice and either this complete permission notice or at
27+
** a minimum a reference to the UPL must be included in all copies or
28+
** substantial portions of the Software.
29+
**
30+
** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31+
** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32+
** FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33+
** AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34+
** LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35+
** OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36+
** SOFTWARE.
37+
*/
38+
139
package oracle.jdbc.provider.hashicorp.hcpvault.authentication;
240

341
import oracle.sql.json.OracleJsonFactory;
@@ -7,11 +45,27 @@
745
import java.net.HttpURLConnection;
846
import java.net.URL;
947
import java.nio.charset.StandardCharsets;
48+
import java.util.Scanner;
1049

50+
/**
51+
* A client for performing OAuth2 operations with HCP Vault Secrets.
52+
* <p>
53+
* This class implements the client_credentials flow to obtain an API token
54+
* required for interacting with HCP Vault Secrets.
55+
* </p>
56+
*/
1157
public final class HcpVaultOAuthClient {
1258

1359
private HcpVaultOAuthClient() {}
1460

61+
/**
62+
* Fetches an access token from HCP Vault Secrets using the client_credentials flow.
63+
*
64+
* @param clientId the OAuth2 client ID. Must not be null or empty.
65+
* @param clientSecret the OAuth2 client secret. Must not be null or empty.
66+
* @return the access token as a {@code String}. Never null or empty.
67+
* @throws IllegalStateException if the token cannot be obtained.
68+
*/
1569
public static String fetchHcpAccessToken(String clientId, String clientSecret) {
1670
HttpURLConnection conn = null;
1771
try {
@@ -30,10 +84,14 @@ public static String fetchHcpAccessToken(String clientId, String clientSecret) {
3084
os.write(body.getBytes(StandardCharsets.UTF_8));
3185
}
3286

33-
if (conn.getResponseCode() == 200) {
34-
try (InputStream in = conn.getInputStream()) {
87+
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
88+
try (InputStream in = conn.getInputStream();
89+
Scanner scanner = new Scanner(in, StandardCharsets.UTF_8.name())) {
90+
scanner.useDelimiter("\\A");
91+
String jsonResponse = scanner.hasNext() ? scanner.next() : "";
92+
3593
OracleJsonObject response = new OracleJsonFactory()
36-
.createJsonTextValue(new ByteArrayInputStream(readAll(in).getBytes(StandardCharsets.UTF_8)))
94+
.createJsonTextValue(new ByteArrayInputStream(jsonResponse.getBytes(StandardCharsets.UTF_8)))
3795
.asJsonObject();
3896

3997
return response.getString("access_token");
@@ -49,14 +107,4 @@ public static String fetchHcpAccessToken(String clientId, String clientSecret) {
49107
}
50108
}
51109
}
52-
53-
private static String readAll(InputStream in) throws IOException {
54-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
55-
byte[] buffer = new byte[1024];
56-
int len;
57-
while ((len = in.read(buffer)) != -1) {
58-
baos.write(buffer, 0, len);
59-
}
60-
return new String(baos.toByteArray(), StandardCharsets.UTF_8);
61-
}
62110
}

0 commit comments

Comments
 (0)