Skip to content

feat(redis): configurable Redis DB index and key prefixes via env vars (single-DB / shared Redis support) - #497

Open
alesnav wants to merge 10 commits into
MISP:masterfrom
alesnav:feat/redis-key-prefixes
Open

alesnav wants to merge 10 commits into
MISP:masterfrom
alesnav:feat/redis-key-prefixes

Conversation

@alesnav

@alesnav alesnav commented Nov 16, 2026 •

Copy link
Copy Markdown
Contributor

What does it do?

Makes the Redis logical database index and key prefixes/namespaces configurable via environment variables, so MISP can run against a Redis backend that only exposes a single logical database and/or is shared with other applications. The motivating case is Azure Managed Redis, which only exposes DB 0 and where several apps share the same instance.

All changes are backward compatible: the defaults reproduce today's behaviour exactly.

New environment variables

Variable Default Effect
PHP_REDIS_SESSION_PREFIX (empty) When set, appends prefix=<value> to php-fpm's session.save_path so PHP session keys are namespaced (default phpredis prefix is PHPREDIS_SESSION:).
BACKGROUND_JOBS_REDIS_NAMESPACE background_jobs Maps to SimpleBackgroundJobs.redis_namespace.
MISP_REDIS_DATABASE 13 Maps to MISP.redis_database.
BACKGROUND_JOBS_REDIS_DATABASE 1 Maps to SimpleBackgroundJobs.redis_database.
ZEROMQ_REDIS_DATABASE 1 Maps to Plugin.ZeroMQ_redis_database.

Changes

  • core/files/entrypoint.sh: declare the five env vars above with their current defaults (backward compatible).
  • core/files/etc/misp-docker/minimum_config.envars.json: expose MISP.redis_database, SimpleBackgroundJobs.redis_database, Plugin.ZeroMQ_redis_database and SimpleBackgroundJobs.redis_namespace through those env vars (enforced via the existing envsubst + enforce_env_settings mechanism).
  • core/files/entrypoint_fpm.sh: optionally append prefix=${PHP_REDIS_SESSION_PREFIX} to the generated session.save_path (inserted into the query string with ?/& as appropriate).

Why

On a single-DB backend (e.g. Azure Managed Redis) all MISP Redis subsystems must target DB 0, and when the instance is shared each subsystem needs its own key prefix to avoid collisions. Today the DB indices (13/1/1) and the session prefix are hard-coded / not reachable via env.

Note: to actually use MISP_REDIS_DATABASE=0 you also need the core fix MISP/MISP#11293 (RedisTool::init() used ?: 13, which treats 0 as falsy and falls back to 13). This PR and that one are complementary: this one lets you set the value from the environment, the core PR lets a value of 0 be honoured.

How was it tested?

Built and run against MISP 2.5.51 on AKS with Azure Managed Redis (single DB 0, shared with other apps). With PHP_REDIS_SESSION_PREFIX=misp:session:, BACKGROUND_JOBS_REDIS_NAMESPACE=misp:background_jobs and the three *_REDIS_DATABASE=0:

  • session keys are written as misp:session:*
  • background job keys as misp:background_jobs:*
  • all subsystems connect to DB 0 (combined with the core fix)

Defaults (no env vars set) reproduce the previous behaviour.

…SION_PREFIX

Append prefix=<value> to session.save_path when PHP_REDIS_SESSION_PREFIX is set,
so PHP session keys can be namespaced (default stays PHPREDIS_SESSION:). Useful when
the Redis instance is shared across applications.
…IS_NAMESPACE

Two optional env vars to namespace Redis keys when Redis is shared between apps:
- PHP_REDIS_SESSION_PREFIX (default empty -> phpredis default PHPREDIS_SESSION:)
- BACKGROUND_JOBS_REDIS_NAMESPACE (default 'background_jobs' -> backward compatible)
Both are backward compatible: defaults preserve current behaviour.
…igurable via env

Map SimpleBackgroundJobs.redis_namespace to ${BACKGROUND_JOBS_REDIS_NAMESPACE} so the
background job Redis keys can be namespaced (e.g. misp:background_jobs) when Redis is
shared. Enforced through the existing envars.json mechanism; default keeps 'background_jobs'.
Expose MISP.redis_database, SimpleBackgroundJobs.redis_database and
Plugin.ZeroMQ_redis_database through env vars (MISP_REDIS_DATABASE /
BACKGROUND_JOBS_REDIS_DATABASE / ZEROMQ_REDIS_DATABASE). Needed for single-DB
backends like Azure Managed Redis (only DB 0). Enforced via envars.json.
…BASE / ZEROMQ_REDIS_DATABASE

Defaults preserve MISP's historical values (13/1/1) for backward compatibility.
Set all to 0 for single-DB backends (e.g. Azure Managed Redis).
@ostefano

Copy link
Copy Markdown
Collaborator

Should these settings be included in the docker-compose or template file?

… docker-compose

Pass MISP_REDIS_DATABASE, BACKGROUND_JOBS_REDIS_DATABASE, ZEROMQ_REDIS_DATABASE,
BACKGROUND_JOBS_REDIS_NAMESPACE and PHP_REDIS_SESSION_PREFIX to the misp-core
service so they are configurable from the user's .env. Defaults preserve current
behaviour (13/1/1, background_jobs, empty prefix).
…in template.env

Add commented MISP_REDIS_DATABASE, BACKGROUND_JOBS_REDIS_DATABASE,
ZEROMQ_REDIS_DATABASE, BACKGROUND_JOBS_REDIS_NAMESPACE (Redis section) and
PHP_REDIS_SESSION_PREFIX (Additional PHP settings) with their defaults and
single-DB / shared-Redis guidance.
@alesnav

alesnav commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

Good question — I'd say both, because in this repo the two files play complementary roles:

  • docker-compose.yml (misp-core environment:) is what actually passes a variable from the host into the container. Without an entry here, even if a user sets e.g. MISP_REDIS_DATABASE in their .env it never reaches the container and the entrypoint.sh default always wins. This is the functionally required part.
  • template.env is the documented template users copy to .env, so the new knobs should be discoverable/commented there (like the rest of the Redis / Additional PHP settings sections).

I've just pushed both to this branch:

  • docker-compose.yml: wired the five vars into misp-core — MISP_REDIS_DATABASE, BACKGROUND_JOBS_REDIS_DATABASE, ZEROMQ_REDIS_DATABASE, BACKGROUND_JOBS_REDIS_NAMESPACE in the # redis settings block, and PHP_REDIS_SESSION_PREFIX in # additional PHP settings. I kept the ${VAR:-default} inline defaults (13/1/1, background_jobs, empty prefix) to match how the neighbouring REDIS_HOST/PHP_SESSION_* entries are wired, so behaviour stays backward compatible even if a user doesn't touch their .env.
  • template.env: documented all five as commented entries with their defaults — DB indices + namespace in the Redis section, and PHP_REDIS_SESSION_PREFIX under Additional PHP settings — including the single-DB / shared-Redis guidance and the note that honouring MISP_REDIS_DATABASE=0 also needs fix(RedisTool): honour redis_database 0 (use ?? instead of ?:) MISP#11293.

Let me know if you'd prefer the compose entries without inline defaults (relying solely on entrypoint.sh) and I'll adjust.

Comment thread core/files/entrypoint.sh
# backend such as Azure Managed Redis (only DB 0 exists); key isolation is by prefix.
export MISP_REDIS_DATABASE=${MISP_REDIS_DATABASE:-13}
export BACKGROUND_JOBS_REDIS_DATABASE=${BACKGROUND_JOBS_REDIS_DATABASE:-1}
export ZEROMQ_REDIS_DATABASE=${ZEROMQ_REDIS_DATABASE:-1}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I understand they might match historical values, but what about existing installations?

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants