Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Security] Add support for IMDS V2 configuration #68

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ Only SSH access is allowed to the bastion host.
* `allowed_cidr` - A list of CIDR Networks to allow ssh access to. Defaults to "0.0.0.0/0"
* `allowed_ipv6_cidr` - A list of IPv6 CIDR Networks to allow ssh access to. Defaults to "::/0"
* `allowed_security_groups` - A list of Security Group ID's to allow access to the bastion host (useful if bastion is deployed internally) Defaults to empty list
* `extra_tags` - Optional a list of Key/Values Tags to be associated to the bastion host (see [Interpolated Tags](https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html))
* `extra_tags` - Optional a list of Key/Values Tags to be associated to the bastion host (see [Interpolated Tags](https://www.terraform.io/docs/providers/aws/r/autoscaling_group.html))
* enable_http_endpoint - Whether the metadata service is available.
* use_imds_v2 - Use (IMDSv2) Instance Metadata Service V2
* http_put_response_hop_limit - The desired HTTP PUT response hop limit for instance metadata requests. Can be an integer from 1 to 64.

## Outputs:

Expand Down Expand Up @@ -134,6 +137,9 @@ PS: In some cases you may consider adding flag `-A` to ssh command to enable for
| subnet\_ids | A list of subnet ids | list | `[]` | no |
| user\_data\_file | | string | `"user_data.sh"` | no |
| vpc\_id | | string | n/a | yes |
| enable_http_endpoint | Whether the metadata service is available | bool | `true` | no |
| use_imds_v2 | Use (IMDSv2) Instance Metadata Service V2 | bool | `false` | no |
| http_put_response_hop_limit | The desired HTTP PUT response hop limit for instance metadata requests. Can be an integer from 1 to 64. | number | `1` | no |

## Outputs

Expand Down
10 changes: 10 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ data "template_file" "user_data" {
// subnet_id = "${var.subnet_id}"
// vpc_security_group_ids = ["${aws_security_group.bastion.id}"]
// user_data = "${template_file.user_data.rendered}"
// http_endpoint = var.enable_http_endpoint ? "enabled" : "disabled"
// http_tokens = var.use_imds_v2 ? "required" : "optional"
// http_put_response_hop_limit = var.http_put_response_hop_limit
//
// count = 1
//
Expand Down Expand Up @@ -95,6 +98,13 @@ resource "aws_launch_configuration" "bastion" {
associate_public_ip_address = var.associate_public_ip_address
key_name = var.key_name

# Doc: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_configuration#metadata_options
metadata_options {
http_endpoint = var.enable_http_endpoint ? "enabled" : "disabled"
http_tokens = var.use_imds_v2 ? "required" : "optional"
http_put_response_hop_limit = var.http_put_response_hop_limit
}

lifecycle {
create_before_destroy = true
}
Expand Down
17 changes: 17 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,20 @@ variable "apply_changes_immediately" {
default = false
}

variable "enable_http_endpoint" {
description = "Whether the metadata service is available."
type = bool
default = true
}

variable "use_imds_v2" {
description = "Use (IMDSv2) Instance Metadata Service V2"
type = bool
default = false
}

variable "http_put_response_hop_limit" {
description = "The desired HTTP PUT response hop limit for instance metadata requests."
type = number
default = 1
}