Skip to content

Instantly share code, notes, and snippets.

@jcefoli
Last active January 29, 2019 20:22
Show Gist options
  • Save jcefoli/7884211254bcb827b179c8842cf795f9 to your computer and use it in GitHub Desktop.
Save jcefoli/7884211254bcb827b179c8842cf795f9 to your computer and use it in GitHub Desktop.
Connect To Azure Redis Instance Using TLS and stunnel
<#
.SYNOPSIS
Connect to Azure Redis CLI over TLS instances using Stunnel.
.Description
This script helps you manage connecting to multiple redis instances in Azure by modifying the stunnel config automagically
ASSUMPTIONS AND WARNINGS:
==============================
- Must run in an elevated PowerShell prompt
- redis-cli Windows binary exists in the system path (aka we can call redis-cli from anywhere and it will run)
- Stunnel is installed to C:\Program Files (x86)\stunnel
- Stunnel is installed as a Windows Service. To do this, run: "C:\Program Files (x86)\stunnel\bin\stunnel.exe -install"
- WARNING: THIS WILL OVERWRITE THE stunnel CONFIG FILE HERE: "C:\Program Files (x86)\stunnel\config\stunnel.conf"
.NOTES
Version: 1.0.0
Author: jcefoli
Creation Date: 1/29/2019
.PARAMETER redisName
The Azure Redis instance name (do not include .redis.cache.windows.net)
.PARAMETER redisPW
The password to the Redis instance
.EXAMPLE
./az-redis-cli.ps1 -redisName "myapp-redis-staging" -redisPW "LetMeIn!"
#>
#Requires -RunAsAdministrator
param (
[Parameter(Mandatory=$true,
HelpMessage="redisName parameter is missing.")]
[string] $redisName,
[Parameter(Mandatory=$true,
HelpMessage="redisPW parameter is missing.")]
[string] $redisPW
)
function Update-Stunnel_Redis_Config {
# Do not change indenting below. Powershell Heredocs should not be indented within the function
$configFileContents = @"
[redis-cli]
client = yes
accept = 127.0.0.1:6380
connect = $redisName.redis.cache.windows.net:6380
"@
#Write new config file
[IO.File]::WriteAllLines("C:\Program Files (x86)\stunnel\config\stunnel.conf", $configFileContents)
}
# Check if Service is Running & Update Config
if ((Get-Service -Name "stunnel").status -eq "Running") {
Update-Stunnel_Redis_Config
. "C:\Program Files (x86)\stunnel\bin\stunnel.exe" -reload
} else {
Update-Stunnel_Redis_Config
. "C:\Program Files (x86)\stunnel\bin\stunnel.exe" -start
}
#Connect to Redis via CLI
$host.ui.RawUI.WindowTitle = "$redisName.redis.cache.windows.net:6380"
. redis-cli -p 6380 -a $redisPW
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment