Skip to content

Commit

Permalink
Support querying instances by autoscaling group name (xen0l#212)
Browse files Browse the repository at this point in the history
  • Loading branch information
xen0l authored Dec 23, 2019
1 parent a38ad94 commit 3dbd573
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ aws-gate session Name:SSM-test
aws-gate session SSM-test
```

* autoscaling group name (uses tag identifier under the hood)
```
aws-gate session asg:dummy-v001
```

#### SSH ProxyCommand support

AWS SSM Session Manager supports tunneling SSH sessions over it. Moreover, _aws-gate_ supports generating ephemeral SSH
Expand Down
8 changes: 8 additions & 0 deletions aws_gate/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ def getinstanceidbyinstancename(name, ec2=None):
return getinstanceidbytag("Name:{}".format(name), ec2=ec2)


def getinstancebyautoscalinggroup(name, ec2=None):
_, asg_name = name.split(":")
return getinstanceidbytag("aws:autoscaling:groupName:{}".format(asg_name), ec2=ec2)


def query_instance(name, ec2=None):
if ec2 is None:
raise ValueError("EC2 client is not initialized")
Expand All @@ -82,6 +87,7 @@ def query_instance(name, ec2=None):
"private-ip-address": getinstanceidbyprivateipaddress,
"tag": getinstanceidbytag,
"name": getinstanceidbyinstancename,
"asg": getinstancebyautoscalinggroup,
}

# If we are provided with instance ID directly, we don't need to contact EC2
Expand All @@ -99,6 +105,8 @@ def query_instance(name, ec2=None):
identifier_type = "dns-name"
elif name.endswith("compute.internal"):
identifier_type = "private-dns-name"
elif name.startswith("asg:"):
identifier_type = "asg"
elif name.count(":") >= 1:
identifier_type = "tag"
else:
Expand Down
2 changes: 2 additions & 0 deletions tests/unit/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def test_query_aws_api_exception(mocker):
"Name:dummy-instance",
"aws:autoscaling:groupName:dummy-v001",
"dummy-instance",
"asg:dummy-v001",
],
ids=[
"private_ip_address",
Expand All @@ -39,6 +40,7 @@ def test_query_aws_api_exception(mocker):
"tag",
"tag (multiple colons)",
"name",
"asg",
],
)
def test_query_instance(name, instance_id, ec2):
Expand Down

0 comments on commit 3dbd573

Please sign in to comment.