Skyscanner / cfripper

Library and CLI tool for analysing CloudFormation templates and check them for security compliance.

Home Page:https://cfripper.readthedocs.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error while parsing template with pycfmodel

nafpliot opened this issue · comments

Hi guys. I get the following error when I ran the lambda

[ERROR] TypeError: ModelMetaclass object argument after ** must be a mapping, not str
Traceback (most recent call last):
  File "/var/task/cfripper/main.py", line 119, in handler
    cfmodel = pycfmodel.parse(template).resolve()
  File "/var/task/pycfmodel/__init__.py", line 19, in parse
    return CFModel(**template)

I've checked and it seems that the template is parsed indeed, but instead of a dict, it is a string. Do you have any clues why this might be happening?

Atm I am triggering the lambda with a test input like this

{
  "project": "test",
  "stack": {
    "name": "stack-name"
  },
  "region": "eu-west-1",
  "account": {
    "id": 1122334455
  }
}

Ok. I think i've found the problem.

In boto3_client.py the get_template method needs in return to pass the stack_content through convert_json_or_yaml_to_dict().

If you are ok with it I can create a PR later.

Absolutely, go for it.

👋 @nafpliot Thanks for your contribution!

That error is super weird 🤔
I have checked again the boto3 docs (https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/cloudformation.html#CloudFormation.Client.get_template) and TemplateBody (the values that we are using) is a python dict.
Were you able to check the content of template variable on line 119 before raising the error?

Hi @oscarbc96 . It is indeed a string. I think these explain why:
boto/boto3#1468
boto/botocore#1058

they don't explain however why after these years the documentation is still incorrect....

So maybe a check if the returned template is a json and if not use the conversion?

That sounds like a reasonable compromise.
We should make sure we add a comment linking to this issue in boto, just in case they end up fixing it.

👋 @nafpliot I created this PR #80 that should fix the problem. Let us know if you have any other problem. 🙂