2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2019, Guillaume Martinez (lunik@tiwabbit.fr)
|
|
|
|
# Copyright (c) 2018, Marcus Watkins <marwatk@marcuswatkins.net>
|
2020-03-09 10:11:07 +01:00
|
|
|
# Based on code:
|
2022-08-05 12:28:29 +02:00
|
|
|
# Copyright (c) 2013, Phillip Gentry <phillip@cx.com>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
module: gitlab_deploy_key
|
2022-11-09 07:18:40 +01:00
|
|
|
short_description: Manages GitLab project deploy keys
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2021-12-20 21:59:12 +01:00
|
|
|
- Adds, updates and removes project deploy keys
|
2020-03-09 10:11:07 +01:00
|
|
|
author:
|
|
|
|
- Marcus Watkins (@marwatk)
|
|
|
|
- Guillaume Martinez (@Lunik)
|
|
|
|
requirements:
|
|
|
|
- python-gitlab python module
|
|
|
|
extends_documentation_fragment:
|
2021-12-20 21:59:12 +01:00
|
|
|
- community.general.auth_basic
|
|
|
|
- community.general.gitlab
|
2023-02-20 17:26:28 +01:00
|
|
|
- community.general.attributes
|
|
|
|
|
|
|
|
attributes:
|
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
options:
|
|
|
|
project:
|
|
|
|
description:
|
|
|
|
- Id or Full path of project in the form of group/name.
|
|
|
|
required: true
|
|
|
|
type: str
|
|
|
|
title:
|
|
|
|
description:
|
|
|
|
- Deploy key's title.
|
|
|
|
required: true
|
|
|
|
type: str
|
|
|
|
key:
|
|
|
|
description:
|
|
|
|
- Deploy key
|
|
|
|
required: true
|
|
|
|
type: str
|
|
|
|
can_push:
|
|
|
|
description:
|
|
|
|
- Whether this key can push to the project.
|
|
|
|
type: bool
|
2022-08-24 19:59:56 +02:00
|
|
|
default: false
|
2020-03-09 10:11:07 +01:00
|
|
|
state:
|
|
|
|
description:
|
2023-06-20 08:14:02 +02:00
|
|
|
- When V(present) the deploy key added to the project if it doesn't exist.
|
|
|
|
- When V(absent) it will be removed from the project if it exists.
|
2020-03-09 10:11:07 +01:00
|
|
|
default: present
|
|
|
|
type: str
|
|
|
|
choices: [ "present", "absent" ]
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
- name: "Adding a project deploy key"
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.gitlab_deploy_key:
|
2020-03-09 10:11:07 +01:00
|
|
|
api_url: https://gitlab.example.com/
|
|
|
|
api_token: "{{ api_token }}"
|
|
|
|
project: "my_group/my_project"
|
|
|
|
title: "Jenkins CI"
|
|
|
|
state: present
|
|
|
|
key: "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9w..."
|
|
|
|
|
|
|
|
- name: "Update the above deploy key to add push access"
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.gitlab_deploy_key:
|
2020-03-09 10:11:07 +01:00
|
|
|
api_url: https://gitlab.example.com/
|
|
|
|
api_token: "{{ api_token }}"
|
|
|
|
project: "my_group/my_project"
|
|
|
|
title: "Jenkins CI"
|
|
|
|
state: present
|
2022-08-24 19:59:56 +02:00
|
|
|
can_push: true
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
- name: "Remove the previous deploy key from the project"
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.gitlab_deploy_key:
|
2020-03-09 10:11:07 +01:00
|
|
|
api_url: https://gitlab.example.com/
|
|
|
|
api_token: "{{ api_token }}"
|
|
|
|
project: "my_group/my_project"
|
|
|
|
state: absent
|
|
|
|
key: "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9w..."
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
RETURN = '''
|
|
|
|
msg:
|
|
|
|
description: Success or failure message
|
|
|
|
returned: always
|
|
|
|
type: str
|
|
|
|
sample: "Success"
|
|
|
|
|
|
|
|
result:
|
|
|
|
description: json parsed response from the server
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
|
|
|
|
error:
|
|
|
|
description: the error message returned by the GitLab API
|
|
|
|
returned: failed
|
|
|
|
type: str
|
|
|
|
sample: "400: key is already in use"
|
|
|
|
|
|
|
|
deploy_key:
|
|
|
|
description: API object
|
|
|
|
returned: always
|
|
|
|
type: dict
|
|
|
|
'''
|
|
|
|
|
|
|
|
from ansible.module_utils.api import basic_auth_argument_spec
|
2022-09-12 20:30:02 +02:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
2021-06-26 23:59:11 +02:00
|
|
|
from ansible.module_utils.common.text.converters import to_native
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-09-12 20:30:02 +02:00
|
|
|
from ansible_collections.community.general.plugins.module_utils.gitlab import (
|
2023-11-11 12:20:12 +01:00
|
|
|
auth_argument_spec, find_project, gitlab_authentication, gitlab
|
2022-09-12 20:30:02 +02:00
|
|
|
)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
class GitLabDeployKey(object):
|
|
|
|
def __init__(self, module, gitlab_instance):
|
|
|
|
self._module = module
|
|
|
|
self._gitlab = gitlab_instance
|
2021-11-16 13:01:32 +01:00
|
|
|
self.deploy_key_object = None
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
'''
|
|
|
|
@param project Project object
|
|
|
|
@param key_title Title of the key
|
|
|
|
@param key_key String of the key
|
2021-11-16 13:01:32 +01:00
|
|
|
@param key_can_push Option of the deploy_key
|
2020-03-09 10:11:07 +01:00
|
|
|
@param options Deploy key options
|
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
def create_or_update_deploy_key(self, project, key_title, key_key, options):
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = False
|
|
|
|
|
2021-02-05 07:30:05 +01:00
|
|
|
# note: unfortunately public key cannot be updated directly by
|
|
|
|
# GitLab REST API, so for that case we need to delete and
|
|
|
|
# than recreate the key
|
2021-11-16 13:01:32 +01:00
|
|
|
if self.deploy_key_object and self.deploy_key_object.key != key_key:
|
2021-10-28 20:07:05 +02:00
|
|
|
if not self._module.check_mode:
|
2021-11-16 13:01:32 +01:00
|
|
|
self.deploy_key_object.delete()
|
|
|
|
self.deploy_key_object = None
|
2021-02-05 07:30:05 +01:00
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
# Because we have already call exists_deploy_key in main()
|
|
|
|
if self.deploy_key_object is None:
|
|
|
|
deploy_key = self.create_deploy_key(project, {
|
2020-03-09 10:11:07 +01:00
|
|
|
'title': key_title,
|
|
|
|
'key': key_key,
|
|
|
|
'can_push': options['can_push']})
|
|
|
|
changed = True
|
|
|
|
else:
|
2021-11-16 13:01:32 +01:00
|
|
|
changed, deploy_key = self.update_deploy_key(self.deploy_key_object, {
|
2023-01-25 23:03:35 +01:00
|
|
|
'title': key_title,
|
2020-03-09 10:11:07 +01:00
|
|
|
'can_push': options['can_push']})
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
self.deploy_key_object = deploy_key
|
2020-03-09 10:11:07 +01:00
|
|
|
if changed:
|
|
|
|
if self._module.check_mode:
|
|
|
|
self._module.exit_json(changed=True, msg="Successfully created or updated the deploy key %s" % key_title)
|
|
|
|
|
|
|
|
try:
|
2021-11-16 13:01:32 +01:00
|
|
|
deploy_key.save()
|
2020-03-09 10:11:07 +01:00
|
|
|
except Exception as e:
|
|
|
|
self._module.fail_json(msg="Failed to update deploy key: %s " % e)
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
'''
|
|
|
|
@param project Project Object
|
2021-11-16 13:01:32 +01:00
|
|
|
@param arguments Attributes of the deploy_key
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
def create_deploy_key(self, project, arguments):
|
2020-03-09 10:11:07 +01:00
|
|
|
if self._module.check_mode:
|
|
|
|
return True
|
|
|
|
|
|
|
|
try:
|
2021-11-16 13:01:32 +01:00
|
|
|
deploy_key = project.keys.create(arguments)
|
2020-03-09 10:11:07 +01:00
|
|
|
except (gitlab.exceptions.GitlabCreateError) as e:
|
|
|
|
self._module.fail_json(msg="Failed to create deploy key: %s " % to_native(e))
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
return deploy_key
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
@param deploy_key Deploy Key Object
|
|
|
|
@param arguments Attributes of the deploy_key
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
def update_deploy_key(self, deploy_key, arguments):
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = False
|
|
|
|
|
|
|
|
for arg_key, arg_value in arguments.items():
|
|
|
|
if arguments[arg_key] is not None:
|
2021-11-16 13:01:32 +01:00
|
|
|
if getattr(deploy_key, arg_key) != arguments[arg_key]:
|
|
|
|
setattr(deploy_key, arg_key, arguments[arg_key])
|
2020-03-09 10:11:07 +01:00
|
|
|
changed = True
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
return (changed, deploy_key)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
'''
|
|
|
|
@param project Project object
|
|
|
|
@param key_title Title of the key
|
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
def find_deploy_key(self, project, key_title):
|
|
|
|
deploy_keys = project.keys.list(all=True)
|
|
|
|
for deploy_key in deploy_keys:
|
|
|
|
if (deploy_key.title == key_title):
|
|
|
|
return deploy_key
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
'''
|
|
|
|
@param project Project object
|
|
|
|
@param key_title Title of the key
|
|
|
|
'''
|
2021-11-16 13:01:32 +01:00
|
|
|
def exists_deploy_key(self, project, key_title):
|
|
|
|
# When project exists, object will be stored in self.project_object.
|
|
|
|
deploy_key = self.find_deploy_key(project, key_title)
|
|
|
|
if deploy_key:
|
|
|
|
self.deploy_key_object = deploy_key
|
2020-03-09 10:11:07 +01:00
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
def delete_deploy_key(self):
|
2020-03-09 10:11:07 +01:00
|
|
|
if self._module.check_mode:
|
|
|
|
return True
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
return self.deploy_key_object.delete()
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
argument_spec = basic_auth_argument_spec()
|
2021-12-20 21:59:12 +01:00
|
|
|
argument_spec.update(auth_argument_spec())
|
2020-03-09 10:11:07 +01:00
|
|
|
argument_spec.update(dict(
|
|
|
|
state=dict(type='str', default="present", choices=["absent", "present"]),
|
|
|
|
project=dict(type='str', required=True),
|
2021-03-12 08:51:47 +01:00
|
|
|
key=dict(type='str', required=True, no_log=False),
|
2020-03-09 10:11:07 +01:00
|
|
|
can_push=dict(type='bool', default=False),
|
|
|
|
title=dict(type='str', required=True)
|
|
|
|
))
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
|
|
|
mutually_exclusive=[
|
|
|
|
['api_username', 'api_token'],
|
2021-12-20 21:59:12 +01:00
|
|
|
['api_username', 'api_oauth_token'],
|
|
|
|
['api_username', 'api_job_token'],
|
|
|
|
['api_token', 'api_oauth_token'],
|
|
|
|
['api_token', 'api_job_token'],
|
2020-03-09 10:11:07 +01:00
|
|
|
],
|
|
|
|
required_together=[
|
|
|
|
['api_username', 'api_password']
|
|
|
|
],
|
|
|
|
required_one_of=[
|
2021-12-20 21:59:12 +01:00
|
|
|
['api_username', 'api_token', 'api_oauth_token', 'api_job_token']
|
2020-03-09 10:11:07 +01:00
|
|
|
],
|
|
|
|
supports_check_mode=True,
|
|
|
|
)
|
2023-11-11 12:20:12 +01:00
|
|
|
|
|
|
|
# check prerequisites and connect to gitlab server
|
|
|
|
gitlab_instance = gitlab_authentication(module)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
state = module.params['state']
|
|
|
|
project_identifier = module.params['project']
|
|
|
|
key_title = module.params['title']
|
|
|
|
key_keyfile = module.params['key']
|
|
|
|
key_can_push = module.params['can_push']
|
|
|
|
|
|
|
|
gitlab_deploy_key = GitLabDeployKey(module, gitlab_instance)
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
project = find_project(gitlab_instance, project_identifier)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if project is None:
|
|
|
|
module.fail_json(msg="Failed to create deploy key: project %s doesn't exists" % project_identifier)
|
|
|
|
|
2021-11-16 13:01:32 +01:00
|
|
|
deploy_key_exists = gitlab_deploy_key.exists_deploy_key(project, key_title)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if state == 'absent':
|
2021-11-16 13:01:32 +01:00
|
|
|
if deploy_key_exists:
|
|
|
|
gitlab_deploy_key.delete_deploy_key()
|
2020-03-09 10:11:07 +01:00
|
|
|
module.exit_json(changed=True, msg="Successfully deleted deploy key %s" % key_title)
|
|
|
|
else:
|
|
|
|
module.exit_json(changed=False, msg="Deploy key deleted or does not exists")
|
|
|
|
|
|
|
|
if state == 'present':
|
2021-11-16 13:01:32 +01:00
|
|
|
if gitlab_deploy_key.create_or_update_deploy_key(project, key_title, key_keyfile, {'can_push': key_can_push}):
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
module.exit_json(changed=True, msg="Successfully created or updated the deploy key %s" % key_title,
|
2021-11-16 13:01:32 +01:00
|
|
|
deploy_key=gitlab_deploy_key.deploy_key_object._attrs)
|
2020-03-09 10:11:07 +01:00
|
|
|
else:
|
|
|
|
module.exit_json(changed=False, msg="No need to update the deploy key %s" % key_title,
|
2021-11-16 13:01:32 +01:00
|
|
|
deploy_key=gitlab_deploy_key.deploy_key_object._attrs)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|