| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2019, Guillaume Martinez ([email protected]) |
| 4 |
# Copyright (c) 2018, Marcus Watkins <[email protected]> |
| 5 |
# Based on code: |
| 6 |
# Copyright (c) 2013, Phillip Gentry <[email protected]> |
| 7 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 8 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 9 |
|
| 10 |
from __future__ import annotations |
| 11 |
|
| 12 |
DOCUMENTATION = r""" |
| 13 |
module: gitlab_deploy_key |
| 14 |
short_description: Manages GitLab project deploy keys |
| 15 |
description: |
| 16 |
- Adds, updates and removes project deploy keys. |
| 17 |
author: |
| 18 |
- Marcus Watkins (@marwatk) |
| 19 |
- Guillaume Martinez (@Lunik) |
| 20 |
requirements: |
| 21 |
- python-gitlab python module |
| 22 |
extends_documentation_fragment: |
| 23 |
- community.general._auth_basic |
| 24 |
- community.general._gitlab |
| 25 |
- community.general._attributes |
| 26 |
|
| 27 |
attributes: |
| 28 |
check_mode: |
| 29 |
support: full |
| 30 |
diff_mode: |
| 31 |
support: none |
| 32 |
|
| 33 |
options: |
| 34 |
project: |
| 35 |
description: |
| 36 |
- ID or Full path of project in the form of group/name. |
| 37 |
required: true |
| 38 |
type: str |
| 39 |
title: |
| 40 |
description: |
| 41 |
- Deploy key's title. |
| 42 |
required: true |
| 43 |
type: str |
| 44 |
key: |
| 45 |
description: |
| 46 |
- Deploy key. |
| 47 |
required: true |
| 48 |
type: str |
| 49 |
can_push: |
| 50 |
description: |
| 51 |
- Whether this key can push to the project. |
| 52 |
type: bool |
| 53 |
default: false |
| 54 |
state: |
| 55 |
description: |
| 56 |
- When V(present) the deploy key is added to the project if it does not exist. |
| 57 |
- When V(absent) it is removed from the project if it exists. |
| 58 |
default: present |
| 59 |
type: str |
| 60 |
choices: ["present", "absent"] |
| 61 |
""" |
| 62 |
|
| 63 |
EXAMPLES = r""" |
| 64 |
- name: "Adding a project deploy key" |
| 65 |
community.general.gitlab_deploy_key: |
| 66 |
api_url: https://gitlab.example.com/ |
| 67 |
api_token: "{{ api_token }}" |
| 68 |
project: "my_group/my_project" |
| 69 |
title: "Jenkins CI" |
| 70 |
state: present |
| 71 |
key: "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9w..." |
| 72 |
|
| 73 |
- name: "Update the above deploy key to add push access" |
| 74 |
community.general.gitlab_deploy_key: |
| 75 |
api_url: https://gitlab.example.com/ |
| 76 |
api_token: "{{ api_token }}" |
| 77 |
project: "my_group/my_project" |
| 78 |
title: "Jenkins CI" |
| 79 |
state: present |
| 80 |
can_push: true |
| 81 |
|
| 82 |
- name: "Remove the previous deploy key from the project" |
| 83 |
community.general.gitlab_deploy_key: |
| 84 |
api_url: https://gitlab.example.com/ |
| 85 |
api_token: "{{ api_token }}" |
| 86 |
project: "my_group/my_project" |
| 87 |
state: absent |
| 88 |
key: "ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIEAiPWx6WM4lhHNedGfBpPJNPpZ7yKu+dnn1SJejgt4596k6YjzGGphH2TUxwKzxcKDKKezwkpfnxPkSMkuEspGRt/aZZ9w..." |
| 89 |
""" |
| 90 |
|
| 91 |
RETURN = r""" |
| 92 |
msg: |
| 93 |
description: Success or failure message. |
| 94 |
returned: always |
| 95 |
type: str |
| 96 |
sample: "Success" |
| 97 |
|
| 98 |
result: |
| 99 |
description: JSON-parsed response from the server. |
| 100 |
returned: always |
| 101 |
type: dict |
| 102 |
|
| 103 |
error: |
| 104 |
description: The error message returned by the GitLab API. |
| 105 |
returned: failed |
| 106 |
type: str |
| 107 |
sample: "400: key is already in use" |
| 108 |
|
| 109 |
deploy_key: |
| 110 |
description: API object. |
| 111 |
returned: always |
| 112 |
type: dict |
| 113 |
""" |
| 114 |
|
| 115 |
from ansible.module_utils.api import basic_auth_argument_spec |
| 116 |
from ansible.module_utils.basic import AnsibleModule |
| 117 |
|
| 118 |
from ansible_collections.community.general.plugins.module_utils._gitlab import ( |
| 119 |
auth_argument_spec, |
| 120 |
find_project, |
| 121 |
gitlab, |
| 122 |
gitlab_authentication, |
| 123 |
list_all_kwargs, |
| 124 |
) |
| 125 |
|
| 126 |
|
| 127 |
class GitLabDeployKey: |
| 128 |
def __init__(self, module, gitlab_instance): |
| 129 |
self._module = module |
| 130 |
self._gitlab = gitlab_instance |
| 131 |
self.deploy_key_object = None |
| 132 |
|
| 133 |
""" |
| 134 |
@param project Project object |
| 135 |
@param key_title Title of the key |
| 136 |
@param key_key String of the key |
| 137 |
@param key_can_push Option of the deploy_key |
| 138 |
@param options Deploy key options |
| 139 |
""" |
| 140 |
|
| 141 |
def create_or_update_deploy_key(self, project, key_title, key_key, options): |
| 142 |
changed = False |
| 143 |
|
| 144 |
# note: unfortunately public key cannot be updated directly by |
| 145 |
# GitLab REST API, so for that case we need to delete and |
| 146 |
# than recreate the key |
| 147 |
if self.deploy_key_object and self.deploy_key_object.key != key_key: |
| 148 |
if not self._module.check_mode: |
| 149 |
self.deploy_key_object.delete() |
| 150 |
self.deploy_key_object = None |
| 151 |
|
| 152 |
# Because we have already call exists_deploy_key in main() |
| 153 |
if self.deploy_key_object is None: |
| 154 |
deploy_key = self.create_deploy_key( |
| 155 |
project, {"title": key_title, "key": key_key, "can_push": options["can_push"]} |
| 156 |
) |
| 157 |
changed = True |
| 158 |
else: |
| 159 |
changed, deploy_key = self.update_deploy_key( |
| 160 |
self.deploy_key_object, {"title": key_title, "can_push": options["can_push"]} |
| 161 |
) |
| 162 |
|
| 163 |
self.deploy_key_object = deploy_key |
| 164 |
if changed: |
| 165 |
if self._module.check_mode: |
| 166 |
self._module.exit_json(changed=True, msg=f"Successfully created or updated the deploy key {key_title}") |
| 167 |
|
| 168 |
try: |
| 169 |
deploy_key.save() |
| 170 |
except Exception as e: |
| 171 |
self._module.fail_json(msg=f"Failed to update deploy key: {e} ") |
| 172 |
return True |
| 173 |
else: |
| 174 |
return False |
| 175 |
|
| 176 |
""" |
| 177 |
@param project Project Object |
| 178 |
@param arguments Attributes of the deploy_key |
| 179 |
""" |
| 180 |
|
| 181 |
def create_deploy_key(self, project, arguments): |
| 182 |
if self._module.check_mode: |
| 183 |
return True |
| 184 |
|
| 185 |
try: |
| 186 |
deploy_key = project.keys.create(arguments) |
| 187 |
except gitlab.exceptions.GitlabCreateError as e: |
| 188 |
self._module.fail_json(msg=f"Failed to create deploy key: {e} ") |
| 189 |
|
| 190 |
return deploy_key |
| 191 |
|
| 192 |
""" |
| 193 |
@param deploy_key Deploy Key Object |
| 194 |
@param arguments Attributes of the deploy_key |
| 195 |
""" |
| 196 |
|
| 197 |
def update_deploy_key(self, deploy_key, arguments): |
| 198 |
changed = False |
| 199 |
|
| 200 |
for arg_key, arg_value in arguments.items(): |
| 201 |
if arg_value is not None: |
| 202 |
if getattr(deploy_key, arg_key) != arg_value: |
| 203 |
setattr(deploy_key, arg_key, arg_value) |
| 204 |
changed = True |
| 205 |
|
| 206 |
return (changed, deploy_key) |
| 207 |
|
| 208 |
""" |
| 209 |
@param project Project object |
| 210 |
@param key_title Title of the key |
| 211 |
""" |
| 212 |
|
| 213 |
def find_deploy_key(self, project, key_title): |
| 214 |
for deploy_key in project.keys.list(**list_all_kwargs): |
| 215 |
if deploy_key.title == key_title: |
| 216 |
return deploy_key |
| 217 |
|
| 218 |
""" |
| 219 |
@param project Project object |
| 220 |
@param key_title Title of the key |
| 221 |
""" |
| 222 |
|
| 223 |
def exists_deploy_key(self, project, key_title): |
| 224 |
# When project exists, object will be stored in self.project_object. |
| 225 |
deploy_key = self.find_deploy_key(project, key_title) |
| 226 |
if deploy_key: |
| 227 |
self.deploy_key_object = deploy_key |
| 228 |
return True |
| 229 |
return False |
| 230 |
|
| 231 |
def delete_deploy_key(self): |
| 232 |
if self._module.check_mode: |
| 233 |
return True |
| 234 |
|
| 235 |
return self.deploy_key_object.delete() |
| 236 |
|
| 237 |
|
| 238 |
def main(): |
| 239 |
argument_spec = basic_auth_argument_spec() |
| 240 |
argument_spec.update(auth_argument_spec()) |
| 241 |
argument_spec.update( |
| 242 |
dict( |
| 243 |
state=dict(type="str", default="present", choices=["absent", "present"]), |
| 244 |
project=dict(type="str", required=True), |
| 245 |
key=dict(type="str", required=True, no_log=False), |
| 246 |
can_push=dict(type="bool", default=False), |
| 247 |
title=dict(type="str", required=True), |
| 248 |
) |
| 249 |
) |
| 250 |
|
| 251 |
module = AnsibleModule( |
| 252 |
argument_spec=argument_spec, |
| 253 |
mutually_exclusive=[ |
| 254 |
["api_username", "api_token"], |
| 255 |
["api_username", "api_oauth_token"], |
| 256 |
["api_username", "api_job_token"], |
| 257 |
["api_token", "api_oauth_token"], |
| 258 |
["api_token", "api_job_token"], |
| 259 |
], |
| 260 |
required_together=[["api_username", "api_password"]], |
| 261 |
required_one_of=[["api_username", "api_token", "api_oauth_token", "api_job_token"]], |
| 262 |
supports_check_mode=True, |
| 263 |
) |
| 264 |
|
| 265 |
# check prerequisites and connect to gitlab server |
| 266 |
gitlab_instance = gitlab_authentication(module) |
| 267 |
|
| 268 |
state = module.params["state"] |
| 269 |
project_identifier = module.params["project"] |
| 270 |
key_title = module.params["title"] |
| 271 |
key_keyfile = module.params["key"] |
| 272 |
key_can_push = module.params["can_push"] |
| 273 |
|
| 274 |
gitlab_deploy_key = GitLabDeployKey(module, gitlab_instance) |
| 275 |
|
| 276 |
project = find_project(gitlab_instance, project_identifier) |
| 277 |
|
| 278 |
if project is None: |
| 279 |
module.fail_json(msg=f"Failed to create deploy key: project {project_identifier} doesn't exists") |
| 280 |
|
| 281 |
deploy_key_exists = gitlab_deploy_key.exists_deploy_key(project, key_title) |
| 282 |
|
| 283 |
if state == "absent": |
| 284 |
if deploy_key_exists: |
| 285 |
gitlab_deploy_key.delete_deploy_key() |
| 286 |
module.exit_json(changed=True, msg=f"Successfully deleted deploy key {key_title}") |
| 287 |
else: |
| 288 |
module.exit_json(changed=False, msg="Deploy key deleted or does not exists") |
| 289 |
|
| 290 |
if state == "present": |
| 291 |
if gitlab_deploy_key.create_or_update_deploy_key(project, key_title, key_keyfile, {"can_push": key_can_push}): |
| 292 |
module.exit_json( |
| 293 |
changed=True, |
| 294 |
msg=f"Successfully created or updated the deploy key {key_title}", |
| 295 |
deploy_key=gitlab_deploy_key.deploy_key_object._attrs, |
| 296 |
) |
| 297 |
else: |
| 298 |
module.exit_json( |
| 299 |
changed=False, |
| 300 |
msg=f"No need to update the deploy key {key_title}", |
| 301 |
deploy_key=gitlab_deploy_key.deploy_key_object._attrs, |
| 302 |
) |
| 303 |
|
| 304 |
|
| 305 |
if __name__ == "__main__": |
| 306 |
main() |