Skip to content

Commit 71efb17

Browse files
Make secure communication default.
Create a shared secret at profile creation time and add it to labconfig. Raise an error if such an entry is not present, unless allow_insecure is set instead.
1 parent ecfb748 commit 71efb17

File tree

3 files changed

+49
-4
lines changed

3 files changed

+49
-4
lines changed

labscript_profile/create.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
import shutil
44
import configparser
55
from pathlib import Path
6+
from subprocess import check_output
67
from labscript_profile import LABSCRIPT_SUITE_PROFILE, default_labconfig_path
78

89
_here = os.path.dirname(os.path.abspath(__file__))
910
DEFAULT_PROFILE_CONTENTS = os.path.join(_here, 'default_profile')
1011

1112

13+
def make_shared_secret(directory):
14+
"""Create a new zprocess shared secret file in the given directory and return its
15+
filepath"""
16+
cmd = [sys.executable, '-m', 'zprocess.makesecret']
17+
path = check_output(cmd, cwd=directory).decode('utf8').splitlines()[-1].strip()
18+
return Path(path)
19+
20+
1221
def make_labconfig_file():
1322
source_path = os.path.join(LABSCRIPT_SUITE_PROFILE, 'labconfig', 'example.ini')
1423
target_path = default_labconfig_path()
@@ -23,14 +32,19 @@ def make_labconfig_file():
2332
config = configparser.ConfigParser()
2433
config.read(target_path)
2534
config.set('DEFAULT', 'labscript_suite', str(LABSCRIPT_SUITE_PROFILE))
26-
if sys.platform in ['linux', 'linux2']:
35+
if sys.platform == 'linux':
2736
config.set('programs', 'text_editor', 'gedit')
2837
elif sys.platform == 'darwin':
2938
config.set('programs', 'text_editor', 'open')
3039
config.set('programs', 'text_editor_arguments', '-a TextEdit {file}')
3140
if sys.platform != 'win32':
3241
config.set('programs', 'hdf5_viewer', 'hdfview')
33-
config.set('DEFAULT', 'shared_drive', str(Path.home() / ' labscript_shared'))
42+
config.set('DEFAULT', 'shared_drive', '$HOME/labscript_shared')
43+
shared_secret = make_shared_secret(target_path.parent)
44+
shared_secret_entry = Path(
45+
'%(labscript_suite)s', shared_secret.relative_to(LABSCRIPT_SUITE_PROFILE)
46+
)
47+
config.set('security', 'shared_secret', str(shared_secret_entry))
3448

3549
with open(target_path, 'w') as f:
3650
config.write(f)

labscript_profile/default_profile/labconfig/example.ini

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,6 @@ integer_indexing = False
5353
autoload_config_file = %(app_saved_configs)s\runmanager\runmanager.ini
5454
output_folder_format = %%Y\%%m\%%d\{sequence_index:04d}
5555
filename_prefix_format = %%Y-%%m-%%d_{sequence_index:04d}_{script_basename}
56+
57+
[security]
58+
shared_secret = %(labscript_suite)s\labconfig\zpsecret-b810f83f.key

labscript_utils/ls_zprocess.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
str = unicode
1717

1818
import sys
19+
import os
1920
from socket import gethostbyname
2021
from distutils.version import LooseVersion
2122
import zmq
@@ -48,6 +49,32 @@
4849

4950
_cached_config = None
5051

52+
_ERR_NO_SHARED_SECRET = """
53+
54+
--------
55+
56+
Security has not been configured. To create a new shared secret, run:
57+
58+
python -m zprocess.makesecret
59+
60+
move the resulting file somewhere (for example the labconfig directory)
61+
and then add it to labconfig like:
62+
63+
[security]
64+
shared_secret = %(labscript_suite)s/labconfig/zpsecret-09f6dfa0.key
65+
66+
You will need to copy the same shared secret to all computers running
67+
the labscript suite that need to communicate with each other. Treat this
68+
file like a password: it allows anyone on the same network acess to
69+
labscript suite programs, most of of which are capable of remote code
70+
execution. If you are on a trusted network and don't want to use encrypted
71+
communication, you may instead set:
72+
73+
[security]
74+
allow_insecure = True
75+
76+
in your configuration, but this is not advised."""
77+
5178
def get_config():
5279
"""Get relevant options from LabConfig, substituting defaults where appropriate and
5380
return as a dict"""
@@ -86,8 +113,9 @@ def get_config():
86113
try:
87114
config['allow_insecure'] = labconfig.getboolean('security', 'allow_insecure')
88115
except (labconfig.NoOptionError, labconfig.NoSectionError):
89-
# Default will be set to False once the security rollout is complete:
90-
config['allow_insecure'] = True
116+
config['allow_insecure'] = False
117+
if config['shared_secret'] is None and not config['allow_insecure']:
118+
raise ValueError(_ERR_NO_SHARED_SECRET.replace('', os.sep))
91119
try:
92120
config['logging_maxBytes'] = labconfig.getint('logging', 'maxBytes')
93121
except (labconfig.NoOptionError, labconfig.NoSectionError):

0 commit comments

Comments
 (0)