generated from NethServer/ns8-kickstart
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from NethServer/user-manager-password-policy
feat(api-moduled): added get-password-policy api
- Loading branch information
Showing
4 changed files
with
99 additions
and
3 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,75 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# | ||
# Copyright (C) 2024 Nethesis S.r.l. | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
|
||
import json | ||
import sys | ||
import subprocess | ||
import os | ||
|
||
SECONDS_PER_DAY = 86400 | ||
ldap_suffix = os.environ["LDAP_SUFFIX"] | ||
|
||
ldapsearch_proc = subprocess.run(["podman", "exec", "openldap", | ||
"ldapsearch", "-LLL", "-b", f"cn=default,ou=PPolicy,{ldap_suffix}", "-s", "base" | ||
], text=True, capture_output=True, check=True) | ||
|
||
|
||
ppolicy = { | ||
"expiration": { | ||
"min_age": 0, | ||
"max_age": 0, | ||
"enforced": False, | ||
}, | ||
"strength": { | ||
"enforced": False, | ||
"history_length": 0, | ||
"password_min_length": 0, | ||
"complexity_check": False, | ||
} | ||
} | ||
|
||
for line in ldapsearch_proc.stdout.split("\n"): | ||
if not line: | ||
continue | ||
|
||
lattr, lval = line.split(":", 1) | ||
lval = lval.strip() # trim wrapping blanks | ||
|
||
if lattr == "pwdMinAge": | ||
ppolicy["expiration"]["min_age"] = int(lval) // SECONDS_PER_DAY | ||
elif lattr == "pwdMaxAge": | ||
ppolicy["expiration"]["max_age"] = int(lval) // SECONDS_PER_DAY | ||
elif lattr == "pwdMinLength": | ||
ppolicy["strength"]["password_min_length"] = int(lval) | ||
elif lattr == "pwdInHistory": | ||
ppolicy["strength"]["history_length"] = int(lval) | ||
elif lattr == "pwdUseCheckModule": | ||
ppolicy["strength"]["complexity_check"] = lval == 'TRUE' | ||
|
||
if ppolicy["strength"]["complexity_check"] is True or \ | ||
ppolicy["strength"]["history_length"] > 0 or \ | ||
ppolicy["strength"]["password_min_length"] > 0: | ||
ppolicy["strength"]["enforced"] = True | ||
else: | ||
ppolicy["strength"]["enforced"] = False | ||
ppolicy["strength"]["history_length"] = 12 | ||
ppolicy["strength"]["password_min_length"] = 8 | ||
ppolicy["strength"]["complexity_check"] = True | ||
|
||
if ppolicy["expiration"]["min_age"] > 0 or \ | ||
ppolicy["expiration"]["max_age"] > 0: | ||
ppolicy["expiration"]["enforced"] = True | ||
else: | ||
ppolicy["expiration"]["enforced"] = False | ||
ppolicy["expiration"]["min_age"] = 0 | ||
ppolicy["expiration"]["max_age"] = 180 | ||
|
||
json.dump({ | ||
"status": "success", | ||
"message": "password_policy", | ||
"policy": ppolicy | ||
}, fp=sys.stdout) |
21 changes: 21 additions & 0 deletions
21
imageroot/api-moduled/handlers/get-password-policy/validate-output.json
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,21 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "get password policy", | ||
"$id": "http://schema.nethserver.org/ns8-openldap/api-moduled/handlers/get-password-policy/validate-output.json", | ||
"type": "object", | ||
"required": [ | ||
"status", | ||
"message" | ||
], | ||
"properties": { | ||
"status": { | ||
"enum": [ | ||
"success", | ||
"failure" | ||
] | ||
}, | ||
"message": { | ||
"type": "string" | ||
} | ||
} | ||
} |
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