aws / amazon-ssm-agent

An agent to enable remote management of your EC2 instances, on-premises servers, or virtual machines (VMs).
https://aws.amazon.com/systems-manager/
Apache License 2.0
1.03k stars 323 forks source link

Dynamically get network interface name in domainjoin_unix_script.go #469

Open DanielTeraSky opened 1 year ago

DanielTeraSky commented 1 year ago

As part of the domain join script for Unix, there is a line where we get the interface IP address, but the interface name is hard-coded (eth0) as you can see here: https://github.com/aws/amazon-ssm-agent/blob/baf58f0dedc4dc84d719101259cea5ed73c9f842/agent/plugins/domainjoin/domainjoin_unix_script.go#L555

Although this works for t2 instance types, it doesn't work for t3and t3a since the network interface name has changed to ensX.

I suggest to change this line to dynamically fetch the the IP without specifying the interface name. There are 2 options to do this:

# Option 1:
ip_address="$(hostname -I)"

# Option 2:
ip_address="$(ip -o -4 addr show $(ip route list | grep default | grep -E  'dev (\w+)' -o | awk '{print $2}') | awk '{print $4}' | cut -d/ -f1)"

This is an important fix as there is currently an issue with this script when running on newer machine types

smhmhmd commented 1 year ago

Thanks for the feedback @DanielTeraSky , the second option looks more portable. Could you submit a pull-request ?

smhmhmd commented 1 year ago

When you submit the PR, also mention what testing you have done.

jaseblenner commented 1 year ago

Thanks for opening this issue @DanielTeraSky - the interface name (eth0) is also hardcoded at line #608 this should be set dynamically also.

A potential solution may be setting an $IF_NAME or similar var early on in the execution. Something like this may work:

"IF_NAME=$(ip -br l | awk '$1 !~ "lo|vir|wl" { print $1}')"

I have come up with a hacky workaround for my builds in the interim but unfortunately my bash skills aren't up to par to submit a PR for this.

edit: disregard, i see your PR has now been merged. Opened PR https://github.com/aws/amazon-ssm-agent/pull/499 as a follow up

smhmhmd commented 1 year ago

The PR has been pushed, it may take sometime to deploy to all regions, please keep your agent uptodate.

https://github.com/aws/amazon-ssm-agent/blob/mainline/agent/plugins/domainjoin/domainjoin_unix_script.go#L556

smhmhmd commented 1 year ago

@jaseblenner,

You are right, for Ubuntu, there is one more spot where eth0 has not been fixed. Thanks for opening PR https://github.com/aws/amazon-ssm-agent/pull/499 , I will incorporate it