Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use new admin webapp REST APIs #19

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions imageroot/actions/configure-module/15start_service
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@
#
# Redirect any output to the journal (stderr)

set -e

exec 1>&2

systemctl --user start postgres
systemctl --user start webapp

podman exec -i postgres sh -s <<'EOF'
query="SELECT EXISTS ( SELECT * FROM pg_tables WHERE schemaname = 'core' AND tablename = 'settings');"
psql -q -U postgres webtop5 -tA -c "$query" 2> /dev/null | grep -q t
db_check=$?
curl -s -f "http://localhost:$TCP_PORT/webtop/api/com.sonicle.webtop.core/v1/admin/settings" \
-H "Authorization: Bearer $WEBAPP_API_TOKEN" \
-H 'accept: application/json'> /dev/null
webapp_check=$?
c=10
while [ "$db_check" -ne 0 -o $c -eq 0 ]; do
while [ "$webapp_check" -ne 0 -o $c -eq 0 ]; do
sleep 1s
psql -q -U postgres webtop5 -tA -c "$query" 2> /dev/null | grep -q t
db_check=$?
curl -s -f "http://localhost:$TCP_PORT/webtop/api/com.sonicle.webtop.core/v1/admin/settings" \
-H "Authorization: Bearer $WEBAPP_API_TOKEN" \
-H 'accept: application/json' > /dev/null
webapp_check=$?
c=$(expr $c - 1 )
done
EOF
182 changes: 88 additions & 94 deletions imageroot/actions/configure-module/20config
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,10 @@ import sys
import os
import agent
import agent.ldapproxy
import subprocess

def domain_setup(user_domain_name, user_domain):
user_domain_uri = "ldapneth://accountprovider" + ":" + user_domain["port"]
user_domain_admin = user_domain["bind_dn"]

user_domain_password = subprocess.check_output(['podman', 'run', '--rm', os.environ["WEBTOP_WEBAPP_IMAGE"], 'bash', '-c', "echo -n " + user_domain["bind_password"] + " | java -classpath /usr/share/webtop/ WebtopPassEncode"], text=True).splitlines().pop()

user_domain_parameters = {
"loginDn": user_domain["base_dn"],
"loginFilter": None,
"userDn": user_domain["base_dn"],
"userFilter": None,
"userIdField": "uid",
"userFirstnameField": "givenName",
"userLastnameField": "sn",
"userDisplayNameField": "gecos",
}

if user_domain["schema"] == "ad":
user_domain_parameters["loginFilter"] = "&(objectCategory=person)(objectClass=user)"
user_domain_parameters["userIdField"] = "sAMAccountName"
user_domain_parameters["userFilter"] = "(&(objectClass=user)(objectCategory=person)(!(isCriticalSystemObject=TRUE)))"
user_domain_parameters["userDisplayNameField"] = "displayName"

with subprocess.Popen(['podman', 'exec', '-i', 'postgres', 'psql', '-U', 'postgres', 'webtop5'], stdin=subprocess.PIPE, text=True) as psql:
print("DELETE FROM core.domains WHERE domain_id = 'NethServer';\n", file=psql.stdin)
print("INSERT INTO core.domains (domain_id, internet_name, enabled, description, user_auto_creation, dir_uri, dir_admin, dir_password, dir_connection_security, dir_case_sensitive, dir_password_policy, dir_parameters) VALUES ('NethServer', '" + user_domain_name + "', 't', 'NethServer', 't', '" + user_domain_uri + "', '" + user_domain_admin + "', '" + user_domain_password + "', null, 'f', 'f', '" + json.dumps(user_domain_parameters) + "');\n", file=psql.stdin)

agent.set_env("USER_DOMAIN_PORT", user_domain["port"])
import urllib.request

api_endpoint = "http://localhost:" + os.environ["TCP_PORT"] + "/webtop/api/com.sonicle.webtop.core/v1"
api_headers={"Content-Type": "application/json", "Authorization": "Bearer " + os.environ["WEBAPP_API_TOKEN"]}

# Try to parse the stdin as JSON.
# If parsing fails, output everything to stderr
Expand Down Expand Up @@ -74,24 +47,19 @@ response = agent.tasks.run(
# Check if traefik configuration has been successfull
agent.assert_exp(response['exit_code'] == 0)

if data["hostname"] != os.getenv("WEBTOP_HOSTNAME"):

public_url = 'https://' + data["hostname"] + '/webtop'
dav_url = 'https://' + data["hostname"] + '/webtop-dav/server.php'
# Configure the webapp
public_url = 'https://' + data["hostname"] + '/webtop'
dav_url = 'https://' + data["hostname"] + '/webtop-dav/server.php'

with subprocess.Popen(['podman', 'exec', '-i', 'postgres', 'psql', '-U', 'postgres', 'webtop5'], stdout=sys.stderr, stdin=subprocess.PIPE, text=True) as psql:
print("DELETE FROM \"core\".\"settings\" WHERE service_id = 'com.sonicle.webtop.core' and key = 'public.url';\n", file=psql.stdin)
print("INSERT INTO \"core\".\"settings\" (\"service_id\", \"key\", \"value\") VALUES ('com.sonicle.webtop.core', 'public.url', \'" + public_url + "\');\n", file=psql.stdin)
try:
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/public.url", data=public_url.encode(), headers=api_headers, method='PUT'))
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/davserver.url", data=dav_url.encode(), headers=api_headers, method='PUT'))
except urllib.error.URLError as e:
raise Exception(f'Error reaching webapp daemon: {e.reason}')

print("DELETE FROM \"core\".\"settings\" WHERE service_id = 'com.sonicle.webtop.core' and key = 'davserver.url';\n", file=psql.stdin)
print("INSERT INTO \"core\".\"settings\" (\"service_id\", \"key\", \"value\") VALUES ('com.sonicle.webtop.core', 'davserver.url',\'" + dav_url + "\');\n", file=psql.stdin)
agent.set_env("WEBTOP_HOSTNAME", data["hostname"])

agent.assert_exp(psql.returncode == 0) # check the command is succesfull

agent.set_env("WEBTOP_HOSTNAME", data["hostname"])
agent.set_env("RESTART_WEBAPP", "1")

if "locale" in data and data["locale"] != os.environ["WEBTOP_LOCALE"]:
if "locale" in data:

locales = {
'it_IT': {
Expand Down Expand Up @@ -152,40 +120,31 @@ if "locale" in data and data["locale"] != os.environ["WEBTOP_LOCALE"]:
}
}

with subprocess.Popen(['podman', 'exec', '-i', 'postgres', 'psql', '-U', 'postgres', 'webtop5'], stdout=sys.stderr, stdin=subprocess.PIPE, text=True) as psql:
print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'default.i18n.languageTag';\n", file=psql.stdin)
print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'default.i18n.startDay';\n", file=psql.stdin)
print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'default.i18n.format.date.short';\n", file=psql.stdin)
print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'default.i18n.format.date.long';\n", file=psql.stdin)
print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'default.i18n.format.time.short';\n", file=psql.stdin)
print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'default.i18n.format.time.long';\n", file=psql.stdin)

print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.languageTag', '" + locales[data['locale']]['LANGUAGE'] + "');\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.startDay', '" + locales[data['locale']]['START_DAY']+ "');\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.format.date.short', '" + locales[data['locale']]['DATE_FORMAT_SHORT'] + "');\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.format.date.long', '" + locales[data['locale']]['DATE_FORMAT_LONG'] + "');\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.format.time.short', '" + locales[data['locale']]['TIME_FORMAT_SHORT'] + "');\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.format.time.long', '" + locales[data['locale']]['TIME_FORMAT_LONG'] + "');\n", file=psql.stdin)

