-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: dokku_acl_app and dokku_acl_service modules (#132)
Add `dokku_acl_app` and `dokku_acl_service` modules for managing access control lists for individual apps/services. Note: The `dokku acl:allowed` command is currently not exposed via ansible modules. If needed, a `dokku_acl_allowed` module could be added (but it would anyhow just pipe through standard output).
- Loading branch information
Showing
5 changed files
with
355 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
from ansible.module_utils.basic import AnsibleModule | ||
from ansible.module_utils.dokku_utils import subprocess_check_output | ||
|
||
DOCUMENTATION = """ | ||
--- | ||
module: dokku_acl_app | ||
short_description: Manage access control list for a given dokku application | ||
options: | ||
app: | ||
description: | ||
- The name of the app | ||
required: True | ||
default: null | ||
aliases: [] | ||
users: | ||
description: | ||
- The list of users who can manage the app | ||
required: True | ||
aliases: [] | ||
state: | ||
description: | ||
- Whether the ACLs should be present or absent | ||
required: False | ||
default: present | ||
choices: ["present", "absent" ] | ||
aliases: [] | ||
author: Leopold Talirz | ||
requirements: | ||
- the `dokku-acl` plugin | ||
""" | ||
|
||
EXAMPLES = """ | ||
- name: let leopold manage hello-world | ||
dokku_acl_app: | ||
app: hello-world | ||
users: | ||
- leopold | ||
- name: remove leopold from hello-world | ||
dokku_acl_app: | ||
app: hello-world | ||
users: | ||
- leopold | ||
state: absent | ||
""" | ||
|
||
|
||
def dokku_acl_app_set(data): | ||
is_error = True | ||
has_changed = False | ||
meta = {"present": False} | ||
|
||
has_changed = False | ||
|
||
# get users for app | ||
command = "dokku acl:list {0}".format(data["app"]) | ||
output, error = subprocess_check_output(command) | ||
|
||
if error is not None: | ||
meta["error"] = error | ||
return (is_error, has_changed, meta) | ||
|
||
users = set(output) | ||
|
||
if data["state"] == "absent": | ||
for user in data["users"]: | ||
if user not in users: | ||
continue | ||
|
||
command = "dokku --quiet acl:remove {0} {1}".format(data["app"], user) | ||
output, error = subprocess_check_output(command) | ||
has_changed = True | ||
if error is not None: | ||
meta["error"] = error | ||
return (is_error, has_changed, meta) | ||
else: | ||
for user in data["users"]: | ||
if user in users: | ||
continue | ||
|
||
command = "dokku --quiet acl:add {0} {1}".format(data["app"], user) | ||
output, error = subprocess_check_output(command) | ||
has_changed = True | ||
if error is not None: | ||
meta["error"] = error | ||
return (is_error, has_changed, meta) | ||
|
||
is_error = False | ||
return (is_error, has_changed, meta) | ||
|
||
|
||
def main(): | ||
fields = { | ||
"app": {"required": True, "type": "str"}, | ||
"users": {"required": True, "type": "list"}, | ||
"state": { | ||
"required": False, | ||
"default": "present", | ||
"choices": ["absent", "present"], | ||
"type": "str", | ||
}, | ||
} | ||
|
||
module = AnsibleModule(argument_spec=fields, supports_check_mode=False) | ||
is_error, has_changed, result = dokku_acl_app_set(module.params) | ||
|
||
if is_error: | ||
module.fail_json(msg=result["error"], meta=result) | ||
module.exit_json(changed=has_changed, meta=result) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
from ansible.module_utils.basic import AnsibleModule | ||
from ansible.module_utils.dokku_utils import subprocess_check_output | ||
|
||
DOCUMENTATION = """ | ||
--- | ||
module: dokku_acl_service | ||
short_description: Manage access control list for a given dokku service | ||
options: | ||
service: | ||
description: | ||
- The name of the service | ||
required: True | ||
default: null | ||
aliases: [] | ||
type: | ||
description: | ||
- The type of the service | ||
required: True | ||
default: null | ||
aliases: [] | ||
users: | ||
description: | ||
- The list of users who can manage the service | ||
required: True | ||
aliases: [] | ||
state: | ||
description: | ||
- Whether the ACLs should be present or absent | ||
required: False | ||
default: present | ||
choices: ["present", "absent" ] | ||
aliases: [] | ||
author: Leopold Talirz | ||
requirements: | ||
- the `dokku-acl` plugin | ||
""" | ||
|
||
EXAMPLES = """ | ||
- name: let leopold manage mypostgres postgres service | ||
dokku_acl_service: | ||
service: mypostgres | ||
type: postgres | ||
users: | ||
- leopold | ||
- name: remove leopold from mypostgres postgres service | ||
dokku_acl_service: | ||
service: hello-world | ||
type: postgres | ||
users: | ||
- leopold | ||
state: absent | ||
""" | ||
|
||
|
||
def dokku_acl_service_set(data): | ||
is_error = True | ||
has_changed = False | ||
meta = {"present": False} | ||
|
||
has_changed = False | ||
|
||
# get users for service | ||
command = "dokku --quiet acl:list {0}".format(data["service"]) | ||
output, error = subprocess_check_output(command) | ||
|
||
if error is not None: | ||
meta["error"] = error | ||
return (is_error, has_changed, meta) | ||
|
||
users = set(output) | ||
|
||
if data["state"] == "absent": | ||
for user in data["users"]: | ||
if user not in users: | ||
continue | ||
|
||
command = "dokku --quiet acl:remove {0} {1} {2}".format( | ||
data["type"], data["service"], user | ||
) | ||
output, error = subprocess_check_output(command) | ||
has_changed = True | ||
if error is not None: | ||
meta["error"] = error | ||
return (is_error, has_changed, meta) | ||
else: | ||
for user in data["users"]: | ||
if user in users: | ||
continue | ||
|
||
command = "dokku --quiet acl:add {0} {1} {2}".format( | ||
data["type"], data["service"], user | ||
) | ||
output, error = subprocess_check_output(command) | ||
has_changed = True | ||
if error is not None: | ||
meta["error"] = error | ||
return (is_error, has_changed, meta) | ||
|
||
is_error = False | ||
return (is_error, has_changed, meta) | ||
|
||
|
||
def main(): | ||
fields = { | ||
"service": {"required": True, "type": "str"}, | ||
"type": {"required": True, "type": "str"}, | ||
"users": {"required": True, "type": "list"}, | ||
"state": { | ||
"required": False, | ||
"default": "present", | ||
"choices": ["absent", "present"], | ||
"type": "str", | ||
}, | ||
} | ||
|
||
module = AnsibleModule(argument_spec=fields, supports_check_mode=False) | ||
is_error, has_changed, result = dokku_acl_service_set(module.params) | ||
|
||
if is_error: | ||
module.fail_json(msg=result["error"], meta=result) | ||
module.exit_json(changed=has_changed, meta=result) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters