Using the console on Windows

I tell you a secret: I’m a Windows user. I don’t use OSX, I don’t use Linux, I use Windows. And I tell you something more: I like it 😉

Usually I develop Java or JavaScript applications which perfectly run under Windows, Linux, OSX or what so ever. So developing under Windows is no problem at all. Runtime environments, IDEs, editors – Windows has it all. However, people keep wondering how I can do the most simple task:

How do you connect to another server? Do you use Putty? – Argh, no, I just type ssh some.server.com and I’m done.

Or:

Do you use your IDE to work with GIT? Or do you have Source Tree? – Argh, no, I just type git add . and git commit -m "..." and I’m done.

But I also know, that not everybody is doing it like this. People use the weirdest tools and techniques when working under Windows. A lot of people use that f***ing small Windows CMD, Putty with its broken key-format or CygWin to be a little bit more Linux-like. But I don’t like all of these. The Windows CMD is unusable, Putty is unnecessary and CygWin is a monster you don’t need. Here is what I do.

Don’t use the Windows default CMD

The first thing I do, is to don’t use Windows’ default CMD. Why? It can’t even mark and copy things! However, there’s an easy and open-source alternative: ConEmu. ConEmu has all the simple things you expect: colors, tabs, resizable, copy-and-paste and much more. You can get it from GitHub and it even works without installation.

2016-05-21 21_51_21-Settings

Use GIT as a toolbox for Windows

The other thing I do, is to use GIT as a toolbox for Windows. When people are talking about using the console, they are actually talking about using tools. They talks about SCP, SSH or CURL like they come with their console – but they don’t! All of those things are just individual programs installed on their machine. They are not related to the command line! So why don’t install them on Windows?

If you use GIT, you already have everything you need in the bin folder (e.g. on C:\Program Files (x86)\Git\bin\):

ssh, scp, curl, cat, grep, less, sh, bash, ls, mv, cp, diff, gzip, and much more…

2016-05-21 22_09_29-Using the console on Windows – Thomas Uhrig

The only thing you need to do is to put GIT’s bin folder to your Windows variable path. You will have everything at a fingertip in your console.

2016-05-21 22_26_43-Environment Variables

Put my SSH-certificates to my user-folder

The last thing I usually do is to put my SSH-certificates to my user folder at C:\Users\tug\.ssh, so that SSH can find them.

Best regards,
Thomas

More

Gradle’s bootRun and Window’s command length limit

Sometimes Gradle’s bootRun and Window’s command length limit are two opponents. If you use bootRun to start your Spring Boot app from Gradle all class path dependencies will be added to the start command. Gradle will run something like this in the end:

That’s fine and will work for a really long time. But as longer you work on your project you will add more and more dependencies. And it might happen that you cross a secret line of no return: 32767. That’s the number of characters which Windows’ CreateProcess function will accept. Any additional character will cause an exception:

Sh*t! So how to start your app? With Gradle, you can use a simple work around: Instead of appending all your dependencies to the start command, you create a JAR file with a manifest file. This manifest file contains all dependencies and will be the only dependency in your start command:

In Gradle code, this looks like this:

The pathingJar task creates a JAR file with a manifest file containing all our dependencies. This file will become pretty big, but that’s totally fine. Now we only extend the bootRun task to use this pathing JAR. This will solve the problem.

Best regards,
Thomas

More

VirtualBox crashes with STATUS_OBJECT_NAME_NOT_FOUND

As I’m a passionate Windows user (sorry…), I often use VirtualBox (with Vagrant) to pull up a Linux box to use Docker or some other “Linux-only” stuff. Usually, this works really fine, but today my VirtualBox crashed with a STATUS_OBJECT_NAME_NOT_FOUND error:

MXhqz

One of those mystic error where everything worked like a charm yesterday at 6 p.m., but when you start your machine today at 9 a.m. it’s broken. Damn.

The solution was (and still is) a small Windows patch you need to install on your machine:

https://support.microsoft.com/en-us/kb/2628582

Install, restart, back at work. I hope this will help you, too.

Best regards,
Thomas

Mount Windows folder to Boot2Docker VM