agent.assert_exp(psql.returncode == 0) # check the command is succesfull
try:
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.languageTag", data=locales[data['locale']]['LANGUAGE'].encode(), headers=api_headers, method='PUT'))
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.startDay", data=locales[data['locale']]['START_DAY'].encode(), headers=api_headers, method='PUT'))
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.format.date.short", data=locales[data['locale']]['DATE_FORMAT_SHORT'].encode(), headers=api_headers, method='PUT'))
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.format.date.long", data=locales[data['locale']]['DATE_FORMAT_LONG'].encode(), headers=api_headers, method='PUT'))
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.format.time.short", data=locales[data['locale']]['TIME_FORMAT_SHORT'].encode(), headers=api_headers, method='PUT'))
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.format.time.long", locales[data['locale']]['TIME_FORMAT_LONG'].encode(), headers=api_headers, method='PUT'))
except urllib.error.URLError as e:
raise Exception(f'Error reaching webapp daemon: {e.reason}')

agent.set_env("WEBTOP_LOCALE", data["locale"])
agent.set_env("RESTART_WEBAPP", "1")

if "timezone" in data and data["timezone"] != os.environ["WEBTOP_TIMEZONE"]:
with subprocess.Popen(['podman', 'exec', '-i', 'postgres', 'psql', '-U', 'postgres', 'webtop5'], stdout=sys.stderr, stdin=subprocess.PIPE, text=True) as psql:
print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'default.i18n.timezone';\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'default.i18n.timezone','" + data["timezone"] + "');\n", file=psql.stdin)

agent.assert_exp(psql.returncode == 0) # check the command is succesfull
if "timezone" in data:
try:
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/default.i18n.timezone", data=data["timezone"].encode(), headers=api_headers, method='PUT'))
except urllib.error.URLError as e:
raise Exception(f'Error reaching webapp daemon: {e.reason}')

agent.set_env("WEBTOP_TIMEZONE", data["timezone"])

agent.set_env("RESTART_WEBAPP", "1")
agent.set_env("RESTART_WEBDAV", "1")
agent.set_env("RESTART_Z_PUSH", "1")

if "mail_module" in data and data["mail_module"] != os.getenv("MAIL_MODULE"):
if "mail_module" in data:
mail_module = data["mail_module"]

smtp_host = rdb.hget(f"module/{mail_module}/srv/tcp/submission", "host") or ""
Expand All @@ -207,38 +166,73 @@ if "mail_module" in data and data["mail_module"] != os.getenv("MAIL_MODULE"):

vmail_password = response['output']['password']

with subprocess.Popen(['podman', 'exec', '-i', 'postgres', 'psql', '-U', 'postgres', 'webtop5'], stdout=sys.stderr, stdin=subprocess.PIPE, text=True) as psql:
print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'smtp.host';\n", file=psql.stdin)
print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.core' and key = 'smtp.port';\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'smtp.host', '" + smtp_host + "');\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.core', 'smtp.port', '" + smtp_port + "');\n", file=psql.stdin)

print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.mail' and key = 'default.host';\n", file=psql.stdin)
print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.mail' and key = 'default.port';\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.mail', 'default.host', '" + imap_host + "');\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.mail', 'default.port', '" + imap_port + "');\n", file=psql.stdin)

print("DELETE FROM core.settings WHERE service_id = 'com.sonicle.webtop.mail' and key = 'nethtop.vmail.secret';\n", file=psql.stdin)
print("INSERT INTO core.settings (service_id, key, value) VALUES ('com.sonicle.webtop.mail', 'nethtop.vmail.secret', '" + vmail_password + "');\n", file=psql.stdin)

agent.assert_exp(psql.returncode == 0) # check the command is succesfull
try:
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/smtp.host", smtp_host.encode(), headers=api_headers, method='PUT'))
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.core/smtp.port", smtp_port.encode(), headers=api_headers, method='PUT'))
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.mail/default.host", imap_host.encode(), headers=api_headers, method='PUT'))
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.mail/default.port", imap_port.encode(), headers=api_headers, method='PUT'))
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/settings/com.sonicle.webtop.mail/nethtop.vmail.secret", vmail_password.encode(), headers=api_headers, method='PUT'))
except urllib.error.URLError as e:
raise Exception(f'Error reaching webapp daemon: {e.reason}')

