Commit 37401c55 authored by zhifan huang's avatar zhifan huang

test: use tempfile and auto remove it

change tools.decrpt func to use tempfile instead create a never removed
file
parent c07f4775
...@@ -2,7 +2,9 @@ import sys ...@@ -2,7 +2,9 @@ import sys
import os import os
import time import time
import subprocess import subprocess
import tempfile
import logging
logger = logging.getLogger(__name__)
with open(os.devnull, "wb") as null: with open(os.devnull, "wb") as null:
tmp = sys.stderr tmp = sys.stderr
...@@ -109,10 +111,15 @@ def serial2prefix(serial): ...@@ -109,10 +111,15 @@ def serial2prefix(serial):
# pkey: private key # pkey: private key
def decrypt(pkey, incontent): def decrypt(pkey, incontent):
with open("node.key", 'w') as f: fd, key_path = tempfile.mkstemp()
f.write(pkey) os.write(fd, pkey)
args = "openssl rsautl -decrypt -inkey node.key".split() os.close(fd)
p = subprocess.Popen( args = ['openssl', 'rsautl', '-decrypt', '-inkey', key_path]
args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
outcontent, err = p.communicate(incontent) outcontent, err = p.communicate(incontent)
try:
os.unlink(key_path)
except:
logger.error("leaked file %s", key_path)
return outcontent return outcontent
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment