Browse Source

update lesspass

master
Markus Bergholz 6 months ago
parent
commit
27db9ec1b8
1 changed files with 39 additions and 26 deletions
  1. +39
    -26
      ansible/lookup_plugins/lesspass.py

+ 39
- 26
ansible/lookup_plugins/lesspass.py View File

@@ -1,3 +1,7 @@
# -*- coding: utf-8 -*-
# Copyright: (c) 2020, Markus Bergholz <markuman@gmail.com>
# Copyright: (c) 2020, Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type

@@ -12,11 +16,11 @@ except ImportError:
import json
import yaml

try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
ANSIBLE_METADATA = {
'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'
}

DOCUMENTATION = """
lookup: lesspass
@@ -29,8 +33,9 @@ short_description: Calculates a password based on lesspass.
description:
- lesspass calculates the password based on a profile and a master password.
- The Minimum profile requirements is a login name and a site name.
- The profile can be read from file (.yml or .json), defined by options, or mixed (overwrite
- The profile can be read from file (.yml or .json), defined by options, or mixed (overwrite
profile file properties with options).
- See U(https://lesspass.com/)

options:
login:
@@ -69,30 +74,39 @@ options:


EXAMPLES = """
- debug: "msg={{ lookup('lesspass', 'password', profile='ansible-vault-private_key.json') }}"
- debug: "msg={{ lookup('lesspass', 'password', profile='ansible-vault-private_key.json', site='ansible.com') }}"
- debug: "msg={{ lookup('lesspass', 'password', profile='ansible-vault-private_key.json', site='ansible.com', login='alf') }}"
- debug: "msg={{ lookup('lesspass', 'password', site='ansible.com', login='alf') }}"
- debug: "msg={{ lookup('lesspass', 'password', site='ansible.com', login='alf', digits=false, symbols=false) }}"
# Read options from file.
- debug: "msg={{ lookup('lesspass', 'mypassword', profile='ansible-vault-private_key.json') }}"

# Read options from file, but overwrite 'site' property.
- debug: "msg={{ lookup('lesspass', 'mypassword', profile='ansible-vault-private_key.json', site='ansible.com') }}"

# Read options from file, but overwrite 'site' and 'login' property.
- debug: "msg={{ lookup('lesspass', 'mypassword', profile='ansible-vault-private_key.json', site='ansible.com', login='alf') }}"

# Use minimum requirements.
- debug: "msg={{ lookup('lesspass', 'mypassword', site='ansible.com', login='alf') }}"

# Use minimum requirements, but disable symbols.
- debug: "msg={{ lookup('lesspass', 'mypassword', site='ansible.com', login='alf', digits=false, symbols=false) }}"
"""


class LookupModule(LookupBase):

def run(self, terms, variables=None, login=None, site=None,
lowercase=None, uppercase=None, symbols=None, digits=None,
counter=None, length=None, profile=None):

if profile is not None:
if profile is not None:
try:
if profile.find(".yml") == (len(profile) -4):
if profile.find(".yml") == (len(profile) - 4):
profile = yaml.load(open(profile))
elif profile.find(".json") == (len(profile) -5):
elif profile.find(".json") == (len(profile) - 5):
profile = json.load(open(profile))
else:
raise AnsibleParserError()
except AnsibleParserError:
raise AnsibleError("Cannot open or parse lesspass profile: %s" % profile)
raise AnsibleError("Cannot open or parse lesspass profile: %s" % profile)

# overwrite profile items if given
if profile is not None:
@@ -105,20 +119,19 @@ class LookupModule(LookupBase):
profile['counter'] = counter or profile.get('counter')
profile['length'] = length or profile.get('length')


# build profile of no profile file was given
# login and site are required in this case
if profile is None and login is not None and site is not None:
profile = {'login': login,
'site': site,
'lowercase': lowercase or True,
'uppercase': uppercase or True,
'symbols': symbols or True,
'digits': digits or True,
'counter': 1,
'length': 32}
'site': site,
'lowercase': lowercase or True,
'uppercase': uppercase or True,
'symbols': symbols or True,
'digits': digits or True,
'counter': 1,
'length': 32}
if profile is None:
raise AnsibleError("A profile or a pair of login and site is a minimum requirement")

return [generate_password(profile, terms[0])]
return [generate_password(profile, terms[0])]

Loading…
Cancel
Save