Error for context-dependent redirects (#78755)

pull/78764/head
Sloane Hertel 2 years ago committed by GitHub
parent 86ab63f281
commit 563f3ecc11
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -0,0 +1,2 @@
bugfixes:
- Error for collection redirects that do not use fully qualified collection names, as the redirect would be determined by the ``collections`` keyword.

@ -502,6 +502,13 @@ class PluginLoader:
redirect = routing_metadata.get('redirect', None) redirect = routing_metadata.get('redirect', None)
if redirect: if redirect:
# Prevent mystery redirects that would be determined by the collections keyword
if not AnsibleCollectionRef.is_valid_fqcr(redirect):
raise AnsibleError(
f"Collection {acr.collection} contains invalid redirect for {fq_name}: {redirect}. "
"Redirects must use fully qualified collection names."
)
# FIXME: remove once this is covered in debug or whatever # FIXME: remove once this is covered in debug or whatever
display.vv("redirecting (type: {0}) {1} to {2}".format(plugin_type, fq_name, redirect)) display.vv("redirecting (type: {0}) {1} to {2}".format(plugin_type, fq_name, redirect))
# The name doing the redirection is added at the beginning of _resolve_plugin_step, # The name doing the redirection is added at the beginning of _resolve_plugin_step,
@ -1171,6 +1178,12 @@ class Jinja2Loader(PluginLoader):
# check redirects # check redirects
redirect = routing_entry.get('redirect', None) redirect = routing_entry.get('redirect', None)
if redirect: if redirect:
if not AnsibleCollectionRef.is_valid_fqcr(redirect):
raise AnsibleError(
f"Collection {acr.collection} contains invalid redirect for {acr.collection}.{acr.resource}: {redirect}. "
"Redirects must use fully qualified collection names."
)
next_key, leaf_key = get_fqcr_and_name(redirect, collection=acr.collection) next_key, leaf_key = get_fqcr_and_name(redirect, collection=acr.collection)
display.vvv('redirecting (type: {0}) {1}.{2} to {3}'.format(self.type, acr.collection, acr.resource, next_key)) display.vvv('redirecting (type: {0}) {1}.{2} to {3}'.format(self.type, acr.collection, acr.resource, next_key))
key = next_key key = next_key

@ -8,7 +8,7 @@ plugin_routing:
deprecation: deprecation:
warning_text: deprecation1 warning_text: deprecation1
redirect_filter1: redirect_filter1:
redirect: redirect_filter2 redirect: testns.testredirect.redirect_filter2
deprecation: deprecation:
warning_text: deprecation2 warning_text: deprecation2
redirect_filter2: redirect_filter2:
@ -16,6 +16,8 @@ plugin_routing:
deprecation: deprecation:
warning_text: deprecation3 warning_text: deprecation3
dead_end: dead_end:
redirect: bad_redirect redirect: testns.testredirect.bad_redirect
recursive_redirect: recursive_redirect:
redirect: recursive_redirect redirect: testns.testredirect.recursive_redirect
invalid_redirect:
redirect: contextual_redirect

@ -40,6 +40,16 @@
that: that:
- redirect_failure is failed - redirect_failure is failed
- '"recursive collection redirect found for ''testns.testredirect.recursive_redirect''" in redirect_failure.msg' - '"recursive collection redirect found for ''testns.testredirect.recursive_redirect''" in redirect_failure.msg'
# invalid filter redirect
- debug: msg="{{ 'data' | testns.testredirect.invalid_redirect }}"
ignore_errors: yes
register: redirect_failure
- assert:
that:
- redirect_failure is failed
- error in redirect_failure.msg
vars:
error: "Collection testns.testredirect contains invalid redirect for testns.testredirect.invalid_redirect: contextual_redirect"
# legacy filter should mask redirected # legacy filter should mask redirected
- assert: - assert:
that: ('' | formerly_core_masked_filter) == 'hello from overridden formerly_core_masked_filter' that: ('' | formerly_core_masked_filter) == 'hello from overridden formerly_core_masked_filter'

Loading…
Cancel
Save