galaxy: Cross check the collection type (#81423)

* User-provided collection type might differ from collection
  source. Cross-check the type before proceeding

Fixes: #79463

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
pull/81440/head
Abhijeet Kasurde 10 months ago committed by GitHub
parent f894ce89b4
commit 95fdd555b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,3 @@
---
bugfixes:
- galaxy - cross check the collection type and collection source (https://github.com/ansible/ansible/issues/79463).

@ -140,7 +140,7 @@ class ConcreteArtifactsManager:
url, sha256_hash, token = self._galaxy_collection_cache[collection]
except KeyError as key_err:
raise RuntimeError(
'The is no known source for {coll!s}'.
'There is no known source for {coll!s}'.
format(coll=collection),
) from key_err
@ -702,6 +702,11 @@ def _get_meta_from_installed_dir(
def _get_meta_from_tar(
b_path, # type: bytes
): # type: (...) -> dict[str, t.Union[str, list[str], dict[str, str], None, t.Type[Sentinel]]]
if not os.path.exists(b_path):
raise AnsibleError(
f"Unable to find collection artifact file at '{to_native(b_path)}'."
)
if not tarfile.is_tarfile(b_path):
raise AnsibleError(
"Collection artifact at '{path!s}' is not a valid tar file.".

@ -298,6 +298,27 @@ def test_build_requirement_from_tar(collection_artifact):
assert actual.ver == u'0.1.0'
def test_build_requirement_from_tar_url(tmp_path_factory):
test_dir = to_bytes(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Collections Input'))
concrete_artifact_cm = collection.concrete_artifact_manager.ConcreteArtifactsManager(test_dir, validate_certs=False)
test_url = 'https://example.com/org/repo/sample.tar.gz'
expected = fr"^Failed to download collection tar from '{to_text(test_url)}'"
with pytest.raises(AnsibleError, match=expected):
Requirement.from_requirement_dict({'name': test_url, 'type': 'url'}, concrete_artifact_cm)
def test_build_requirement_from_tar_url_wrong_type(tmp_path_factory):
test_dir = to_bytes(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Collections Input'))
concrete_artifact_cm = collection.concrete_artifact_manager.ConcreteArtifactsManager(test_dir, validate_certs=False)
test_url = 'https://example.com/org/repo/sample.tar.gz'
expected = fr"^Unable to find collection artifact file at '{to_text(test_url)}'\.$"
with pytest.raises(AnsibleError, match=expected):
# Specified wrong collection type for http URL
Requirement.from_requirement_dict({'name': test_url, 'type': 'file'}, concrete_artifact_cm)
def test_build_requirement_from_tar_fail_not_tar(tmp_path_factory):
test_dir = to_bytes(tmp_path_factory.mktemp('test-ÅÑŚÌβŁÈ Collections Input'))
test_file = os.path.join(test_dir, b'fake.tar.gz')

Loading…
Cancel
Save