Skip to content

Commit 607e5f5

Browse files
authored
Support AWS Instance Metadata Service (IMDS) Versions 1 and 2 (#38)
1 parent 4034968 commit 607e5f5

File tree

7 files changed

+319
-18
lines changed

7 files changed

+319
-18
lines changed

.circleci/config.yml

+34-9
Original file line numberDiff line numberDiff line change
@@ -23,27 +23,49 @@ jobs:
2323
path: /tmp/logs
2424
- store_test_results:
2525
path: /tmp/logs
26-
bats_ubuntu1604:
26+
bats_ubuntu1604_imds_v1:
2727
# We need to run Docker Compose with privileged settings, which isn't supported by CircleCI's Docker executor, so
2828
# we have to use the machine executor instead.
2929
machine: true
3030
steps:
3131
- checkout
32-
- run: docker-compose up --exit-code-from bats_ubuntu1604 bats_ubuntu1604
33-
bats_ubuntu1804:
32+
- run: docker-compose up --exit-code-from bats_ubuntu1604_imds_v1 bats_ubuntu1604_imds_v1
33+
bats_ubuntu1604_imds_v2:
3434
# We need to run Docker Compose with privileged settings, which isn't supported by CircleCI's Docker executor, so
3535
# we have to use the machine executor instead.
3636
machine: true
3737
steps:
3838
- checkout
39-
- run: docker-compose up --exit-code-from bats_ubuntu1804 bats_ubuntu1804
40-
bats_ubuntu2004:
39+
- run: docker-compose up --exit-code-from bats_ubuntu1604_imds_v2 bats_ubuntu1604_imds_v2
40+
41+
bats_ubuntu1804_imds_v1:
42+
# We need to run Docker Compose with privileged settings, which isn't supported by CircleCI's Docker executor, so
43+
# we have to use the machine executor instead.
44+
machine: true
45+
steps:
46+
- checkout
47+
- run: docker-compose up --exit-code-from bats_ubuntu1804_imds_v1 bats_ubuntu1804_imds_v1
48+
bats_ubuntu1804_imds_v2:
49+
# We need to run Docker Compose with privileged settings, which isn't supported by CircleCI's Docker executor, so
50+
# we have to use the machine executor instead.
51+
machine: true
52+
steps:
53+
- checkout
54+
- run: docker-compose up --exit-code-from bats_ubuntu1804_imds_v2 bats_ubuntu1804_imds_v2
55+
bats_ubuntu2004_imds_v1:
56+
# We need to run Docker Compose with privileged settings, which isn't supported by CircleCI's Docker executor, so
57+
# we have to use the machine executor instead.
58+
machine: true
59+
steps:
60+
- checkout
61+
- run: docker-compose up --exit-code-from bats_ubuntu2004_imds_v1 bats_ubuntu2004_imds_v1
62+
bats_ubuntu2004_imds_v2:
4163
# We need to run Docker Compose with privileged settings, which isn't supported by CircleCI's Docker executor, so
4264
# we have to use the machine executor instead.
4365
machine: true
4466
steps:
4567
- checkout
46-
- run: docker-compose up --exit-code-from bats_ubuntu2004 bats_ubuntu2004
68+
- run: docker-compose up --exit-code-from bats_ubuntu2004_imds_v2 bats_ubuntu2004_imds_v2
4769

4870
workflows:
4971
version: 2
@@ -53,6 +75,9 @@ workflows:
5375
- integration_test:
5476
context:
5577
- Gruntwork Admin
56-
- bats_ubuntu1604
57-
- bats_ubuntu1804
58-
- bats_ubuntu2004
78+
- bats_ubuntu1604_imds_v1
79+
- bats_ubuntu1604_imds_v2
80+
- bats_ubuntu1804_imds_v1
81+
- bats_ubuntu1804_imds_v2
82+
- bats_ubuntu2004_imds_v1
83+
- bats_ubuntu2004_imds_v2

README.md

+11
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,17 @@ cp -r bash-commons/modules/bash-commons/src /opt/gruntwork/bash-commons
7171
sudo chown -R "my-os-username:my-os-group" /opt/gruntwork/bash-commons
7272
```
7373

74+
## Instance Metadata Service versions
75+
76+
`bash-commons` supports both Instance Metadata Service (IMDS) version 1 and 2. Gruntwork and AWS both recommend using version 2 of the Instance Metadata Service whenever possible. Although version 1 is still supported and considered fully secure by AWS, version 2 has been specially hardened against specific threat vectors and is therefore preferable.
77+
78+
To understand more about Instance Metadata Service version 2 and its features, read [the official AWS documentation on IMDSv2](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html).
79+
80+
There are two ways to specify the version of the Instance Metadata Service that `bash-commons` should use:
81+
82+
1. Set the environment variable `GRUNTWORK_BASH_COMMONS_IMDS_VERSION` to the version of IMDS that you wish to use. Valid values are either `1` or `2`.
83+
2. Change the value of `default_instance_metadata_version` to either `1` or `2`.
84+
7485
#### Example of `dynamic-ubuntu-wait.sh` usage:
7586

7687
You can use the `dynamic-ubuntu-wait.sh` command after you [install bash-commons](#install):

docker-compose.yml

+51-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: '3'
1+
version: "3"
22
services:
33
shellcheck:
44
build:
@@ -8,7 +8,7 @@ services:
88
- ./:/usr/local/src/bash-commons
99
working_dir: /usr/local/src/bash-commons/.circleci
1010
command: ./shellcheck.sh
11-
bats_ubuntu1604:
11+
bats_ubuntu1604_imds_v1:
1212
build:
1313
context: ./
1414
dockerfile: Dockerfile.ubuntu16.04.bats
@@ -19,7 +19,34 @@ services:
1919
command: bats test
2020
# Necessary so we can run a mock EC2 metadata service on port 80 on a special IP
2121
privileged: true
22-
bats_ubuntu1804:
22+
# We intentionally omit the GRUNTWORK_BASH_COMMONS_IMDS_VERSION env var here to ensure the default behavior works as expected
23+
bats_ubuntu1604_imds_v2:
24+
build:
25+
context: ./
26+
dockerfile: Dockerfile.ubuntu16.04.bats
27+
volumes:
28+
# Mount all the files so you have "hot reload" of all changes from the host
29+
- ./:/usr/local/src/bash-commons
30+
working_dir: /usr/local/src/bash-commons
31+
command: bats test
32+
# Necessary so we can run a mock EC2 metadata service on port 80 on a special IP
33+
privileged: true
34+
environment:
35+
# Signal to bash-commons module to use Instance Metadata Service version 2
36+
- GRUNTWORK_BASH_COMMONS_IMDS_VERSION=2
37+
bats_ubuntu1804_imds_v1:
38+
build:
39+
context: ./
40+
dockerfile: Dockerfile.ubuntu18.04.bats
41+
volumes:
42+
# Mount all the files so you have "hot reload" of all changes from the host
43+
- ./:/usr/local/src/bash-commons
44+
working_dir: /usr/local/src/bash-commons
45+
command: bats test
46+
# Necessary so we can run a mock EC2 metadata service on port 80 on a special IP
47+
privileged: true
48+
# We intentionally omit the GRUNTWORK_BASH_COMMONS_IMDS_VERSION env var here to ensure the default behavior works as intended
49+
bats_ubuntu1804_imds_v2:
2350
build:
2451
context: ./
2552
dockerfile: Dockerfile.ubuntu18.04.bats
@@ -30,7 +57,24 @@ services:
3057
command: bats test
3158
# Necessary so we can run a mock EC2 metadata service on port 80 on a special IP
3259
privileged: true
33-
bats_ubuntu2004:
60+
environment:
61+
# Signal to bash-commons module to use Instance Metadata Service version 2
62+
- GRUNTWORK_BASH_COMMONS_IMDS_VERSION=2
63+
bats_ubuntu2004_imds_v1:
64+
build:
65+
context: ./
66+
dockerfile: Dockerfile.ubuntu20.04.bats
67+
volumes:
68+
# Mount all the files so you have "hot reload" of all changes from the host
69+
- ./:/usr/local/src/bash-commons
70+
working_dir: /usr/local/src/bash-commons
71+
command: bats test
72+
# Necessary so we can run a mock EC2 metadata service on port 80 on a special IP
73+
privileged: true
74+
environment:
75+
# Signal to bash-commons module to use Instance Metadata Service version 1
76+
- GRUNTWORK_BASH_COMMONS_IMDS_VERSION=1
77+
bats_ubuntu2004_imds_v2:
3478
build:
3579
context: ./
3680
dockerfile: Dockerfile.ubuntu20.04.bats
@@ -41,3 +85,6 @@ services:
4185
command: bats test
4286
# Necessary so we can run a mock EC2 metadata service on port 80 on a special IP
4387
privileged: true
88+
environment:
89+
# Signal to bash-commons module to use Instance Metadata Service version 2
90+
- GRUNTWORK_BASH_COMMONS_IMDS_VERSION=2

0 commit comments

Comments
 (0)