Skip to content

Commit

Permalink
fix: Converting p12 files with filenames containing special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisburr committed Dec 15, 2023
1 parent c619b45 commit 486f8dd
Showing 1 changed file with 15 additions and 9 deletions.
24 changes: 15 additions & 9 deletions src/DIRAC/Core/scripts/dirac_cert_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import os
import sys
import shutil
import subprocess
from datetime import datetime
from subprocess import PIPE, run, STDOUT
from tempfile import TemporaryDirectory

from DIRAC import gLogger
from DIRAC.Core.Utilities.Subprocess import shellCall
from DIRAC.Core.Base.Script import Script


Expand Down Expand Up @@ -38,15 +40,19 @@ def main():
shutil.move(old, old + nowPrefix)

# new OpenSSL version require OPENSSL_CONF to point to some accessible location',
gLogger.notice("Converting p12 key to pem format")
result = shellCall(900, f"export OPENSSL_CONF=/tmp && openssl pkcs12 -nocerts -in {p12} -out {key}")
# The last command was successful
if result["OK"] and result["Value"][0] == 0:
gLogger.notice("Converting p12 certificate to pem format")
result = shellCall(900, f"export OPENSSL_CONF=/tmp && openssl pkcs12 -clcerts -nokeys -in {p12} -out {cert}")
with TemporaryDirectory() as tmpdir:
env = os.environ | {"OPENSSL_CONF": tmpdir}
gLogger.notice("Converting p12 key to pem format")
cmd = ["openssl", "pkcs12", "-nocerts", "-in", p12, "-out", key]
res = run(cmd, env=env, check=False, timeout=900, text=True, stdout=PIPE, stderr=STDOUT)
# The last command was successful
if res.returncode == 0:
gLogger.notice("Converting p12 certificate to pem format")
cmd = ["openssl", "pkcs12", "-clcerts", "-nokeys", "-in", p12, "-out", cert]
res = run(cmd, env=env, check=False, timeout=900, text=True, stdout=PIPE, stderr=STDOUT)
# Something went wrong
if not result["OK"] or result["Value"][0] != 0:
gLogger.fatal(result.get("Message", result["Value"][2]))
if res.returncode != 0:
gLogger.fatal(res.stdout)
for old in [cert, key]:
if os.path.isfile(old + nowPrefix):
gLogger.notice(f"Restore {old} file from the {old + nowPrefix}")
Expand Down

0 comments on commit 486f8dd

Please sign in to comment.