Skip to content

Commit

Permalink
move hardcoded path to os.path.join (Open-MSS#2090)
Browse files Browse the repository at this point in the history
  • Loading branch information
nilupulmanodya authored Nov 14, 2023
1 parent be3e513 commit 83b3985
Showing 1 changed file with 16 additions and 14 deletions.
30 changes: 16 additions & 14 deletions mslib/mscolab/mscolab.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,10 @@ def handle_mscolab_certificate_init():

try:
cmd = ["openssl", "req", "-newkey", "rsa:4096", "-keyout",
f"{mscolab_settings.MSCOLAB_SSO_DIR}/key_mscolab.key",
os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "key_mscolab.key"),
"-nodes", "-x509", "-days", "365", "-batch", "-subj",
"/CN=localhost", "-out", f"{mscolab_settings.MSCOLAB_SSO_DIR}/crt_mscolab.crt"]
"/CN=localhost", "-out", os.path.join(mscolab_settings.MSCOLAB_SSO_DIR,
"crt_mscolab.crt")]
subprocess.run(cmd, check=True)
logging.info("generated CRTs for the mscolab server.")
return True
Expand All @@ -117,9 +118,9 @@ def handle_local_idp_certificate_init():

try:
cmd = ["openssl", "req", "-newkey", "rsa:4096", "-keyout",
f"{mscolab_settings.MSCOLAB_SSO_DIR}/key_local_idp.key",
os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "key_local_idp.key"),
"-nodes", "-x509", "-days", "365", "-batch", "-subj",
"/CN=localhost", "-out", f"{mscolab_settings.MSCOLAB_SSO_DIR}/crt_local_idp.crt"]
"/CN=localhost", "-out", os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "crt_local_idp.crt")]
subprocess.run(cmd, check=True)
logging.info("generated CRTs for the local identity provider")
return True
Expand Down Expand Up @@ -250,7 +251,7 @@ def handle_mscolab_backend_yaml_init():
# name_id_format_allow_create: true
"""
try:
file_path = f"{mscolab_settings.MSCOLAB_SSO_DIR}/mss_saml2_backend.yaml"
file_path = os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "mss_saml2_backend.yaml")
with open(file_path, "w", encoding="utf-8") as file:
file.write(saml_2_backend_yaml_content)
return True
Expand All @@ -271,14 +272,15 @@ def handle_mscolab_metadata_init(repo_exists):
print('generating metadata file for the mscolab server')

try:
command = ["python", "mslib/mscolab/mscolab.py", "start"] if repo_exists else ["mscolab", "start"]
command = ["python", os.path.join("mslib", "mscolab", "mscolab.py"),
"start"] if repo_exists else ["mscolab", "start"]
process = subprocess.Popen(command)

# Add a small delay to allow the server to start up
time.sleep(10)

cmd_curl = ["curl", "http://localhost:8083/metadata/localhost_test_idp",
"-o", f"{mscolab_settings.MSCOLAB_SSO_DIR}/metadata_sp.xml"]
"-o", os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "metadata_sp.xml")]
subprocess.run(cmd_curl, check=True)
process.terminate()
logging.info('mscolab metadata file generated succesfully')
Expand All @@ -293,27 +295,27 @@ def handle_local_idp_metadata_init(repo_exists):
print('generating metadata for localhost identity provider')

try:
if os.path.exists(f"{mscolab_settings.MSCOLAB_SSO_DIR}/idp.xml"):
os.remove(f"{mscolab_settings.MSCOLAB_SSO_DIR}/idp.xml")
if os.path.exists(os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "idp.xml")):
os.remove(os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "idp.xml"))

idp_conf_path = "mslib/msidp/idp_conf.py"
idp_conf_path = os.path.join("mslib", "msidp", "idp_conf.py")

if not repo_exists:
import site
site_packages_path = site.getsitepackages()[0]
idp_conf_path = os.path.join(site_packages_path, "mslib/msidp/idp_conf.py")
idp_conf_path = os.path.join(site_packages_path, "mslib", "msidp", "idp_conf.py")

cmd = ["make_metadata", idp_conf_path]

with open(f"{mscolab_settings.MSCOLAB_SSO_DIR}/idp.xml",
with open(os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "idp.xml"),
"w", encoding="utf-8") as output_file:
subprocess.run(cmd, stdout=output_file, check=True)
logging.info("idp metadata file generated succesfully")
return True
except subprocess.CalledProcessError as error:
# Delete the idp.xml file when the subprocess fails
if os.path.exists(f"{mscolab_settings.MSCOLAB_SSO_DIR}/idp.xml"):
os.remove(f"{mscolab_settings.MSCOLAB_SSO_DIR}/idp.xml")
if os.path.exists(os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "idp.xml")):
os.remove(os.path.join(mscolab_settings.MSCOLAB_SSO_DIR, "idp.xml"))
print(f"Error while generating metadata for localhost identity provider: {error}")
return False

Expand Down

0 comments on commit 83b3985

Please sign in to comment.