Browse Source

#14 first draft for password modules

main
Markus Bergholz 1 month ago
parent
commit
7b96818954
Signed by: m GPG Key ID: B45724801354B174
  1. 2
      galaxy.yml
  2. 20
      plugins/lookup/passwords.py
  3. 20
      plugins/module_utils/nextcloud.py
  4. 40
      plugins/modules/password_info.py
  5. 7
      tests/passwords.yml

2
galaxy.yml

@ -1,7 +1,7 @@
namespace: "markuman"
description: bring up your nextcloud usage to the next level 🚀
name: "nextcloud"
version: "6.0.0"
version: "7.0.0"
readme: "README.md"
authors:
- "Markus Bergholz <markuman+spambelongstogoogle@gmail.com>"

20
plugins/lookup/passwords.py

@ -56,23 +56,5 @@ class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
nc = NextcloudHandler(kwargs)
r = nc.get("index.php/apps/passwords/api/1.0/password/list")
details = kwargs.get('details') or False
ret = []
for term in terms:
try:
if r.status_code == 200:
for item in r.json():
if item['label'] == term:
if details:
ret.append(item)
else:
ret.append(item['password'])
else:
raise AnsibleParserError()
except AnsibleParserError:
raise AnsibleError("nextcloud responded with status code: %d" % r.status_code)
return ret
return nc.list_passwords(term)

20
plugins/module_utils/nextcloud.py

@ -17,6 +17,8 @@ class NextcloudHandler:
elif kwargs.get('ssl_mode') == 'skip':
self.ssl = False
self.details = kwargs.get('details') or False
self.HOST = kwargs.get('host') or os.environ.get('NEXTCLOUD_HOST')
if self.HOST is None:
raise AnsibleError('Unable to continue. No Nextcloud Host is given.')
@ -94,6 +96,24 @@ class NextcloudHandler:
else:
status_code_error(r.status_code)
def list_passwords(self, term):
r = self.get("index.php/apps/passwords/api/1.0/password/list")
ret = []
try:
if r.status_code == 200:
for item in r.json():
if item['label'] == term:
if self.details:
ret.append(item)
else:
ret.append(item['password'])
else:
status_code_error(r.status_code)
except AnsibleParserError:
status_code_error(r.status_code)
return ret
def user(self):
return self.USER

40
plugins/modules/password_info.py

@ -0,0 +1,40 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
DOCUMENTATION = '''
module: markuman.nextcloud.password_info
short_description: module to fetch password details
description:
- Add, remove, enable or disable users
version_added: "7.0.0"
author:
- "Markus Bergholz"
requirements:
- requests python module
'''
EXAMPLES = '''
'''
from ansible.module_utils.basic import *
from ansible_collections.markuman.nextcloud.plugins.module_utils.nextcloud import NextcloudHandler
def main():
module = AnsibleModule(
argument_spec = dict(
host = dict(required=False, type='str'),
api_token = dict(required=False, type='str', no_log=True, aliases=['access_token']),
ssl_mode = dict(required=False, type='str', default='https'),
name = dict(required=True, type='str')
)
)
module.params["details"] = True
nc = NextcloudHandler(module.params)
retval = nc.list_passwords(module.params.get('name'))
module.exit_json(**retval[0])
if __name__ == '__main__':
main()

7
tests/passwords.yml

@ -7,3 +7,10 @@
- name: Retrieve Password with label "dokuwiki"
debug:
var: lookup('markuman.nextcloud.passwords', 'dokuwiki')
- name: password info test
markuman.nextcloud.password_info:
name: dokuwiki
register: out
- debug: msg="{{ out }}"
Loading…
Cancel
Save