Automation and security are top priorities. IBM i is a first class citizen if we let it be. If we take the effort to get more open source on the platform. This article does that.
If you haven't noticed, security is regularly in the news and generally has been a hot topic the past couple years. Most recently, there was the Kemuri Water Company breach, which unfortunately was against their "AS/400." The quote still ringing in my ears from that article is the following:
"The facts in the report do speak for themselves and it’s readily apparent the specific affected water utility was trapped in a past decade (or even two decades ago) in a time when they had little reason to expect their company, business operations or water control systems would ever become the desired target for a sophisticated cyber attack,"
It's easy to think you’re immune to security threats. It might be tempting to operate under the premise of "hoping for the best," but that stance can be generally classified as security ignorance. It's time we not only replace our ignorance but also start pursuing "ethical hacking," as MC Press author Steve Pitcher has done with excellence.
Not only do we need security in place, but it needs to be automated so we become less hesitant to implement it. The point on automation was made by Steve Will, Chief IBM i Architect at IBM, in this blog post. I think we need to heed Steve’s advice.
It's the combination of security and automation that brings me to the subject matter of this article. Last year, a co-worker told me about a new initiative called LetsEncrypt.org. The aim is to make SSL certificates free, automatic, secure, transparent, open, and cooperative. You can learn more about each on the LetsEncrypt.org About page.
One of the interesting limitations of LetsEncrypt.org is that certificates signed by them are valid only for a max of 90 days. It would be a major pain in the butt to renew keys every 90 days. That's why there are many open-source projects in a variety of programming languages that automate the process. Click here to see them all. I scanned through the list and hoped to find one that was not specific to an existing language because I am hoping many IBM i websites will make use of this approach. Lo and behold, there are a couple for bash (Bourne Again Shell), which I have on IBM i because I've installed 5733OPS option 3. Specifically, it is this project: https://github.com/Neilpang/le
To install that GitHub project on the IBM i, you can either download the source and FTP it to the IFS or use git clone, as shown below.
$ git clone
Cloning into 'le'...
remote: Counting objects: 793, done.
remote: Compressing objects: 100% (12/12), done.
remote: Total 793 (delta 4), reused 0 (delta 0), pack-reused 781
Receiving objects: 100% (793/793), 183.52 KiB | 0 bytes/s, done.
Resolving deltas: 100% (318/318), done.
Checking connectivity... done.
Now go into the directory that was created.
$ cd le
The project install instructions tell us to run the following command.
$ ./le.sh install
Please install crontab first.
try to install 'cron, crontab, crontabs or vixie-cron'.
We need to set cron job to renew the certs automatically.
Pre-check failed, can not install.
Hmm…errors. What to do?
Remember the "Drivers, Start your Nginx!" article I previously wrote where I included the painful steps of how I was able to get Nginx installed? In that article, I asked whether people like to see the specific details of how to troubleshoot within PASE. Well, I am still getting positive responses from that article with encouragement to do more. So, are you ready for some more blood, sweat, and tears? Time to see if we can get this open-source project ported to IBM i.
Going back to the error, IBM i doesn't have crontab by default. First, go to perzl.org to see if it exists in the list of packages. It doesn't. But, remembering that list isn't comprehensive, I went to the "everything RPMS" link. Using Ctrl+F on the web page, I didn't find anything relating to "cron".
I then searched the github.com source and found the following lines:
if ! _exists "crontab" ; then
_err "Please install crontab first.
try to install 'cron, crontab, crontabs or vixie-cron'."
_err "We need to set cron job to renew the certs automatically."
return 1
fi
Since I don't intend on auto-updating (yet), I will comment out those lines by placing a # character in front of each line, as shown below.
# if ! _exists "crontab" ; then
# _err "Please install crontab first.
try to install 'cron, crontab, crontabs or vixie-cron'."
# _err "We need to set cron job to renew the certs automatically."
# return 1
# fi
Now I will run it again.
$ ./le.sh install
It is recommended to install nc first, try to install 'nc' or 'netcat'.
We use nc for standalone server if you use standalone mode.
If you don't use standalone mode, just ignore this warning.
Installing to /home/AARON/.le
Installed to /home/AARON/.le/le.sh
OK, Close and reopen your terminal to start using le
Installing cron job
./le.sh: line 1372: crontab: command not found
./le.sh: line 1379: crontab: command not found
./le.sh: line 1379: crontab: command not found
Install cron job failed. You need to manually renew your certs.
Or you can add cronjob by yourself:
LE_WORKING_DIR="/home/AARON/.le" "/home/AARON/.le"/le.sh cron > /dev/null
OK
OK, it got past my commented lines, although it still attempted to install crontab. I don't believe that will be an issue. As the message above notes, I can simply manually renew my certs.
Next, I exit the PASE shell per the directions on the Github page.
$ exit
Log back in and cd into the location where you did the git clone.
$ cd /home/aaron/git/le
Now it's time to have a certificate issued for a domain. This involves many steps that happen under the covers. One of the steps involves verifying you actually own the domain. There are a handful of ways to verify the domain. One of them is to have a predefined and generated file be hosted on your domain. Then, letsencrypt.org will attempt to access that file. They figure if you can get a file served from the domain, then you must own it. This means we need to tell the script where it should generate the file. In my case, I've created a directory named /www/mysite/htdocs/letsencrypt.
I am using Nginx on IBM i instead of Apache. Below is the location mapping necessary to receive a request from letsencrypt.org and route it to my newly created htdocs/letsencrypt directory.
---/www/mysite/conf/nginx.conf---
. . .
location /.well-known/acme-challenge {
root /www/mysite/htdocs/letsencrypt;
}
. . .
What's up with the weird URL of /.well-known/acme-challenge? I'm not sure. I just know that's what LetsEncrypt.org uses. The next step on the GitHub instructions is to "issue" a certificate using the following syntax.
$ le issue /www/mysite/htdocs/letsencrypt mysite.com
First, we see the le command, which is aliased to le.sh. We can confirm this by running the which command.
$ which le
le: aliased to /home/AARON/.le/le.sh
After le, we see three parameters. The first declares the action—"issue" in this case. The second is the web-accessible directory we created earlier. The third is the domain needing a new certificate.
At this point, I hit Enter. Everything was going good until I received the following error.
$ le issue /www/mysite/htdocs/letsencrypt mysite.com
Creating account key
Use default length 2048
Registering account
Registered
Creating domain key
Use length 2048
Creating csr
Single domain=mysite.com
Verify each domain
Getting token for domain=mysite.com
printf: usage: printf [-v var] format [arguments]
Verifying:mysite.com
/home/AARON/.le/le.sh: line 1115:
/www/mysite/htdocs/letsencrypt/.well-known/acme-challenge/:
Is a directory
chown: /www/mysite/htdocs/letsencrypt/ is an unknown username.
curl: no URL specified!
curl: try 'curl --help' or 'curl --manual' for more information
mysite.com:Verify error:
I guessed that the printf and chown command failures might have to do with an issue with curl. How do I know that? Well, curl is a program used to return the results of a web request. If that fails, and if error trapping isn't adequate, then a subsequent printf or chown might fail because of blank variables.
At this point, I dug further into le.sh and noticed there are debugging capabilities. In the case of this project, debugging means you set the DEBUG environment variable to true (i.e., DEBUG=1) and it will generate various logs to the console for review. I ran the command again as shown below.
$ DEBUG=1 le issue /www/mysite/htdocs/letsencrypt mysite.com
When you only want an environment variable to be set for the duration of a program invocation, you can put it right before the command, as I did above. The other approach to make the value stay put for the duration of this shell session is to instead use "export DEBUG=1".
I am not going to post the entire debug results as they were quite long (and full of secret keys I don't want shared). I did find that a dump would occur if the curl request wasn't successful on this line, so I used the cat command to display the contents.
$ cat /home/AARON/.le/curl.dump
== Info: TLSv1.2, TLS alert, Server hello (2):
=> Send SSL data, 2 bytes (0x2)
0000: .0
== Info: SSL certificate problem: unable to get local issuer certificate
== Info: Closing connection 0
== Info: TLSv1.2, TLS alert, Client hello (1):
=> Send SSL data, 2 bytes (0x2)
The above error means the certificates installed on the machine aren't adequate to do the SSL handshake. This is a relatively common error I come across on IBM i. An adequate cacerts.pem file can be obtained from the following URL: https://curl.haxx.se/docs/caextract.html. Download and place in a directory on your IBM i. I put it in /home/aaron/certs.
Now set the CURL_CA_BUNDLE environment variable to that path the next time you invoke the le script, as shown below.
$ CURL_CA_BUNDLE=~/certs/cacert.pem le issue /www/mysite/htdocs/letsencrypt mysite.com
Creating account key
Use default length 2048
Registering account
grep: illegal option -- o
usage: grep [-r] [-R] [-H] [-L] [-E|-F] [-c|-l|-q] [-insvxbhwyu]
[-p[parasep]] -e pattern_list...
[-f pattern_file...] [file...]
usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwyu] [-p[parasep]]
[-e pattern_list...]
-f pattern_file... [file...]
usage: grep [-E|-F] [-c|-l|-q] [-insvxbhwyu]
[-p[parasep]] pattern_list [file...]
(23) Failed writing body
Register account Error: {"type":"urn:acme:error:badNonce","detail":
"JWS has no anti-replay nonce","status":400}
Snap! Another error.
The PASE version of grep isn't quite the same as Linux. No worries, perzl.org to the rescue! If you need the perzl.org version of grep, first download and install IBM i Chroot (or install 5733OPS option 3). Then run the following commands.
$ cd /dir/where/ibm-i-chroot/exists
$ ./pkg_setup.sh pkg_perzl_utils.lst
That will install various programs in /opt/freeware/bin. We now need to make sure the /opt/freeware/bin directory is searched when we run commands. Modify the PATH environment to have that directory first, as shown below. The PATH environment works much like a library list. It contains a list of delimited directories that should be searched for commands and programs.
$ export PATH=/opt/freeware/bin:$PATH
At this point, I was getting an error every subsequent time I ran the script. It recommended I set FORCE=1 if I wanted to rerun the process. I've added that environment variable to the call, as shown below.
% CURL_CA_BUNDLE=~/certs/cacert.pem FORCE=1 le issue
/www/mysite/htdocs/letsencrypt mysite.com
Creating account key
Use default length 2048
Account key exists, skip
Skip register account key
Creating domain key
Use length 2048
Creating csr
Single domain=mysite.com
Verify each domain
Getting token for domain=mysite.com
Verifying:mysite.com
chown: /www/mysite/htdocs/letsencrypt/ is an unknown username.
mysite.com:Verify error:{"type":"http-01","status":"valid","uri":
"https://acme-v01.api.letsencrypt.org/acme/challenge/Na7sxxxxxxxs_3ReHuXxxxxxxxYY/99999999",
"token":
"iPMnxxxxxxxxxxxxxxbP8_n6gs","keyAuthorization":
"iPMnxxxxxxxxxxxxxbP8_n6gs.CP2SxxxxxxxxxxEPF0","validationRecord":[{"url":
"http://mysite.com/.well-known/acme-challenge/iPMnxxxxxxxxxxxxP8_n6gs",
"hostname":"spaces.litmis.com",
"port":"80","addressesResolved":["69.99.99.144"],"addressUsed":"69.99.99.144"},{"url":
"https://mysite.com/.well-known/acme-challenge/iPMnxxxxxxxxxxTRbP8_n6gs","hostname":"mysite.com","port":"443","addressesResolved":["69.99.99.144"],"addressUsed":"69.99.99.144"}]}
OK, we've made progress. However, the script stops on this "Verify error," as shown above. This one took me awhile to resolve. I had to add many echo statements to le.sh to learn why it was failing. It turns out there was an issue with how the regular expression on the egrep command was written. Once I figured that out, I went ahead and forked the GitHub project, made my change, and issued a pull request. You can see my changes here. My pull request was accepted by the author of the parent repository and further questions were asked to learn if there was more they could do to facilitate this running on IBM i. You can see the full transcript here.
Now that the fix for egrep is in place, it's time to run the command again.
$ CURL_CA_BUNDLE=~/certs/cacert.pem FORCE=1 le issue /www/mysite/htdocs/letsencrypt mysite.com
Creating account key
Use default length 2048
Account key exists, skip
Registering account
Registered
Creating domain key
Use length 2048
Creating csr
Single domain=mysite.com
Verify each domain
Getting token for domain=mysite.com
Verifying:mysite.com
chown: /www/mysite/htdocs/letsencrypt/ is an unknown username.
Success
Verify finished, start to sign.
Cert success.
-----BEGIN CERTIFICATE-----
.....contents omitted for privacy...
-----END CERTIFICATE-----
Your cert is in /home/AARON/.le/mysite.com/mysite.com.cer
The intermediate CA cert is in /home/AARON/.le/mysite.com/ca.cer
And the full chain certs is there: /home/AARON/.le/mysite.com/fullchain.cer
Victory! The certificate has been issued and is on my IBM i. The last step is to setup Nginx for SSL. The word count of this article is already getting long, so that will be a topic for another day!
In conclusion, I'll comment on what you'd need to do to use this open-source project. Since my pull requests were accepted by the parent repo, you shouldn't have the cron or egrep issues to contend with. Instead, you will just need to obtain the Github source, obtain necessary programs from perzl.org (i.e., grep), and obtain the cacerts.pem file.
If you have any specific questions, please let me know in the comments.
LATEST COMMENTS
MC Press Online