I just stumbled over a post on Stackoverflow (http://stackoverflow.com/questions/30864466/whats-the-best-way-to-share-files-from-windows-to-boot2docker-vm) with the question how to mount a Windows folder to a Boot2Docker VM. Although the steps are a little bit confusing, in the end it is not difficult to do.

Boot2Docker

Boot2Docker is a simple VM to run Docker. The VM will run on VirtualBox and Boot2Docker is just a tool to provision this VM (very similar to Vagrant, but smaller and customized for using Docker). You simply download and install Boot2Docker and run boot2docker up to start the VM. After the VM is up, you can run boot2docker ssh to login. Now, we can start to get our Windows folder.

Mounting the folder

To use one of your Windows folders in your Boot2Docker VM, you need to mount it. To do so, you mount your Windows folder to the VM:

Now you login to your VM via SSH (with boot2docker ssh) and do the following:

Make a folder inside your VM:

Mount your stuff from Windows:

After that, you can access c:/my/folder/with/code inside your Boot2Docker VM:

Now, that your code is present inside your VM, you can use it with Docker. Either by mounting it as a volume to the container:

Or by using it while building your Docker image:

Best regards,
Thomas

A Windows SSO (for Java on client and server)

A couple of months ago I worked on a single sign-on (SSO) for a Windows client and server made in Java. The scenario was the following:

  • A client made with Java running on Windows
  • A server made with Java running on Windows
  • Both where logged-in to the same domain (an Active Directory LDAP)

The question was, how the server could get the identity (the name of the Windows account) of the client and – of course – how it could trust this information. But if the client would just send a name (e.g. from Java’s System.getProperty("user.name"); method), the client could send anything.

The solution for this dilemma (trust what the client sends to you) is to use a (so called) trusted third party. A trusted third party is an instance which both, client and server, know and trust. The client authenticates itself to this party and the server can verify requests against it. In the scenario above, the domain of the company (an Active Directory LDAP) is the trusted third party. Each client identifies itself against this domain when it logs-in to Windows. Its Windows username and password are checked by the domain/LDAP. On the other side, the server has also access to the domain controller and can verify information send by the client.

The nice thing about this is, that the Windows domain is already configured on nearly every machine which stands in a company. Every company, bigger than maybe five people, will have a Windows domain to log-in. Therefor, a SSO based on the Windows domain will work right out of the box in most cases and we don’t need and configuration in our Java code, since it is already configured in Windows.

Java Native Access (JNA)

To use Windows and the domain controller for authentication, we can use native Windows APIs. To call those APIs in Java, we can use the Java Native Access (JNA) library, which you can find on GitHub at https://github.com/twall/jna and on Maven central:

For example, to get all user groups of the current user, you would do:

Waffle

On top of JNA exists a library called Waffle which encapsulates all functionality you need to implement user authentication. You can find it on GitHub at https://github.com/dblock/waffle and also on Maven central:

You can use Waffle to create a token on the client, send it to the server (e.g. over HTTP or whatever) and to validate that token on the server. At the end of this process (create, send and validate) you will know on the server who the client is – for sure!

Here is an example of how to identify a client on the server. Note that this piece of code is executed completely on one machine. However, you could easily split it into two parts, one on the client and one on the server. The only thing you would need to do, is to exchange the byte[] tokens between client and server. I commented the appropriate lines of code.

(By the way, I asked this myself on Stackoverflow some times ago).

The only thing that is a little bit complicated with that solution is, that you need to do a small handshake between client and server. The client will send a token to the server, which will response with another token, which the client needs to answer again to get the final “you are authenticated” token from the server. To do this, you need to hold some state on the server for the duration of the handshake. Since the handshake is done in a second or two, I just used a limited cache from Google’s Guava library to hold maybe 100 client contexts on the server.

The exchanged tokens are validated against the underlying Windows and its domain.

Best regards,
Thomas

Cloud vendors with Windows

The cloud is build on Linux – that is my own humbling opinion. But is it really? To answer this question for myself, I took a look at a bunch of cloud vendors to see what they got under the hood. Here is what I found.

But note that the list is neither complete nor representative. I am also comparing two very different things: IaaS and PaaS. While IaaS vendors like AWS provide virtual machines, PaaS vendors like Heroku provide a tooling to setup complete environments.

However, the list shows that most of the vendors use Linux as their base system and the more you go to the PaaS direction, the more Windows vanishes.

Vendor Windows Linux Type Comment
Microsoft Azure yes yes IaaS
AWS yes yes IaaS AWS has a lot of Linux distributions and Windows version on their IaaS EC2.
AWS Elastic Beanstalk yes yes IaaS
eNlight Cloud yes yes CentOS, Red Hat Enterprise Linux, SUSE Linux, Oracle Linux, Ubuntu, Fedora, Debian, Window Server 2003, Windows Server 2008, Windows 7.
Google App Engine PaaS Google App Engine has a sandbox and hides the OS.
Google Compute Engine yes yes IaaS Linux, FreeBSD, Microsoft Windows
Heroku yes PaaS Ubuntu
Jelastic yes PaaS
HP Cloud yes IaaS Based on OpenStack.
OpenShift yes PaaS Red Hat Enterprise Linux
Engine Yard yes PaaS Ubuntu, Gentoo
Rackspace yes yes
Cloud Foundry yes PaaS

Best regards,
Thomas

When your Acer Aspire 5560G doesn’t shutdown anymore

Last year I bought an Acer Aspire 5560G as my second laptop (yes, sometimes two are better than one). A half year later, I was working pretty much with this laptop and decided to upgrade it with a SSD drive. So I bought a brand new Samsung SSD 840 PRO with 256 GB. I was very happy. Well, was…

After installing the SSD and reinstalling Windows 7 there was something strange – Windows didn’t shutdown anymore. Whenever I tried to shutdown my laptop (or restart it or put it to hibernate) Windows went off, but the laptop was still on. The ventilator was running, power was on and the lights at the front of my laptop were blinking. So what?

So I tried to fix it. I reinstalled Windows (both, Windows 7 and Windows 8.1). I installed all kind of driver, even of devices I don’t have. I updated, I formated, I read the internet and so on. I even flashed my BIOS to be sure. But nothing worked for me.

Last night I was searching the web again and I was very despaired. I found a forum post of some guy who had the same problem. And his solution was pretty simple: he installed Windows using an USB driver rather than a DVD. I thought what the fuck, he’s joking, forget about it. But as I woke up this morning I was still despraid and I thought, well, let’s try it. I mean, why not?

So I put my Windows 7 DVD into my drive and copied it to an USB drive using some dubios freeware software that tried to install one browser-toolbar after another. But ten minutes later I had my Windows copy on my bootable USB stick. Restart, format, install, waiting… During the installation the laptop restarted several times and I assumed that this is a good sign.

After aproximately 30 minutes the big moment came and I pressed on “shutdown” in my brand new Windows 7 installation. Aaaannnddd baaammm it went off!

I studied 11 semesters of computer science right now. I installed all kinds of Windows (98, XP, Vista, 7, 8), some Linux distributions and other stuff. But that there is a difference between installing Windows from a DVD or from an USB stick doesn’t make any f***ing sense to me. Thank you Microsoft.

reinstalling_windows

Best regards,
Thomas Uhrig