jsborjesson Yet another dev blog

Always Use Double Quotes

In which language, you ask? All of them.

This is an age old war on many fronts, which never seems to end. It is a debate which is almost impossible to avoid if you are at all interested in consistency and readability.

I wanted a rule that would easily answer the question of which quote I should use in any circumstance, language, or framework, without wasting more of my life on this.

The strategy I came up with is this:

Always use double quotes*

* Never fight to switch - if a certain style guide has already won this battle, take their side and use it consistently in that project.

* Still use single quotes where it lets you have less escaping.

This is the rule that I've found causes the least friction, the least amount of effort, and is the most widely applicable. When considering the caveats above, it works no matter the language and project style guide and can be applied almost without thought or context.

Language fit

JavaScript

Both quotes types are exactly equivalent. Single quotes are very prevalent in style guides though, don't fight it.

Python

Both quotes types are exactly equivalent. PEP8 makes no recommendation except for docstrings which should always be double. Use double quotes for everything else as well and never think about it.

Ruby

Both single and double quotes are strings, but they are different.

This lets you use strings differently based on whether you need an interpolated string or a more literal one.

Double quotes are more capable, but their extra features rarely get in the way, stop flipping back and forth!

Using them accurately and consistently based on semantics is a nice idea, but the human factor walks all over it, destroying any reason to trust this visual cue in anything but the personal projects of pundits.

PHP

Same as Ruby, the strings differ and people fight about performance. It is a pointless micro-optimization and you do not have enough string literals for it to matter even if it does have a performance impact.

C# and Java

Single quotes are not even strings.

Go

Go Fmt will force you to use double quotes.

Counter Arguments

Always using single quotes, they are easier to type

They are, but readability and consistency are more important than typing.

Besides, this strategy doesn't work in languages where the quotes aren't equivalent - so always using single quotes is in practice not an option unless you only write in these languages.

That makes this argument about using them as often as possible but not nearly always as you sometimes have to use double quotes.

You also can't type this simple English sentence using single quotes without escaping it. I don't know about you but I often write English sentences.

Switch quotes based on semantics to improve readability

This is asking a lot of humans, and is sure to waste far more time on this war than it can ever be worth, even if it may have readability or performance benefits.

Randomly switch quotes based on your current mood, it doesn't matter

I respect you, it is a stupid topic that should not be given this much thought. However, consistency is important and I hope you can be converted to my very simple rule of thumb.

That being said, if you really don't care you are probably not reading this anyway.

Conclusion

Strong opinions loosely held

Despite the controversial title, and vehemently picking a side in this war while at the same time calling it pointless, my only aim with this post is to share my internal rule of thumb which has personally let me put this issue to rest and stop worrying about it.

I hope it may also give you some peace (of mind).

Setting up Keycloak on AWS

When trying to set up a testing environment of Keycloak on AWS, I bounced between a bunch of different documentation, blog posts and guides setting it up. It was fairly frustrating as all of the guides I found either didn't cover all of what I wanted to do, were slightly outdated or didn't work in some minor way, or simply lead me down the wrong rabbit holes. I decided to document what worked for me in the end.

This guide will cover setting up a testing environment of Keycloak on an AWS EC2 instance, and setting up HTTPS for it. The Keycloak installation described here will not yield a production ready environment, however the instructions for setting up HTTPS should be fairly similar.

EC2

Installing Keycloak

Launch an EC2 instance

Launch an EC2 instance to run the Keycloak server. I will use the Ubuntu Server 18.04 LTS (HVM), SSD Volume Type of the free t2.micro type.

Make sure the Security Group allows you to SSH into it, and opens the ports that Keycloak listens to1:

Type Protocol Port Range Source Description
SSH TCP 22 0.0.0.0/0 Terminal access
Custom TCP Rule TCP 8080 0.0.0.0/0, ::/0 Keycloak HTTP
Custom TCP Rule TCP 8443 0.0.0.0/0, ::/0 Keycloak SSL
HTTPS TCP 443 0.0.0.0/0, ::/0 Load Balancer HTTPS

Create or choose a key pair that you have access to, you will need the .pem file to SSH into the instance.

Download and install Keycloak on the machine

Go to the EC2 Console and wait until your instance reports that its Instance State is Running and the Status Checks are complete, note the Private IP in its Description-tab.

SSH into your newly created machine.

Instructions for this pop up when selecting your instance and clicking Connect, if you get an error about permissions of the .pem file, make sure to run chmod 400 keypair.pem.

# Connect to the EC2 instance
ssh -i "keypair.pem" ubuntu@ec2-xx-xxx-xxx-xxx.region-x.compute.amazonaws.com

# Install Java
sudo apt update
sudo apt install default-jre
java -version

# Download and extract Keycloak
# https://www.keycloak.org/downloads.html
wget https://downloads.jboss.org/keycloak/8.0.1/keycloak-8.0.1.tar.gz
tar -zxvf keycloak-8.0.1.tar.gz
cd keycloak-8.0.1/

# Create the admin user
./bin/add-user-keycloak.sh -r master -u <username> -p <password>

# Find the Private IP of your instance in the EC2 Console, then
# start the Keycloak server
./bin/standalone.sh -b XXX.XX.XX.XXX

This will start the server in your current terminal session.

You can stop the server by pressing Ctrl + C or closing the terminal session.

To let it keep running once you close your SSH connection, press Ctrl + Z (which pauses the process), then run bg (which resumes it in the background) and disown (which detaches it from your job list).

Now you should be able to access Keycloak over HTTP. Get the Public DNS (IPv4) address from the EC2 Console, and enter it in your browser, followed by :8080, and make sure it works.

http://ec2-xx-xxx-xxx-xxx.region-x.compute.amazonaws.com:8080

This means Keycloak is running correctly, but if you try to enter the Administration Console, it will simply give you an error saying HTTPS required. This is what we will set up next.

Route 53

Setting up a domain name

The next thing we will do is to set up a domain for our Keycloak instance.2

Go to the Route 53 Console, and if you haven't already - buy a domain name. This might take a while to register.

ACM

Getting a certificate for HTTPS

Go to Amazon Certificate Manager and follow the instructions to request a public certificate for your domain. Since we use Amazon's own Route 53 to manage our domain, we can let it verify the domain by automatically creating a CNAME record for us.

Elastic Load Balancing

Mapping our certificate to our Keycloak server

A certificate managed by ACM cannot be directly assigned to an EC2 instance, it has to be assigned to a Load Balancer (ELB).

Go back to the EC2 Console and go to Load Balancers in the left hand side menu.

Create an Application Load Balancer,


Set it to be internet-facing.

Select HTTPS as the Load Balancer Protocol, and make sure to select the same VPC as your EC2 instance, and select all availability zones shown.


Select the certificate from ACM.


Select the same Security Group as we created before.1


Create a new target group.

Set the Target type to Instance and the protocol to HTTPS - for Health checks too.


Register your instance as a Target for the Load Balancer, and make sure to set the Port to 8443 to redirect incoming HTTPS requests to the secure Keycloak port.

You should now be able to test the Load Balancer by going to the address shown in the DNS name of the Load Balancer:

https://myloadbalancer-xxxxxxxxxx.region-x.elb.amazonaws.com

Your Keycloak instance should now allow you to log in as admin with the secure connection, but your browser will tell you that the website is insecure.

Route 53

Routing traffic to your Load Balancer

Now we are ready to assign our proper domain name to our secure Load Balancer.

Go back to the Route 53 Console and enter the Hosted zone of your domain name.

Click Create Record Set and enter the subdomain you wish to dedicate to Keycloak as the Name. The Type should be an A-record.

Choose Yes in the Alias-section, and select your Load Balancer from the dropdown menu.

Click Create.

Give the domain some time to propagate, but after a little while you should be able to access Keycloak securely, over HTTPS, without any warnings at the proper domain!

Sources

  1. Note that you may have a separate Security Group for the Load Balancer which only has HTTPS exposed, and you may restrict access to the Keycloak access, but for this testing environment that doesn't matter.  2

  2. Many guides I was trying to follow suggest using Let's Encrypt to get a certificate directly on the EC2 instance. However, at the time of writing (2020-01-30), the EC2 domains are blacklisted by Let's Encrypt. This is for good reason, since they can belong to you one day, and someone else the next. 

A Digression in Vexillology

