Skip to content

Commit bf4dee8

Browse files
authored
Bootstrap: require user to pass Bastion remote access CIDRs (#22)
1 parent ceab73b commit bf4dee8

File tree

7 files changed

+66
-13
lines changed

7 files changed

+66
-13
lines changed

README.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,13 @@ required:
6161
created the repository and upload the image.
6262
3. Example: `./publish-ecr xrd-vrouter-container-x86.7.9.1.tgz`
6363
2. Run the `aws-quickstart` script.
64-
1. This has two mandatory arguments, the username and password to be
65-
used for the XRd root user.
64+
1. This has three mandatory arguments: the username and password to be
65+
used for the XRd root user, and a comma-separated list of IPv4 CIDR
66+
blocks to allow SSH access to the Bastion instance.
6667
2. This will first build an AMI using the
6768
[XRd Packer](https://github.com/ios-xr/xrd-packer) templates if one
6869
is not detected.
69-
3. Example: `./aws-quickstart -u user -p password`
70+
3. Example: `./aws-quickstart -u user -p password -b 10.0.0.0/8,172.16.0.0/12,192.168.0.0/16`
7071

7172
This will bring up an EKS cluster called 'xrd-cluster', some worker nodes,
7273
and a dummy topology with a pair of back-to-back XRd instances running an
@@ -140,6 +141,13 @@ terraform -chdir=examples/bootstrap init
140141
terraform -chdir=examples/bootstrap apply
141142
```
142143

144+
This accepts a number of input variables described in
145+
[`variables.tf`](/examples/bootstrap/variables.tf). In particular, the
146+
`bastion_remote_access_cidr_blocks` variable is required, which is a list of
147+
IPv4 CIDR blocks to allow SSH access to the Bastion instance. Pass `null` to
148+
prevent access to the Bastion instance, or `["0.0.0.0/0"]` to allow SSH access
149+
from any IPv4 address.
150+
143151
Terraform will show you a changeset and ask you to confirm that it should
144152
proceed. It takes around 15 minutes to bring up the configuration.
145153

@@ -194,7 +202,7 @@ When you've finished with the topology, it can be torn down with:
194202
```
195203
terraform -chdir=examples/overlay/workload destroy -var-file=$PWD/vars.tfvars
196204
terraform -chdir=examples/overlay/infra destroy
197-
terraform -chdir=examples/bootstrap destroy
205+
terraform -chdir=examples/bootstrap destroy -var=bastion_remote_access_cidr_blocks=null
198206
```
199207

200208
N.B. It is recommended to pass the same configuration to `terraform destroy`

aws-quickstart

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ set -o pipefail
88
usage() {
99
>&2 cat << EOF
1010
USAGE:
11-
aws-quickstart [OPTIONS] -u XR_USERNAME -p XR_PASSWORD
11+
aws-quickstart [OPTIONS] -u XR_USERNAME -p XR_PASSWORD -b BASTION_REMOTE_ACCESS_CIDR_BLOCKS
1212
1313
EOF
1414
}
@@ -27,6 +27,9 @@ ARGS:
2727
-p, --password
2828
XR password.
2929
30+
-b, --bastion-remote-access-cidr-blocks
31+
IPv4 CIDR blocks to allow SSH access to the Bastion instance.
32+
3033
OPTIONS:
3134
-a, --ami
3235
AMI ID of an image used to launch the EKS worker nodes (default: use
@@ -48,6 +51,7 @@ DESTROY=""
4851
KUBERNETES_VERSION="1.30"
4952
XR_USERNAME=""
5053
XR_PASSWORD=""
54+
BASTION_REMOTE_ACCESS_CIDR_BLOCKS=""
5155

5256
# Parse the arguments
5357
while [ $# -gt 0 ]; do
@@ -60,6 +64,10 @@ while [ $# -gt 0 ]; do
6064
XR_PASSWORD="$2"
6165
shift
6266
;;
67+
-b|--bastion-remote-access-cidr-blocks )
68+
BASTION_REMOTE_ACCESS_CIDR_BLOCKS="$2"
69+
shift
70+
;;
6371
-a|--ami )
6472
AMI_ID="$2"
6573
shift
@@ -88,6 +96,10 @@ if [ -z "${XR_PASSWORD:-}" ] && [ -z "$DESTROY" ]; then
8896
>&2 echo "error: XR password (-p|--password) must be specified"
8997
ERROR=1
9098
fi
99+
if [ -z "${BASTION_REMOTE_ACCESS_CIDR_BLOCKS:-}" ] && [ -z "$DESTROY" ]; then
100+
>&2 echo "error: Bastion remote access CIDR blocks (-b|--bastion-remote-access-cidr-blocks) must be specified"
101+
ERROR=1
102+
fi
91103

92104
if [ "${KUBERNETES_VERSION}" != "1.23" ] &&
93105
[ "${KUBERNETES_VERSION}" != "1.24" ] &&
@@ -144,9 +156,28 @@ terraform_apply () {
144156

145157
trap terraform_destroy ERR EXIT
146158

159+
if [ "$BASTION_REMOTE_ACCESS_CIDR_BLOCKS" != "null" ]; then
160+
# This script takes a comma-separated list as input, but Terraform wants
161+
# this as a list of strings in HCL format.
162+
#
163+
# For example, we must convert:
164+
# 10.0.0.0/8,172.16.0.0/12,192.168.0.0/16
165+
# to:
166+
# ["10.0.0.0/8","172.16.0.0/12","192.168.0.0/16"]
167+
#
168+
# Do this in two steps:
169+
# Replace any ',' with '","'.
170+
# Prepend '["', and append '"]'.
171+
bastion_var_value="${BASTION_REMOTE_ACCESS_CIDR_BLOCKS//,/\",\"}"
172+
bastion_var_value="[\"${bastion_var_value}\"]"
173+
else
174+
bastion_var_value="null"
175+
fi
176+
147177
terraform -chdir="$SCRIPT_DIR"/examples/bootstrap apply \
148178
-auto-approve \
149-
-var "cluster_version=$KUBERNETES_VERSION"
179+
-var "cluster_version=$KUBERNETES_VERSION" \
180+
-var "bastion_remote_access_cidr_blocks=$bastion_var_value"
150181
terraform -chdir="$SCRIPT_DIR"/examples/overlay/infra apply \
151182
-auto-approve \
152183
${AMI_ID:+"-var node_ami=$AMI_ID"}
@@ -166,7 +197,8 @@ terraform_destroy () {
166197
terraform -chdir="$SCRIPT_DIR"/examples/overlay/infra destroy \
167198
-auto-approve
168199
terraform -chdir="$SCRIPT_DIR"/examples/bootstrap destroy \
169-
-auto-approve
200+
-auto-approve \
201+
-var "bastion_remote_access_cidr_blocks=null"
170202
}
171203

172204
if [ -z "$DESTROY" ]; then

examples/bootstrap/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ terraform destroy
4141
| Name | Description | Type | Default | Required |
4242
|------|-------------|------|---------|:--------:|
4343
| <a name="input_azs"></a> [azs](#input\_azs) | List of exactly two availability zones in the currently configured AWS region.<br>A private subnet and a public subnet is created in each of these availability zones.<br>Each cluster node is launched in one of the private subnets.<br>If null, then the first two availability zones in the currently configured AWS region is used. | `list(string)` | `null` | no |
44+
| <a name="input_bastion_remote_access_cidr_blocks"></a> [bastion\_remote\_access\_cidr\_blocks](#input\_bastion\_remote\_access\_cidr\_blocks) | Allowed CIDR blocks for external SSH access to the Bastion instance.<br>This must be a list of strings.<br>If null, then access to the Bastion instance is prevented. | `list(string)` | n/a | yes |
4445
| <a name="input_cluster_version"></a> [cluster\_version](#input\_cluster\_version) | Cluster version | `string` | `"1.30"` | no |
4546
| <a name="input_name_prefix"></a> [name\_prefix](#input\_name\_prefix) | Used as a prefix for the 'Name' tag for each created resource.<br>If null, then a random name 'xrd-terraform-[0-9a-z]{8}' is used. | `string` | `null` | no |
4647

examples/bootstrap/main.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ provider "aws" {
1010
module "bootstrap" {
1111
source = "../../modules/aws/bootstrap"
1212

13-
azs = coalesce(var.azs, slice(data.aws_availability_zones.available.names, 0, 2))
14-
cluster_version = var.cluster_version
15-
name_prefix = var.name_prefix
13+
azs = coalesce(var.azs, slice(data.aws_availability_zones.available.names, 0, 2))
14+
bastion_remote_access_cidr_blocks = var.bastion_remote_access_cidr_blocks
15+
cluster_version = var.cluster_version
16+
name_prefix = var.name_prefix
1617
}

examples/bootstrap/variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@ variable "azs" {
2323
}
2424
}
2525

26+
variable "bastion_remote_access_cidr_blocks" {
27+
description = <<-EOT
28+
Allowed CIDR blocks for external SSH access to the Bastion instance.
29+
This must be a list of strings.
30+
If null, then access to the Bastion instance is prevented.
31+
EOT
32+
type = list(string)
33+
}
34+
2635
variable "cluster_version" {
2736
description = "Cluster version"
2837
type = string

modules/aws/bastion/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ variable "name" {
2929
variable "remote_access_cidr" {
3030
description = "Allowed CIDR blocks for external SSH access to the Bastion instance"
3131
type = list(string)
32-
default = ["0.0.0.0/0"]
32+
default = []
3333
nullable = false
3434
}
3535

modules/aws/bootstrap/variables.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ variable "azs" {
99
}
1010

1111
variable "bastion_remote_access_cidr_blocks" {
12-
description = "Allowed CIDR blocks for external SSH access to the Bastion instance"
12+
description = <<-EOT
13+
Allowed CIDR blocks for external SSH access to the Bastion instance.
14+
If null, then access to the Bastion instance is prevented.
15+
EOT
1316
type = list(string)
14-
default = null
1517
}
1618

1719
variable "cluster_version" {

0 commit comments

Comments
 (0)