Skip to content

Commit

Permalink
Fixes #547: Add MSSQL Driver Features Support (#548)
Browse files Browse the repository at this point in the history
* Fixes #547: Add MSSQL Driver Features Support

Add an Example on how to connect with MSSQL using ZDBC natively.
Adding the entry into main ZDBC Readme, on features for MSSQL and Redshift.

* Fixes #547: Add MSSQL Driver Features Support

Add an Example on how to connect with MSSQL using ZDBC natively.
Adding the entry into main ZDBC Readme, on features for MSSQL and Redshift.

Signed-off-by: NataS <[email protected]>

---------

Signed-off-by: NataS <[email protected]>
  • Loading branch information
natashell666 authored Mar 5, 2024
1 parent cc02c58 commit 05951eb
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,4 @@ samples/ziti-jdbc-spring-jpa/network/.env

samples/ziti-jdbc-postgresql/network/databaseClient.json
samples/ziti-jdbc-postgresql/network/tunnel
samples/ziti-jdbc-mssql/network/DBClient.json
1 change: 1 addition & 0 deletions samples/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ include 'sample-okhttp'
include 'ziti-spring-boot'
include 'ziti-spring-boot-client'
include 'ziti-jdbc-postgresql'
include 'ziti-jdbc-mssql'
include 'ziti-jdbc-spring-jpa:initial:server'
include 'ziti-jdbc-spring-jpa:initial:client'
include ':ziti-jdbc-spring-jpa:complete:server'
Expand Down
47 changes: 47 additions & 0 deletions samples/ziti-jdbc-mssql/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Microsoft SQL Server Example
This project contains a simple java application that uses the [OpenZiti](https://openziti.github.io/ziti/overview.html) Java SDK to connect to a Microsoft SQL Server (MSSQL) database and read the contents of a table.

# What you will need
* (Maven) [https://maven.apache.org/] to build the sample
* A Java JDK to build and run the sample. If you don't have one you can get one from the [Oracle Open JDK download site](https://jdk.java.net/)

# Building the sample
Compilation is done using the Maven build tooling.

1. Change into the `ziti-sdk-jvm/samples/ziti-jdbc-mssql` sample directory
1. execute `../../gradlew -PbuildForAndroid=false build`

# Running the example
Running the example is done using the gradle run facility.
It's assumed you already have the OpenZiti network running and the ziti services configured.

Your services must have as intercept configuration the host `mssql_host` with the port `1433`; if different you may change the connection string in the `MSSQLExample.java` file.

It's also assumed you have a Microsoft SQL Server running and a database named `DBNAME` with a table called `TABLENAME` with two columns `column_1` and `column_2`.


You can run the example from the sample directory using:

```
../../gradlew -PbuildForAndroid=false run
```

## Example Output
```
> Task :ziti-jdbc-mssql:runWithJavaExec
[main] INFO org.openziti.impl.ZitiImpl - ZitiSDK version 0.25.1 @344b49b()
Feb 22, 2024 5:32:02 PM org.openziti.jdbc.ZitiDriver setupZiti
INFO: Ziti JDBC wrapper is configuring Ziti identities. Production applications should manage Ziti identities directly
[DefaultDispatcher-worker-2] INFO org.openziti.api.Controller - controller[https://ziti-edge-controller/] version(v0.28.1/f9a62c0baf1c)
[DefaultDispatcher-worker-1] INFO org.openziti.api.Controller - controller[https://ziti-edge-controller/] version(v0.28.1/f9a62c0baf1c)
Column_1: AAA0000 - Column_2: 10.10.10.0
Column_1: AAA0001 - Column_2: 10.10.10.1
Column_1: AAA0002 - Column_2: 10.10.10.2
Column_1: AAA0003 - Column_2: 10.10.10.3
Column_1: AAA0004 - Column_2: 10.10.10.4
Column_1: AAA0005 - Column_2: 10.10.10.5
Column_1: AAA0006 - Column_2: 10.10.10.6
Column_1: AAA0007 - Column_2: 10.10.10.7
Column_1: AAA0008 - Column_2: 10.10.10.8
Column_1: AAA0009 - Column_2: 10.10.10.9
```
38 changes: 38 additions & 0 deletions samples/ziti-jdbc-mssql/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2018-2024 NetFoundry, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

plugins {
id 'java'
}

ext {
javaMainClass = "org.openziti.jdbc.MSSQLExample"
}

dependencies {
implementation "org.openziti:ziti-jdbc:${versions.ziti}"
implementation deps.ziti
implementation files('lib/mssql-jdbc-12.6.0.jre11.jar')
implementation deps.slf4jSimple
}

task runWithJavaExec(type: JavaExec) {
group = "Execution"
description = "Run the ZDBC example"
classpath = sourceSets.main.runtimeClasspath
mainClass = javaMainClass
args "network/DBClient.json"
}
2 changes: 2 additions & 0 deletions samples/ziti-jdbc-mssql/network/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Microsoft SQL Server Test Network
This directory contains the identity JSON.
63 changes: 63 additions & 0 deletions samples/ziti-jdbc-mssql/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.openziti.jdbc.samples</groupId>
<artifactId>mssql</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>

<dependencies>

<!-- MSSQL jdbc driver -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>12.6.0.jre11</version>
</dependency>

<!-- ZDBC wrapper jar -->
<dependency>
<groupId>org.openziti</groupId>
<artifactId>zdbc</artifactId>
</dependency>

<!-- Ziti SDK -->
<dependency>
<groupId>org.openziti</groupId>
<artifactId>ziti</artifactId>
</dependency>

<!-- Optional logging dependencies -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.22</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.22</version>
</dependency>

</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.0.0</version>
<configuration>
<mainClass>org.openziti.jdbc.MSSQLExample</mainClass>
<arguments>
<argument>network/DBClient.json</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright (c) NetFoundry, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package org.openziti.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import org.openziti.Ziti;
import org.openziti.ZitiContext;

public class MSSQLExample {
public static void main(String[] args) throws Exception {
if (args.length != 1) {
System.out.println("Usage: App <ziti identity.json>");
System.exit(1);
}

// Initialize the OpenZiti Java SDK with our identity
final ZitiContext zitiContext = Ziti.newContext(args[0], "".toCharArray());

String url = "zdbc:sqlserver://mssql_host:1433;databaseName=DBNAME";

Properties props = new Properties();

// General MSSQL properties
props.setProperty("user", "zdbc");
props.setProperty("password", "netfoundry!1");
props.setProperty("connectTimeout", "240");
props.setProperty("encrypt", "False");

// To use MSSQL with zdbc driver we need to set the mssql driver's socketFactoryClass to ZitiSocketFactory
props.setProperty("socketFactoryClass", "org.openziti.net.ZitiSocketFactory");

// Ziti specific properties
props.setProperty("zitiIdentityFile", "network\\DBClient.json");
props.setProperty("zitiDriverUrlPattern", "^zdbc:sqlserver:.*");
props.setProperty("zitiDriverClassname", "com.microsoft.sqlserver.jdbc.SQLServerDriver");


try (Connection conn = DriverManager.getConnection(url, props)) {
try (Statement stmt = conn.createStatement()) {
try (ResultSet rs = stmt.executeQuery("select top 10 * from dbo.TABLE")) {
while (rs.next()) {
System.out.println("Column_1: " + rs.getString(1) + " - Column_2: " + rs.getString(2));
}
}
}
} finally {
Ziti.getContexts().forEach(c -> c.destroy());
}

System.exit(0);
}
}
24 changes: 14 additions & 10 deletions ziti-jdbc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ The goals of this project are:
## Driver Features
Each JDBC driver needs specific ziti features in order to work. This table attempts to capture which each driver requires

| Driver | Shim Included | Ziti Features | Notes |
| ------ | :------------:| ------------- | ----- |
| org.postgresql.Driver | Y | Socket Factory | Requires jdbc property socketFactory |
| oracle.jdbc.OracleDriver | Y | init with seamless mode | <ul><li>The current Oracle shim does not support NIO or OOB. As such, the shim will set the following property values <ul><li>oracle.jdbc.javaNetNio=false</li><li>oracle.net.disableOob=false</li></ul></li><li>Tested with public and private autonomous databases</li><li>Requires the database host to be resolvable (1)</li></ul> |
| com.mysql.jdbc.Driver | Y | init with seamless mode | |
| org.h2.Driver | N | init with seamless mode | Requires the database host to be resolvable (1) |
| Driver | Shim Included | Ziti Features | Notes |
| ------ |:-------------:| ------------- |----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| org.postgresql.Driver | Y | Socket Factory | Requires jdbc property socketFactory |
| oracle.jdbc.OracleDriver | Y | init with seamless mode | <ul><li>The current Oracle shim does not support NIO or OOB. As such, the shim will set the following property values <ul><li>oracle.jdbc.javaNetNio=false</li><li>oracle.net.disableOob=false</li></ul></li><li>Tested with public and private autonomous databases</li><li>Requires the database host to be resolvable (1)</li></ul> |
| com.mysql.jdbc.Driver | Y | init with seamless mode | |
| org.h2.Driver | N | init with seamless mode | Requires the database host to be resolvable (1) |
| com.microsoft.sqlserver.jdbc.SQLServerDriver| N | Socket Factory | <ul><li>Requires MSSQL jdbc property <b>socketFactoryClass</b> set as: `org.openziti.net.ZitiSocketFactory`</li><li>Requires Custom URL pattern, set as: `^zdbc:sqlserver:.*`</li><li>Connection string set as: `zdbc:sqlserver://<host>:<port>;databaseName=<database>`</li></ul> |
| com.amazon.redshift.jdbc.Driver | N | init with seamless mode | <ul><li>Requires Custom URL pattern, set as: `^zdbc:redshift:.*`</li><li>Connection string set as: `zdbc:redshift://<host>:<port>/<database>`</li></ul> |

(1) These drivers attempt to resolve the name of the database host via a DNS request before connecting to them. The database hostname must be resolvable until something like <https://openjdk.java.net/jeps/418> is adopted. No connection attempt will be made to this host, it is simply required to satisfy the driver DNS resolution.

Expand All @@ -41,8 +43,10 @@ The zdbc driver needs your ziti network identity to connect. There are three way
1. Configure a ziti network and postgres database following the cheatsheet <https://github.com/openziti/ziti-sdk-jvm/blob/main/samples/jdbc-postgres/cheatsheet.md>
1. Copy the Ziti all-in-one jar into the Squirrel-Sql `lib` folder

> ls $SQUIRREL_HOME\lib | grep ziti <br>
ziti-jdbc-0.23.14-full.jar
> ls $SQUIRREL_HOME\lib | grep ziti <br/>
ziti-0.25.1-full.jar <br/>
ziti-jdbc-0.25.1.jar


1. Start Squirrel-Sql
1. Configure the Squirrel-Sql PostgreSQL driver
Expand All @@ -65,5 +69,5 @@ The zdbc driver needs your ziti network identity to connect. There are three way

# Example of integrating into a Java application
This repository includes a coupld of examples using Java to connect to a dark database.
* [samples/postgresql/README.md](samples/postgresql/README.md): Postgresql example using the JDBC DriverManager to connect to a dark database.
* [samples/spring-jpa/README.md](samples/spring-jpa/README.md): A learning excercise adding spring-data-jpa and spring-data-rest to host a dark http server that connects to a dark postgres database
* [samples/postgresql/README.md](https://github.com/openziti/ziti-sdk-jvm/blob/main/samples/jdbc-postgres/cheatsheet.md): Postgresql example using the JDBC DriverManager to connect to a dark database.
* [samples/spring-jpa/README.md](https://github.com/openziti/ziti-sdk-jvm/tree/main/samples/ziti-jdbc-spring-jpa): A learning exercise adding spring-data-jpa and spring-data-rest to host a dark http server that connects to a dark postgres database

0 comments on commit 05951eb

Please sign in to comment.