Skip to content

Commit

Permalink
fix: invalid content of datasource aws_imdsv2_token (#4305)
Browse files Browse the repository at this point in the history
Signed-off-by: Xiaoxue Wang <[email protected]>
  • Loading branch information
JoySnow authored Dec 16, 2024
1 parent 08fd76a commit d324e9e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
13 changes: 10 additions & 3 deletions insights/specs/datasources/aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
from insights.core.spec_factory import simple_command
from insights.specs import Specs

_aws_imdsv2_token_invalid_keyworks = [
'warning: ',
]


class LocalSpecs(Specs):
""" Local specs used only by aws datasources """
Expand All @@ -34,9 +38,12 @@ 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)
token_lower = token.lower()
if token and not any(k in token_lower 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
8 changes: 8 additions & 0 deletions insights/tests/datasources/test_aws.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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)

0 comments on commit d324e9e

Please sign in to comment.