Skip to content

Commit 486f8dd

Browse files
committed
fix: Converting p12 files with filenames containing special characters
1 parent c619b45 commit 486f8dd

File tree

1 file changed

+15
-9
lines changed

1 file changed

+15
-9
lines changed

src/DIRAC/Core/scripts/dirac_cert_convert.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,12 @@
66
import os
77
import sys
88
import shutil
9+
import subprocess
910
from datetime import datetime
11+
from subprocess import PIPE, run, STDOUT
12+
from tempfile import TemporaryDirectory
1013

1114
from DIRAC import gLogger
12-
from DIRAC.Core.Utilities.Subprocess import shellCall
1315
from DIRAC.Core.Base.Script import Script
1416

1517

@@ -38,15 +40,19 @@ def main():
3840
shutil.move(old, old + nowPrefix)
3941

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

0 commit comments

Comments
 (0)