Skip to content

Commit 2888524

Browse files
committed
Renamed 'ServerConfig' to 'GlobalConfig' and 'get_security_config' to 'get_global_config'
1 parent 203eaac commit 2888524

File tree

11 files changed

+74
-65
lines changed

11 files changed

+74
-65
lines changed

src/murfey/cli/add_user.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from murfey.server.api.auth import hash_password
66
from murfey.server.murfey_db import url
7-
from murfey.util.config import get_security_config
7+
from murfey.util.config import get_global_config
88
from murfey.util.db import MurfeyUser as User
99

1010

@@ -21,7 +21,7 @@ def run():
2121
new_user = User(
2222
username=args.username, hashed_password=hash_password(args.password)
2323
)
24-
_url = url(get_security_config())
24+
_url = url(get_global_config())
2525
engine = create_engine(_url)
2626
with Session(engine) as murfey_db:
2727
murfey_db.add(new_user)

src/murfey/cli/decrypt_db_password.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from cryptography.fernet import Fernet
44

5-
from murfey.util.config import get_security_config
5+
from murfey.util.config import get_global_config
66

77

88
def run():
@@ -12,6 +12,6 @@ def run():
1212

1313
args = parser.parse_args()
1414

15-
security_config = get_security_config()
16-
f = Fernet(security_config.crypto_key.encode("ascii"))
15+
global_config = get_global_config()
16+
f = Fernet(global_config.crypto_key.encode("ascii"))
1717
print(f.decrypt(args.password.encode("ascii")).decode())

src/murfey/cli/generate_db_password.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33

44
from cryptography.fernet import Fernet
55

6-
from murfey.util.config import get_security_config
6+
from murfey.util.config import get_global_config
77

88

99
def run():
10-
security_config = get_security_config()
11-
f = Fernet(security_config.crypto_key.encode("ascii"))
10+
global_config = get_global_config()
11+
f = Fernet(global_config.crypto_key.encode("ascii"))
1212
alphabet = string.ascii_letters + string.digits
1313
password = "".join(secrets.choice(alphabet) for i in range(32))
1414
print(f.encrypt(password.encode("ascii")).decode())

src/murfey/cli/inject_spa_processing.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
from murfey.server.ispyb import TransportManager
1212
from murfey.server.murfey_db import url
13-
from murfey.util.config import get_machine_config, get_microscope, get_security_config
13+
from murfey.util.config import get_global_config, get_machine_config, get_microscope
1414
from murfey.util.db import (
1515
AutoProcProgram,
1616
ClientEnvironment,
@@ -97,13 +97,13 @@ def run():
9797
os.environ["BEAMLINE"] = args.microscope
9898

9999
machine_config = get_machine_config()
100-
security_config = get_security_config()
100+
global_config = get_global_config()
101101
_url = url(machine_config)
102102
engine = create_engine(_url)
103103
murfey_db = Session(engine)
104104

105105
_transport_object = TransportManager(args.transport)
106-
_transport_object.feedback_queue = security_config.feedback_queue
106+
_transport_object.feedback_queue = global_config.feedback_queue
107107

108108
query = (
109109
select(Movie)

src/murfey/cli/spa_ispyb_messages.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from murfey.server.ispyb import Session, TransportManager, get_session_id
2323
from murfey.server.murfey_db import url
2424
from murfey.util import db
25-
from murfey.util.config import get_machine_config, get_microscope, get_security_config
25+
from murfey.util.config import get_global_config, get_machine_config, get_microscope
2626

2727

2828
def run():
@@ -341,7 +341,7 @@ def run():
341341
.where(db.ProcessingJob.recipe == "em-spa-preprocess")
342342
).one()
343343
machine_config = get_machine_config()
344-
security_config = get_security_config()
344+
global_config = get_global_config()
345345
params = db.SPARelionParameters(
346346
pj_id=collected_ids[2].id,
347347
angpix=float(metadata["pixel_size_on_image"]) * 1e10,
@@ -378,7 +378,7 @@ def run():
378378

379379
if args.flush_preprocess:
380380
_transport_object = TransportManager(args.transport)
381-
_transport_object.feedback_queue = security_config.feedback_queue
381+
_transport_object.feedback_queue = global_config.feedback_queue
382382
stashed_files = murfey_db.exec(
383383
select(db.PreprocessStash)
384384
.where(db.PreprocessStash.session_id == args.session_id)

src/murfey/server/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@
5151
from murfey.util import LogFilter
5252
from murfey.util.config import (
5353
MachineConfig,
54+
get_global_config,
5455
get_hostname,
5556
get_machine_config,
5657
get_microscope,
57-
get_security_config,
5858
)
5959
from murfey.util.spa_params import default_spa_parameters
6060
from murfey.util.state import global_state
@@ -74,7 +74,7 @@
7474
_transport_object: TransportManager | None = None
7575

7676
try:
77-
_url = url(get_security_config())
77+
_url = url(get_global_config())
7878
engine = create_engine(_url)
7979
murfey_db = Session(engine, expire_on_commit=False)
8080
except Exception:
@@ -296,9 +296,9 @@ def run():
296296
# Set up logging now that the desired verbosity is known
297297
_set_up_logging(quiet=args.quiet, verbosity=args.verbose)
298298

299-
security_config = get_security_config()
299+
global_config = get_global_config()
300300
if not args.temporary and _transport_object:
301-
_transport_object.feedback_queue = security_config.feedback_queue
301+
_transport_object.feedback_queue = global_config.feedback_queue
302302
rabbit_thread = Thread(
303303
target=feedback_listen,
304304
daemon=True,

src/murfey/server/api/auth.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
from murfey.server import sanitise
2121
from murfey.server.murfey_db import murfey_db, url
22-
from murfey.util.config import get_machine_config, get_security_config
22+
from murfey.util.config import get_global_config, get_machine_config
2323
from murfey.util.db import MurfeyUser as User
2424
from murfey.util.db import Session as MurfeySession
2525

@@ -63,19 +63,19 @@ async def __call__(self, request: Request):
6363

6464

6565
# Set up variables used for authentication
66-
security_config = get_security_config()
66+
global_config = get_global_config()
6767
machine_config = get_machine_config()
6868
auth_url = (
6969
machine_config[os.getenv("BEAMLINE", "")].auth_url
7070
if machine_config.get(os.getenv("BEAMLINE", ""))
7171
else ""
7272
)
73-
ALGORITHM = security_config.auth_algorithm or "HS256"
74-
SECRET_KEY = security_config.auth_key or secrets.token_hex(32)
75-
if security_config.auth_type == "password":
73+
ALGORITHM = global_config.auth_algorithm or "HS256"
74+
SECRET_KEY = global_config.auth_key or secrets.token_hex(32)
75+
if global_config.auth_type == "password":
7676
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
7777
else:
78-
oauth2_scheme = CookieScheme(cookie_key=security_config.cookie_key)
78+
oauth2_scheme = CookieScheme(cookie_key=global_config.cookie_key)
7979
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
8080

8181
instrument_server_tokens: Dict[float, dict] = {}
@@ -96,7 +96,7 @@ def hash_password(password: str) -> str:
9696

9797
# Set up database engine
9898
try:
99-
_url = url(security_config)
99+
_url = url(global_config)
100100
engine = create_engine(_url)
101101
except Exception:
102102
engine = None
@@ -114,7 +114,7 @@ def validate_user(username: str, password: str) -> bool:
114114
def validate_visit(visit_name: str, token: str) -> bool:
115115
if validators := entry_points().select(
116116
group="murfey.auth.session_validation",
117-
name=security_config.auth_type,
117+
name=global_config.auth_type,
118118
):
119119
return validators[0].load()(visit_name, token)
120120
return True
@@ -166,12 +166,12 @@ async def validate_token(token: Annotated[str, Depends(oauth2_scheme)]):
166166
if auth_url:
167167
headers = (
168168
{}
169-
if security_config.auth_type == "cookie"
169+
if global_config.auth_type == "cookie"
170170
else {"Authorization": f"Bearer {token}"}
171171
)
172172
cookies = (
173-
{security_config.cookie_key: token}
174-
if security_config.auth_type == "cookie"
173+
{global_config.cookie_key: token}
174+
if global_config.auth_type == "cookie"
175175
else {}
176176
)
177177
async with aiohttp.ClientSession(cookies=cookies) as session:
@@ -186,7 +186,7 @@ async def validate_token(token: Annotated[str, Depends(oauth2_scheme)]):
186186
else:
187187
if validators := entry_points().select(
188188
group="murfey.auth.token_validation",
189-
name=security_config.auth_type,
189+
name=global_config.auth_type,
190190
):
191191
validators[0].load()(token)
192192
else:
@@ -290,8 +290,8 @@ async def mint_session_token(session_id: MurfeySessionID, db=murfey_db):
290290
db.exec(select(MurfeySession).where(MurfeySession.id == session_id)).one().visit
291291
)
292292
expiry_time = None
293-
if security_config.session_token_timeout:
294-
expiry_time = time.time() + security_config.session_token_timeout
293+
if global_config.session_token_timeout:
294+
expiry_time = time.time() + global_config.session_token_timeout
295295
token = create_access_token(
296296
{
297297
"session": session_id,

src/murfey/server/api/clem.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def validate_and_sanitise(
7676
machine_config = get_machine_config(instrument_name=instrument_name)[
7777
instrument_name
7878
]
79+
if not machine_config.rsync_basepath:
80+
raise ValueError("rsync basepath not set")
7981
base_path = machine_config.rsync_basepath.as_posix()
8082

8183
# Check that full file path doesn't contain unallowed characters

src/murfey/server/main.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import murfey.server.websocket
2222
import murfey.util.models
2323
from murfey.server import template_files
24-
from murfey.util.config import get_security_config
24+
from murfey.util.config import get_global_config
2525

2626
# Import Murfey server or demo server based on settings
2727
if os.getenv("MURFEY_DEMO"):
@@ -39,7 +39,7 @@ class Settings(BaseSettings):
3939
murfey_machine_configuration: str = ""
4040

4141

42-
security_config = get_security_config()
42+
global_config = get_global_config()
4343

4444
settings = Settings()
4545

@@ -50,7 +50,7 @@ class Settings(BaseSettings):
5050

5151
app.add_middleware(
5252
CORSMiddleware,
53-
allow_origins=security_config.allow_origins,
53+
allow_origins=global_config.allow_origins,
5454
allow_credentials=True,
5555
allow_methods=["*"],
5656
allow_headers=["*"],

src/murfey/server/murfey_db.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,23 @@
88
from sqlalchemy.pool import NullPool
99
from sqlmodel import Session, create_engine
1010

11-
from murfey.util.config import ServerConfig, get_security_config
11+
from murfey.util.config import GlobalConfig, get_global_config
1212

1313

14-
def url(security_config: ServerConfig | None = None) -> str:
15-
security_config = security_config or get_security_config()
16-
with open(security_config.murfey_db_credentials, "r") as stream:
14+
def url(global_config: GlobalConfig | None = None) -> str:
15+
global_config = global_config or get_global_config()
16+
with open(global_config.murfey_db_credentials, "r") as stream:
1717
creds = yaml.safe_load(stream)
18-
f = Fernet(security_config.crypto_key.encode("ascii"))
18+
f = Fernet(global_config.crypto_key.encode("ascii"))
1919
p = f.decrypt(creds["password"].encode("ascii"))
2020
return f"postgresql+psycopg2://{creds['username']}:{p.decode()}@{creds['host']}:{creds['port']}/{creds['database']}"
2121

2222

2323
def get_murfey_db_session(
24-
security_config: ServerConfig | None = None,
24+
global_config: GlobalConfig | None = None,
2525
) -> Session: # type: ignore
26-
_url = url(security_config)
27-
if security_config and not security_config.sqlalchemy_pooling:
26+
_url = url(global_config)
27+
if global_config and not global_config.sqlalchemy_pooling:
2828
engine = create_engine(_url, poolclass=NullPool)
2929
else:
3030
engine = create_engine(_url)
@@ -37,7 +37,7 @@ def get_murfey_db_session(
3737

3838
murfey_db_session = partial(
3939
get_murfey_db_session,
40-
get_security_config(),
40+
get_global_config(),
4141
)
4242

4343
murfey_db: Session = Depends(murfey_db_session)

0 commit comments

Comments
 (0)