fix: replace json by re

This commit is contained in:
dettlaff 2024-11-21 21:01:29 +04:00
parent ecbe52e22c
commit 74b3046e5d

View file

@ -3,7 +3,7 @@ from typing import Optional
import os import os
import subprocess import subprocess
import requests import requests
import json import re
from contextlib import contextmanager from contextlib import contextmanager
from selfprivacy_api.utils import get_domain from selfprivacy_api.utils import get_domain
@ -53,29 +53,17 @@ class KanidmAdminToken:
@staticmethod @staticmethod
def create_and_save_token(kanidm_admin_password: str) -> str: def create_and_save_token(kanidm_admin_password: str) -> str:
with temporary_env_var(key="KANIDM_PASSWORD", value=kanidm_admin_password): with temporary_env_var(key="KANIDM_PASSWORD", value=kanidm_admin_password):
# kanidm_admin_token = subprocess.check_output( kanidm_admin_token = subprocess.check_output(
# [ [
# "kanidm", "kanidm",
# "service-account", "service-account",
# "api-token", "api-token",
# "generate", "generate",
# "--rw", "--rw",
# "selfprivacy", "selfprivacy",
# "token2", "token2",
# ] ]
# ) )
try:
kanidm_admin_token = (
subprocess.check_output(
"kanidm service-account api-token generate --rw selfprivacy token2",
shell=True,
stderr=subprocess.STDOUT,
)
.decode("utf-8")
.strip()
)
except subprocess.CalledProcessError as e:
print(e.output.decode())
redis.set("kanidm:token", kanidm_admin_token) redis.set("kanidm:token", kanidm_admin_token)
return kanidm_admin_token return kanidm_admin_token
@ -95,10 +83,11 @@ class KanidmAdminToken:
stderr=subprocess.DEVNULL, stderr=subprocess.DEVNULL,
).decode("utf-8") ).decode("utf-8")
data = json.loads(output) new_kanidm_admin_password = re.search(r'{"password":"([^"]+)"}', output).group(
new_kanidm_admin_password = data.get("password", "").strip() 1
)
redis.set("kanidm:password", new_kanidm_admin_password) redis.set("kanidm:password", new_kanidm_admin_password)
return new_kanidm_admin_password return new_kanidm_admin_password