| 1 |
#!/usr/bin/python |
| 2 |
# |
| 3 |
# Scaleway SSH keys management module |
| 4 |
# |
| 5 |
# Copyright (C) 2018 Online SAS. |
| 6 |
# https://www.scaleway.com |
| 7 |
# |
| 8 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 9 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 10 |
|
| 11 |
from __future__ import annotations |
| 12 |
|
| 13 |
DOCUMENTATION = r""" |
| 14 |
module: scaleway_sshkey |
| 15 |
short_description: Scaleway SSH keys management module |
| 16 |
author: Remy Leone (@remyleone) |
| 17 |
description: |
| 18 |
- This module manages SSH keys on Scaleway account (U(https://developer.scaleway.com)). |
| 19 |
extends_documentation_fragment: |
| 20 |
- community.general._scaleway |
| 21 |
- community.general._attributes |
| 22 |
- community.general._scaleway.actiongroup_scaleway |
| 23 |
|
| 24 |
attributes: |
| 25 |
check_mode: |
| 26 |
support: full |
| 27 |
diff_mode: |
| 28 |
support: none |
| 29 |
action_group: |
| 30 |
version_added: 11.3.0 |
| 31 |
|
| 32 |
options: |
| 33 |
state: |
| 34 |
type: str |
| 35 |
description: |
| 36 |
- Indicate desired state of the SSH key. |
| 37 |
default: present |
| 38 |
choices: |
| 39 |
- present |
| 40 |
- absent |
| 41 |
ssh_pub_key: |
| 42 |
type: str |
| 43 |
description: |
| 44 |
- The public SSH key as a string to add. |
| 45 |
required: true |
| 46 |
api_url: |
| 47 |
type: str |
| 48 |
description: |
| 49 |
- Scaleway API URL. |
| 50 |
default: 'https://account.scaleway.com' |
| 51 |
aliases: ['base_url'] |
| 52 |
""" |
| 53 |
|
| 54 |
EXAMPLES = r""" |
| 55 |
- name: "Add SSH key" |
| 56 |
community.general.scaleway_sshkey: |
| 57 |
ssh_pub_key: "ssh-rsa AAAA..." |
| 58 |
state: "present" |
| 59 |
|
| 60 |
- name: "Delete SSH key" |
| 61 |
community.general.scaleway_sshkey: |
| 62 |
ssh_pub_key: "ssh-rsa AAAA..." |
| 63 |
state: "absent" |
| 64 |
|
| 65 |
- name: "Add SSH key with explicit token" |
| 66 |
community.general.scaleway_sshkey: |
| 67 |
ssh_pub_key: "ssh-rsa AAAA..." |
| 68 |
state: "present" |
| 69 |
oauth_token: "6ecd2c9b-6f4f-44d4-a187-61a92078d08c" |
| 70 |
""" |
| 71 |
|
| 72 |
RETURN = r""" |
| 73 |
data: |
| 74 |
description: This is only present when O(state=present). |
| 75 |
returned: when O(state=present) |
| 76 |
type: dict |
| 77 |
sample: |
| 78 |
{ |
| 79 |
"ssh_public_keys": [ |
| 80 |
{ |
| 81 |
"key": "ssh-rsa AAAA...." |
| 82 |
} |
| 83 |
] |
| 84 |
} |
| 85 |
""" |
| 86 |
|
| 87 |
from ansible.module_utils.basic import AnsibleModule, env_fallback |
| 88 |
|
| 89 |
from ansible_collections.community.general.plugins.module_utils._scaleway import Scaleway, scaleway_argument_spec |
| 90 |
|
| 91 |
|
| 92 |
def extract_present_sshkeys(raw_organization_dict): |
| 93 |
ssh_key_list = raw_organization_dict["organizations"][0]["users"][0]["ssh_public_keys"] |
| 94 |
ssh_key_lookup = [ssh_key["key"] for ssh_key in ssh_key_list] |
| 95 |
return ssh_key_lookup |
| 96 |
|
| 97 |
|
| 98 |
def extract_user_id(raw_organization_dict): |
| 99 |
return raw_organization_dict["organizations"][0]["users"][0]["id"] |
| 100 |
|
| 101 |
|
| 102 |
def sshkey_user_patch(ssh_lookup): |
| 103 |
ssh_list = {"ssh_public_keys": [{"key": key} for key in ssh_lookup]} |
| 104 |
return ssh_list |
| 105 |
|
| 106 |
|
| 107 |
def core(module): |
| 108 |
ssh_pub_key = module.params["ssh_pub_key"] |
| 109 |
state = module.params["state"] |
| 110 |
account_api = Scaleway(module) |
| 111 |
response = account_api.get("organizations") |
| 112 |
|
| 113 |
status_code = response.status_code |
| 114 |
organization_json = response.json |
| 115 |
|
| 116 |
if not response.ok: |
| 117 |
module.fail_json(msg=f"Error getting ssh key [{status_code}: {response.json['message']}]") |
| 118 |
|
| 119 |
user_id = extract_user_id(organization_json) |
| 120 |
present_sshkeys = [] |
| 121 |
try: |
| 122 |
present_sshkeys = extract_present_sshkeys(organization_json) |
| 123 |
except (KeyError, IndexError): |
| 124 |
module.fail_json(changed=False, data="Error while extracting present SSH keys from API") |
| 125 |
|
| 126 |
if state in ("present",): |
| 127 |
if ssh_pub_key in present_sshkeys: |
| 128 |
module.exit_json(changed=False) |
| 129 |
|
| 130 |
# If key not found create it! |
| 131 |
if module.check_mode: |
| 132 |
module.exit_json(changed=True) |
| 133 |
|
| 134 |
present_sshkeys.append(ssh_pub_key) |
| 135 |
payload = sshkey_user_patch(present_sshkeys) |
| 136 |
|
| 137 |
response = account_api.patch(f"/users/{user_id}", data=payload) |
| 138 |
|
| 139 |
if response.ok: |
| 140 |
module.exit_json(changed=True, data=response.json) |
| 141 |
|
| 142 |
module.fail_json(msg=f"Error creating ssh key [{response.status_code}: {response.json}]") |
| 143 |
|
| 144 |
elif state in ("absent",): |
| 145 |
if ssh_pub_key not in present_sshkeys: |
| 146 |
module.exit_json(changed=False) |
| 147 |
|
| 148 |
if module.check_mode: |
| 149 |
module.exit_json(changed=True) |
| 150 |
|
| 151 |
present_sshkeys.remove(ssh_pub_key) |
| 152 |
payload = sshkey_user_patch(present_sshkeys) |
| 153 |
|
| 154 |
response = account_api.patch(f"/users/{user_id}", data=payload) |
| 155 |
|
| 156 |
if response.ok: |
| 157 |
module.exit_json(changed=True, data=response.json) |
| 158 |
|
| 159 |
module.fail_json(msg=f"Error deleting ssh key [{response.status_code}: {response.json}]") |
| 160 |
|
| 161 |
|
| 162 |
def main(): |
| 163 |
argument_spec = scaleway_argument_spec() |
| 164 |
argument_spec.update( |
| 165 |
dict( |
| 166 |
state=dict(default="present", choices=["absent", "present"]), |
| 167 |
ssh_pub_key=dict(required=True), |
| 168 |
api_url=dict( |
| 169 |
fallback=(env_fallback, ["SCW_API_URL"]), default="https://account.scaleway.com", aliases=["base_url"] |
| 170 |
), |
| 171 |
) |
| 172 |
) |
| 173 |
module = AnsibleModule( |
| 174 |
argument_spec=argument_spec, |
| 175 |
supports_check_mode=True, |
| 176 |
) |
| 177 |
|
| 178 |
core(module) |
| 179 |
|
| 180 |
|
| 181 |
if __name__ == "__main__": |
| 182 |
main() |