From acc6ab7ffe33f8557fe840928118c0f93a96caf8 Mon Sep 17 00:00:00 2001 From: Xiaoxue Wang Date: Thu, 12 Dec 2024 12:05:59 +0800 Subject: [PATCH] fix: invalid content of datasource aws_imdsv2_token Signed-off-by: Xiaoxue Wang --- insights/specs/datasources/aws.py | 13 ++++++++++--- insights/tests/datasources/test_aws.py | 8 ++++++++ 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/insights/specs/datasources/aws.py b/insights/specs/datasources/aws.py index b498ae6093..77c8436883 100644 --- a/insights/specs/datasources/aws.py +++ b/insights/specs/datasources/aws.py @@ -8,6 +8,11 @@ from insights.core.spec_factory import simple_command from insights.specs import Specs +_aws_imdsv2_token_invalid_keyworks = [ + 'Warning: ', + 'curlrc: ' +] + class LocalSpecs(Specs): """ Local specs used only by aws datasources """ @@ -34,9 +39,11 @@ def aws_imdsv2_token(broker): SkipComponent: When an error occurs or no token is generated """ try: - token = broker[LocalSpecs.aws_imdsv2_token].content[0].strip() - if token: - return str(token) + _token = broker[LocalSpecs.aws_imdsv2_token].content[0].strip() + if _token: + token = str(_token) + if token and not any(k in token for k in _aws_imdsv2_token_invalid_keyworks): + return token except Exception as e: raise SkipComponent("Unexpected exception:{e}".format(e=str(e))) raise SkipComponent diff --git a/insights/tests/datasources/test_aws.py b/insights/tests/datasources/test_aws.py index eeb46443e4..bca8c62018 100644 --- a/insights/tests/datasources/test_aws.py +++ b/insights/tests/datasources/test_aws.py @@ -8,6 +8,8 @@ TOKEN = "1234567890\n" +TOKEN_INVALID = "Warning: /root/.curlrc:1: warning: 'proxy' requires parameter" + def test_aws_imdsv2_token(): input_spec = Mock() @@ -30,3 +32,9 @@ def test_aws_imdsv2_token_exp(): broker = {LocalSpecs.aws_imdsv2_token: input_spec} with pytest.raises(SkipComponent) as ex: aws_imdsv2_token(broker) + + input_spec = Mock() + input_spec.content = [TOKEN_INVALID, ] + broker = {LocalSpecs.aws_imdsv2_token: input_spec} + with pytest.raises(SkipComponent) as ex: + aws_imdsv2_token(broker)