Manage Keystore Certs

In development environments it’s common enough to come across self assigned certs which can cause all sorts of problems when calling out to APIs.

Here’s a common error you will see for a self assigned cert in in Java:

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

There’s a lot of information out there on in code work arounds but this means writing code that opens you up to man in the middle attacks.

So, what to do? You can import the self assigned cert. All the APIs I’ve dealt use HTTP and this means you can use a browser to download the cert.

All guides will have you using command line or terminal to import the certs, fortunately there is a really handy GUI based tool called KeyStore Explorer that lets you create your own keystore or better yet, open existing ones.

This means for me in Java I can browse to my cacerts keystore (default password is changeit) and can import self assigned certs that will get around my SSLHandshakeException.

Sources:

Generate a Secure Random Password in Java

Recently I had to generate a random password that met the password security requirements of AWS – 32 characters in length with 1 special character and 1 digit.

Turned out to be a simple enough thing to do in java.

String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789~`!@#$%^&*()-_=+[{]}\\|;:\'\",<.>/?";
String password = RandomStringUtils.random(32, characters);

Just make sure you include the Apache Commons Lang

Source: Generate a Secure Random Password in Java with Minimum Special Character Requirements

Spring Boot component scanning for test libraries

Recently I had an issue where I was trying to use a custom test library I had built in Spring Boot within an existing Spring Boot app. I ran into a weird issue where by using the test library I broke the main app.

The issue was that the main app was trying to initialise the test library before it was ready, it was attempting to autowire beans within it… turned out the issue was because the test library used the same base package naming convention as the main app.

For example, if the main app was com.alanfeekery.app and the test library was com.alanfeekery.test and my application-context.xml looked like this:


<context:component-scan base-package="com.alanfeekery"></context:component-scan>

Then Spring would just go ahead and try configure everything under the package com.alanfeekery, this is bad as the app shouldn’t be configuring the test library. The solution looks like this:


<context:component-scan base-package="com.alanfeekery">
	<context:include-filter type="regex" expression=".*app"/>
	<context:exclude-filter type="regex" expression=".*test"/>
</context:component-scan>

With these include & exclude filters my app no longer scanned into my test library automatically and everything worked as expected.

FYI

Another thing to be aware about, if are including a custom test library in your Spring Boot app, make sure you flag the dependency scope to test this means that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phase.

Here’s an example:


<dependency>
  <groupId>com.alanfeekery</groupId>
  <artifactId>test</artifactId>
  <version>0.0.35</version>
  <scope>test</scope>
</dependency>

Source:

Finding the right Mockito import

I really enjoy working with Mockito, it’s a fantastic mocking framework for Java. However it can be a pain sometimes to know which packages to import for the tests you are writing.

A quick tip is to import everything while writing your tests & mocks like this:

import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import static org.mockito.Matchers.*;

Then once you have completed writing your tests use the CTRL+SHIFT+O (it’s CMD+SHIFT+O on Mac) shortcut in Eclipse to breakdown your imported packages to what you actually used.

You’ll end up with something like this:

import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.mockito.Matchers.anyString;

Much easier yeah? 🙂

Source: Finding import static statements for Mockito constructs

Change passwords on other accounts using Terminal

In work I have multiple accounts with security requirements making me change the passwords every 90 days. Normally with Windows I used to use CTRL+ALT+DEL and select the change password option. However on Mac OS X it’s not as straight forward… or is it? Using Terminal you can switch into any account by typing:

su yourAccountName

You will be prompted to enter the password for yourAccountName.

Then you can change the password for this account by typing:

passwd

You will have to enter the old password, then your new password and then repeat your new password.

Done…

Source:

How to switch between users on one terminal?
Change password on root user and user account

OS X Default Apps Not Changing

I ran into another weird issue during my first week with my MacBook Pro, for some reason I couldn’t change any of the default apps. Since this is my work laptop I wanted Outlook to be my default email client not Apple’s Mail.

However, it turned out I needed to reset my default app preferences as the preferences file had become corrupt! Use the following command in terminal to do it:

/System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/Support/lsregister -kill -r -all local,system,user

Worked great for me 🙂

Source: Unable to change default email application