| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# |
| 4 |
# Copyright (c) 2020 T-Systems MMS |
| 5 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 6 |
# |
| 7 |
# This module is free software: you can redistribute it and/or modify |
| 8 |
# it under the terms of the GNU General Public License as published by |
| 9 |
# the Free Software Foundation, either version 3 of the License, or |
| 10 |
# (at your option) any later version. |
| 11 |
# |
| 12 |
# This software is distributed in the hope that it will be useful, |
| 13 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
# GNU General Public License for more details. |
| 16 |
# |
| 17 |
# You should have received a copy of the GNU General Public License |
| 18 |
# along with this software. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
|
| 20 |
from __future__ import absolute_import, division, print_function |
| 21 |
|
| 22 |
__metaclass__ = type |
| 23 |
|
| 24 |
DOCUMENTATION = ''' |
| 25 |
--- |
| 26 |
module: mongodb_atlas_ldap_user |
| 27 |
short_description: Manage LDAP users in Atlas |
| 28 |
description: |
| 29 |
- The mongodb_atlas_ldap_user module lets you create LDAP groups on the admin database by mapping LDAP groups to MongoDB roles on your Atlas databases. |
| 30 |
- Each user or group has a set of roles that provide access to the project's databases. |
| 31 |
- L(API Documentation,https://docs.atlas.mongodb.com/security-ldaps/) |
| 32 |
author: "Martin Schurz (@schurzi) / Derek Giri" |
| 33 |
extends_documentation_fragment: community.mongodb.atlas_options |
| 34 |
options: |
| 35 |
database_name: |
| 36 |
description: |
| 37 |
- Database against which Atlas authenticates the user. |
| 38 |
choices: ["admin", "$external"] |
| 39 |
default: "admin" |
| 40 |
type: str |
| 41 |
aliases: [ "databaseName" ] |
| 42 |
ldap_auth_type: |
| 43 |
description: |
| 44 |
- Type of LDAP authorization for the user i.e. USER or GROUP |
| 45 |
choices: ["GROUP", "USER"] |
| 46 |
default: "GROUP" |
| 47 |
type: str |
| 48 |
aliases: [ "ldapAuthType" ] |
| 49 |
username: |
| 50 |
description: |
| 51 |
- Username for authenticating to MongoDB. |
| 52 |
required: true |
| 53 |
type: str |
| 54 |
roles: |
| 55 |
description: |
| 56 |
- Array of this user's roles and the databases / collections on which the roles apply. |
| 57 |
- A role must include folliwing elements |
| 58 |
suboptions: |
| 59 |
database_name: |
| 60 |
required: true |
| 61 |
type: str |
| 62 |
description: |
| 63 |
- Database on which the user has the specified role. |
| 64 |
- A role on the admin database can include privileges that apply to the other databases. |
| 65 |
aliases: [ "databaseName" ] |
| 66 |
role_name: |
| 67 |
required: true |
| 68 |
type: str |
| 69 |
description: |
| 70 |
- Name of the role. This value can either be a built-in role or a custom role. |
| 71 |
aliases: ["roleName" ] |
| 72 |
required: true |
| 73 |
type: list |
| 74 |
elements: dict |
| 75 |
''' |
| 76 |
|
| 77 |
EXAMPLES = ''' |
| 78 |
- name: LDAP Group or Username |
| 79 |
community.mongodb.mongodb_atlas_ldap_user: |
| 80 |
api_username: "API_user" |
| 81 |
api_password: "API_passwort_or_token" |
| 82 |
atlas_ldap_user: "USER DN or GROUP DN" |
| 83 |
group_id: "GROUP_ID" |
| 84 |
database_name: "admin" |
| 85 |
username: my_app_user |
| 86 |
roles: |
| 87 |
- database_name: private_info |
| 88 |
role_name: read |
| 89 |
- database_name: public_info |
| 90 |
role_name: readWrite |
| 91 |
''' |
| 92 |
|
| 93 |
from ansible.module_utils.basic import AnsibleModule |
| 94 |
from ansible_collections.community.mongodb.plugins.module_utils.mongodb_atlas import ( |
| 95 |
AtlasAPIObject, |
| 96 |
) |
| 97 |
|
| 98 |
|
| 99 |
# =========================================== |
| 100 |
# Module execution. |
| 101 |
# |
| 102 |
def main(): |
| 103 |
# add our own arguments |
| 104 |
argument_spec = dict( |
| 105 |
state=dict(default="present", choices=["absent", "present"]), |
| 106 |
api_username=dict(required=True, aliases=['apiUsername']), |
| 107 |
api_password=dict(required=True, no_log=True, aliases=['apiPassword']), |
| 108 |
group_id=dict(required=True, aliases=['groupId']), |
| 109 |
ldap_auth_type=dict(default="GROUP", choices=["GROUP", "USER"], aliases=["ldapAuthType"]), |
| 110 |
database_name=dict(default="admin", choices=["admin", "$external"], aliases=["databaseName"]), |
| 111 |
username=dict(required=True), |
| 112 |
roles=dict( |
| 113 |
required=True, |
| 114 |
type="list", |
| 115 |
elements="dict", |
| 116 |
options=dict( |
| 117 |
database_name=dict(required=True, aliases=["databaseName"]), |
| 118 |
role_name=dict(required=True, aliases=["roleName"]), |
| 119 |
), |
| 120 |
), |
| 121 |
) |
| 122 |
|
| 123 |
# Define the main module |
| 124 |
module = AnsibleModule( |
| 125 |
argument_spec=argument_spec, supports_check_mode=True |
| 126 |
) |
| 127 |
|
| 128 |
data = { |
| 129 |
"databaseName": module.params["database_name"], |
| 130 |
"ldapAuthType": module.params["ldap_auth_type"], |
| 131 |
"username": module.params["username"], |
| 132 |
"roles": [], |
| 133 |
} |
| 134 |
|
| 135 |
# remap keys to API format |
| 136 |
for role in module.params.get("roles"): |
| 137 |
data["roles"].append({ |
| 138 |
"databaseName": role.get("database_name"), |
| 139 |
"roleName": role.get("role_name") |
| 140 |
}) |
| 141 |
|
| 142 |
try: |
| 143 |
atlas = AtlasAPIObject( |
| 144 |
module=module, |
| 145 |
path="/databaseUsers", |
| 146 |
object_name="username", |
| 147 |
group_id=module.params["group_id"], |
| 148 |
data=data, |
| 149 |
) |
| 150 |
except Exception as e: |
| 151 |
module.fail_json( |
| 152 |
msg="unable to connect to Atlas API. Exception message: %s" % e |
| 153 |
) |
| 154 |
|
| 155 |
changed, diff = atlas.update(module.params["state"]) |
| 156 |
module.exit_json( |
| 157 |
changed=changed, |
| 158 |
data=atlas.data, |
| 159 |
diff=diff, |
| 160 |
) |
| 161 |
|
| 162 |
|
| 163 |
# import module snippets |
| 164 |
if __name__ == "__main__": |
| 165 |
main() |