aws / git-remote-codecommit

An implementation of Git Remote Helper that makes it easier to interact with AWS CodeCommit
Apache License 2.0
272 stars 39 forks source link

Error: json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0) #42

Open john-aws opened 1 year ago

john-aws commented 1 year ago

If your underlying credentials profile uses a CLI credential provider and that CLI request fails for some reason, git commands will fail with an unobvious error:

me:src$ git pull
Traceback (most recent call last):
  File "/usr/local/bin/git-remote-codecommit", line 8, in <module>
    sys.exit(main())
  File "/usr/local/lib/python3.9/site-packages/git_remote_codecommit/__init__.py", line 176, in main
    context = Context.from_url(remote_url)
  File "/usr/local/lib/python3.9/site-packages/git_remote_codecommit/__init__.py", line 148, in from_url
    credentials = session.get_credentials()
  File "/Users/me/Library/Python/3.9/lib/python/site-packages/botocore/session.py", line 430, in get_credentials
    self._credentials = self._components.get_component(
  File "/Users/me/Library/Python/3.9/lib/python/site-packages/botocore/credentials.py", line 1962, in load_credentials
    creds = provider.load()
  File "/Users/me/Library/Python/3.9/lib/python/site-packages/botocore/credentials.py", line 948, in load
    creds_dict = self._retrieve_credentials_using(credential_process)
  File "/Users/me/Library/Python/3.9/lib/python/site-packages/botocore/credentials.py", line 974, in _retrieve_credentials_using
    parsed = botocore.compat.json.loads(stdout.decode('utf-8'))
  File "/usr/local/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/__init__.py", line 346, in loads
    return _default_decoder.decode(s)
  File "/usr/local/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/local/Cellar/python@3.9/3.9.16/Frameworks/Python.framework/Versions/3.9/lib/python3.9/json/decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)

The issue seems to be that the underlying CLI credential provider is expected to return JSON of the form:

{
  "Version": 1,
  "AccessKeyId": "ASIAxxxxxxx",
  "SecretAccessKey": "xxxxx",
  "SessionToken": "xxxxx",
  "Expiration": "2023-03-23T01:02:03Z"
}

If, instead, it fails and returns something else that is not JSON, then git-remote-codecommit fails to decode the not-JSON and throws an ugly error message that does not give the user a good idea of what has gone wrong.

Wonder if the git-remote-codecommit helper could catch these JSON decode errors and do something more meaningful to the user e.g. simply surface the underlying credentials provider response verbatim. Thanks.