A while back, I spent an entire evening categorizing the world's flags into different categories in a spreadsheet - because I literally had nothing better to do. I saw a lot of similarities between flags, and though I'm sure there are interesting historical reasons, I was more interested in exploring the current state of flags in the world.

The data

I decided to limit my dataset to the 193 UN member states, because defining what a country is is not as easy as it sounds, and this makes for a reasonably complete list. It covers the area marked in green:

UN Coverage

It does have some notable exceptions:

  • England, Scotland, Wales, and Northern Ireland which are only members as part of the United Kingdom
  • Western Sahara, which is a disputed territory
  • Palestine, which has a decent flag
  • The Vatican, which doesn't

Stripes

128 (66%) of all flags in the world have stripes. Exactly 100 (52%) of the flags have horizontal stripes, 35 (18%) have vertical stripes.1

Only 7 (4%) countries have both vertical and horizontal stripes:

You can see why this is not a popular option.

57 (30%) countries have exactly 3 horizontal stripes, and 23 (12%) have exactly 3 vertical stripes.

Most stripey

Whereas no country has more than 3 vertical stripes (that would be insane), Malaysia and the US are tied for first place in the horizontal stripe race:

Country Horizontal Stripes
Malaysia 13
USA 13
Liberia 11
Greece 9
Uruguay 9
Zimbabwe 7
Uganda 6

15 countries technically have 5 stripes, although only Costa Rica, Cuba, Thailand and Togo properly have 5 stripes.

The others are basically wannabe 3-stripers but with little gaps between the main stripes. Israel is the most subtle which really does look like 3 stripes, but actually has white stripes at the top and bottom:

Flag of Israel

Only the Central African Republic, Comoros and Mauritius have 4 stripes, Mauritius being the only plain 4-striped flag in the world.

Flag of Mauritius

Stars

There are a total number of 273 stars on all of the country flags, spread out among 72 (37%) of the countries.

Unlike in the stripes department where the US had to settle for a tie, when it comes to stars the US with its 50 stars absolutely destroys its competition with just under twice as many stars as the runner up, Brazil which has 27. Brazil in turn has more than twice as many stars as Uzbekistan which scanters into third with a measly 12 stars.

Country Stars
USA 50
Brazil 27
Uzbekistan 12
Cape Verde 10
Dominica 10
Tuvalu 9
Bosnia and Herzegovina 9*
Venezuela 8

42 (22%) of the flags have but a single star.

Seeing stars

I thought counting stars would be at worst boring, provided more flags looked similar to the US flag. Boy was I wrong, what counts as a star?

Look at the dataset to see how I got the final tally.

Crosses

13 (7%) countries have a cross on their flag.

The following have the Scandinavian off-center cross:

And the rest have a symmetrical cross:

Georgia really likes crosses and has 5:

Flag of Georgia

Cantons

13 (7%) other countries have a canton on their flag:

I personally don't think that any of these are particularly good, except for Liberia which is a nicer and cleaner version of the US flag. Slim down the number of stripes (without going too far like Chile), replace the star extravaganza with a single classy star, and make the square actually square and a more tasteful size, much better:

Flag of Liberia

Though Liberia has one of the nicer country flags in the world, this pedigree does not trickle down to its 15 counties.

Maps

Only one of the UN Member States has a map of their own country on their flag, Cyprus:

Flag of Cyprus

I think it's brilliant.2

Though they are not members of the UN, I still want to mention the flag of Kosovo, and Graham Bartram design of the flag of Antarctica, which I also really like:

Flag of Kosovo

Flag of Antarctica

Toplist

These all make my list for basically the same reason: I think that they are all the best examples of very popular and traditional flag designs, while at the same time being unique and having their own twist to them, making them both instantly recognizable, familiar, but also memorable and clearly distinct from the many other similar flags out there. World class.

They are of course my personal opinion and nothing else.

Honorable mentions: United Kingdom, South Korea, Guyana, Jamaica

1. South Africa

By far my favourite, excellent colours and shapes.

Flag of South Africa

2. Norway

The best of the Scandinavian crosses, followed by Iceland which has the colors flipped, the other ones have pretty poor color choices in my opinion.

Flag of Norway

3. Congo

Simple 3 stripes African colors, but diagonally - beautiful.

Flag of Congo

Bottomlist

I don't know if I even have to put a disclaimer here saying that this is only my personal opinion, because these are truly terrible flags.

