-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathbastion-create.sh
More file actions
executable file
·150 lines (125 loc) · 5.13 KB
/
bastion-create.sh
File metadata and controls
executable file
·150 lines (125 loc) · 5.13 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#!/bin/bash
# Script to add a bastion and related resources to an existing VPC environment
# It adds:
# - a subnet
# - a bastion and a maintenance security group with rules
# - a VSI for the bastion
# - a floating IP
#
# It needs the following environment variables set
# - VPCID
# - BASENAME
# - BASTION_SSHKEY
# - BASTION_IMAGE (optional, default Ubuntu)
# - BASTION_NAME (optional, default "bastion")
# - BASTION_ZONE
#
# It exports the following variables
# - BASTION_IP_ADDRESS
# - SGBASTION
# - SGMAINT
# (C) 2019 IBM
#
# Written by Henrik Loeser, hloeser@de.ibm.com
# Exit on errors
set -e
set -o pipefail
# include common functions
. $(dirname "$0")/../scripts/common.sh
# Some checks before getting started...
#
# check that we know the VPC id
if [ -z "$VPCID" ]; then
echo "Bastion: VPCID required"
exit
fi
# check that IDs for SSH keys have been provided
if [ -z "$BASTION_SSHKEY" ]; then
echo "Bastion: SSH key required (BASTION_SSHKEY)"
exit
fi
# we need to have the zone to provision to
if [ -z "$BASTION_ZONE" ]; then
echo "Bastion: zone required (BASTION_ZONE)"
exit
fi
# check for the basename
if [ -z "$BASENAME" ]; then
echo "Bastion: basename required"
exit
fi
# check for the optional image ID
if [ -z "$BASTION_IMAGE" ]; then
echo "Bastion: no image specified, using Ubuntu"
ImageName=$(ubuntu2204)
BASTION_IMAGE=$(ibmcloud is images --output json | jq -r '.[] | select (.name=="'${ImageName}'") | .id')
fi
# check for the optional bastion name
if [ -z "$BASTION_NAME" ]; then
echo "Bastion: no bastion name specified, using 'bastion'"
BASTION_NAME="bastion"
fi
if ! SUB_BASTION=$(ibmcloud is subnet-create ${BASENAME}-${BASTION_NAME}-subnet $VPCID $BASTION_ZONE --ipv4-address-count 256 --output json)
then
code=$?
echo ">>> ibmcloud is subnet-create ${BASENAME}-${BASTION_NAME}-subnet $VPCID $BASTION_ZONE --ipv4-address-count 256 --output json"
echo "${SUB_BASTION}"
exit $code
fi
SUB_BASTION_ID=$(echo "$SUB_BASTION" | jq -r '.id')
vpcResourceAvailable subnets ${BASENAME}-${BASTION_NAME}-subnet
# Bastion SG
echo "Bastion: Creating security groups"
if ! SGBASTION_JSON=$(ibmcloud is security-group-create ${BASENAME}-${BASTION_NAME}-sg $VPCID --output json)
then
code=$?
echo ">>> ibmcloud is security-group-create ${BASENAME}-${BASTION_NAME}-sg $VPCID --output json"
echo "${SGBASTION_JSON}"
exit $code
fi
export SGBASTION=$(echo "${SGBASTION_JSON}" | jq -r '.id')
# Maintenance / admin SG
if ! SGMAINT_JSON=$(ibmcloud is security-group-create ${BASENAME}-maintenance-sg $VPCID --output json)
then
code=$?
echo ">>> ibmcloud is security-group-create ${BASENAME}-maintenance-sg $VPCID --output json"
echo "${SGMAINT_JSON}"
exit $code
fi
export SGMAINT=$(echo "${SGMAINT_JSON}" | jq -r '.id')
sleep 20
#ibmcloud is security-group-rule-add GROUP_ID DIRECTION PROTOCOL
echo "Bastion: Creating rules"
#echo "bastion"
# inbound
ibmcloud is security-group-rule-add $SGBASTION inbound tcp --remote "0.0.0.0/0" --port-min 22 --port-max 22 > /dev/null
ibmcloud is security-group-rule-add $SGBASTION inbound icmp --remote "0.0.0.0/0" --icmp-type 8 > /dev/null
# outbound
ibmcloud is security-group-rule-add $SGBASTION outbound tcp --remote $SGMAINT --port-min 22 --port-max 22 > /dev/null
#echo "maintenance"
# inbound
ibmcloud is security-group-rule-add $SGMAINT inbound tcp --remote $SGBASTION --port-min 22 --port-max 22 > /dev/null
# outbound
ibmcloud is security-group-rule-add $SGMAINT outbound tcp --remote "0.0.0.0/0" --port-min 443 --port-max 443 > /dev/null
ibmcloud is security-group-rule-add $SGMAINT outbound tcp --remote "0.0.0.0/0" --port-min 80 --port-max 80 > /dev/null
ibmcloud is security-group-rule-add $SGMAINT outbound tcp --remote "0.0.0.0/0" --port-min 53 --port-max 53 > /dev/null
ibmcloud is security-group-rule-add $SGMAINT outbound udp --remote "0.0.0.0/0" --port-min 53 --port-max 53 > /dev/null
# Bastion server
echo "Bastion: Creating bastion VSI"
if ! BASTION_VSI=$(ibmcloud is instance-create ${BASENAME}-${BASTION_NAME}-vsi $VPCID $BASTION_ZONE $(instance_profile) $SUB_BASTION_ID --image-id $BASTION_IMAGE --key-ids $BASTION_SSHKEY --security-group-ids $SGBASTION --output json)
then
code=$?
echo ">>> ibmcloud is instance-create ${BASENAME}-${BASTION_NAME}-vsi $VPCID $BASTION_ZONE $(instance_profile) $SUB_BASTION_ID --image-id $BASTION_IMAGE --key-ids $BASTION_SSHKEY --security-group-ids $SGBASTION --output json"
echo "${BASTION_VSI}"
exit $code
fi
BASTION_VSI_NIC_ID=$(echo "$BASTION_VSI" | jq -r '.primary_network_interface.id')
vpcResourceRunning instances ${BASENAME}-${BASTION_NAME}-vsi
# Floating IP for bastion
BASTION_IP_JSON=$(ibmcloud is floating-ip-reserve ${BASENAME}-${BASTION_NAME}-ip --nic-id $BASTION_VSI_NIC_ID --output json)
BASTION_IP_ID=$(echo "${BASTION_IP_JSON}" | jq -r '.id')
vpcResourceAvailable floating-ips ${BASENAME}-${BASTION_NAME}-ip
BASTION_IP_ADDRESS_JSON=$(ibmcloud is floating-ip $BASTION_IP_ID --output json)
export BASTION_IP_ADDRESS=$(echo "${BASTION_IP_ADDRESS_JSON}" | jq -r '.address')
echo "Bastion: Your bastion IP address: $BASTION_IP_ADDRESS"
export BASTION_MESSAGE="Your bastion IP address: $BASTION_IP_ADDRESS"