Setting the file. One moment. Db2 Kerberos Connection · RDS Db2 · aws/agent-toolkit-for-aws · Skills Docs70
Creating Amazon Aurora Db Cluster With Instances
93
Routing Traffic With Route53 And CloudFront
Resilience Program Design
Creating API Gateway Stage
(opens in a new tab)
scripts/Db2KerberosConnection.java
Java·218 lines·9 KB
* via the IBM JDBC driver's sslCertLocation property — no KeyStore or
16 * keytool required.
17 *
18 * Reference:
19 * https://aws.amazon.com/blogs/database/
20 * create-an-ssl-connection-to-amazon-rds-for-db2-in-java-without-keystore-or-keytool/
21 *
22 * Usage (TCPIP):
23 * java Db2KerberosConnection <HOST> <DATABASE> <PORT> TCPIP
24 *
25 * Usage (SSL):
26 * java Db2KerberosConnection <HOST> <DATABASE> <PORT> SSL <CERT_PEM_PATH>
27 *
28 * CERT_PEM_PATH — region-specific PEM bundle from AWS, e.g.
29 * us-east-1-bundle.pem (do NOT use global-bundle.pem;
30 * the IBM JDBC driver only supports single-region bundles)
31 */
32public class Db2KerberosConnection {
33
34 // Db2 JDBC security mechanism: 11 = Kerberos
35 private static final String KERBEROS_SECURITY_MECHANISM = "11";
36
37 public static void main(String[] args) {
38 ConnectionConfig config = parseArgs(args);
39 if (config == null) {
40 printUsage();
41 System.exit(1);
42 }
43
44 Connection connection = loadDriverAndConnect(config);
45 if (connection != null) {
46 verifyConnection(connection);
47 closeQuietly(connection);
48 } else {
49 System.exit(2);
50 }
51 }
52
53 // -------------------------------------------------------------------------
54 // Argument parsing
55 // -------------------------------------------------------------------------
56
57 private static ConnectionConfig parseArgs(String[] args) {
58 if (args.length < 4) return null;
59
60 String host = args[0];
61 String database = args[1];
62 String port = args[2];
63 String mode = args[3].toUpperCase();
64
65 if (mode.equals("TCPIP")) {
66 return new ConnectionConfig(host, database, port, false, null);
67 }
68
69 if (mode.equals("SSL")) {
70 if (args.length < 5) {
71 System.err.println("ERROR: SSL mode requires <CERT_PEM_PATH>");
72 return null;
73 }
74 String certPath = args[4];
75 java.io.File certFile = new java.io.File(certPath);
76 if (!certFile.exists()) {
77 System.err.println("ERROR: Certificate file not found: " + certPath);
78 System.err.println(" Download it with:");
79 System.err.println(" curl -sL https://truststore.pki.rds.amazonaws.com/"
80 + "<region>/<region>-bundle.pem -o <region>-bundle.pem");
81 return null;
82 }
83 return new ConnectionConfig(host, database, port, true, certPath);
84 }
85
86 System.err.println("ERROR: Unknown mode '" + args[3] + "'. Use TCPIP or SSL.");
87 return null;
88 }
89
90 private static void printUsage() {
91 System.err.println();
92 System.err.println("Usage (TCPIP):");
93 System.err.println(" java Db2KerberosConnection <HOST> <DATABASE> <PORT> TCPIP");
94 System.err.println();
95 System.err.println("Usage (SSL):");
96 System.err.println(" java Db2KerberosConnection <HOST> <DATABASE> <PORT> SSL <CERT_PEM_PATH>");
97 System.err.println();
98 System.err.println(" CERT_PEM_PATH — region-specific PEM bundle, e.g. <region>-bundle.pem");
99 System.err.println(" Download: curl -sL https://truststore.pki.rds.amazonaws.com/");
100 System.err.println(" <region>/<region>-bundle.pem -o <region>-bundle.pem");
101 System.err.println();
102 }
103
104 // -------------------------------------------------------------------------
105 // Driver loading and connection
106 // -------------------------------------------------------------------------
107
108 private static Connection loadDriverAndConnect(ConnectionConfig config) {
109 try {
110 Class.forName("com.ibm.db2.jcc.DB2Driver");
111 } catch (ClassNotFoundException e) {
112 System.err.println("ERROR: DB2 JDBC driver not found. "
113 + "Ensure db2jcc4.jar (v4.33+) is on the classpath.");
114 e.printStackTrace(System.err);
115 return null;
116 }
117 System.out.println("DB2 driver loaded successfully.");
118
119 System.out.println("Connecting to : " + config.host + ":" + config.port + "/" + config.database);
120 System.out.println("Mode : " + (config.useSsl ? "SSL (PEM)" : "TCPIP"));
121 if (config.useSsl) {
122 System.out.println("Certificate : " + config.certPath);
123 }
124
125 try {
126 // Use a javax.sql.DataSource with dedicated setter methods rather than
127 // concatenating host/port/database into a JDBC URL string. Passing the
128 // connection parameters as typed properties avoids JDBC connection-string
129 // injection (nothing is parsed back out of a URL).
130 DataSource ds = buildDataSource(config);
131 Connection conn = ds.getConnection();
132 System.out.println("Connected to Db2 successfully using Kerberos"
133 + (config.useSsl ? " over SSL!" : "!"));
134 return conn;
135 } catch (SQLException e) {
136 System.err.println("ERROR: Failed to connect to Db2.");
137 e.printStackTrace(System.err);
138 return null;
139 }
140 }
141
142 // -------------------------------------------------------------------------
143 // DataSource builder — sets connection parameters via dedicated setters
144 // (no JDBC URL string concatenation, so no connection-string injection)
145 // -------------------------------------------------------------------------
146
147 private static DataSource buildDataSource(ConnectionConfig config) {
148 DB2SimpleDataSource ds = new DB2SimpleDataSource();
149 ds.setDriverType(4);
150 ds.setServerName(config.host);
151 ds.setPortNumber(Integer.parseInt(config.port));
152 ds.setDatabaseName(config.database);
153
154 // Kerberos — no user/password needed (security mechanism 11 = Kerberos)
155 ds.setSecurityMechanism(Integer.parseInt(KERBEROS_SECURITY_MECHANISM));
156
157 if (config.useSsl) {
158 // PEM-based SSL: no KeyStore, no keytool. Requires db2jcc4.jar v4.33+
159 ds.setSslConnection(true);
160 // Enforce TLS 1.2 so the driver cannot negotiate down to TLS 1.0/1.1
161 ds.setSslVersion("TLSv1.2");
162 ds.setSslCertLocation(config.certPath);
163 }
164 return ds;
165 }
166
167 // -------------------------------------------------------------------------
168 // Post-connect verification
169 // -------------------------------------------------------------------------
170
171 private static void verifyConnection(Connection conn) {
172 String sql = "SELECT CURRENT SERVER, CURRENT TIMESTAMP FROM SYSIBM.SYSDUMMY1";
173 try (PreparedStatement pstmt = conn.prepareStatement(sql);
174 ResultSet rs = pstmt.executeQuery()) {
175 if (rs.next()) {
176 System.out.println("Server : " + rs.getString(1));
177 System.out.println("Timestamp : " + rs.getTimestamp(2));
178 }
179 } catch (SQLException e) {
180 System.err.println("WARNING: Connected but verification query failed.");
181 e.printStackTrace(System.err);
182 }
183 }
184
185 // -------------------------------------------------------------------------
186 // Helpers
187 // -------------------------------------------------------------------------
188
189 private static void closeQuietly(Connection conn) {
190 try {
191 conn.close();
192 System.out.println("Connection closed.");
193 } catch (SQLException e) {
194 e.printStackTrace(System.err);
195 }
196 }
197
198 // -------------------------------------------------------------------------
199 // Inner config class
200 // -------------------------------------------------------------------------
201
202 private static class ConnectionConfig {
203 final String host;
204 final String database;
205 final String port;
206 final boolean useSsl;
207 final String certPath; // path to region-specific .pem file (SSL only)
208
209 ConnectionConfig(String host, String database, String port,
210 boolean useSsl, String certPath) {
211 this.host = host;
212 this.database = database;
213 this.port = port;
214 this.useSsl = useSsl;
215 this.certPath = certPath;
216 }
217 }
218}