Author Archives: Niklas - Page 32

Charset problem in Play framework after upgrading OSX

This is such a strange problem that I just have to write it down for future reference.
Involved systems:
* One Ubuntu 14.04.2 LTS on AWS (Amazon Web Services)
* One MacBook Pro 2010 with 10.7.5 with iTerm 2.1.1
* One MacBook Pro 2015 with 10.10.3 with iTerm 2.1.1
* One Play framework 2.3 application

Problem description:
After starting the Play application with the MacBook that has the 10.10.3 version all files that were written to disk had all non-ascii characters shown as ??. When starting the Play application with the old computer (10.7.5) these characters where displayed correctly.

After quite a lot of trial-and-error I found that the command ‘locale’ on the remote AWS server complained about:
“locale: Cannot set LC_CTYPE to default locale: No such file or directory”
“locale: Cannot set LC_ALL to default locale: No such file or directory”
when using the newer computer but not with the old

Solution:
The ‘locale’ command error lead me to the following solution in iTerm:
Untick the Terminal option “Set locale variables automaticly” in Preference
QQ20140113-3

This option is AFAIK default on in iTerm

After this was done the ‘locale’ error was gone and all files had the correct charset

SSL Certificates: From CSR to a JKS storage

I have started doing this quite a lot these days so I’d better put a post up here to get rid of all the Google searching 🙂 It’s not that complicated but I know I will forget if I don’t do it for a while.

Let’s start with creating the CSR
First we create a key

openssl genrsa -out domain.com.key 2048

This will create a private key called domain.com.key and with a key size of 2048 bits

Now it’s time to create the CSR

openssl req -new -sha256 -key domain.com.key -out domain.com.csr

When creating a CSR you need to input some details about the site/organisation that are going to use the certificate, eg.:

Country Name (2 letter code) [AU]:
State or Province Name (full name) [Some-State]:
Locality Name (eg, city) []:
Organization Name (eg, company) [Internet Widgits Pty Ltd]:
Organizational Unit Name (eg, section) []:
Common Name (e.g. server FQDN or YOUR name) []:my.domain.name
Email Address []:admin@domain.name

Out of these questions there is one that is CRUCIAL and that is the Common Name. For an SSL certificate this HAS to be the domain name with or without a subdomain that the certificate is going to be valid for, so if the URL that is called my.domain.com the Common Name should be “my.domain.com” and if it is called domain.com the Common Name should be “domain.com”

After these questions have been answered the openssl program creates a CSR file called domain.com.csr that we can send to our certificate supplier (DigiCert/Go-Daddy/Amazon and many others). The supplier will then get back to us with a certificate, root certificate and maybe some intermediate certificates

When we have received the certificates from our supplier it is time to start assembling the signed key .p12 file. For this we use the domain.crt (supplier) and domain.com.key_nopasswd (same key we created in the beginning) files.

First we remove any password from the key file (depending on application this might not always be necessary)

openssl rsa -in domain.com.key -out domain.com.key_nopasswd

You will be prompted for the password of your .key file

Once the key file is without a password we can create the .p12 file

openssl pkcs12 -export -name somename -in domain.crt -inkey domain.com.key_nopasswd -out keystore.p12

Now we have the .p12 file. Time to put it into the jks container

keytool -importkeystore -destkeystore mykeystore.jks -srckeystore keystore.p12 -srcstoretype pkcs12 -alias somealias

Lastly we need the CA and any intermediate certificates (one command run per certificate file)

keytool -import -keystore mykeystore.jks -file someca.crt -alias someotheralias

The jks is now ready for use!

Tested on Ubuntu 16.04 (AWS) and Play Framework 2.3

My time number format knockout binding

I made this binding so that I could correct the time number format written by a user (hh:mm). A way to make inputing time values easier. The binding will create time strings from one, two, three and four values. It vill also correct any mistyped characters in between hour and minutes

ko.bindingHandlers.timebox = {
  init: function(element, valueAccessor) {
    var allNumbers = /^\d{4}$/;
    var oneNumber = /^\d$/;
    var twoNumbers = /^\d{2}$/;
    var threeNumbers = /^\d{3}$/;
    var correct = /^\d{2}.\d{2}$/;
    var observable = valueAccessor();
    var formatted = ko.computed({
      read: function (key) {
        return observable();
      },
      write: function (value) {
        if(correct.test(value)){
          value = value.substring(0,2) + ':' + value.substring(3);
        }
        else if(allNumbers.test(value)) {
          value = value.substring(0,2) + ':' + value.substring(2);
        }
        else if(threeNumbers.test(value)) {
          value = '0' + value.substring(0,1) + ':' + value.substring(1);
        }
        else if(oneNumber.test(value)) {
          value = '0' + value + ':00';
        }
        else if(twoNumbers.test(value)) {
          value = value + ':00';
        }
        else {
          alert("Wrong time format specified (hh:mm)");
          value = '';
        }
        observable(value); // Write to underlying storage 
      },
      disposeWhenNodeIsRemoved: element                
    });
 
    //apply the actual value binding with our new computed
    ko.applyBindingsToNode(element, { value: formatted });
  }        
};

Using it is really easy. Just replace ‘value’ with ‘timebox’ like this:

<input id="my_id" type="text" data-bind="timebox:my_observable"/>

Tested on OSX 10.7.5, Chrome 37.0.2062.94, KnockoutJS 3.1.0