Skip to content

aws-cli fails to parse kubeconfig #7966

@mvgmb

Description

@mvgmb

Describe the bug

When running aws-cli with the ruamel-yaml lib (0.17.29+), it fails to parse the kubeconfig file when executing eks update-kubeconfig more than once.

Expected Behavior

Expected to update the kubeconfig with the new cluster configuration.

Current Behavior

$ rm /root/.kube/config
$ aws eks update-kubeconfig --name cluster-a
Updated context cluster-a in /root/.kube/config
$ aws --debug eks update-kubeconfig --name cluster-b
2023-06-09 23:31:26,938 - MainThread - awscli.customizations.eks.update_kubeconfig - WARNING - Passing /root/.kube/config:preferences is wrong type: <class 'dict'> (Should be <class 'collections.OrderedDict'>)
2023-06-09 23:31:26,941 - MainThread - awscli.clidriver - DEBUG - Exception caught in main()
Traceback (most recent call last):
  File "/usr/lib/python3.11/site-packages/awscli/clidriver.py", line 460, in main
    return command_table[parsed_args.command](remaining, parsed_args)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/awscli/clidriver.py", line 595, in __call__
    return command_table[parsed_args.operation](remaining, parsed_globals)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/awscli/customizations/commands.py", line 205, in __call__
    rc = self._run_main(parsed_args, parsed_globals)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/awscli/customizations/eks/update_kubeconfig.py", line 134, in _run_main
    config = config_selector.choose_kubeconfig(
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/awscli/customizations/eks/update_kubeconfig.py", line 232, in choose_kubeconfig
    return self._loader.load_kubeconfig(self._paths[0])
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/awscli/customizations/eks/kubeconfig.py", line 166, in load_kubeconfig
    self._validator.validate_config(loaded_config)
  File "/usr/lib/python3.11/site-packages/awscli/customizations/eks/kubeconfig.py", line 90, in validate_config
    self._validate_config_types(config)
  File "/usr/lib/python3.11/site-packages/awscli/customizations/eks/kubeconfig.py", line 107, in _validate_config_types
    raise KubeconfigCorruptedError(
awscli.customizations.eks.kubeconfig.KubeconfigCorruptedError: preferences is wrong type: <class 'dict'> (Should be <class 'collections.OrderedDict'>)

preferences is wrong type: <class 'dict'> (Should be <class 'collections.OrderedDict'>)

Reproduction Steps

$ docker run -v $(pwd):/root -it alpine:3.18 sh
# apk add aws-cli
# aws eks update-kubeconfig --name cluster-a
Updated context cluster-a in /root/.kube/config
# aws eks update-kubeconfig --name cluster-b
preferences is wrong type: <class 'dict'> (Should be <class 'collections.OrderedDict'>)

Possible Solution

The ruamel.yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG value used to be a string, but it was changed to a Tag struct: https://sourceforge.net/p/ruamel-yaml/tickets/467/

This broke the YAML parser the aws-cli uses to read the kubeconfig file:

def _ordered_representer(dumper, data):
return dumper.represent_mapping(
ruamel.yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
data.items())
def ordered_yaml_load(stream):
""" Load an OrderedDict object from a yaml stream."""
yaml = ruamel.yaml.YAML(typ="safe", pure=True)
yaml.Constructor.add_constructor(
ruamel.yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG,
_ordered_constructor)
return yaml.load(stream)

This problem can be solved by casting the DEFAULT_MAPPING_TAG to a string, as suggested by this ticket:

- yaml.Constructor.add_constructor(ruamel.yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG, _ordered_constructor)
+ yaml.Constructor.add_constructor(str(ruamel.yaml.resolver.BaseResolver.DEFAULT_MAPPING_TAG), _ordered_constructor) 

Additional Information/Context

No response

CLI version used

aws-cli/2.11.25 Python/3.11.4 Linux/5.4.231-137.341.amzn2.x86_64 source/x86_64.alpine.3 prompt/off

Environment details (OS name and version, etc.)

alpine:3.18

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.dependenciesThis issue is a problem in a dependency.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions