Skip to content

feat(constance): support explicit CONSTANCE_* env var overrides for NLP settings DEV-1898#6933

Merged
rajpatel24 merged 2 commits into
release/2.026.12from
dev-1898-make-nlp-configureable-via-env-vars
Apr 10, 2026
Merged

feat(constance): support explicit CONSTANCE_* env var overrides for NLP settings DEV-1898#6933
rajpatel24 merged 2 commits into
release/2.026.12from
dev-1898-make-nlp-configureable-via-env-vars

Conversation

@rajpatel24
Copy link
Copy Markdown
Contributor

📖 Description

This PR allows the Google NLP (ASR/MT) settings to be configured directly via environment variables, eliminating the need for manual setup in the Django admin panel during deployments.

The following four settings have been explicitly updated in base.py to support this pattern:

  • CONSTANCE_ASR_MT_GOOGLE_PROJECT_ID
  • CONSTANCE_ASR_MT_GOOGLE_STORAGE_BUCKET_PREFIX
  • CONSTANCE_ASR_MT_GOOGLE_TRANSLATION_LOCATION
  • CONSTANCE_ASR_MT_GOOGLE_CREDENTIALS

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Apr 10, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 7658f295-b76c-4331-bd4b-75b3081539f5

📥 Commits

Reviewing files that changed from the base of the PR and between c06045a and e361b09.

📒 Files selected for processing (1)
  • kobo/settings/base.py
✅ Files skipped from review due to trivial changes (1)
  • kobo/settings/base.py

📝 Walkthrough

📣 Summary

Make Google NLP (ASR/MT) Constance settings configurable via environment variables to allow deployment-time configuration without using the Django admin.

📖 Description

This change updates four Constance-backed settings so they can be overridden at startup using environment variables, centralizing ASR/MT Google configuration in the environment and removing the need for post-deployment manual configuration in Django admin.

👷 Description for instance maintainers

Four Constance settings in kobo/settings/base.py now read defaults from environment variables:

  • CONSTANCE_ASR_MT_GOOGLE_PROJECT_ID — env: CONSTANCE_ASR_MT_GOOGLE_PROJECT_ID (default: "kobo-asr-mt")
  • CONSTANCE_ASR_MT_GOOGLE_STORAGE_BUCKET_PREFIX — env: CONSTANCE_ASR_MT_GOOGLE_STORAGE_BUCKET_PREFIX (default: "kobo-asr-mt-tmp")
  • CONSTANCE_ASR_MT_GOOGLE_TRANSLATION_LOCATION — env: CONSTANCE_ASR_MT_GOOGLE_TRANSLATION_LOCATION (default: "us-central1")
  • CONSTANCE_ASR_MT_GOOGLE_CREDENTIALS — env: CONSTANCE_ASR_MT_GOOGLE_CREDENTIALS (default: "")

These values remain Constance-configurable in the Django admin, but when the corresponding CONSTANCE_* environment variable is set at startup it becomes the effective default, simplifying automated deployments and container-based setups.

💭 Notes

  • Configuration-only change; no runtime behavior changes beyond allowing env-var overrides.
  • Defaults are unchanged from previous hardcoded values to preserve backward compatibility.
  • ASR_MT_GOOGLE_CREDENTIALS continues to default to an empty string.

👀 Preview steps

  1. Ensure you have an account and project with ASR/MT enabled.
  2. Set one or more env vars before starting the app, e.g.:
    • CONSTANCE_ASR_MT_GOOGLE_PROJECT_ID=my-project
    • CONSTANCE_ASR_MT_GOOGLE_CREDENTIALS='{"type":"service_account", ...}'
  3. Start the application/container.
  4. In the running instance, verify Constance shows the expected defaults (via Django admin or by reading constance.config).
  5. Confirm ASR/MT functionality uses the configured project/credentials as expected.

Walkthrough

Four Constance configuration entries in kobo/settings/base.py were changed to derive their default values from environment variables via env.str('CONSTANCE_<NAME>', <previous default>) instead of using hardcoded literals. The entries updated are ASR_MT_GOOGLE_PROJECT_ID, ASR_MT_GOOGLE_STORAGE_BUCKET_PREFIX, ASR_MT_GOOGLE_TRANSLATION_LOCATION, and ASR_MT_GOOGLE_CREDENTIALS. The fallback defaults remain the same as before.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~2 minutes

🚥 Pre-merge checks | ✅ 2
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change (supporting environment variable overrides for NLP settings), follows the required format with type(scope), and includes the task ID.
Description check ✅ Passed The description accurately explains the purpose of the changes, lists the specific settings being updated, and relates directly to the changeset.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Apr 10, 2026

Greptile Summary

This PR adds support for configuring four Google NLP (ASR/MT) Constance settings via environment variables (CONSTANCE_ASR_MT_GOOGLE_PROJECT_ID, CONSTANCE_ASR_MT_GOOGLE_STORAGE_BUCKET_PREFIX, CONSTANCE_ASR_MT_GOOGLE_TRANSLATION_LOCATION, CONSTANCE_ASR_MT_GOOGLE_CREDENTIALS), eliminating the need for manual Django admin setup on fresh deployments. It also adds a code comment establishing CONSTANCE_ as the new standard prefix for env-var-backed Constance defaults, addressing a prior review thread.

  • The PR description is missing a ### 👷 Description for instance maintainers section, which is the primary audience for this change — they need to know the four new env var names, their defaults, and the expected format for CONSTANCE_ASR_MT_GOOGLE_CREDENTIALS (JSON string).

Confidence Score: 5/5

Safe to merge — minimal, well-scoped change that correctly wires env-var defaults for four constance settings with no functional regressions.

All code changes are correct: the fallback literals match the previous hardcoded defaults, and the CONSTANCE_ prefix comment addresses the prior review thread. The only finding (P2) is a missing PR template section, which does not affect runtime behavior.

No files require special attention.

Flowchart

%%{init: {'theme': 'neutral'}}%%
flowchart TD
    A[Deployment starts] --> B{Is CONSTANCE_*\nenv var set?}
    B -- Yes --> C[env.str reads env var\nas constance default]
    B -- No --> D[Hardcoded fallback\ne.g. 'kobo-asr-mt']
    C --> E{Value exists\nin DB/Redis?\ne.g. previously set\nvia Django admin}
    D --> E
    E -- Yes --> F[Use DB value\nadmin value wins]
    E -- No --> G[Use default value\nenv var takes effect]
    F --> H[constance.config.ASR_MT_GOOGLE_*\nused at runtime]
    G --> H
Loading

Reviews (2): Last reviewed commit: "feat(constance): add code comment clarif..." | Re-trigger Greptile

Comment thread kobo/settings/base.py
Comment on lines 352 to +372
@@ -362,14 +362,14 @@
),
),
'ASR_MT_GOOGLE_TRANSLATION_LOCATION': (
'us-central1',
env.str('CONSTANCE_ASR_MT_GOOGLE_TRANSLATION_LOCATION', 'us-central1'),
(
'Google Cloud location to use for large translation tasks. It'
' cannot be `global`, and Google only allows certain locations.'
),
),
'ASR_MT_GOOGLE_CREDENTIALS': (
'',
env.str('CONSTANCE_ASR_MT_GOOGLE_CREDENTIALS', ''),
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.

P2 Naming convention differs from existing constance-backed env vars

Other constance settings that read from env vars use either a project-specific prefix (KOBO_SUPPORT_EMAIL, KOBO_SUPPORT_URL) or the bare setting name (USAGE_LIMIT_ENFORCEMENT). The four new vars introduce a third convention: CONSTANCE_<SETTING_NAME>. This is arguably the clearest pattern, but it's inconsistent with the rest of the file and could confuse operators reading the code side-by-side. A brief code comment explaining why the CONSTANCE_ prefix is used here (and whether it should become the standard going forward) would help future contributors.

Suggested change
'ASR_MT_GOOGLE_PROJECT_ID': (
# Env var prefixed with CONSTANCE_ to signal it seeds the constance default.
env.str('CONSTANCE_ASR_MT_GOOGLE_PROJECT_ID', 'kobo-asr-mt'),
'ID of the Google Cloud project used to access ASR/MT APIs',
),

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

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.

@rajpatel24, the bot is right, it could be the standard in the future.
We could start to deprecat other not prefixed env vars and use the prefix everywhere.
Let's add a comment here. We can tackle this after migrating to Constance 4.x

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
kobo/settings/base.py (1)

352-372: ⚠️ Potential issue | 🟠 Major

CONSTANCE_* env vars here act as initialization defaults, not runtime overrides

The commit message claims "explicit CONSTANCE_* env var overrides," but the implementation only sets these as CONSTANCE_CONFIG defaults at startup. With the custom DatabaseBackend (line 807), the get() method returns database-stored values if they exist, falling back to None rather than the CONSTANCE_CONFIG defaults. This means on instances with pre-existing database entries for these keys, environment variables will be silently ignored—they only take effect during initial setup when the database is empty.

Clarify whether the requirement is:

  • Env vars should set one-time initialization defaults (current behavior), or
  • Env vars should override database values at runtime (not currently implemented)

If runtime override is needed, the custom backend's get() method must be updated to check environment variables before querying the database.


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 185862f6-babc-48b4-814e-cdc30d99a1d1

📥 Commits

Reviewing files that changed from the base of the PR and between f6522c5 and c06045a.

📒 Files selected for processing (1)
  • kobo/settings/base.py

@rajpatel24 rajpatel24 requested a review from noliveleger April 10, 2026 12:52
Comment thread kobo/settings/base.py
Comment on lines 352 to +372
@@ -362,14 +362,14 @@
),
),
'ASR_MT_GOOGLE_TRANSLATION_LOCATION': (
'us-central1',
env.str('CONSTANCE_ASR_MT_GOOGLE_TRANSLATION_LOCATION', 'us-central1'),
(
'Google Cloud location to use for large translation tasks. It'
' cannot be `global`, and Google only allows certain locations.'
),
),
'ASR_MT_GOOGLE_CREDENTIALS': (
'',
env.str('CONSTANCE_ASR_MT_GOOGLE_CREDENTIALS', ''),
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.

@rajpatel24, the bot is right, it could be the standard in the future.
We could start to deprecat other not prefixed env vars and use the prefix everywhere.
Let's add a comment here. We can tackle this after migrating to Constance 4.x

@rajpatel24 rajpatel24 requested a review from noliveleger April 10, 2026 13:24
Copy link
Copy Markdown
Contributor

@noliveleger noliveleger left a comment

Choose a reason for hiding this comment

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

LGTM

@rajpatel24 rajpatel24 merged commit 48c17c9 into release/2.026.12 Apr 10, 2026
21 checks passed
@rajpatel24 rajpatel24 deleted the dev-1898-make-nlp-configureable-via-env-vars branch April 10, 2026 14:34
rajpatel24 added a commit that referenced this pull request Apr 27, 2026
… Constance settings DEV-2017 (#6965)

### 📖 Description
We previously introduced `CONSTANCE_`-prefixed environment variable
fallbacks (using `env.str()`) for the Google NLP settings
(`ASR_MT_GOOGLE_*`) within the `CONSTANCE_CONFIG` dictionary in
`base.py` ([#6933](#6933)).

The Sysadmin team has since identified that injecting these
configurations especially complex JSON credentials via environment
variables leads to escaping issues in Kubernetes deployments.

To improve stability and security, the team is transitioning to securely
mounted files (e.g., Kubernetes Secrets) and relying on native Google
library mechanisms (such as the `GOOGLE_APPLICATION_CREDENTIALS`
environment variable) for NLP authentication and configuration.

This PR removes the custom environment variable fallback logic from the
codebase.

The following four settings in base.py have been reverted to their
original values to support this pattern:
* `CONSTANCE_ASR_MT_GOOGLE_PROJECT_ID` →`'kobo-asr-mt'`
* `CONSTANCE_ASR_MT_GOOGLE_STORAGE_BUCKET_PREFIX` →`'kobo-asr-mt-tmp'`
* `CONSTANCE_ASR_MT_GOOGLE_TRANSLATION_LOCATION` →`'us-central1'`
* `CONSTANCE_ASR_MT_GOOGLE_CREDENTIALS` → `""`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants