Skip to content

Commit c01ab41

Browse files
committed
ExadataExpress CloudConnectionManager for Java
1 parent 02494de commit c01ab41

File tree

6 files changed

+291
-0
lines changed

6 files changed

+291
-0
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Cloud Connection Manager
2+
3+
Cloud Connection Manager provides an easy way with establishing a connection to
4+
an Oracle Exadata Express Cloud Service.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
.metadata
3+
bin/
4+
tmp/
5+
*.tmp
6+
*.bak
7+
*.swp
8+
*~.nib
9+
local.properties
10+
.settings/
11+
.loadpath
12+
.recommenders
13+
14+
# Eclipse Core
15+
.project
16+
17+
# External tool builders
18+
.externalToolBuilders/
19+
20+
# Locally stored "Eclipse launch configurations"
21+
*.launch
22+
23+
# PyDev specific (Python IDE for Eclipse)
24+
*.pydevproject
25+
26+
# CDT-specific (C/C++ Development Tooling)
27+
.cproject
28+
29+
# JDT-specific (Eclipse Java Development Tools)
30+
.classpath
31+
32+
# Java annotation processor (APT)
33+
.factorypath
34+
35+
# PDT-specific (PHP Development Tools)
36+
.buildpath
37+
38+
# sbteclipse plugin
39+
.target
40+
41+
# Tern plugin
42+
.tern-project
43+
44+
# TeXlipse plugin
45+
.texlipse
46+
47+
# STS (Spring Tool Suite)
48+
.springBeans
49+
50+
# Code Recommenders
51+
.recommenders/
52+
53+
# Scala IDE specific (Scala & Java development for Eclipse)
54+
.cache-main
55+
.scala_dependencies
56+
.worksheet
57+
/target/
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>CloudConnectionManager</groupId>
4+
<artifactId>CloudConnectionManager</artifactId>
5+
<version>0.1.0</version>
6+
<name>CloudConnectionManager</name>
7+
<description>A connection manager for Exadata Express cloud service JDBC connections.</description>
8+
<build>
9+
<plugins>
10+
<plugin>
11+
<artifactId>maven-compiler-plugin</artifactId>
12+
<version>3.5.1</version>
13+
<configuration>
14+
<source/>
15+
<target/>
16+
</configuration>
17+
</plugin>
18+
</plugins>
19+
</build>
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.oracle</groupId>
23+
<artifactId>ojdbc7</artifactId>
24+
<version>12.1.0.2</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>com.oracle</groupId>
28+
<artifactId>osdt_core</artifactId>
29+
<version>12.1.0.2</version>
30+
</dependency>
31+
<dependency>
32+
<groupId>com.oracle</groupId>
33+
<artifactId>osdt_cert</artifactId>
34+
<version>12.1.0.2</version>
35+
</dependency>
36+
<dependency>
37+
<groupId>com.oracle</groupId>
38+
<artifactId>oraclepki</artifactId>
39+
<version>12.1.0.2</version>
40+
</dependency>
41+
</dependencies>
42+
</project>
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
/*
2+
* author: gvenzl
3+
* created: 19 Mar 2017
4+
*/
5+
6+
package com.oracle;
7+
8+
import java.io.File;
9+
import java.io.FileInputStream;
10+
import java.io.IOException;
11+
import java.math.BigInteger;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
import java.security.NoSuchAlgorithmException;
15+
import java.security.SecureRandom;
16+
import java.sql.Connection;
17+
import java.sql.DriverManager;
18+
import java.sql.SQLException;
19+
import java.util.Enumeration;
20+
import java.util.zip.ZipEntry;
21+
import java.util.zip.ZipFile;
22+
23+
import javax.crypto.Cipher;
24+
25+
import oracle.security.pki.OracleWallet;
26+
import oracle.security.pki.textui.OraclePKIGenFunc;
27+
28+
public class CloudConnectionManager {
29+
30+
/**
31+
* Gets a new database connnection
32+
* @param clientCredentials Path to the client credentials file
33+
* @param user Username for database connection
34+
* @param password Password for database connection
35+
* @param serviceName Service name for database connections
36+
* @return A new connection to Exadata Express cloud service
37+
* @throws IOException Any IOException while reading the client credentials file
38+
* @throws SQLException Any SQLException while opening a connection to the database
39+
* @throws NoJCEUnlimitedStrengthSetupException If JCE unlimited strength is not setup correctly
40+
* @throws NoSuchAlgorithmException If transformation is not a valid transformation, i.e. in the form of "algorithm" or "algorithm/mode/padding"
41+
*/
42+
public static Connection getConnection(String clientCredentials, String user, String password, String serviceName)
43+
throws IOException, SQLException, NoJCEUnlimitedStrengthSetupException, NoSuchAlgorithmException {
44+
45+
File fClientCredentials = new File(clientCredentials);
46+
return getConnection(fClientCredentials, user, password, serviceName);
47+
}
48+
49+
/**
50+
* Gets a new database connnection
51+
* @param fClientCredentials Client credentials file
52+
* @param user Username for database connection
53+
* @param password Password for database connection
54+
* @param serviceName Service name for database connections
55+
* @return A new connection to Exadata Express cloud service
56+
* @throws IOException Any IOException while reading the client credentials file
57+
* @throws SQLException Any SQLException while opening a connection to the database
58+
* @throws NoJCEUnlimitedStrengthSetupException If JCE unlimited strength is not setup correctly
59+
* @throws NoSuchAlgorithmException If transformation is not a valid transformation, i.e. in the form of "algorithm" or "algorithm/mode/padding"
60+
*/
61+
public static Connection getConnection(File fClientCredentials, String user, String password, String serviceName)
62+
throws IOException, SQLException, NoJCEUnlimitedStrengthSetupException, NoSuchAlgorithmException {
63+
64+
// Check whether JCE is installed
65+
checkJCEUnlimitedStrengthSetup();
66+
67+
String pathToTrustStore = createTrustStore(fClientCredentials);
68+
69+
System.setProperty("oracle.net.tns_admin", pathToTrustStore);
70+
71+
System.setProperty("oracle.net.ssl_server_dn_match", "true");
72+
System.setProperty("oracle.net.ssl_version", "1.2");
73+
74+
// open the CA's wallet
75+
OracleWallet caWallet = new OracleWallet();
76+
caWallet.open(pathToTrustStore, null);
77+
78+
String passwd = generateRandomSecurePassword();
79+
char[] keyAndTrustStorePasswd = OraclePKIGenFunc.getCreatePassword(passwd, false);
80+
81+
// certs
82+
OracleWallet jksK = caWallet.migratePKCS12toJKS(keyAndTrustStorePasswd, OracleWallet.MIGRATE_KEY_ENTIRES_ONLY);
83+
84+
// migrate (trusted) cert entries from p12 to different jks store
85+
OracleWallet jksT = caWallet.migratePKCS12toJKS(keyAndTrustStorePasswd,
86+
OracleWallet.MIGRATE_TRUSTED_ENTRIES_ONLY);
87+
String trustPath = pathToTrustStore + "/sqlclTrustStore.jks";
88+
String keyPath = pathToTrustStore + "/sqlclKeyStore.jks";
89+
jksT.saveAs(trustPath);
90+
jksK.saveAs(keyPath);
91+
92+
System.setProperty("javax.net.ssl.trustStore", trustPath);
93+
System.setProperty("javax.net.ssl.trustStorePassword", passwd.toString());
94+
System.setProperty("javax.net.ssl.keyStore", keyPath);
95+
System.setProperty("javax.net.ssl.keyStorePassword", passwd.toString());
96+
97+
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@" + serviceName, user, password);
98+
return conn;
99+
}
100+
101+
/**
102+
* Creates Trust Store.
103+
* @param fClientCredentials Client credentials files
104+
* @return Path to trust store
105+
* @throws IOException
106+
*/
107+
private static String createTrustStore(File fClientCredentials) throws IOException {
108+
Path tmp = Files.createTempDirectory("oracle_cloud_config");
109+
// clean up on exit
110+
tmp.toFile().deleteOnExit();
111+
112+
// Create new temporary zip file
113+
Path pzip = tmp.resolve("temp.zip");
114+
Files.copy(new FileInputStream(fClientCredentials), pzip);
115+
116+
// Extract all files from the zip file
117+
ZipFile zf = new ZipFile(pzip.toFile());
118+
Enumeration<? extends ZipEntry> entities = zf.entries();
119+
while (entities.hasMoreElements()) {
120+
ZipEntry entry = entities.nextElement();
121+
String name = entry.getName();
122+
Path p = tmp.resolve(name);
123+
Files.copy(zf.getInputStream(entry), p);
124+
}
125+
zf.close();
126+
127+
return tmp.toFile().getAbsolutePath();
128+
}
129+
130+
/**
131+
* Checks whether JCE Unlimited Strength is setup.
132+
* @throws NoSuchAlgorithmException If transformation is not a valid transformation, i.e. in the form of "algorithm" or "algorithm/mode/padding"
133+
* @throws NoJCEUnlimitedStrengthSetupException If JCE unlimited strength is not setup correctly
134+
*/
135+
private static void checkJCEUnlimitedStrengthSetup()
136+
throws NoSuchAlgorithmException, NoJCEUnlimitedStrengthSetupException
137+
{
138+
// Check whether Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files 8 are in place
139+
// This will generate a huge int if installed correctly
140+
int maxKeySize = Cipher.getMaxAllowedKeyLength("AES");
141+
142+
// Throw exception if JCE is not in place
143+
if (maxKeySize <= 128) {
144+
throw new NoJCEUnlimitedStrengthSetupException();
145+
}
146+
}
147+
148+
/**
149+
* Generates new random password
150+
* @return A new random password
151+
*/
152+
private static String generateRandomSecurePassword() {
153+
return new BigInteger(130, new SecureRandom()).toString(32);
154+
}
155+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* author: gvenzl
3+
* created: 19 Mar 2017
4+
*/
5+
6+
package com.oracle;
7+
8+
public class NoJCEUnlimitedStrengthSetupException extends Exception {
9+
10+
private static final long serialVersionUID = -1278664890453760133L;
11+
12+
public NoJCEUnlimitedStrengthSetupException() {
13+
super();
14+
}
15+
16+
public NoJCEUnlimitedStrengthSetupException(String message) {
17+
super(message);
18+
}
19+
20+
public NoJCEUnlimitedStrengthSetupException(String message, Throwable cause) {
21+
super(message, cause);
22+
}
23+
24+
public NoJCEUnlimitedStrengthSetupException(Throwable cause) {
25+
super(cause);
26+
}
27+
}

ExadataExpress/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Tools for Oracle Exadata Express Cloud Service
2+
This folder contains tooling for [Oracle Exadata Express Cloud Service][1].
3+
4+
* CloudConnectionManager
5+
6+
[1]: https://cloud.oracle.com/en_US/database/exadata-express/features

0 commit comments

Comments
 (0)