canonical / canonical/craft-application

Launchpad: Handle invalid credentials files better

Open
#241 1 comment 0 reactions 0 assignees View on GitHub
Status: Triaged Type: Enhancement
Dominant language
Python
Stars
14
Forks
29
Avg merge
2d 3h
Merged PRs (30d)
12

Description

### What needs to get done

If `Launchpad.login` errors out due to `configparser` issues, try deleting the credentials file and logging in again.

### Why it needs to get done

This isn't an acceptable error message:

```
+ charmcraft remote-build --verbose --launchpad-accept-public-upload
Starting charmcraft, version 2.5.5.post102+g0f95a52
Logging execution to '/root/.local/state/charmcraft/log/charmcraft-20240226-125356.635385.log'
remote-build is experimental and is subject to change. Use with caution.
charmcraft internal error: No option 'consumer_key' in section: '1'
Full execution log: '/root/.local/state/charmcraft/log/charmcraft-20240226-125356.635385.log'
Traceback (most recent call last):
File "/snap/charmcraft/x1/usr/lib/python3.10/configparser.py", line 791, in get
value = d[option]
File "/snap/charmcraft/x1/usr/lib/python3.10/collections/__init__.py", line 986, in __getitem__
return self.__missing__(key) # support subclasses that define __missing__
File "/snap/charmcraft/x1/usr/lib/python3.10/collections/__init__.py", line 978, in __missing__
raise KeyError(key)
KeyError: 'consumer_key'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/snap/charmcraft/x1/bin/charmcraft", line 8, in
sys.exit(main())
File "/snap/charmcraft/x1/lib/python3.10/site-packages/charmcraft/application/main.py", line 150, in main
return app.run()
File "/snap/charmcraft/x1/lib/python3.10/site-packages/craft_application/application.py", line 465, in run
return_code = dispatcher.run() or 0
File "/snap/charmcraft/x1/lib/python3.10/site-packages/craft_cli/dispatcher.py", line 487, in run
return self._loaded_command.run(self._parsed_command_args)
File "/snap/charmcraft/x1/lib/python3.10/site-packages/craft_application/commands/base.py", line 171, in run
result = self._run(parsed_args, **kwargs) or result
File "/snap/charmcraft/x1/lib/python3.10/site-packages/charmcraft/application/commands/remote.py", line 110, in _run
builder = self._services.remote_build
File "/snap/charmcraft/x1/lib/python3.10/site-packages/craft_application/services/service_factory.py", line 95, in __getattr__
instance.setup()
File "/snap/charmcraft/x1/lib/python3.10/site-packages/craft_application/services/remotebuild.py", line 69, in setup
self.lp = self._get_lp_client()
File "/snap/charmcraft/x1/lib/python3.10/site-packages/craft_application/services/remotebuild.py", line 202, in _get_lp_client
return launchpad.Launchpad.login(
File "/snap/charmcraft/x1/lib/python3.10/site-packages/craft_application/launchpad/launchpad.py", line 98, in login
launchpadlib.launchpad.Launchpad.login_with(
File "/snap/charmcraft/x1/lib/python3.10/site-packages/launchpadlib/launchpad.py", line 700, in login_with
return cls._authorize_token_and_login(
File "/snap/charmcraft/x1/lib/python3.10/site-packages/launchpadlib/launchpad.py", line 445, in _authorize_token_and_login
cached_credentials = credential_store.load(
File "/snap/charmcraft/x1/lib/python3.10/site-packages/launchpadlib/credentials.py", line 345, in load
return self.do_load(unique_key)
File "/snap/charmcraft/x1/lib/python3.10/site-packages/launchpadlib/credentials.py", line 486, in do_load
return Credentials.load_from_path(self.filename)
File "/snap/charmcraft/x1/lib/python3.10/site-packages/lazr/restfulclient/authorize/oauth.py", line 229, in load_from_path
credentials.load(credentials_file)
File "/snap/charmcraft/x1/lib/python3.10/site-packages/lazr/restfulclient/authorize/oauth.py", line 205, in load
consumer_key = parser.get(CREDENTIALS_FILE_VERSION, "consumer_key")
File "/snap/charmcraft/x1/usr/lib/python3.10/configparser.py", line 794, in get
raise NoOptionError(option, section)
configparser.NoOptionError: No option 'consumer_key' in section: '1'
-----
.
2024-02-26 12:53:56 Restoring multipass:ubuntu-22.04-64:tests/spread/smoketests/remote-build:jammy (multipass:ubuntu-22.04-64)...
```

Contributor guide

Open the contributing guide

Research direction

Start in craft_application/launchpad/launchpad.py at Launchpad.login and inspect its call from craft_application/services/remotebuild.py:_get_lp_client. Reproduce the configparser failure with an invalid credentials file, then run the relevant existing tests if available. Done means this failure removes the invalid credentials and retries login instead of exposing the configparser traceback.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
authentication, cli
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.