Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New module: cyberark_credential #52190

Closed
wants to merge 22 commits into from
Closed
Changes from 14 commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
769e2b9
Create cyberark_credential.py
JimmyJamTQBD Aug 16, 2018
3429690
Update cyberark_credential.py
JimmyJamTQBD Feb 13, 2019
5bc4b2c
Update cyberark_credential.py
JimmyJamTQBD Feb 13, 2019
e7eb210
Update cyberark_credential.py
JimmyJamTQBD Feb 14, 2019
f659d47
Update cyberark_credential.py
JimmyJamTQBD Feb 14, 2019
491ddd4
Update cyberark_credential.py
JimmyJamTQBD Feb 14, 2019
4a8a508
Update cyberark_credential.py
JimmyJamTQBD Feb 14, 2019
d260550
Update cyberark_credential.py
JimmyJamTQBD Feb 14, 2019
c33e329
Update cyberark_credential.py
JimmyJamTQBD Feb 14, 2019
c99a422
Update cyberark_credential.py
JimmyJamTQBD Feb 14, 2019
f7dfdcb
Update cyberark_credential.py
JimmyJamTQBD Feb 14, 2019
7226244
Update cyberark_credential.py
JimmyJamTQBD Feb 14, 2019
afc0278
Update cyberark_credential.py
JimmyJamTQBD Feb 14, 2019
5e89157
Update cyberark_credential.py
JimmyJamTQBD Feb 14, 2019
71164ed
Update lib/ansible/modules/identity/cyberark/cyberark_credential.py
gundalow Feb 26, 2019
45a3066
Update lib/ansible/modules/identity/cyberark/cyberark_credential.py
gundalow Feb 26, 2019
337b779
Update lib/ansible/modules/identity/cyberark/cyberark_credential.py
gundalow Feb 26, 2019
83d9830
Update lib/ansible/modules/identity/cyberark/cyberark_credential.py
gundalow Feb 26, 2019
bd8013b
Update lib/ansible/modules/identity/cyberark/cyberark_credential.py
gundalow Feb 26, 2019
5232dd7
Update lib/ansible/modules/identity/cyberark/cyberark_credential.py
gundalow Feb 26, 2019
1ea317d
Update lib/ansible/modules/identity/cyberark/cyberark_credential.py
gundalow Feb 26, 2019
bcd4117
Update cyberark_credential.py
JimmyJamTQBD Feb 26, 2019
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
259 changes: 259 additions & 0 deletions lib/ansible/modules/identity/cyberark/cyberark_credential.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,259 @@
#!/usr/bin/python
# Copyright: (c) 2017, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)

from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}

DOCUMENTATION = """
module: cyberark_credential
short_description: Module for retrieval of CyberArk vaulted credential using PAS Web Services SDK through the Central Credential Provider
JimmyJamTQBD marked this conversation as resolved.
Show resolved Hide resolved
author:
- Edward Nunez (@enunez-cyberark)
- Cyberark Bizdev (@cyberark-bizdev)
- erasmix (@erasmix)
- James Stutes (@jimmyjamcabd)
version_added: 2.8
JimmyJamTQBD marked this conversation as resolved.
Show resolved Hide resolved
description:
- Creates a URI for retrieving a credential from the Cyberark Vault through the Privileged
Account Security Web Services SDK by requesting access to a specific object through an Application ID
It returns an Ansible fact called I(cyberarkcredential) as a JSON message with object information
that can be used by other modules. Every module can use this fact as C(cyberarkcredential) parameter.
options:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are working on improving module documentation, please include type: str,int, bool for the options.

api_base_url:
description:
- A string containing the base URL of the server hosting the Central Credential Provider
JimmyJamTQBD marked this conversation as resolved.
Show resolved Hide resolved
validate_certs:
type: bool
default: 'true'
description:
- If C(false), SSL certificate chain will not be validated. This should only
set to C(true) if you have a root CA certificate installed on each node.
app_id:
description:
- A string containing the Application ID authorized for retrieving the credential.
query:
description:
- A string containing details of the object being queried
parameters
Safe=<safe name>
Folder=<folder name within safe>
Object=<object name>
UserName=<username of object>
Address=<address listed for object>
Database=<optional file category for database objects>
PolicyID=<platform id managing object>.
required: True
client_cert:
required: False
JimmyJamTQBD marked this conversation as resolved.
Show resolved Hide resolved
description:
- A string containing the file location and name of the client certificate used for authentication.
client_key:
required: False
JimmyJamTQBD marked this conversation as resolved.
Show resolved Hide resolved
description:
- A string containing the file location and name of the private key of the client certificate used for authentication.
reason:
required: False
JimmyJamTQBD marked this conversation as resolved.
Show resolved Hide resolved
description:
- Reason for requesting credential if required by policy.
state:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will there be other states in the future?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this particular module the state will always be present, there is no workflow for absent.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why have a state option at all then if there is only one value allowed?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had originally left it off and I want to say that during lint testing it was added back. I think it was conflicting with either documentation or another setting. I would prefer to leave it off.

default: present
choices: [present]
description:
- Specifies the state.
"""

EXAMPLES = """
- name: Retrieve credential from CyberArk Vault using PAS Web Services SDK via Central Credential Provider
cyberark_credential:
api_base_url: "{{ web_services_base_url }}"
app_id: "{{ application_id }}"
query: "Safe=test&UserName=admin"
register: cyberarkcredential
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation doesn't look right here.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll be honest, I am not completely sure that it is or isn't, but it mirrors my playbooks from the Gist where I ran them. If there is something I am missing, please let me know.

result:
'{ api_base_url }"/AIMWebService/api/Accounts?AppId="{ app_id }"&"{ query }'
- name: Retrieve credential from CyberArk Vault using PAS Web Services SDK via Central Credential Provider
JimmyJamTQBD marked this conversation as resolved.
Show resolved Hide resolved
cyberark_credential:
api_base_url: "{{ web_services_base_url }}"
validate_certs: yes
client_cert: /etc/pki/ca-trust/source/client.pem
client_key: /etc/pki/ca-trust/source/priv-key.pem
app_id: "{{ application_id }}"
query: "Safe=test&UserName=admin"
reason: "requesting credential for Ansible deployment"
register: cyberarkcredential
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indentation doesn't look right here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same with this one.

result:
'{ api_base_url }"/AIMWebService/api/Accounts?AppId="{ app_id }"&"{ query }'
"""

RETURN = """
cyberark_credential:
description: CyberArk credential retrieved.
returned: success
type: dict
sample:
Address:
description: The target address of the credential being queried
type: string
returned: if required
Content:
description: The password for the object being queried
type: string
returned: always
CreationMethod:
description: This is how the object was created in the Vault
type: string
returned: always
DeviceType:
description: An internal File Category for more granular management of Platforms
type: string
returned: always
Folder:
description: The folder within the Safe where the credential is stored
type: string
returned: always
Name:
description: The Cyberark unique object ID of the credential being queried
type: string
returned: always
PasswordChangeInProcess:
description: If the password has a change flag placed by the CPM
type: bool
returned: always
PolicyID:
description: Whether or not SSL certificates should be validated.
type: string
returned: if assigned to a policy
Safe:
description: The safe where the queried credential is stored
type: string
returned: always
Username:
description: The username of the credential being queried
type: string
returned: if required
LogonDomain:
description: The Address friendly name resolved by the CPM
type: string
returned: if populated
CPMDisabled:
description: A description of why this vaulted credential is not being managed by the CPM
type: string
returned: if CPM management is disabled and a reason is given
status_code: 200
"""

from ansible.module_utils._text import to_text
from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils.urls import open_url
from ansible.module_utils.six.moves.urllib.error import HTTPError
import json
import urllib
try:
import httplib
except ImportError:
# Python 3
import http.client as httplib


def retrieveCredential(module):

# Getting parameters from module

api_base_url = module.params["api_base_url"]
validate_certs = module.params["validate_certs"]
app_id = module.params["app_id"]
query = module.params["query"]
client_cert = None
client_key = None

if "client_cert" in module.params:
client_cert = module.params["client_cert"]
if "client_key" in module.params:
client_key = module.params["client_key"]

end_point = "/AIMWebService/api/Accounts?AppId=%s&Query=%s" % (urllib.quote(app_id), urllib.quote(query))

if "reason" in module.params and module.params["reason"] is not None:
reason = urllib.quote(module.params["reason"])
end_point = end_point + "&reason=%s" % reason

result = None
response = None

try:

response = open_url(
api_base_url + end_point,
method="GET",
validate_certs=validate_certs,
client_cert=client_cert,
client_key=client_key)

except (HTTPError, httplib.HTTPException) as http_exception:

module.fail_json(
msg=("Error while retrieving credential."
"Please validate parameters provided, and permissions for the application and provider in CyberArk."
"\n*** end_point=%s%s\n ==> %s" % (api_base_url, end_point, to_text(http_exception))),
status_code=http_exception.code)

except Exception as unknown_exception:

module.fail_json(
msg=("Unknown error while retrieving credential."
"\n*** end_point=%s%s\n%s" % (api_base_url, end_point, to_text(unknown_exception))),
status_code=-1)

if response.getcode() == 200: # Success

# Result token from REST Api uses a different key based
try:
result = json.loads(response.read())
except Exception as e:
module.fail_json(
msg="Error obtain cyberark credential result from http body\n%s" % (to_text(e)),
status_code=-1)

return (result, response.getcode())

else:
module.fail_json(
msg="error in end_point=>" +
end_point)


def main():

fields = {
"api_base_url": {"required": True, "type": "str"},
"app_id": {"required": True, "type": "str"},
"query": {"required": True, "type": "str"},
"reason": {"required": False, "type": "str"},
"validate_certs": {"type": "bool",
"default": True},
"client_cert": {"type": "str", "required": False},
"client_key": {"type": "str", "required": False},
"state": {"type": "str",
"choices": ["present"],
"default": "present"},
}

module = AnsibleModule(
argument_spec=fields,
supports_check_mode=True)

(result, status_code) = retrieveCredential(module)

module.exit_json(
changed=False,
result=result,
status_code=status_code)


if __name__ == '__main__':
main()