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

Optionally enforce IMDSv2 on kitchen instances. #571

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,10 @@ driver:

elastic_network_interface_id's have a format like eni-0545666738adeed14. You can only attach the network interface to instances in the same Availability Zone.

#### `enforce_imdsv2`

Setting this boolean to `true` will enforce IMDSv2 on kitchen instances. See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/configuring-instance-metadata-service.html for more information on IMDS. Defaults to `false`

### Disk Configuration

#### <a name="config-block_device_mappings"></a> `block_device_mappings`
Expand Down
7 changes: 7 additions & 0 deletions lib/kitchen/driver/aws/instance_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,13 @@ def ec2_instance_data # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
config[:instance_initiated_shutdown_behavior].empty?
i[:instance_initiated_shutdown_behavior] = config[:instance_initiated_shutdown_behavior]
end
if config[:enforce_imdsv2]
i[:metadata_options] = {
http_endpoint: "enabled",
http_tokens: "required",
http_put_response_hop_limit: 1,
}
end
i
end

Expand Down
1 change: 1 addition & 0 deletions lib/kitchen/driver/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class Ec2 < Kitchen::Driver::Base
default_config :instance_initiated_shutdown_behavior, nil
default_config :ssl_verify_peer, true
default_config :skip_cost_warning, false
default_config :enforce_imdsv2, false

def initialize(*args, &block)
super
Expand Down
32 changes: 32 additions & 0 deletions spec/kitchen/driver/aws/instance_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -720,5 +720,37 @@
)
end
end

context "when enforcing IMDSv2" do
let(:config) do
{
region: "us-east-1",
instance_type: "micro",
ebs_optimized: true,
image_id: "ami-123",
subnet_id: "s-456",
private_ip_address: "0.0.0.0",
enforce_imdsv2: true,
}
end

it "returns the correct metadata" do
expect(generator.ec2_instance_data).to eq(
instance_type: "micro",
ebs_optimized: true,
image_id: "ami-123",
key_name: nil,
subnet_id: "s-456",
private_ip_address: "0.0.0.0",
metadata_options: {
http_endpoint: "enabled",
http_tokens: "required",
http_put_response_hop_limit: 1,
},
max_count: 1,
min_count: 1
)
end
end
end
end