Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions awx/main/models/credential.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
)
from awx.main.models import Team, Organization
from awx.main.utils import encrypt_field
from awx_plugins.credentials import injectors as builtin_injectors

# DAB
from ansible_base.resource_registry.tasks.sync import get_resource_server_client
Expand Down Expand Up @@ -438,6 +437,7 @@ class Meta:
default=dict,
help_text=_('Enter injectors using either JSON or YAML syntax. Refer to the documentation for example syntax.'),
)
custom_injectors = None

@classmethod
def from_db(cls, db, field_names, values):
Expand All @@ -446,6 +446,7 @@ def from_db(cls, db, field_names, values):
native = ManagedCredentialType.registry[instance.namespace]
instance.inputs = native.inputs
instance.injectors = native.injectors
instance.custom_injectors = native.custom_injectors
return instance

def get_absolute_url(self, request=None):
Expand Down Expand Up @@ -547,6 +548,7 @@ def setup_tower_managed_defaults(cls, apps: Apps = None, app_config: AppConfig =

@classmethod
def load_plugin(cls, ns, plugin):
# TODO: User "side-loaded" credential custom_injectors isn't supported
ManagedCredentialType(namespace=ns, name=plugin.name, kind='external', inputs=plugin.inputs)

def inject_credential(self, credential, env, safe_env, args, private_data_dir):
Expand Down Expand Up @@ -575,9 +577,9 @@ def inject_credential(self, credential, env, safe_env, args, private_data_dir):
files)
"""
if not self.injectors:
if self.managed and credential.credential_type.namespace in dir(builtin_injectors):
if self.managed and credential.credential_type.custom_injectors:
injected_env = {}
getattr(builtin_injectors, credential.credential_type.namespace)(credential, injected_env, private_data_dir)
credential.credential_type.custom_injectors(credential, injected_env, private_data_dir)
env.update(injected_env)
safe_env.update(build_safe_env(injected_env))
return
Expand Down Expand Up @@ -686,6 +688,7 @@ def __init__(self, namespace, **kwargs):
for k in ('inputs', 'injectors'):
if k not in kwargs:
kwargs[k] = {}
kwargs.setdefault('custom_injectors', None)
super(ManagedCredentialType, self).__init__(namespace=namespace, **kwargs)
if namespace in ManagedCredentialType.registry:
raise ValueError(
Expand All @@ -706,7 +709,9 @@ def get_creation_params(self):
)

def create(self):
return CredentialType(**self.get_creation_params())
res = CredentialType(**self.get_creation_params())
res.custom_injectors = self.custom_injectors
return res


class CredentialInputSource(PrimordialModel):
Expand Down
3 changes: 2 additions & 1 deletion awx/main/tests/functional/test_inventory_source_injectors.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,10 @@ def create_reference_data(source_dir, env, content):
json.dump(env, f, indent=4, sort_keys=True)


@mock.patch('awx_plugins.interfaces._temporary_private_licensing_api.detect_server_product_name', return_value='NOT-AWX')
@pytest.mark.django_db
@pytest.mark.parametrize('this_kind', discover_available_cloud_provider_plugin_names())
def test_inventory_update_injected_content(this_kind, inventory, fake_credential_factory, mock_me):
def test_inventory_update_injected_content(product_name, this_kind, inventory, fake_credential_factory, mock_me):
if this_kind.endswith('_supported'):
this_kind = this_kind[:-10]

Expand Down