Saturday, September 8, 2018

PGP Encryption using Apache Camel

PGP (Pretty Good Privacy) Encryption is a widely used encryption mechanism which can be used to provide public key encryption.
There are many PGP encryption implementations using Java language which are quite complex. But Apache Camel Crypto component facilitates a very simple and convenient way of implementing PGP encryption.
Following code encrypts a given message using Apache Camel. org.apache.camel:camel-crypto is the only one library i have used here.
Most of the example in the internet shows the way which a file is used as the input for the encryption. But here i will show you how to directly encrypt a string (text message).
I'm using the public key in the public.gpg file to encrypt the message. The code is really straight foreword. Complete working project is available on GitHub and the URL is available at the bottom. All required key files are available there. You can clone the project and execute the program.

Following is the GitHub URL of complete project of this tutorial.
https://github.com/lakjcomspace/PGPEncryption

Monday, January 1, 2018

Docker MySQL Automatic Backups using Cron and Supervisor

mysqldump command allows you to create an SQL dump file of the database. By executing this command using a scheduler application like Windows Task Scheduler or Linux Cron you can create periodic backups automatically.
But when it comes to Docker MySQL image you cannot do this since base image does not contain any scheduler application. Even though we can install a scheduler utility like Cron on MySQL image, it does not do what we want since the MySQL container executes only the DB application at the container startup. It does not trigger the executable related to the scheduler application.
Here is my solution to the above problem. Using 'Supervisor' application to execute both MySQL and Cron. Supervisor is a process monitoring and controlling application for UNIX-like operating systems.

Following is the step by step guide to the solution. All these steps are instructions in the Dockerfile of the final output image. You don't want to do these things manually except copying the contents of the files. My Dockerfile will do the job. I'm listing these thing here just for your clarification.

1. Use MySQL docker image as the base image.
2. Install Cron on base image.
3. Install Supervisor on base image.
4. Prepare supervisord.conf Supervisor configuration file so that it starts MySQL DB and Contrab.
5. Add an entry to Crontab file so that it execute the dbdump.sh (Shell script which contains the mysqldump commands) everyday 11.45pm.

Following are the files which do all these things. Place all files inside a single folder and build image of the final container.

Every commands are straight forward where you can search and find more information. But touchrootcron in supervisord.conf file is something special. This is a trick to solve permission problem in Docker layered file system (Read for Docker layered file system and copy-on-write (CoW) strategy). root crontab file should be in the writable layer container layer.


Don't hesitate to put a comment if you have any problem.

Friday, April 28, 2017

Excluding Property Files from Jar File : Maven Shade Plugin

Recently, i wanted to create a single Jar file with all the dependencies while the property files are excluded which are coming from the project. But the property files which are coming from dependencies should be preserved. My idea was to externalize property file so that someone can edit them without going inside Jar.

I tried several options.

Resources plugin perfectly work for the need. But it excludes the property files even when i run the project from the IDE (Eclipse/NetBeans)

I tried jar-with-dependencies pre-defined assembly descriptor with few customization. I used exclusion in unpackOptions. But it excludes property files which are coming from all the dependent modules.

Finally i found the working solution. It was Maven Shade Plugin

Following is example pom.xml. There you can see how i have used Maven Shade Plugin with property file excluding configurations. Shade plugin can be use to filter any file file from any dependent module conveniently.
Since i wanted an executable Jar i have specially used ManifestResourceTransformer in the Shade configurations.