Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug | Leverage crashes on malformed HCL in config.tf and locals.tf #294

Open
angelofenoglio opened this issue Oct 31, 2024 · 0 comments
Open
Assignees
Labels
bug Something isn't working

Comments

@angelofenoglio
Copy link
Contributor

Describe the Bug

Leverage panics when running Terraform commands on layers were the config.tf or the locals.tf files contain malformed HCL.

It displays a message like:

Traceback (most recent call last):
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 59, in get_action
    return states[state][token.type]
           ~~~~~~~~~~~~~^^^^^^^^^^^^
KeyError: '$END'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/linuxbrew/.linuxbrew/bin/leverage", line 8, in <module>
    sys.exit(leverage())
             ^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/click/core.py", line 1137, in __call__
    return self.main(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/click/core.py", line 1062, in main
    rv = self.invoke(ctx)
         ^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/click/core.py", line 1668, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/click/core.py", line 1668, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/click/core.py", line 1404, in invoke
    return ctx.invoke(self.callback, **ctx.params)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/click/core.py", line 763, in invoke
    return __callback(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/leverage/_internals.py", line 50, in new_command
    return command(ctx.obj.container, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/click/decorators.py", line 26, in new_func
    return f(get_current_context(), *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/leverage/modules/terraform.py", line 95, in plan
    invoke_for_all_commands(layers, _plan, args)
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/leverage/_internals.py", line 50, in new_command
    return command(ctx.obj.container, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/leverage/modules/terraform.py", line 271, in invoke_for_all_commands
    command(args=args)
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/leverage/_internals.py", line 50, in new_command
    return command(ctx.obj.container, *args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/leverage/modules/terraform.py", line 325, in _plan
    exit_code = tf.start_in_layer("plan", *tf.tf_default_args, *args)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/leverage/container.py", line 605, in start_in_layer
    return self.start(command, *arguments)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/leverage/container.py", line 598, in start
    with AwsCredsEntryPoint(self, self.entrypoint):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/leverage/_utils.py", line 87, in __init__
    auth_method = container.auth_method()
                  ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/leverage/container.py", line 511, in auth_method
    refresh_layer_credentials(self)
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/leverage/modules/auth.py", line 86, in refresh_layer_credentials
    tf_profile, raw_profiles = get_profiles(cli)
                               ^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/leverage/modules/auth.py", line 69, in get_profiles
    tf_config = hcl2.load(tf_file)
                ^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/hcl2/api.py", line 9, in load
    return loads(file.read())
           ^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/hcl2/api.py", line 18, in loads
    return hcl2.parse(text + "\n")
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/lark/lark.py", line 464, in parse
    return self.parser.parse(text, start=start)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/lark/parser_frontends.py", line 115, in parse
    return self._parse(token_stream, start)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/lark/parser_frontends.py", line 63, in _parse
    return self.parser.parse(input, start, *args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 35, in parse
    return self.parser.parse(*args)
           ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 111, in parse
    _action, arg = get_action(token)
                   ^^^^^^^^^^^^^^^^^
  File "/home/linuxbrew/.linuxbrew/opt/python@3.11/lib/python3.11/site-packages/lark/parsers/lalr_parser.py", line 66, in get_action
    raise UnexpectedToken(token, expected, state=state, puppet=puppet)
lark.exceptions.UnexpectedToken: Unexpected token Token('$END', '') at line 32, column 1.
Expected one of: 
        * RBRACE

In this case a right curly brace was missing in a locals.tf file.

Expected Behavior

Leverage recognizes the scenario and shows a informative error message.

Steps to Reproduce

Steps to reproduce the behavior:

  1. Go to a particular layer in a Ref Arch implementation
  2. Delete a curly brace form either locals.tf or config.tf
  3. Run a terraform command such as leverage tf init
  4. See error
@angelofenoglio angelofenoglio added the bug Something isn't working label Oct 31, 2024
@angelofenoglio angelofenoglio self-assigned this Oct 31, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant