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
import os
import time
import subprocess
import tempfile
import logging
logger = logging.getLogger(__name__)
with open(os.devnull, "wb") as null:
tmp = sys.stderr
......@@ -109,10 +111,15 @@ def serial2prefix(serial):
# pkey: private key
def decrypt(pkey, incontent):
with open("node.key", 'w') as f:
f.write(pkey)
args = "openssl rsautl -decrypt -inkey node.key".split()
p = subprocess.Popen(
args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
fd, key_path = tempfile.mkstemp()
os.write(fd, pkey)
os.close(fd)
args = ['openssl', 'rsautl', '-decrypt', '-inkey', key_path]
p = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
outcontent, err = p.communicate(incontent)
try:
os.unlink(key_path)
except:
logger.error("leaked file %s", key_path)
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