user_domain_name = rdb.hget(f"module/{mail_module}/srv/tcp/imap", "user_domain") or ""
user_domain = agent.ldapproxy.Ldapproxy().get_domain(user_domain_name) or {}

domain_setup(user_domain_name, user_domain)
user_domain_json = {
"enabled": True,
"displayName": "NethServer",
"authDomainName": user_domain_name,
"domainName": user_domain_name,
"publicURL": "https://" + data["hostname"] + "/webtop",
"userAutoCreation": True,
"dirUri": "ldapneth://accountprovider" + ":" + user_domain["port"],
"dirAdmin": user_domain["bind_dn"],
"dirPassword": user_domain["bind_password"],
"dirConnSecurity": "OFF",
"dirCaseSensitive": False,
"dirRawParameters": {
"loginDn": user_domain["base_dn"],
"loginFilter": None,
"userDn": user_domain["base_dn"],
"userFilter": None,
"userIdField": "uid",
"userFirstnameField": "givenName",
"userLastnameField": "sn",
"userDisplayNameField": "gecos"
},
"passwordPolicies": {
"minLength": None,
"complexity": False,
"avoidConsecutiveChars": False,
"avoidOldSimilarity": False,
"avoidUsernameSimilarity": False,
"expiration": None,
"verifyAtLogin": False
}
}

if user_domain["schema"] == "ad":
user_domain_json["dirRawParameters"]["loginFilter"] = "&(objectCategory=person)(objectClass=user)"
user_domain_json["dirRawParameters"]["userIdField"] = "sAMAccountName"
user_domain_json["dirRawParameters"]["userFilter"] = "(&(objectClass=user)(objectCategory=person)(!(isCriticalSystemObject=TRUE)))"
user_domain_json["dirRawParameters"]["userDisplayNameField"] = "displayName"

try:
#Check if the domain already exist
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/domains/NethServer", headers=api_headers))
#Update the doiman
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/domains/NethServer?update_options=3", json.dumps(user_domain_json).encode(), headers=api_headers, method='PUT'))
except urllib.error.HTTPError as e:
if e.code == 404:
# Create the doiman
user_domain_json["domainId"] = "NethServer"
urllib.request.urlopen(urllib.request.Request(f"{api_endpoint}/admin/domains", json.dumps(user_domain_json).encode(), headers=api_headers, method='POST'))
pass
except urllib.error.URLError as e:
raise Exception(f'Error reaching webapp daemon: {e.reason}')

agent.set_env("MAIL_MODULE", mail_module)
agent.set_env("RESTART_WEBAPP", "1")

# In case of module move/migrate/restore, check if the port of local the ldapproxy is changed
if "MAIL_MODULE" in os.environ and ("mail_module" not in data or data["mail_module"] == os.getenv("MAIL_MODULE")) :
user_domain_name = rdb.hget(f'module/{data["mail_module"]}/srv/tcp/imap', "user_domain") or ""
user_domain = agent.ldapproxy.Ldapproxy().get_domain(user_domain_name) or {}

if user_domain["port"] != os.environ["USER_DOMAIN_PORT"]:
domain_setup(user_domain_name, user_domain)
agent.set_env("RESTART_WEBAPP", "1")

if "webapp" in data:
if "debug" in data["webapp"] and data["webapp"]["debug"] != os.getenv("WEBAPP_JS_DEBUG"):
Expand Down
1 change: 1 addition & 0 deletions imageroot/scripts/expandconfig-webapp
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ webtop.log.target=console
webtop.session.forcesecurecookie=true
webtop.js.debug=$WEBAPP_JS_DEBUG
webtop.home=/var/lib/nethserver/webtop
webtop.provisioning.api.token=$WEBAPP_API_TOKEN
EOF