don't spawn a shell in create-onionbalance-config
authorJulien Cristau <jcristau@debian.org>
Mon, 2 Oct 2017 12:48:50 +0000 (14:48 +0200)
committerJulien Cristau <jcristau@debian.org>
Mon, 2 Oct 2017 12:49:13 +0000 (14:49 +0200)
python can do these things.

modules/onion/files/create-onionbalance-config

index 5903a74..4df5e70 100755 (executable)
@@ -42,7 +42,9 @@
 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 # OTHER DEALINGS IN THE SOFTWARE.
 
+import os
 import os.path
+import shutil
 import subprocess
 import yaml
 
@@ -52,7 +54,8 @@ outfile = '/etc/onionbalance/config.yaml'
 relkeydir = 'private_keys'
 keydir = os.path.join('/etc/onionbalance', relkeydir)
 
-data = yaml.safe_load(open(j))
+with open(j) as conf:
+  data = yaml.safe_load(conf)
 
 service_instances = {}
 for entry in data:
@@ -70,8 +73,11 @@ services = []
 for s in service_instances:
   keyfile = os.path.join(keydir, s+'.key')
   relkeyfile = os.path.join(relkeydir, s+'.key')
-  if (not os.path.exists(keyfile)):
-    subprocess.check_call('umask 0027 && openssl genrsa -out %s 1024 && chgrp onionbalance %s && chmod 0640 %s'%(keyfile, keyfile, keyfile), shell=True)
+  if not os.path.exists(keyfile):
+    subprocess.check_call(['openssl', 'genrsa', '-out', keyfile, '1024'],
+                          preexec_fn=lambda: os.umask(0027))
+    shutil.chown(keyfile, group='onionbalance')
+    os.chmod(keyfile, 0o640)
 
   service = {
     'key': relkeyfile,