|
| 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 | +package oracle.jdbc.provider.gcp.resource; |
| 39 | + |
| 40 | +import oracle.jdbc.datasource.impl.OracleDataSource; |
| 41 | +import oracle.jdbc.provider.Configuration; |
| 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 | + * A standalone example that configures Oracle JDBC to be provided with the |
| 51 | + * connection properties retrieved from OCI Object Storage. |
| 52 | + */ |
| 53 | +public class VaultSecretPasswordResourceExample { |
| 54 | + /** |
| 55 | + * An GCP SecretManager resource name configured as a JVM system property, |
| 56 | + * environment variable, or configuration.properties file entry named |
| 57 | + * "gcp_secret_version_name". |
| 58 | + */ |
| 59 | + private static final String RESOURCE_NAME = Configuration |
| 60 | + .getRequired("gcp_secret_version_name"); |
| 61 | + |
| 62 | + private static final String DB_URL = "(description=(retry_count=2)(retry_delay=3)(address=(protocol=tcps)(port=1521)(host=adb.us-phoenix-1.oraclecloud.com))(connect_data=(service_name=gebqqvpozhjbqbs_awyonurbg0gp0smq_tp.adb.oraclecloud.com))(security=(ssl_server_dn_match=yes)))"; |
| 63 | + |
| 64 | + /** |
| 65 | + * <p> |
| 66 | + * Connects to a database using connection properties retrieved from GCP |
| 67 | + * Object Storage. |
| 68 | + * </p> |
| 69 | + * |
| 70 | + * @param args the command line arguments |
| 71 | + * @throws SQLException if an error occurs during the database calls |
| 72 | + */ |
| 73 | + public static void main(String[] args) throws SQLException { |
| 74 | + String url = "jdbc:oracle:thin:@" + DB_URL; |
| 75 | + |
| 76 | + // Standard JDBC code |
| 77 | + OracleDataSource ds = new OracleDataSource(); |
| 78 | + ds.setURL(url); |
| 79 | + ds.setUser("DB_USER"); |
| 80 | + Properties properties = new Properties(); |
| 81 | + properties.put("oracle.jdbc.provider.password", "ojdbc-provider-gcp-secret-password"); |
| 82 | + properties.put("oracle.jdbc.provider.password.secretVersionName", RESOURCE_NAME); |
| 83 | + ds.setConnectionProperties(properties); |
| 84 | + |
| 85 | + try (Connection cn = ds.getConnection()) { |
| 86 | + String connectionString = cn.getMetaData().getURL(); |
| 87 | + System.out.println("Connected to: " + connectionString); |
| 88 | + |
| 89 | + Statement st = cn.createStatement(); |
| 90 | + ResultSet rs = st.executeQuery("SELECT 'Hello, db' FROM sys.dual"); |
| 91 | + if (rs.next()) |
| 92 | + System.out.println(rs.getString(1)); |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments