Skip to content

Commit

Permalink
[PR #6841/4d704c03 backport][stable-7] htpasswd: deprecate crypt_sche…
Browse files Browse the repository at this point in the history
…me (#6858)

htpasswd: deprecate crypt_scheme (#6841)

* htpasswd: rename crypt_scheme with hash_scheme

* add changelog frag

* fixed chglog frag

* adjusted code for parameter name change

(cherry picked from commit 4d704c0)

Co-authored-by: Alexei Znamensky <[email protected]>
  • Loading branch information
patchback[bot] and russoz authored Jul 6, 2023
1 parent 42cc528 commit 4fa1f1a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
2 changes: 2 additions & 0 deletions changelogs/fragments/6841-htpasswd-crypt-scheme.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
minor_changes:
- htpasswd - the parameter ``crypt_scheme`` is being renamed as ``hash_scheme`` and added as an alias to it (https://github.com/ansible-collections/community.general/pull/6841).
27 changes: 14 additions & 13 deletions plugins/modules/htpasswd.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,20 @@
description:
- Password associated with user.
- Must be specified if user does not exist yet.
crypt_scheme:
hash_scheme:
type: str
required: false
default: "apr_md5_crypt"
description:
- Encryption scheme to be used. As well as the four choices listed
- Hashing scheme to be used. As well as the four choices listed
here, you can also use any other hash supported by passlib, such as
V(portable_apache22) and V(host_apache24); or V(md5_crypt) and V(sha256_crypt),
which are Linux passwd hashes. Only some schemes in addition to
the four choices below will be compatible with Apache or Nginx, and
supported schemes depend on passlib version and its dependencies.
- See U(https://passlib.readthedocs.io/en/stable/lib/passlib.apache.html#passlib.apache.HtpasswdFile) parameter C(default_scheme).
- 'Some of the available choices might be: V(apr_md5_crypt), V(des_crypt), V(ldap_sha1), V(plaintext).'
aliases: [crypt_scheme]
state:
type: str
required: false
Expand Down Expand Up @@ -99,7 +100,7 @@
path: /etc/mail/passwords
name: alex
password: oedu2eGh
crypt_scheme: md5_crypt
hash_scheme: md5_crypt
"""


Expand Down Expand Up @@ -131,24 +132,24 @@ def create_missing_directories(dest):
os.makedirs(destpath)


def present(dest, username, password, crypt_scheme, create, check_mode):
def present(dest, username, password, hash_scheme, create, check_mode):
""" Ensures user is present
Returns (msg, changed) """
if crypt_scheme in apache_hashes:
if hash_scheme in apache_hashes:
context = htpasswd_context
else:
context = CryptContext(schemes=[crypt_scheme] + apache_hashes)
context = CryptContext(schemes=[hash_scheme] + apache_hashes)
if not os.path.exists(dest):
if not create:
raise ValueError('Destination %s does not exist' % dest)
if check_mode:
return ("Create %s" % dest, True)
create_missing_directories(dest)
if LooseVersion(passlib.__version__) >= LooseVersion('1.6'):
ht = HtpasswdFile(dest, new=True, default_scheme=crypt_scheme, context=context)
ht = HtpasswdFile(dest, new=True, default_scheme=hash_scheme, context=context)
else:
ht = HtpasswdFile(dest, autoload=False, default=crypt_scheme, context=context)
ht = HtpasswdFile(dest, autoload=False, default=hash_scheme, context=context)
if getattr(ht, 'set_password', None):
ht.set_password(username, password)
else:
Expand All @@ -157,9 +158,9 @@ def present(dest, username, password, crypt_scheme, create, check_mode):
return ("Created %s and added %s" % (dest, username), True)
else:
if LooseVersion(passlib.__version__) >= LooseVersion('1.6'):
ht = HtpasswdFile(dest, new=False, default_scheme=crypt_scheme, context=context)
ht = HtpasswdFile(dest, new=False, default_scheme=hash_scheme, context=context)
else:
ht = HtpasswdFile(dest, default=crypt_scheme, context=context)
ht = HtpasswdFile(dest, default=hash_scheme, context=context)

found = None
if getattr(ht, 'check_password', None):
Expand Down Expand Up @@ -215,7 +216,7 @@ def main():
path=dict(type='path', required=True, aliases=["dest", "destfile"]),
name=dict(type='str', required=True, aliases=["username"]),
password=dict(type='str', required=False, default=None, no_log=True),
crypt_scheme=dict(type='str', required=False, default="apr_md5_crypt"),
hash_scheme=dict(type='str', required=False, default="apr_md5_crypt", aliases=["crypt_scheme"]),
state=dict(type='str', required=False, default="present", choices=["present", "absent"]),
create=dict(type='bool', default=True),

Expand All @@ -227,7 +228,7 @@ def main():
path = module.params['path']
username = module.params['name']
password = module.params['password']
crypt_scheme = module.params['crypt_scheme']
hash_scheme = module.params['hash_scheme']
state = module.params['state']
create = module.params['create']
check_mode = module.check_mode
Expand Down Expand Up @@ -267,7 +268,7 @@ def main():

try:
if state == 'present':
(msg, changed) = present(path, username, password, crypt_scheme, create, check_mode)
(msg, changed) = present(path, username, password, hash_scheme, create, check_mode)
elif state == 'absent':
if not os.path.exists(path):
module.exit_json(msg="%s not present" % username,
Expand Down

0 comments on commit 4fa1f1a

Please sign in to comment.