Skip to content

fix(settings): DJ_LOG_LEVEL accepts any case; config files loaded or skipped at import are reported - #1736

Open
dimitri-yatsenko wants to merge 4 commits into
masterfrom
fix/config-follow-ups
Open

dimitri-yatsenko wants to merge 4 commits into
masterfrom
fix/config-follow-ups

Conversation

@dimitri-yatsenko

@dimitri-yatsenko dimitri-yatsenko commented Nov 21, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-ups to the 2.3.6 changes for config and secret files found by searching (GHSA-88hh-c8vj-9xx9), and two problems with the log level.

  • DJ_LOG_LEVEL in any case. DJ_LOG_LEVEL=error python -c "import datajoint" raised a pydantic validation error. The loglevel setting now accepts any case, as logging.py already did. Unknown levels are still rejected.
  • Import-time log records are shown. The settings loaded before the package logger's handler was attached, so the records from that step were lost. The handler is now attached first. Because logging.py now runs before the settings are checked, it falls back to INFO on a level it does not recognize, so an invalid DJ_LOG_LEVEL (such as bogus or info) still fails with the settings error that names DJ_LOG_LEVEL and lists the allowed levels, not with ValueError: Unknown level.
  • The config file loaded at import is logged at DEBUG. Loading configuration from <path> is logged at DEBUG instead of INFO for the config file loaded at import (found by searching, or named by DJ_CONFIG_FILE) and for the one each dj.Instance(...) loads, so neither prints it at the default INFO level. Set DJ_LOG_LEVEL=debug to see which file was loaded. An explicit dj.config.load(path) still logs at INFO, as before. With a datajoint.json you own, import datajoint prints nothing at INFO. A file owned by another user is the exception (next bullets).
  • A skipped file is hard to miss. When a datajoint.json or secrets location is ignored because another user owns it in a location anyone can write, DataJoint now also issues a UserWarning. Before, it only logged a warning, which a logging setup could hide.
  • Another user's file is named when used. A file owned by another user is still used when it sits in a protected directory, such as a shared project folder. That is now logged at INFO: Using <path>, owned by another user (uid <uid>). In a shared project folder whose datajoint.json or secrets directory one user owns, every other user therefore sees this line at the default INFO level, at import and again for each dj.Instance(...). The owner does not, and neither does anyone for a file owned by root.
  • Effective user ID. Ownership is compared with os.geteuid() instead of os.getuid(). This matters under setuid, or after a process changes its effective user.

Fixes #1718.

Behavior change

  • A loglevel set in datajoint.json now takes effect at import. Before, logging.py ran after the settings had loaded and reset the logger to DJ_LOG_LEVEL, or INFO when that was unset. dj.config.loglevel then read, for example, WARNING while the logger still printed INFO records. Now the logger level is the configured one from import on. DJ_LOG_LEVEL still takes precedence over the file.
  • Loading configuration from <path> is logged at DEBUG, not INFO, for the config file loaded at import and by dj.Instance(...). Neither prints it at the default level any more. dj.config.load(path) still logs it at INFO.
  • A skipped config file or secrets location also issues a UserWarning.
  • Using another user's config file or secrets directory from a protected directory logs one INFO line per file, at import and for each dj.Instance(...). In a shared project folder, each user other than the file's owner sees it at the default level.

Changes

  • src/datajoint/settings.py: a mode="before" validator upper-cases loglevel; _is_foreign uses os.geteuid(); _reject_foreign also calls warnings.warn; _read_found_file and _use_found_dir log at info level when they use another user's file; _load_text logs the file path at DEBUG for the file found at import and at INFO for Config.load().
  • src/datajoint/__init__.py: import .logging before the modules that load the settings.
  • src/datajoint/logging.py: fall back to INFO when DJ_LOG_LEVEL is not a level logging recognizes, leaving the check of allowed levels to the settings.

Tests

  • tests/unit/test_settings.py:
    • TestLogLevel: DJ_LOG_LEVEL and assignment in any case; an unknown level still raises.
    • TestImportTimeLogging, each in a new process: import datajoint succeeds with DJ_LOG_LEVEL=error; with DJ_LOG_LEVEL set to bogus, info, or jobs it fails with an error that names DJ_LOG_LEVEL and lists the allowed levels; it prints nothing at the default level with a datajoint.json present; it prints Loading configuration from ... with DJ_LOG_LEVEL=debug; a loglevel of WARNING or debug in datajoint.json is the logger's level after import; DJ_LOG_LEVEL=error overrides a loglevel in the file. In the test process: Config.load() logs the path at INFO.
    • TestDiscoveredFileOwnership: the effective user ID decides ownership; a skipped config file or secrets directory issues a UserWarning, even with the logger disabled; using another user's config file or secrets directory is logged at info level, and using your own is not; a symbolic-link loop (ELOOP) at a secret file name is skipped with a warning.
  • On master, 15 tests fail: 14 new ones, and test_no_posix_ownership_keeps_search, which now removes os.geteuid instead of os.getuid. The new tests that pass on master guard behavior that was already right: DJ_LOG_LEVEL=ERROR, rejecting an unknown level, the invalid-level error at import (it regressed on this branch before the logging.py fallback), the quiet import (on master the record was lost rather than logged at DEBUG), Config.load() logging at INFO, no log line for your own file, and the ELOOP tests, which the issue asked for.
  • The unit suite and the full integration suite pass locally on MySQL and PostgreSQL.
  • Running tests/unit and some integration files in one session fails on master too, because some unit tests re-import datajoint. fix(stores): <filepath@> insert checks stores.filepath_default; folder downloads work on older fsspec #1731 fixes that.

Documentation

datajoint/datajoint-docs#317 documents the loglevel change, the UserWarning for a skipped file and the debug-level Loading configuration line, and merges after this PR.

Notes for reviewers

…records

- The loglevel setting accepts any case, so DJ_LOG_LEVEL=error no longer
  makes `import datajoint` fail.
- The package logger's handler is attached before the settings load, so
  the records emitted at import time (which config file was loaded or
  skipped) are shown.
- A config file or secrets directory skipped because another user owns it
  in a location anyone can write is also reported with warnings.warn.
- Using a file found by searching that another user owns is logged at
  info level, with its path and owner.
- File ownership is compared with the effective user ID.
- Test: a symbolic-link loop at a secret file name is skipped with a
  warning.

Fixes #1718.
import datajoint printed 'Loading configuration from <path>' at the default
INFO level once the handler was attached before the settings load. The file
found at import is now logged at DEBUG; an explicit dj.config.load() still
logs at INFO. Tests cover the quiet import, the loglevel from datajoint.json
applying at import, and DJ_LOG_LEVEL overriding it. The test module imports
the log levels with 'from logging import ...' so that its import block does
not collide with the one in the environment-variable precedence change.

@MilagrosMarin MilagrosMarin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dimitri-yatsenko, this closes the gaps from #1718 cleanly.

  • Ran the settings tests (135 pass) and the full unit (1099) and integration (974 pass, 17 skipped) suites with master merged in. With master's src/ restored, 15 of the settings tests fail, as the description says.
  • import datajoint with a datajoint.json present writes nothing to stdout or stderr at the default level. DJ_LOG_LEVEL=debug shows the Loading configuration from ... line. DJ_LOG_LEVEL=error and ERROR both set the logger to 40.
  • One regression from the import reorder: because logging.py now runs before the settings are validated, DJ_LOG_LEVEL=bogus fails with ValueError: Unknown level: 'BOGUS' from logging.setLevel, instead of the previous pydantic error that named DJ_LOG_LEVEL and listed the allowed levels. DJ_LOG_LEVEL=" info" hits the same path. Could logging.py check the value against the allowed levels (or fall back to INFO and let the settings validation raise), with a test for an invalid value at import?
  • Minor, for the docs PR: log records go to stdout by default while the new UserWarning goes to stderr, so a skipped file shows up once on each stream.

Once the invalid-level message names the variable again, the import-time logging story here is in good shape.

logging.py now runs before the settings load, and passed an unknown level
straight to logger.setLevel, so DJ_LOG_LEVEL=bogus failed with
"ValueError: Unknown level: 'BOGUS'". It now falls back to INFO on a level
logging does not recognize, and the settings validation raises its error,
which names DJ_LOG_LEVEL and lists the allowed levels. The allowed levels
stay defined in one place, the settings.

@MilagrosMarin MilagrosMarin left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @dimitri-yatsenko, the logging.py fallback does it.

  • DJ_LOG_LEVEL=bogus and " info" now fail with the settings error that names DJ_LOG_LEVEL and lists the allowed levels. With the previous logging.py restored, the new test fails for both, so it guards the fix. test_settings.py (138) and the unit suite (1102) pass on the head commit.

Two minor observations on the "Using , owned by another user" line, neither blocking:

  • In a shared project folder, a non-owner sees one INFO line for the config file, one for .secrets, and one for each secret file in it. With database.user, database.password and one stores.* file, that is five lines on every import, and again per dj.Instance(...). Since the folder line already names the location, could the files read from a secrets directory that was just accepted log at DEBUG?
  • The line for the config file is logged before that file's loglevel is applied, so a shared datajoint.json with "loglevel": "WARNING" still prints it at import. Fine to leave as is, but worth a sentence in datajoint/datajoint-docs#317 next to the loglevel change.

With the invalid-level message restored, this is ready from my side.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants