From 508c1d8256fc9c118d14eaac110c14b453263454 Mon Sep 17 00:00:00 2001 From: Michael DeHaan Date: Sat, 20 Jul 2013 18:22:58 -0400 Subject: [PATCH] Update htpasswd module for various API incompatibilities --- library/web_infrastructure/htpasswd | 39 ++++++++++++++++++++++++----- 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/library/web_infrastructure/htpasswd b/library/web_infrastructure/htpasswd index 1fe2b6e22c7..62822c0a6ea 100644 --- a/library/web_infrastructure/htpasswd +++ b/library/web_infrastructure/htpasswd @@ -92,17 +92,40 @@ def present(dest, username, password, create, check_mode): if check_mode: return ("Create %s" % dest, True) create_missing_directories(dest) - ht = HtpasswdFile(dest, new=True) - ht.set_password(username, password) + try: + ht = HtpasswdFile(dest, new=True) + except: + # library version doesn't take 'new', deal with it. + fh = open(dest, 'w') + fh.write('') + fh.close() + ht = HtpasswdFile(dest) + if getattr(ht, 'set_password', None): + ht.set_password(username, password) + else: + ht.update(username, password) ht.save() return ("Created %s and added %s" % (dest, username), True) else: - ht = HtpasswdFile(dest, new=False) - if ht.check_password(username, password): + try: + ht = HtpasswdFile(dest, new=False) + except: + ht = HtpasswdFile(dest) + + found = None + if getattr(ht, 'check_password', None): + found = ht.check_password(username, password) + else: + found = ht.verify(username, password) + + if found: return ("%s already present" % username, False) else: if not check_mode: - ht.set_password(username, password) + if getattr(ht, 'set_password', None): + ht.set_password(username, password) + else: + ht.update(username, password) ht.save() return ("Add/update %s" % username, True) @@ -114,7 +137,11 @@ def absent(dest, username, check_mode): if not os.path.exists(dest): raise ValueError("%s does not exists" % dest) - ht = HtpasswdFile(dest, new=False) + try: + ht = HtpasswdFile(dest, new=False) + except: + ht = HtpasswdFile(dest) + if username not in ht.users(): return ("%s not present" % username, False) else: