Repository: ansible/awx

Issue:
Access to all keys out of the CyberArk object
### Please confirm the following

- [X] I agree to follow this project's [code of conduct](https://docs.ansible.com/ansible/latest/community/code_of_conduct.html).
- [X] I have checked the [current issues](https://github.com/ansible/awx/issues) for duplicates.
- [X] I understand that AWX is open source software provided for free and that I might not receive a timely response.

### Feature type

Enhancement to Existing Feature

### Feature Summary

https://issues.redhat.com/browse/AAPRFE-28

We want to pull all the Keys out of the CyberArk object instead of just the content key or the content value. When we run the API call from the playbook, CyberArk presents all the keys and values associated with that object. We can set the fact and use the keys and values as variables in the playbook. But when we use the custom credential it only gives us the content value and does not give us any other keys and values. We want to be able to use custom credential to pull all the keys and values associated with an object and assigned into variables in AAP natively using the credential plugin.

This feature request is for changes to the credential lookup plugin as used in AAP2.

### Select the relevant components

- [X] UI
- [ ] API
- [ ] Docs
- [ ] Collection
- [ ] CLI
- [ ] Other

### Steps to reproduce

1. Configure a credential using the credential type "CyberArk Central Credential Provider Lookup".
2. Only "Content" key value from JSON response is returned.  "Content" is the password value of the account object.

### Current results

The credential plugin "CyberArk Central Credential Provider Lookup" is hardcoded to only return the "Content" key value from a JSON response that includes more keys that could be utilized.

### Sugested feature result

The suggested feature would include an additional field in the metadata of the credential plugin to allow the user to specify a different key value to utilize outside of the "Content" which should still remain as the default key.

### Additional information

Spoke with Oleksii Baranov on 01-05-2023 about how to include this feature.  Being developed by CyberArk's @infamousjoeg.  PR to come shortly.

For more information, please reference the Issue at https://issues.redhat.com/browse/AAPRFE-28.
Access to all keys out of the CyberArk object
### Please confirm the following

- [X] I agree to follow this project's [code of conduct](https://docs.ansible.com/ansible/latest/community/code_of_conduct.html).
- [X] I have checked the [current issues](https://github.com/ansible/awx/issues) for duplicates.
- [X] I understand that AWX is open source software provided for free and that I might not receive a timely response.

### Feature type

Enhancement to Existing Feature

### Feature Summary

https://issues.redhat.com/browse/AAPRFE-28

We want to pull all the Keys out of the CyberArk object instead of just the content key or the content value. When we run the API call from the playbook, CyberArk presents all the keys and values associated with that object. We can set the fact and use the keys and values as variables in the playbook. But when we use the custom credential it only gives us the content value and does not give us any other keys and values. We want to be able to use custom credential to pull all the keys and values associated with an object and assigned into variables in AAP natively using the credential plugin.

This feature request is for changes to the credential lookup plugin as used in AAP2.

### Select the relevant components

- [X] UI
- [ ] API
- [ ] Docs
- [ ] Collection
- [ ] CLI
- [ ] Other

### Steps to reproduce

1. Configure a credential using the credential type "CyberArk Central Credential Provider Lookup".
2. Only "Content" key value from JSON response is returned.  "Content" is the password value of the account object.

### Current results

The credential plugin "CyberArk Central Credential Provider Lookup" is hardcoded to only return the "Content" key value from a JSON response that includes more keys that could be utilized.

### Sugested feature result

The suggested feature would include an additional field in the metadata of the credential plugin to allow the user to specify a different key value to utilize outside of the "Content" which should still remain as the default key.

### Additional information

Spoke with Oleksii Baranov on 01-05-2023 about how to include this feature.  Being developed by CyberArk's @infamousjoeg.  PR to come shortly.

For more information, please reference the Issue at https://issues.redhat.com/browse/AAPRFE-28.


Discussion:

Resolving patch:
diff --git a/awx/main/credential_plugins/aim.py b/awx/main/credential_plugins/aim.py
--- a/awx/main/credential_plugins/aim.py
+++ b/awx/main/credential_plugins/aim.py
@@ -54,6 +54,12 @@
             'help_text': _('Lookup query for the object. Ex: Safe=TestSafe;Object=testAccountName123'),
         },
         {'id': 'object_query_format', 'label': _('Object Query Format'), 'type': 'string', 'default': 'Exact', 'choices': ['Exact', 'Regexp']},
+        {
+            'id': 'object_property',
+            'label': _('Object Property'),
+            'type': 'string',
+            'help_text': _('The property of the object to return. Default: Content Ex: Username, Address, etc.'),
+        },
         {
             'id': 'reason',
             'label': _('Reason'),
@@ -74,6 +80,7 @@ def aim_backend(**kwargs):
     app_id = kwargs['app_id']
     object_query = kwargs['object_query']
     object_query_format = kwargs['object_query_format']
+    object_property = kwargs.get('object_property', '')
     reason = kwargs.get('reason', None)
     if webservice_id == '':
         webservice_id = 'AIMWebService'
@@ -98,7 +105,18 @@ def aim_backend(**kwargs):
             allow_redirects=False,
         )
     raise_for_status(res)
-    return res.json()['Content']
+    # CCP returns the property name capitalized, username is camel case
+    # so we need to handle that case
+    if object_property == '':
+        object_property = 'Content'
+    elif object_property.lower() == 'username':
+        object_property = 'UserName'
+    elif object_property not in res:
+        raise KeyError('Property {} not found in object'.format(object_property))
+    else:
+        object_property = object_property.capitalize()
+
+    return res.json()[object_property]
 
 
 aim_plugin = CredentialPlugin('CyberArk Central Credential Provider Lookup', inputs=aim_inputs, backend=aim_backend)


Test patch: