Skip to content

Commit

Permalink
feat: AWS public IPv4 spec (#3741)
Browse files Browse the repository at this point in the history
Signed-off-by: Lukas Zapletal <[email protected]>
  • Loading branch information
lzap authored Apr 14, 2023
1 parent ed4b702 commit 2cbd09e
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 3 deletions.
60 changes: 58 additions & 2 deletions insights/parsers/aws_instance_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,18 @@
These parsers read the output of commands to collect identify information
from AWS instances.
* ``curl -s http://169.254.169.254/latest/dynamic/instance-identity/document`` and
* ``curl -s http://169.254.169.254/latest/dynamic/instance-identity/pkcs7``
AWSInstanceIdDoc - ``curl -s http://169.254.169.254/latest/dynamic/instance-identity/document``
-----------------------------------------------------------------------------------------------
AWSInstanceIdPkcs7 - ``curl -s http://169.254.169.254/latest/dynamic/instance-identity/pkcs7``
----------------------------------------------------------------------------------------------
AWSPublicIpv4Addresses - ``curl -s http://169.254.169.254/latest/meta-data/public-ipv4``
----------------------------------------------------------------------------------------
AWSPublicHostname ``curl -s http://169.254.169.254/latest/meta-data/public-hostname``
-------------------------------------------------------------------------------------
"""
from __future__ import print_function
import json
Expand Down Expand Up @@ -131,3 +141,49 @@ def parse_content(self, content):
startline += 1

self.signature = '-----BEGIN PKCS7-----\n' + '\n'.join([l.rstrip() for l in content[startline:]]) + "\n-----END PKCS7-----"


@parser(Specs.aws_public_ipv4_addresses)
class AWSPublicIpv4Addresses(CommandParser, list):
"""
Class for parsing the AWS public IP. At the moment, a single instance can only have assigned one public
IPv4 address on AWS EC2. This parsers takes the value and makes it a list just in case the capability
is added later. The data is fetched via
curl -s http://169.254.169.254/latest/meta-data/public-ipv4
command and contains a pure string, e.g. "1.2.3.4" without newline.
Raises:
SkipComponent: When content is empty or cannot be parsed.
"""

def parse_content(self, content):
"""Parse output of command."""
if not content or 'curl: ' in content[0]:
raise SkipComponent()

self.append(content[0])


@parser(Specs.aws_public_hostnames)
class AWSPublicHostnames(CommandParser, list):
"""
Class for parsing the AWS public hostname. At the moment, a single instance can only have assigned one public
IPv4 address on AWS EC2. This parsers takes the value and makes it a list just in case the capability
is added later. The data is fetched via
curl -s http://169.254.169.254/latest/meta-data/public-hostname
command and contains a pure string, e.g. "ec2-1-2-3-4.us-east-1.awscloud.com" without newline.
Raises:
SkipComponent: When content is empty or cannot be parsed.
"""

def parse_content(self, content):
"""Parse output of command."""
if not content or 'curl: ' in content[0]:
raise SkipComponent()

self.append(content[0])
2 changes: 2 additions & 0 deletions insights/specs/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class Specs(SpecSet):
avc_hash_stats = RegistryPoint()
aws_instance_id_doc = RegistryPoint()
aws_instance_id_pkcs7 = RegistryPoint()
aws_public_ipv4_addresses = RegistryPoint()
aws_public_hostnames = RegistryPoint()
awx_manage_check_license = RegistryPoint()
awx_manage_check_license_data = RegistryPoint(filterable=True)
awx_manage_print_settings = RegistryPoint()
Expand Down
2 changes: 2 additions & 0 deletions insights/specs/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ class DefaultSpecs(Specs):
auditd_conf = simple_file("/etc/audit/auditd.conf")
aws_instance_id_doc = command_with_args('/usr/bin/curl -s -H "X-aws-ec2-metadata-token: %s" http://169.254.169.254/latest/dynamic/instance-identity/document --connect-timeout 5', aws.aws_imdsv2_token, deps=[aws.aws_imdsv2_token])
aws_instance_id_pkcs7 = command_with_args('/usr/bin/curl -s -H "X-aws-ec2-metadata-token: %s" http://169.254.169.254/latest/dynamic/instance-identity/pkcs7 --connect-timeout 5', aws.aws_imdsv2_token, deps=[aws.aws_imdsv2_token])
aws_public_ipv4_addresses = command_with_args('/usr/bin/curl -s -H "X-aws-ec2-metadata-token: %s" http://169.254.169.254/latest/meta-data/public-ipv4 --connect-timeout 5', aws.aws_imdsv2_token, deps=[aws.aws_imdsv2_token])
aws_public_hostnames = command_with_args('/usr/bin/curl -s -H "X-aws-ec2-metadata-token: %s" http://169.254.169.254/latest/meta-data/public-hostname --connect-timeout 5', aws.aws_imdsv2_token, deps=[aws.aws_imdsv2_token])
awx_manage_check_license = simple_command("/usr/bin/awx-manage check_license")
awx_manage_check_license_data = awx_manage.awx_manage_check_license_data_datasource
awx_manage_print_settings = simple_command("/usr/bin/awx-manage print_settings INSIGHTS_TRACKING_STATE SYSTEM_UUID INSTALL_UUID TOWER_URL_BASE AWX_CLEANUP_PATHS AWX_PROOT_BASE_PATH LOG_AGGREGATOR_ENABLED LOG_AGGREGATOR_LEVEL --format json")
Expand Down
27 changes: 26 additions & 1 deletion insights/tests/parsers/test_aws_instance_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

from insights.core.exceptions import ParseException, SkipComponent
from insights.parsers import aws_instance_id
from insights.parsers.aws_instance_id import AWSInstanceIdDoc, AWSInstanceIdPkcs7
from insights.parsers.aws_instance_id import AWSInstanceIdDoc, AWSInstanceIdPkcs7,\
AWSPublicIpv4Addresses, AWSPublicHostnames
from insights.tests import context_wrap

AWS_CURL_ERROR = """
Expand Down Expand Up @@ -206,3 +207,27 @@ def test_doc_examples():
}
failed, total = doctest.testmod(aws_instance_id, globs=env)
assert failed == 0


def test_aws_public_ipv4_addresses():
with pytest.raises(SkipComponent):
AWSPublicIpv4Addresses(context_wrap(AWS_CURL_ERROR))

with pytest.raises(SkipComponent):
AWSPublicIpv4Addresses(context_wrap(""))

doc = AWSPublicIpv4Addresses(context_wrap("1.2.3.4"))
assert doc is not None
assert doc == ["1.2.3.4"]


def test_aws_public_hostnames():
with pytest.raises(SkipComponent):
AWSPublicHostnames(context_wrap(AWS_CURL_ERROR))

with pytest.raises(SkipComponent):
AWSPublicHostnames(context_wrap(""))

doc = AWSPublicHostnames(context_wrap("1.2.3.4"))
assert doc is not None
assert doc == ["1.2.3.4"]

0 comments on commit 2cbd09e

Please sign in to comment.