Dishonorable mentions:

  • Nepal which isn't even shaped like a flag
  • Sri Lanka which has two flags in one - one okayish and one awful
  • Turkmenistan which rolled out a rug on its flag

1. Haiti

When you use MS Paint to put a picture with a white background in the middle of your flag, should you really have one?

Flag of Haiti

2. Lebanon

It'd be difficult to let this one stay up on the fridge for long even if drawn by your own child.

Flag of Lebanon

3. Maldives

This one is just dreary - the border is too wide, the moon is too narrow and the colors are depressing. It also happens to be the single flag in the world which has a moon but not a star. Based on this flag, you clearly need a star if you have a moon.

Flag of Maldives

Sources

  • Dataset - I created this myself by going through all the flags filling it in. It was a great night.
  • UN Member States
  • Every single world flag

Spot a mistake? Please let me know and I'll fix it and credit you in the post.

  1. Not counting crosses as stripes in either direction. 

  2. Though I like them - maps are not very good flag design. 

AWS - A Bird's-eye View

AWS is a huge and complex place - this post aims to do nothing more than provide an overview with short and simple descriptions of some of the services I use and find relevant right now.

Service Full name Description
ACM AWS Certificate Manager Issue and manage certificates to set up HTTPS on your domain names
EB Elastic Beanstalk Deploy and scale web applications and services
EC2 Elastic Compute Cloud The virtual servers that almost everything on AWS runs on sooner or later
ECR Elastic Container Registry Upload your Docker containers, similar to DockerHub
ECS Elastic Container Service Deploy and orchestrate Docker containers with Amazon's own service (free)
EKS Elastic Kubernetes Service Deploy and orchestrate Docker containers with Kubernetes (expensive)
ELB Elastic Load Balancing Manage load balancers that route requests from the internet to your instances
IAM Identity and Access Management Manage permissions to AWS
RDS Relational Database Service Run SQL databases
Route 53 Route 53 DNS - Manage domain names
S3 Simple Storage Service Store and host files
VPC Virtual Private Cloud The networking layer for EC2. Deals with subnets and Security Groups.

Writing in other languages in Vim

Vim is, like it or not, made to be used with a U.S. Qwerty layout. I've stopped switching back and forth between keyboard layouts in the OS, the U.S. layout is actually not too bad to write in the other languages that I need (Swedish, and sometimes German).

Thankfully, to write in all of these three languages, you only need a few more characters than the English alphabet:

Letter U.S. layout Vim
Åå Alt + A <C-k>aa
Ää Alt + U, A <C-k>a:
Öö Alt + U, O <C-k>o:
Üü Alt + U, U <C-k>u:
Éé Alt + E, E <C-k>e'
ß Alt + S <C-k>ss

As you can see, they are somewhat reasonable both in and outside of Vim (see :help digraphs to get a list of Vim's characters), but there's no reason they have to be different. All of the combinations in the middle column are perfectly free in Insert mode - so why not use them in Vim too.

This maps all these letters so that you can type them in Vim's Insert mode just as you can everywhere else:

" Mend meta-mappings

" This is sometimes needed to get combinations with Alt to work.
" (probably not in NeoVim, check if it works without them first)
"
" CAUTION: ^[ is NOT two characters, it's the escape character.
"
" You have to insert it in Vim with <C-v><Esc>,
" then it should look like this but behave as a single character.
" If not, these settings will not work.

set <M-S-a>=^[A
set <M-a>=^[a
set <M-e>=^[e
set <M-s>=^[s
set <M-u>=^[u

" International letters
" Allow writing Swedish/German letters the 'normal' way on a US layout

noremap! <M-S-a> <C-k>AA
noremap! <M-a> <C-k>aa
noremap! <M-e>E <C-k>E'
noremap! <M-e>e <C-k>e'
noremap! <M-s> <C-k>ss
noremap! <M-u>A <C-k>A:
noremap! <M-u>O <C-k>O:
noremap! <M-u>U <C-k>U:
noremap! <M-u>a <C-k>a:
noremap! <M-u>o <C-k>o:
noremap! <M-u>u <C-k>u:

Note that I'm using the peculiarly named noremap! function, which maps them both in Insert and Command-line mode. Command-line mode is important because you want to be able to search for foreign words.