Skip to content

Instance lookup by IP and hostname via private IP addresses on attached NIC#54

Merged
ncatelli merged 16 commits intodisneystreaming:developfrom
ncatelli:feature/instance-loopkup-by-nic-association
Sep 24, 2021
Merged

Instance lookup by IP and hostname via private IP addresses on attached NIC#54
ncatelli merged 16 commits intodisneystreaming:developfrom
ncatelli:feature/instance-loopkup-by-nic-association

Conversation

@ncatelli
Copy link
Copy Markdown

@ncatelli ncatelli commented Sep 1, 2021

General

This PR adds the capability to lookup an instance ID via the private address of an attached NIC.

Functionally this would be similar to the following bash one-liner

ssm session -i $(aws ec2 describe-network-interfaces --filters Name=addresses.private-ip-address,Values=<SOME PRIVATE IP> --query 'NetworkInterfaces[0].Attachment.InstanceId' | tr -d '"')

However this allows the added convenience of checking across multiple accounts, regions and handling the IP resolution.

Example commands

Session

ssm session -a 10.0.10.0
ssm session -a 10.0.10.0,10.0.10.1
ssm session -a some.internal.dns.record.example.com

Run

bash
ssm session -a 10.0.10.0 -c 'uname -a'

Changes

  • Adds a new flag --address/-a
  • Adds a new Resolver interface
  • Adds an --attribute flag for additional formatting data
    • VpcId

TODO

  • Documentation

@ncatelli ncatelli changed the title Instance Lookup By IP and Hostname via Private IP Addresses on Attached NIC Instance lookup by IP and hostname via private IP addresses on attached NIC Sep 1, 2021
@ncatelli
Copy link
Copy Markdown
Author

ncatelli commented Sep 2, 2021

This is ONLY implemented for the session subcommand currently. If the feature is agreeable, and it's desired, it would be fairly quick to port this over to the run subcommand as well.

@ncatelli
Copy link
Copy Markdown
Author

ncatelli commented Sep 7, 2021

Ported changes over to run sub-command.

}

// AddsAttributeFlag adds the --attribute flag to command.
func AddAttributeFlag(cmd *cobra.Command) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small addition to add the ability to include additional formatting flags at the selection level, to supplement the --tags flag.

ec2/helpers.go Outdated
}

// GetEC2InstanceAttributes accepts any number of instance strings and returns a populated InstanceAttributes{} object for each instance
func GetEC2InstanceAttributes(client ec2iface.EC2API, instances []*string) (ec2Attributes map[string]map[string]string, err error) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Retrieves instance attributes. Currently it only does VpcId. TBH i kinda hate this and am wondering if we should just transmute fields on the ec2.Instance struct into a map if the value is a non-compound type. i.e. strings or numeric types

for _, instance := range page.InstanceInformationList {
output = append(output, instance)
}
output = append(output, page.InstanceInformationList...)
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small lint fix

@ncatelli ncatelli requested a review from sendqueery September 14, 2021 18:34
}

func addInstanceInfo(instanceID *string, tags map[string]string, instancePool *instance.InstanceInfoSafe, profile string, region string) {
func addInstanceInfo(instanceID *string, ec2Instance ec2.Instance, instancePool *instance.InstanceInfoSafe, profile string, region string) {
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strips out the individual tags and attributes in favor of formatting to the struct from within this method.

@ncatelli
Copy link
Copy Markdown
Author

Got the signoff from @sendqueery , going to merge.

@ncatelli ncatelli merged commit d7241cb into disneystreaming:develop Sep 24, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant