Skip to content

Commit 2b9ab6b

Browse files
Add sample for resource providers (#185)
* Add samples for AWS Secrets Manager resource providers * Add samples for HCP Vault Dedicated and HCP Vault Secrets * Add samples for hcp vault dedicated and hcp vault secrets * remove unnecessary TCPS Wallet Provider usage in samples
1 parent b7f32a7 commit 2b9ab6b

15 files changed

+1276
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
** Copyright (c) 2025 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+
39+
package oracle.jdbc.provider.aws.resource;
40+
41+
import oracle.jdbc.datasource.impl.OracleDataSource;
42+
43+
import java.sql.Connection;
44+
import java.sql.ResultSet;
45+
import java.sql.SQLException;
46+
import java.sql.Statement;
47+
import java.util.Properties;
48+
49+
/**
50+
* Example demonstrating how to configure Oracle JDBC with the AWS Secrets Manager
51+
* Connection String Provider to retrieve connection strings from a tnsnames.ora
52+
* file stored in AWS Secrets Manager.
53+
*/
54+
public class SimpleConnectionStringProviderExample {
55+
public static void main(String[] args) throws SQLException {
56+
try {
57+
OracleDataSource ds = new OracleDataSource();
58+
ds.setURL("jdbc:oracle:thin:@");
59+
ds.setUser("DB_USERNAME");
60+
ds.setPassword("DB_PASSWORD");
61+
62+
Properties connectionProps = new Properties();
63+
64+
// Connection String Provider for retrieving tnsnames.ora content
65+
// from AWS Secrets Manager
66+
connectionProps.put("oracle.jdbc.provider.connectionString",
67+
"ojdbc-provider-aws-secrets-manager-tnsnames");
68+
connectionProps.put("oracle.jdbc.provider.connectionString.secretName",
69+
"secret-name");
70+
connectionProps.put("oracle.jdbc.provider.connectionString.tnsAlias",
71+
"tns-alias");
72+
73+
ds.setConnectionProperties(connectionProps);
74+
75+
try (Connection cn = ds.getConnection()) {
76+
String query = "SELECT 'Hello, db' FROM sys.dual";
77+
try (Statement st = cn.createStatement();
78+
ResultSet rs = st.executeQuery(query)) {
79+
if (rs.next()) {
80+
System.out.println(rs.getString(1));
81+
}
82+
}
83+
}
84+
} catch (SQLException e) {
85+
throw new RuntimeException("Connection failed: ", e);
86+
}
87+
}
88+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
** Copyright (c) 2025 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+
39+
package oracle.jdbc.provider.aws.resource;
40+
41+
import oracle.jdbc.datasource.impl.OracleDataSource;
42+
43+
import java.sql.Connection;
44+
import java.sql.ResultSet;
45+
import java.sql.SQLException;
46+
import java.sql.Statement;
47+
import java.util.Properties;
48+
49+
/**
50+
* Example demonstrating how to use the AWS Secrets Manager Password Provider
51+
* with Oracle JDBC to securely retrieve a database password from AWS Secrets Manager.
52+
*/
53+
public class SimplePasswordProviderExample {
54+
private static final String DB_URL = "(description=(retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=your_db_host))(connect_data=(service_name=your_service_name))(security=(ssl_server_dn_match=yes)))";
55+
private static final String JDBC_URL = "jdbc:oracle:thin:@" + DB_URL;
56+
57+
public static void main(String[] args) throws SQLException {
58+
try {
59+
OracleDataSource ds = new OracleDataSource();
60+
ds.setURL(JDBC_URL);
61+
ds.setUser("DB_USER");
62+
63+
Properties connectionProps = new Properties();
64+
connectionProps.put("oracle.jdbc.provider.password", "ojdbc-provider-aws-secrets-manager-password");
65+
connectionProps.put("oracle.jdbc.provider.password.secretName", "secret-name");
66+
connectionProps.put("oracle.jdbc.provider.password.fieldName", "password");
67+
68+
ds.setConnectionProperties(connectionProps);
69+
70+
try (Connection cn = ds.getConnection()) {
71+
String connectionString = cn.getMetaData().getURL();
72+
System.out.println("Connected to: " + connectionString);
73+
Statement st = cn.createStatement();
74+
ResultSet rs = st.executeQuery("SELECT 'Hello, db' FROM sys.dual");
75+
if (rs.next()) {
76+
System.out.println(rs.getString(1));
77+
}
78+
}
79+
} catch (SQLException e) {
80+
throw new RuntimeException("Connection failed: ", e);
81+
}
82+
}
83+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
** Copyright (c) 2025 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+
39+
package oracle.jdbc.provider.aws.resource;
40+
41+
import oracle.jdbc.datasource.impl.OracleDataSource;
42+
43+
import java.sql.Connection;
44+
import java.sql.ResultSet;
45+
import java.sql.SQLException;
46+
import java.sql.Statement;
47+
import java.util.Properties;
48+
49+
/**
50+
* Example demonstrating how to configure Oracle JDBC with the AWS Secrets Manager
51+
* SEPS Wallet Provider to retrieve database credentials from a Secure External
52+
* Password Store (SEPS) wallet stored in AWS Secrets Manager.
53+
*/
54+
public class SimpleSEPSWalletProviderExample {
55+
private static final String DB_URL = "(description=(retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=your_db_host))(connect_data=(service_name=your_service_name))(security=(ssl_server_dn_match=yes)))";
56+
private static final String JDBC_URL = "jdbc:oracle:thin:@" + DB_URL;
57+
58+
public static void main(String[] args) throws SQLException {
59+
try {
60+
OracleDataSource ds = new OracleDataSource();
61+
ds.setURL(JDBC_URL);
62+
63+
Properties connectionProps = new Properties();
64+
connectionProps.put("oracle.jdbc.provider.username", "ojdbc-provider-aws-secrets-manager-seps");
65+
connectionProps.put("oracle.jdbc.provider.password", "ojdbc-provider-aws-secrets-manager-seps");
66+
connectionProps.put("oracle.jdbc.provider.username.secretName", "secret-name");
67+
connectionProps.put("oracle.jdbc.provider.password.secretName", "secret-name");
68+
69+
ds.setConnectionProperties(connectionProps);
70+
71+
try (Connection cn = ds.getConnection()) {
72+
String connectionString = cn.getMetaData().getURL();
73+
System.out.println("Connected to: " + connectionString);
74+
Statement st = cn.createStatement();
75+
ResultSet rs = st.executeQuery("SELECT 'Hello, db' FROM sys.dual");
76+
if (rs.next()) {
77+
System.out.println(rs.getString(1));
78+
}
79+
}
80+
} catch (SQLException e) {
81+
throw new RuntimeException("Connection failed: ", e);
82+
}
83+
}
84+
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
** Copyright (c) 2025 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+
39+
package oracle.jdbc.provider.aws.resource;
40+
41+
import oracle.jdbc.datasource.impl.OracleDataSource;
42+
43+
import java.sql.Connection;
44+
import java.sql.ResultSet;
45+
import java.sql.SQLException;
46+
import java.sql.Statement;
47+
import java.util.Properties;
48+
49+
/**
50+
* Example demonstrating how to configure Oracle JDBC with the AWS Secrets Manager
51+
* TCPS Wallet Provider to establish a secure TLS connection using a wallet stored
52+
* in AWS Secrets Manager.
53+
*/
54+
public class SimpleTCPSWalletProviderExample {
55+
private static final String DB_URL = "(description=(retry_count=20)(retry_delay=3)(address=(protocol=tcps)(port=1522)(host=your_db_host))(connect_data=(service_name=your_service_name))(security=(ssl_server_dn_match=yes)))";
56+
private static final String JDBC_URL = "jdbc:oracle:thin:@" + DB_URL;
57+
private static final String USERNAME = "DB_USER";
58+
private static final String PASSWORD = "DB_PASSWORD";
59+
60+
public static void main(String[] args) throws SQLException {
61+
try {
62+
OracleDataSource ds = new OracleDataSource();
63+
ds.setURL(JDBC_URL);
64+
ds.setUser(USERNAME);
65+
ds.setPassword(PASSWORD);
66+
67+
Properties connectionProps = new Properties();
68+
connectionProps.put("oracle.jdbc.provider.tlsConfiguration",
69+
"ojdbc-provider-aws-secrets-manager-tls");
70+
connectionProps.put("oracle.jdbc.provider.tlsConfiguration.secretName",
71+
"secret-name");
72+
connectionProps.put("oracle.jdbc.provider.tlsConfiguration.type", "SSO");
73+
74+
ds.setConnectionProperties(connectionProps);
75+
76+
try (Connection cn = ds.getConnection()) {
77+
String connectionString = cn.getMetaData().getURL();
78+
System.out.println("Connected to: " + connectionString);
79+
Statement st = cn.createStatement();
80+
ResultSet rs = st.executeQuery("SELECT 'Hello, db' FROM sys.dual");
81+
if (rs.next()) {
82+
System.out.println(rs.getString(1));
83+
}
84+
}
85+
} catch (SQLException e) {
86+
throw new RuntimeException("Connection failed: ", e);
87+
}
88+
}
89+
}

0 commit comments

Comments
 (0)