Skip to content

Commit c532cf9

Browse files
committed
Simple Sample GcpSecret
1 parent 909b27e commit c532cf9

File tree

3 files changed

+93
-2
lines changed

3 files changed

+93
-2
lines changed

ojdbc-provider-gcp/src/main/java/oracle/jdbc/provider/gcp/configuration/GcpSecretProvider.java

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package oracle.jdbc.provider.gcp.configuration;
22

3+
import com.google.cloud.secretmanager.v1.Secret;
4+
import com.google.cloud.secretmanager.v1.SecretManagerServiceClient;
35
import oracle.jdbc.driver.OracleConfigurationJsonProvider;
46

7+
import java.io.ByteArrayInputStream;
8+
import java.io.IOException;
59
import java.io.InputStream;
610
import java.sql.SQLException;
711

@@ -11,8 +15,14 @@
1115
**/
1216
public class GcpSecretProvider extends OracleConfigurationJsonProvider {
1317
@Override
14-
public InputStream getJson(String s) throws SQLException {
15-
return null;
18+
public InputStream getJson(String secretName) throws SQLException {
19+
try {
20+
SecretManagerServiceClient client = SecretManagerServiceClient.create();
21+
Secret secret = client.getSecret(secretName);
22+
return new ByteArrayInputStream(secret.toByteArray());
23+
} catch (IOException e) {
24+
throw new RuntimeException(e);
25+
}
1626
}
1727

1828
@Override

ojdbc-provider-samples/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
<artifactId>ojdbc-provider-oci</artifactId>
2626
<version>1.0.1</version>
2727
</dependency>
28+
<dependency>
29+
<groupId>com.oracle.database.jdbc</groupId>
30+
<artifactId>ojdbc-provider-gcp</artifactId>
31+
<version>1.0.1</version>
32+
</dependency>
2833
<dependency>
2934
<groupId>com.oracle.database.security</groupId>
3035
<artifactId>oraclepki</artifactId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
** Copyright (c) 2023 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+
package oracle.jdbc.provider.gcp.configuration;
39+
40+
import oracle.jdbc.datasource.impl.OracleDataSource;
41+
42+
import java.sql.Connection;
43+
import java.sql.ResultSet;
44+
import java.sql.SQLException;
45+
import java.sql.Statement;
46+
47+
/**
48+
* A standalone example that configures Oracle JDBC to be provided with the
49+
* connection properties retrieved from Gcp Secret Manager.
50+
*/
51+
public class SimpleGcpSecretJsonExample {
52+
53+
private static String url;
54+
55+
public static void main(String[] args) throws SQLException {
56+
57+
// Sample default URL if non present
58+
if (args.length == 0) {
59+
url = "jdbc:oracle:thin:@config-gcpsecret://projects/264897122777/secrets/jdbcconfig";
60+
} else {
61+
url = args[0];
62+
}
63+
64+
// No changes required, configuration provider is loaded at runtime
65+
OracleDataSource ds = new OracleDataSource();
66+
ds.setURL(url);
67+
68+
// Standard JDBC code
69+
try (Connection cn = ds.getConnection()) {
70+
Statement st = cn.createStatement();
71+
ResultSet rs = st.executeQuery("SELECT 'Hello, db' FROM sys.dual");
72+
if (rs.next())
73+
System.out.println(rs.getString(1));
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)