canonical / charmcraft

Collaborate, build and publish charmed operators for Kubernetes, Linux and Windows.
Apache License 2.0
64 stars 66 forks source link

`fetch-libs` command requires being logged into charmhub #1671

Open lengau opened 2 months ago

lengau commented 2 months ago

Bug Description

The fetch-libs command uses a logged in charmhub session, though it doesn't need to.

To Reproduce

  1. Create a fresh VM/container
  2. Add one or more libraries to a new charm
  3. Run charmcraft fetch-libs

Expected:

Libraries are collected

Actual:

Requests login

Environment

n/a

charmcraft.yaml

name: cc-1654

type: charm

bases:
  - build-on:
    - name: ubuntu
      channel: "22.04"
    run-on:
    - name: ubuntu
      channel: "22.04"

# (Required)
summary: A very short one-line summary of the flask application.

# (Required)
description: |
  A comprehensive overview of your Flask application.
]
charm-libs:
  - lib: mysql.mysql
    version: 0

Relevant log output

2024-05-07 10:17:58.552 Getting library metadata from charmhub
2024-05-07 10:17:58.552 Retrieving credentials for 'charmcraft' on 'api.charmhub.io' from keyring 'SecretService Keyring'.
2024-05-07 10:17:58.553 Credentials not found in the keyring 'SecretService Keyring'
2024-05-07 10:17:58.553 charmcraft internal error: CredentialsUnavailable("No credentials found for 'charmcraft' on 'api.charmhub.io'.")
2024-05-07 10:17:58.554 Traceback (most recent call last):
2024-05-07 10:17:58.554   File "/snap/charmcraft/3643/lib/python3.10/site-packages/craft_application/application.py", line 510, in run
2024-05-07 10:17:58.554     return_code = dispatcher.run() or 0
2024-05-07 10:17:58.554   File "/snap/charmcraft/3643/lib/python3.10/site-packages/craft_cli/dispatcher.py", line 487, in run
2024-05-07 10:17:58.554     return self._loaded_command.run(self._parsed_command_args)
2024-05-07 10:17:58.554   File "/snap/charmcraft/3643/lib/python3.10/site-packages/charmcraft/application/commands/store.py", line 1676, in run
2024-05-07 10:17:58.554     libs_metadata = store.get_libraries_metadata_by_name(charm_libs)
2024-05-07 10:17:58.554   File "/snap/charmcraft/3643/lib/python3.10/site-packages/charmcraft/services/store.py", line 220, in get_libraries_metadata_by_name
2024-05-07 10:17:58.554     for lib in self.get_libraries_metadata(libraries)
2024-05-07 10:17:58.554   File "/snap/charmcraft/3643/lib/python3.10/site-packages/charmcraft/services/store.py", line 212, in get_libraries_metadata
2024-05-07 10:17:58.554     return self.client.fetch_libraries_metadata(store_libs)
2024-05-07 10:17:58.554   File "/snap/charmcraft/3643/lib/python3.10/site-packages/charmcraft/store/client.py", line 202, in fetch_libraries_metadata
2024-05-07 10:17:58.554     response = self.request_urlpath_json("POST", "/v1/charm/libraries/bulk", json=libs)
2024-05-07 10:17:58.554   File "/snap/charmcraft/3643/lib/python3.10/site-packages/charmcraft/store/client.py", line 134, in request_urlpath_json
2024-05-07 10:17:58.554     response = super().request(method, self.api_base_url + urlpath, *args, **kwargs)
2024-05-07 10:17:58.554   File "/snap/charmcraft/3643/lib/python3.10/site-packages/craft_store/base_client.py", line 184, in request
2024-05-07 10:17:58.554     headers["Authorization"] = self._get_authorization_header()
2024-05-07 10:17:58.554   File "/snap/charmcraft/3643/lib/python3.10/site-packages/craft_store/store_client.py", line 108, in _get_authorization_header
2024-05-07 10:17:58.554     auth = creds.unmarshal_candid_credentials(self._auth.get_credentials())
2024-05-07 10:17:58.554   File "/snap/charmcraft/3643/lib/python3.10/site-packages/craft_store/auth.py", line 277, in get_credentials
2024-05-07 10:17:58.554     raise errors.CredentialsUnavailable(self.application_name, self.host)
2024-05-07 10:17:58.554 craft_store.errors.CredentialsUnavailable: No credentials found for 'charmcraft' on 'api.charmhub.io'.
syncronize-issues-to-jira[bot] commented 2 months ago

Thank you for reporting us your feedback!

The internal ticket has been created: https://warthogs.atlassian.net/browse/CRAFT-2878.

This message was autogenerated

gregory-schiano commented 2 months ago

While investigating this issue I also found out that the validation on the charm name in the libs, is in contradiction with what the charmhub API expects.

If I put the following in my charmcraft.yaml

charm-libs:
  - lib: traefik-k8s.ingress
    version: "2"

I get the following error:

- Invalid charm name in lib 'traefik-k8s.ingress'. Try replacing hyphens ('-') with underscores ('_'). (in field 'charm-libs[0].lib')

But if I use underscore instead the charmhub API doesn't recognize the charm-name

❯ curl -X POST 'https://api.charmhub.io/v1/charm/libraries/bulk' -H "Content-Type: application/json" --data '[{"charm-name": "traefik_k8s", "library-name": "ingress", "api": 2}]'
{"error-list":[{"code":null,"message":"Items need to include 'library_id' or 'package_id'"}]}

While using an hyphen:

❯ curl -X POST 'https://api.charmhub.io/v1/charm/libraries/bulk' -H "Content-Type: application/json" --data '[{"charm-name": "traefik-k8s", "library-name": "ingress", "api": 2}]'
{"libraries":[{"api":2,"charm-name":"traefik-k8s","created-at":"2024-05-02T11:09:33.567275","hash":"7410561327d3b8867651b6d664b7a5aab3a0f00aebac81a36911ec8dc11eb96d","library-id":"e6de2a5cd5b34422a204668f3b8f90d2","library-name":"ingress","patch":13}]}