forked from ibm-watson-data-lab/stunredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstunredis.sh
executable file
·72 lines (63 loc) · 2.12 KB
/
stunredis.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/bin/bash
#
# Stunredis.sh
#
# Copyright 2018 IBM Corp.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
DATABASE_URL=$1
LOCALPORT=${2:-6830}
# This is the location of the validation chain file
lechain=./lechain.pem
# URL parsing based on https://stackoverflow.com/a/17287984
# extract the protocol
proto="`echo $DATABASE_URL | grep '://' | sed -e's,^\(.*://\).*,\1,g'`"
# remove the protocol
url=`echo $DATABASE_URL | sed -e s,$proto,,g`
# extract the user and password (if any)
userpass="`echo $url | grep @ | cut -d@ -f1`"
pass=`echo $userpass | grep : | cut -d: -f2`
if [ -n "$pass" ]; then
user=`echo $userpass | grep : | cut -d: -f1`
else
user=$userpass
fi
hostport=`echo $url | sed -e s,$userpass@,,g | cut -d/ -f1`
port=`echo $hostport | grep : | cut -d: -f2`
if [ -n "$port" ]; then
host=`echo $hostport | grep : | cut -d: -f1`
else
host=$hostport
fi
# Now we create our configuration file as a variable
stunnelconf=""
stunnelconf+=$"foreground=yes\n"
stunnelconf+=$"[redis-cli]\n"
stunnelconf+=$"client=yes\n"
stunnelconf+=$"accept=127.0.0.1:$LOCALPORT\n"
stunnelconf+=$"verifyChain=yes\n"
stunnelconf+=$"checkHost=$host\n"
stunnelconf+=$"CAfile=$lechain\n"
stunnelconf+=$"connect=$hostport\n"
# We expand that out in echo and feed the result to stunnel
# which is set to take its configuration from a file descriptor
# in this case, 0, stdin.
echo -e $stunnelconf | stunnel -fd 0 &
# Grab the pid
stunnelpid=$!
# Sleep a moment to let the connection establish
sleep 1
# Now call redis-cli for the user to interact with
redis-cli -p $LOCALPORT -a ${pass}
# Once they leave that, kill the stunnel
kill $stunnelpid