Skip to content

Commit 6de6b4d

Browse files
committed
Added samples and corrected password provider
1 parent 12acf18 commit 6de6b4d

File tree

6 files changed

+306
-4
lines changed

6 files changed

+306
-4
lines changed

ojdbc-provider-gcp/src/main/java/oracle/jdbc/provider/gcp/resource/GcpVaultSecretPasswordProvider.java

+7-1
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,13 @@
3737
*/
3838
package oracle.jdbc.provider.gcp.resource;
3939

40+
import java.io.UnsupportedEncodingException;
41+
import java.nio.charset.Charset;
4042
import java.util.Base64;
4143
import java.util.Map;
4244

45+
import com.google.protobuf.ByteString;
46+
4347
import oracle.jdbc.spi.PasswordProvider;
4448

4549
/**
@@ -60,7 +64,9 @@ public GcpVaultSecretPasswordProvider() {
6064

6165
@Override
6266
public char[] getPassword(Map<Parameter, CharSequence> parameterValues) {
63-
return Base64.getEncoder().encodeToString(getSecret(parameterValues).toByteArray()).toCharArray();
67+
ByteString secret = getSecret(parameterValues);
68+
String password = secret.toString(Charset.defaultCharset());
69+
return password.toCharArray();
6470
}
6571

6672
}

ojdbc-provider-samples/example-configuration.properties

+5
Original file line numberDiff line numberDiff line change
@@ -107,3 +107,8 @@ client_certificate_password=$3cr3t
107107
# Set to redirect URL for an interactive authentication which use your account in
108108
# Azure Active Directory to directly authenticate.
109109
azure_redirect_url=http://localhost:7071
110+
111+
# Google Cloud Platform
112+
gcp_secret_version_name=projects/138028249883/secrets/test-secret/versions/2
113+
gcp_secret_version_name_config=projects/138028249883/secrets/config-secret/versions/4
114+
gcp_object_storage_properties=project=onyx-eye-426013-i5;bucket=fm-test-bucket-123564;object=testObjectStorage.json

ojdbc-provider-samples/pom.xml

+8-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<name>Oracle JDBC Provider Code Samples</name>
66

77
<artifactId>ojdbc-provider-samples</artifactId>
8-
<version>1.0.1</version>
8+
<version>${project.parent.version}</version>
99
<packaging>jar</packaging>
1010

1111
<parent>
@@ -18,12 +18,17 @@
1818
<dependency>
1919
<groupId>com.oracle.database.jdbc</groupId>
2020
<artifactId>ojdbc-provider-azure</artifactId>
21-
<version>1.0.1</version>
21+
<version>${project.parent.version}</version>
2222
</dependency>
2323
<dependency>
2424
<groupId>com.oracle.database.jdbc</groupId>
2525
<artifactId>ojdbc-provider-oci</artifactId>
26-
<version>1.0.1</version>
26+
<version>${project.parent.version}</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.oracle.database.jdbc</groupId>
30+
<artifactId>ojdbc-provider-gcp</artifactId>
31+
<version>${project.parent.version}</version>
2732
</dependency>
2833
<dependency>
2934
<groupId>com.oracle.database.security</groupId>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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.configuration;
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+
48+
/**
49+
* A standalone example that configures Oracle JDBC to be provided with the
50+
* connection properties retrieved from OCI Object Storage.
51+
*/
52+
public class ObjectStorageExample {
53+
/**
54+
* An GCP Object Storage properties configured as a JVM system property,
55+
* environment variable, or configuration.properties file entry named
56+
* "gcp_object_storage_properties".
57+
*/
58+
private static final String OBJECT_PROPERTIES = Configuration
59+
.getRequired("gcp_object_storage_properties");
60+
61+
/**
62+
* <p>
63+
* Connects to a database using connection properties retrieved from GCP
64+
* Object Storage.
65+
* </p>
66+
*
67+
* @param args the command line arguments
68+
* @throws SQLException if an error occurs during the database calls
69+
*/
70+
public static void main(String[] args) throws SQLException {
71+
String url = "jdbc:oracle:thin:@config-gcpobject://" + OBJECT_PROPERTIES;
72+
73+
// Standard JDBC code
74+
OracleDataSource ds = new OracleDataSource();
75+
ds.setURL(url);
76+
77+
System.out.println("Connection URL: " + url);
78+
79+
try (Connection cn = ds.getConnection()) {
80+
String connectionString = cn.getMetaData().getURL();
81+
System.out.println("Connected to: " + connectionString);
82+
83+
Statement st = cn.createStatement();
84+
ResultSet rs = st.executeQuery("SELECT 'Hello, db' FROM sys.dual");
85+
if (rs.next())
86+
System.out.println(rs.getString(1));
87+
}
88+
}
89+
90+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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.configuration;
39+
40+
import java.sql.Connection;
41+
import java.sql.ResultSet;
42+
import java.sql.SQLException;
43+
import java.sql.Statement;
44+
45+
import oracle.jdbc.datasource.impl.OracleDataSource;
46+
import oracle.jdbc.provider.Configuration;
47+
48+
/**
49+
* A standalone example that configures Oracle JDBC to be provided with the
50+
* connection properties retrieved from GCP Secret Manager.
51+
*/
52+
public class SimpleVaultJsonExample {
53+
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_config".
58+
*/
59+
private static final String RESOURCE_NAME = Configuration
60+
.getRequired("gcp_secret_version_name_config");
61+
62+
/**
63+
* <p>
64+
* Simple example to retrieve connection properties from GCP Secret Manager.
65+
* </p>
66+
* <p>
67+
* To run this example, the payload needs to be stored in GCP Secret Manager.
68+
* The payload examples can be found in
69+
* {@link oracle.jdbc.spi.OracleConfigurationProvider}.
70+
* </p>
71+
* Users need to indicate the resource name of the Secret with the following
72+
* syntax:
73+
*
74+
* <pre>
75+
* jdbc:oracle:thin:@config-gcpvault:{resource-name}
76+
* </pre>
77+
*
78+
* @param args the command line arguments
79+
* @throws SQLException if an error occurs during the database calls
80+
**/
81+
public static void main(String[] args) throws SQLException {
82+
String url = "jdbc:oracle:thin:@config-gcpsecret://" + RESOURCE_NAME;
83+
// Sample default URL if non present
84+
if (args.length > 0) {
85+
url = args[0];
86+
}
87+
88+
// No changes required, configuration provider is loaded at runtime
89+
OracleDataSource ds = new OracleDataSource();
90+
ds.setURL(url);
91+
92+
// Standard JDBC code
93+
try (Connection cn = ds.getConnection()) {
94+
Statement st = cn.createStatement();
95+
ResultSet rs = st.executeQuery("SELECT 'Hello, db' FROM sys.dual");
96+
if (rs.next())
97+
System.out.println(rs.getString(1));
98+
}
99+
}
100+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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

Comments
 (0)