Monthly Archives: December 2018

Azure: Adding A Network Security Rule To An Existing Network Security Group

This has caused me a lot of pain, I hope it saves someone else.

One of the things I do after creating a new VM in Azure is add rules to the NSG. This can be mundane and error prone, especially if you have a lot of rules. It’s much easier to deploy your VM with the basic RDP rule and then add your custom rules later. It’s even easier if you can script it. I prefer to use Powershell and the the AZ module on macOS, that way I don’t have to switch computers.

One of the issues I had was adding a NSG rule where “-SourceAddressPrefix” had multiple values. I can easily do this in the GUI by adding the rule manually.

(The IP addresses are just an example to show an IP address and a CIDR notation)

Screen Shot 2018-12-27 at 7.25.42 PM

If I click add here, I create a new rule. But I want to do this with Powershell. But every time I run my script, I get a 400 error on “SourceAddressPrefix”, telling me it’s a bad request. To make it worse, Microsoft doesn’t even offer an example in the official documentation of how to add more than 1 IP or CIDR value.

It took a lot of just trial and error, but I finally got it. If you’re passing more than 1 value to “-SourceAddressPrefix”, it expects that the “,” is NOT in quotations.

So where it feels natural to create a variable like this:

$officeWhiteList = "8.8.8.8,10.1.1.0/24"

It’s wrong. Your “,” is inside the quotes. If you change it to this:

$officeWhiteList = "8.8.8.8","10.1.1.0/24"

It works! Some simple documentation or better error messages would have saved me a ton of time. Here’s my full example to add a network security rule to an existing NSG:

#---------------------------------------------------------------------------
# Summary:
# Add an Azure Network Security Group Rule to an existing NSG

#---------------------------------------------------------------------------
$subscriptionToUse = "12345678-1234-1234-1234-123456789012" #just an example
$officeWhiteList = "8.8.8.8","10.1.1.0/25" #just an example
$rgName = "Resource-Group"
$nsgName = "Resource-Group-nsg"
$customServerRule = "allow-custom-server"
$customServerPort = 15001

# Select subscription ------------------------------------------------------
Set-AzureRmContext -SubscriptionId $subscriptionToUse

# Get the NSG resource -----------------------------------------------------
$resource = Get-AzureRmResource | Where-Object `
{$_.ResourceGroupName `
–eq $rgName `
-and $_.ResourceType `
-eq "Microsoft.Network/networkSecurityGroups"}

$nsg = Get-AzureRmNetworkSecurityGroup `
-Name $nsgName `
-ResourceGroupName $rgName

# Add customServerPort --------------------------------------------------------
$nsg | Add-AzureRmNetworkSecurityRuleConfig `
-Name $customServerRule `
-Description $customServerRule `
-Access Allow `
-Protocol * `
-Direction Inbound `
-Priority 301 `
-SourceAddressPrefix $officeWhiteList `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange $customServerPort

# Save changes to NSG ------------------------------------------------------
$nsg | Set-AzureRmNetworkSecurityGroup

If you want to add more than 1 rule, just add another “$nsg | Add-AzureRmNetworkSecurityRuleConfig” statement. When you’re done, make sure the last statement is “$nsg | Set-AzureRmNetworkSecurityGroup“, this saves your changes to the NSG

Bitbucket Server SSL Setup

Bitbucket SSL Setup

To secure Bitbucket server with a self-signed certificate, you’ll need to stop the server, generate a certificate, create a bitbucket.properties file, and restart the server.

This is for testing purposes only. In production, you should not use a self-signed certificate. And I’m assuming Bitbucket server is already running http on the default port 7990 on Ubuntu 18.04 LTS.

Stop bitbucket server:

sudo service atlbitbucket stop

Generate SSL keystore:
If you do not have Java installed, you must first install and verify you have the keytool command

#install Java
sudo apt-get install oracle-java8-installer

Verify keytool:

which keytool

Generate a self-signed certificate:

sudo keytool -genkey -alias tomcat -keyalg RSA -sigalg SHA256withRSA \
-keystore /var/atlassian/application-data/bitbucket/shared/config/ssl-keystore

Answers to the certificate creation … Note “What is your first and last name?” is the “Common Name” and should be the server name. Also the default password for tomcat is “changeit” … for testing purposes, it’s easiest to just use it as the default password.

Enter keystore password: changeit
Re-enter new password: changeit
What is your first and last name?
What is the name of your organizational unit?
What is the name of your organization?
What is the name of your City or Locality?
What is the name of your State or Province?
What is the two-letter country code for this unit?
Is CN=bitbucket-gk, OU=testco, O=testco, L=Phoenix, ST=Arizona, C=AZ correct?
Ignore the Warning:

The JKS keystore uses a proprietary format”.

There is No need to convert in dev environment. But you can convert it to “PKCS12”, but you have to add an additional line to your bitbucket.properties files:

server.ssl.key-store-type=pkcs12


Create bitbucket.properties file:

This file does not exist by default. If it’s missing, you must create it.

sudo nano /var/atlassian/application-data/bitbucket/shared/bitbucket.properties

Paste these into the bitbucket.properties file and save:

#ssl settings
server.port=8443
server.ssl.enabled=true
server.ssl.key-store=/var/atlassian/application-data/bitbucket/shared/config/ssl-keystore
server.ssl.key-store-password=changeit
server.ssl.key-password=changeit

To start bitbucket server:

sudo service atlbitbucket start

Wait about 3-5 minutes for the server to restart and access it on https://localhost:8443


To Export Out Certificate

If you want to export out the Java Keystore to a public .cer, and import into macOS keychain.

keytool -export -alias tomcat -file /tmp/bitbucket-gk.cer \
-keystore /var/atlassian/application-data/bitbucket/shared/config/ssl-keystore

Copy to local machine:

scp -rp ssh username@bitbucket-gk:/tmp/bitbucket-gk.cer /tmp

Once it’s copied to local machine you can double click the .cer to open the “Add Certificates” dialog box and import it into your local keychain. Once it’s imported, you can change the trust level to “Always Trust”.