| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
|
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
# STARTREMOVE (downstream) |
| 11 |
DOCUMENTATION = r""" |
| 12 |
|
| 13 |
module: openshift_adm_groups_sync |
| 14 |
|
| 15 |
short_description: Sync OpenShift Groups with records from an external provider. |
| 16 |
|
| 17 |
version_added: "2.1.0" |
| 18 |
|
| 19 |
author: |
| 20 |
- Aubin Bikouo (@abikouo) |
| 21 |
|
| 22 |
description: |
| 23 |
- In order to sync/prune OpenShift Group records with those from an external provider, determine which Groups you wish to sync |
| 24 |
and where their records live. |
| 25 |
- Analogous to `oc adm prune groups` and `oc adm group sync`. |
| 26 |
- LDAP sync configuration file syntax can be found here |
| 27 |
U(https://docs.openshift.com/container-platform/4.9/authentication/ldap-syncing.html). |
| 28 |
- The bindPassword attribute of the LDAP sync configuration is expected to be a string, |
| 29 |
please use ansible-vault encryption to secure this information. |
| 30 |
|
| 31 |
extends_documentation_fragment: |
| 32 |
- kubernetes.core.k8s_auth_options |
| 33 |
|
| 34 |
options: |
| 35 |
state: |
| 36 |
description: |
| 37 |
- Determines if the group should be sync when set to C(present) or pruned when set to C(absent). |
| 38 |
type: str |
| 39 |
default: present |
| 40 |
choices: [ absent, present ] |
| 41 |
type: |
| 42 |
description: |
| 43 |
- which groups allow and deny list entries refer to. |
| 44 |
type: str |
| 45 |
default: ldap |
| 46 |
choices: [ ldap, openshift ] |
| 47 |
sync_config: |
| 48 |
description: |
| 49 |
- Provide a valid YAML definition of an LDAP sync configuration. |
| 50 |
type: dict |
| 51 |
aliases: |
| 52 |
- config |
| 53 |
- src |
| 54 |
required: True |
| 55 |
deny_groups: |
| 56 |
description: |
| 57 |
- Denied groups, could be openshift group name or LDAP group dn value. |
| 58 |
- When parameter C(type) is set to I(ldap) this should contains only LDAP group definition |
| 59 |
like I(cn=developers,ou=groups,ou=rfc2307,dc=ansible,dc=redhat). |
| 60 |
- The elements specified in this list will override the ones specified in C(allow_groups). |
| 61 |
type: list |
| 62 |
elements: str |
| 63 |
default: [] |
| 64 |
allow_groups: |
| 65 |
description: |
| 66 |
- Allowed groups, could be openshift group name or LDAP group dn value. |
| 67 |
- When parameter C(type) is set to I(ldap) this should contains only LDAP group definition |
| 68 |
like I(cn=developers,ou=groups,ou=rfc2307,dc=ansible,dc=redhat). |
| 69 |
type: list |
| 70 |
elements: str |
| 71 |
default: [] |
| 72 |
|
| 73 |
requirements: |
| 74 |
- python >= 3.6 |
| 75 |
- kubernetes >= 12.0.0 |
| 76 |
- python-ldap |
| 77 |
""" |
| 78 |
|
| 79 |
EXAMPLES = r""" |
| 80 |
# Prune all orphaned groups |
| 81 |
- name: Prune all orphan groups |
| 82 |
openshift_adm_groups_sync: |
| 83 |
state: absent |
| 84 |
src: "{{ lookup('file', '/path/to/ldap-sync-config.yaml') | from_yaml }}" |
| 85 |
|
| 86 |
# Prune all orphaned groups from a list of specific groups specified in allow_groups |
| 87 |
- name: Prune all orphan groups from a list of specific groups specified in allow_groups |
| 88 |
openshift_adm_groups_sync: |
| 89 |
state: absent |
| 90 |
src: "{{ lookup('file', '/path/to/ldap-sync-config.yaml') | from_yaml }}" |
| 91 |
allow_groups: |
| 92 |
- cn=developers,ou=groups,ou=rfc2307,dc=ansible,dc=redhat |
| 93 |
- cn=developers,ou=groups,ou=rfc2307,dc=ansible,dc=redhat |
| 94 |
|
| 95 |
# Sync all groups from an LDAP server |
| 96 |
- name: Sync all groups from an LDAP server |
| 97 |
openshift_adm_groups_sync: |
| 98 |
src: |
| 99 |
kind: LDAPSyncConfig |
| 100 |
apiVersion: v1 |
| 101 |
url: ldap://localhost:1390 |
| 102 |
insecure: true |
| 103 |
bindDN: cn=admin,dc=example,dc=org |
| 104 |
bindPassword: adminpassword |
| 105 |
rfc2307: |
| 106 |
groupsQuery: |
| 107 |
baseDN: "cn=admins,ou=groups,dc=example,dc=org" |
| 108 |
scope: sub |
| 109 |
derefAliases: never |
| 110 |
filter: (objectClass=*) |
| 111 |
pageSize: 0 |
| 112 |
groupUIDAttribute: dn |
| 113 |
groupNameAttributes: [cn] |
| 114 |
groupMembershipAttributes: [member] |
| 115 |
usersQuery: |
| 116 |
baseDN: "ou=users,dc=example,dc=org" |
| 117 |
scope: sub |
| 118 |
derefAliases: never |
| 119 |
pageSize: 0 |
| 120 |
userUIDAttribute: dn |
| 121 |
userNameAttributes: [mail] |
| 122 |
tolerateMemberNotFoundErrors: true |
| 123 |
tolerateMemberOutOfScopeErrors: true |
| 124 |
|
| 125 |
# Sync all groups except the ones from the deny_groups from an LDAP server |
| 126 |
- name: Sync all groups from an LDAP server using deny_groups |
| 127 |
openshift_adm_groups_sync: |
| 128 |
src: "{{ lookup('file', '/path/to/ldap-sync-config.yaml') | from_yaml }}" |
| 129 |
deny_groups: |
| 130 |
- cn=developers,ou=groups,ou=rfc2307,dc=ansible,dc=redhat |
| 131 |
- cn=developers,ou=groups,ou=rfc2307,dc=ansible,dc=redhat |
| 132 |
|
| 133 |
# Sync all OpenShift Groups that have been synced previously with an LDAP server |
| 134 |
- name: Sync all OpenShift Groups that have been synced previously with an LDAP server |
| 135 |
openshift_adm_groups_sync: |
| 136 |
src: "{{ lookup('file', '/path/to/ldap-sync-config.yaml') | from_yaml }}" |
| 137 |
type: openshift |
| 138 |
""" |
| 139 |
|
| 140 |
|
| 141 |
RETURN = r""" |
| 142 |
builds: |
| 143 |
description: |
| 144 |
- The groups that were created, updated or deleted |
| 145 |
returned: success |
| 146 |
type: list |
| 147 |
elements: dict |
| 148 |
sample: [ |
| 149 |
{ |
| 150 |
"apiVersion": "user.openshift.io/v1", |
| 151 |
"kind": "Group", |
| 152 |
"metadata": { |
| 153 |
"annotations": { |
| 154 |
"openshift.io/ldap.sync-time": "2021-12-17T12:20:28.125282", |
| 155 |
"openshift.io/ldap.uid": "cn=developers,ou=groups,ou=rfc2307,dc=ansible,dc=redhat", |
| 156 |
"openshift.io/ldap.url": "localhost:1390" |
| 157 |
}, |
| 158 |
"creationTimestamp": "2021-12-17T11:09:49Z", |
| 159 |
"labels": { |
| 160 |
"openshift.io/ldap.host": "localhost" |
| 161 |
}, |
| 162 |
"managedFields": [{ |
| 163 |
"apiVersion": "user.openshift.io/v1", |
| 164 |
"fieldsType": "FieldsV1", |
| 165 |
"fieldsV1": { |
| 166 |
"f:metadata": { |
| 167 |
"f:annotations": { |
| 168 |
".": {}, |
| 169 |
"f:openshift.io/ldap.sync-time": {}, |
| 170 |
"f:openshift.io/ldap.uid": {}, |
| 171 |
"f:openshift.io/ldap.url": {} |
| 172 |
}, |
| 173 |
"f:labels": { |
| 174 |
".": {}, |
| 175 |
"f:openshift.io/ldap.host": {} |
| 176 |
} |
| 177 |
}, |
| 178 |
"f:users": {} |
| 179 |
}, |
| 180 |
"manager": "OpenAPI-Generator", |
| 181 |
"operation": "Update", |
| 182 |
"time": "2021-12-17T11:09:49Z" |
| 183 |
}], |
| 184 |
"name": "developers", |
| 185 |
"resourceVersion": "2014696", |
| 186 |
"uid": "8dc211cb-1544-41e1-96b1-efffeed2d7d7" |
| 187 |
}, |
| 188 |
"users": ["[email protected]"] |
| 189 |
} |
| 190 |
] |
| 191 |
""" |
| 192 |
# ENDREMOVE (downstream) |
| 193 |
|
| 194 |
import copy |
| 195 |
|
| 196 |
from ansible_collections.kubernetes.core.plugins.module_utils.args_common import ( |
| 197 |
AUTH_ARG_SPEC, |
| 198 |
) |
| 199 |
|
| 200 |
|
| 201 |
def argument_spec(): |
| 202 |
args = copy.deepcopy(AUTH_ARG_SPEC) |
| 203 |
args.update( |
| 204 |
dict( |
| 205 |
state=dict(type="str", choices=["absent", "present"], default="present"), |
| 206 |
type=dict(type="str", choices=["ldap", "openshift"], default="ldap"), |
| 207 |
sync_config=dict(type="dict", aliases=["config", "src"], required=True), |
| 208 |
deny_groups=dict(type="list", elements="str", default=[]), |
| 209 |
allow_groups=dict(type="list", elements="str", default=[]), |
| 210 |
) |
| 211 |
) |
| 212 |
return args |
| 213 |
|
| 214 |
|
| 215 |
def main(): |
| 216 |
from ansible_collections.community.okd.plugins.module_utils.openshift_groups import ( |
| 217 |
OpenshiftGroupsSync, |
| 218 |
) |
| 219 |
|
| 220 |
module = OpenshiftGroupsSync( |
| 221 |
argument_spec=argument_spec(), supports_check_mode=True |
| 222 |
) |
| 223 |
module.run_module() |
| 224 |
|
| 225 |
|
| 226 |
if __name__ == "__main__": |
| 227 |
main() |