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.
- Configure and start api-moduled instance to serve backend API - Configure Traefik path-based route for the web portal - Implement basic API: login and change-password handlers
- Loading branch information
1 parent
b09cd9b
commit 638fa6a
Showing
10 changed files
with
244 additions
and
4 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
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,44 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# | ||
# Copyright (C) 2023 Nethesis S.r.l. | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
|
||
import agent | ||
import agent.tasks | ||
import sys | ||
import os | ||
import string | ||
import secrets | ||
|
||
|
||
_, amld_port, _ = (os.environ['TCP_PORTS'] + ',,').split(",", 2) | ||
|
||
agent.assert_exp(int(amld_port) > 0) # Ensure TCP port for api-moduled was allocated | ||
|
||
alphabet = string.ascii_letters + string.digits + '+-/,.-_^' | ||
amld_secret = ''.join([secrets.choice(alphabet) for i in range(32)]) | ||
|
||
agent.write_envfile("api-moduled.env", { | ||
"AMLD_JWT_REALM": os.environ["LDAP_DOMAIN"], | ||
"AMLD_JWT_SECRET": amld_secret, | ||
"AMLD_BIND_ADDRESS": ":" + amld_port, | ||
"AMLD_EXPORT_ENV": "NODE_ID LDAP_DOMAIN REDIS_ADDRESS" | ||
}) | ||
|
||
# Configure Traefik to route "/user-admin/<LDAP_DOMAIN>" path requests to | ||
# the api-moduled backend service: | ||
response = agent.tasks.run( | ||
agent_id=agent.resolve_agent_id('traefik@node'), | ||
action='set-route', | ||
data={ | ||
'instance': os.environ['MODULE_ID'] + '-amld', | ||
'url': 'http://127.0.0.1:' + amld_port, | ||
'path': '/users-admin/' + os.environ['LDAP_DOMAIN'], | ||
'http2https': True, | ||
'strip_prefix': True, | ||
}, | ||
) | ||
|
||
agent.run_helper("systemctl", "-T", "--user", "enable", "--now", "api-moduled.service") |
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,19 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# | ||
# Copyright (C) 2023 Nethesis S.r.l. | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
|
||
import os | ||
import agent | ||
import agent.tasks | ||
|
||
# Remove traefik route | ||
response = agent.tasks.run( | ||
agent_id=agent.resolve_agent_id('traefik@node'), | ||
action='delete-route', | ||
data={ | ||
'instance': os.environ['MODULE_ID'] + '-amld' | ||
}, | ||
) |
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,54 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# | ||
# Copyright (C) 2023 Nethesis S.r.l. | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
|
||
import json | ||
import sys | ||
import os | ||
import agent | ||
import subprocess | ||
|
||
from agent.ldapproxy import Ldapproxy | ||
from agent.ldapclient import Ldapclient | ||
|
||
request = json.load(sys.stdin) | ||
|
||
odomain = Ldapproxy().get_domain(os.environ["LDAP_DOMAIN"]) | ||
|
||
oldapcli = Ldapclient.factory(**odomain) | ||
|
||
try: | ||
ouser = oldapcli.get_user_entry(os.environ["JWT_ID"]) | ||
except agent.ldapclient.exceptions.LdapclientEntryNotFound: | ||
json.dump({"status": "failure", "message": "user_not_found"}, fp=sys.stdout) | ||
sys.exit(0) | ||
|
||
oldapcli.ldapconn.unbind() # close LDAP connection FD | ||
|
||
# First attempt: try to change the password with simple bind, using the | ||
# user's credentials | ||
proc_lpasswd = subprocess.run(["podman", "exec", "-i", "openldap", | ||
"ldappasswd", "-e", "ppolicy", "-S", "-W", "-E", "-x", "-D", ouser["dn"]], | ||
input=request["current_password"] + "\n" + request["new_password"] + "\n" + request["new_password"] + "\n", | ||
text=True, capture_output=True) | ||
|
||
if proc_lpasswd.returncode == 49 and "Password expired" in proc_lpasswd.stderr: | ||
print(agent.SD_WARNING + f"User {os.environ['JWT_ID']} is changing their expired password", file=sys.stderr) | ||
# Second attempt: use SASL EXTERNAL authentication to get admin | ||
# privileges and work around the expired password bind error. As | ||
# admins have ACL "write" permission on userPassword field, the | ||
# password quality checks are still applied: | ||
proc_lpasswd = subprocess.run(["podman", "exec", "-i", "openldap", | ||
"ldappasswd", "-e", "ppolicy", "-T", "/dev/stdin", ouser["dn"]], | ||
input=request["new_password"], text=True, capture_output=True) | ||
|
||
# Log the last command output: it might contain troubleshoot information! | ||
print(proc_lpasswd.stdout, file=sys.stderr) | ||
|
||
if proc_lpasswd.returncode == 0: | ||
json.dump({"status": "success", "message": "password_changed"}, fp=sys.stdout) | ||
else: | ||
json.dump({"status": "failure", "message": proc_lpasswd.stdout or f"ldap_error_{proc_lpasswd.returncode}"}, fp=sys.stdout) |
18 changes: 18 additions & 0 deletions
18
imageroot/api-moduled/handlers/change-password/validate-input.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,18 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "change-password input", | ||
"$id": "http://schema.nethserver.org/ns8-openldap/api-moduled/handlers/change-password/validate-input.json", | ||
"type": "object", | ||
"required": [ | ||
"current_password", | ||
"new_password" | ||
], | ||
"properties": { | ||
"current_password": { | ||
"type": "string" | ||
}, | ||
"new_password": { | ||
"type": "string" | ||
} | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
imageroot/api-moduled/handlers/change-password/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": "change-password output", | ||
"$id": "http://schema.nethserver.org/ns8-openldap/api-moduled/handlers/change-password/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#!/usr/bin/env python3 | ||
|
||
# | ||
# Copyright (C) 2023 Nethesis S.r.l. | ||
# SPDX-License-Identifier: GPL-3.0-or-later | ||
# | ||
|
||
import json | ||
import sys | ||
import os | ||
import agent | ||
import subprocess | ||
|
||
from agent.ldapproxy import Ldapproxy | ||
from agent.ldapclient import Ldapclient | ||
|
||
request = json.load(sys.stdin) | ||
|
||
odomain = Ldapproxy().get_domain(os.environ["LDAP_DOMAIN"]) | ||
|
||
oldapcli = Ldapclient.factory(**odomain) | ||
|
||
try: | ||
user_dn = oldapcli.get_user_entry(request['username'])["dn"] | ||
ouser = oldapcli.get_user(request['username']) | ||
except agent.ldapclient.exceptions.LdapclientEntryNotFound: | ||
sys.exit(2) # User not found | ||
|
||
proc_whoami = subprocess.run(["podman", "exec", "-i", "openldap", | ||
"ldapwhoami", "-x", "-e", "ppolicy", "-D", user_dn, "-y", "/dev/stdin"], | ||
input=request["password"], text=True, capture_output=True) | ||
|
||
oclaims = { | ||
"uid": ouser["user"], | ||
"groups": ouser["groups"], | ||
} | ||
|
||
if proc_whoami.returncode == 49 and "Password expired" in proc_whoami.stderr: | ||
# Password must be changed immediately: return a token limited to | ||
# password changing: | ||
oclaims["scope"] = ["change-password"] | ||
elif proc_whoami.returncode != 0: | ||
sys.exit(3) # Login failed | ||
|
||
json.dump(oclaims, fp=sys.stdout) |
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,18 @@ | ||
{ | ||
"$schema": "http://json-schema.org/draft-07/schema#", | ||
"title": "login input", | ||
"$id": "http://schema.nethserver.org/ns8-openldap/api-moduled/handlers/login/validate-input.json", | ||
"type": "object", | ||
"required": [ | ||
"username", | ||
"password" | ||
], | ||
"properties": { | ||
"username": { | ||
"type": "string" | ||
}, | ||
"password": { | ||
"type": "string" | ||
} | ||
} | ||
} |
Empty file.