GithubHelp home page GithubHelp logo

taizyeris / java-jdbc-mariadb Goto Github PK

View Code? Open in Web Editor NEW

This project forked from properssl/java-jdbc-mariadb

0.0 2.0 0.0 104 KB

Example of connecting to MariaDB via JDBC and validating a self-signed certificate

java-jdbc-mariadb's Introduction

JDBC SSL Connection to MariaDB

This example demonstrates how to properly create an SSL connection to a MariaDB server.

The majority of examples on the internet of using SSL with MariaDB (and MySQL) instruct the JDBC driver to not validate the server's certificate. This is vulnerable to a man in the middle attack. This is particularly important with the rise of cloud based database-as-a-service platforms where client applications are connecting to the remote servers through the public internet.

The latest release (as of writing this) of the MariaDB Java client driver, v1.1.3, includes the ability to validate self signed server certificates by setting a new connection property, serverSslCert. This example demonstrates how to use it to secure your JDBC connection to your database.

When the property serverSslCert is set the MariaDB JDBC driver sets up the connection to use a TrustManager and SSLSocketFactory that can validate against a pre-shared certificate. It provides protection against MITM. Additionaly, through certificate pinning a self-signed certificate can be even more secure than one signed by a trusted certificate authority as it will also be immune to CA compromises.

For more detail about configuring the MariaDB Java client driver see its about page.

For other examples of properly establishing SSL connections check out http://properssl.org/.

Installing the JDBC driver

At the moment the MariaDB Java client driver is not available in the central Maven repository so you have to manually install it. Here are the steps:

  1. Download the latest version of the driver from: https://downloads.mariadb.org/client-java/

  2. Change directory to the JAR file you downloaded. It should be named something like mariadb-java-client-1.1.3.jar

  3. Execute the follow from the command line to add it to your local Maven repository. Make sure to edit the JDBC_DRIVER_VERSION to match the version you downloaded.

    $ JDBC_DRIVER_VERSION=1.1.3 $ mvn install:install-file -Dfile=mariadb-java-client-${JDBC_DRIVER_VERSION}.jar -DgroupId=org.mariadb.jdbc -DartifactId=mariadb-java-client -Dversion=${JDBC_DRIVER_VERSION} -Dpackaging=jar

Vagrant

The tests are configured to run against the local machine. For convenience a Vagrantfile is included to automatically provision a MariaDB server with a test user and database. To create it run:

$ vagrant up

After creating the VM the install script will:

  1. Run apt-get update/upgrade to update the system packages
  2. Install MariaDB
  3. Update the MariaDB configuration files to allow inbound connections
  4. Create a test user and database

Server SSL Certificate

After installation run the following from the root directory of the project to extract the randomly generated server SSL certificate:

$ vagrant ssh 'sudo cat /etc/mysql/server.crt' > src/test/resources/server.crt

Building

To build use maven:

mvn clean compile

Running Tests

To run the tests use maven:

mvn test

java-jdbc-mariadb's People

Contributors

sehrope avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.