Skip to content

Conversation

@Hercilio1
Copy link
Contributor

@Hercilio1 Hercilio1 commented Oct 7, 2025

Addressing critical issue that is blocking the staging environment from running

Other PRs:

Summary by CodeRabbit

  • Documentation
    • Clarified .env comment that the dummy encryption key is for local development.
  • Refactor
    • Centralized encryption key resolution to improve maintainability with no public API changes.
  • Tests
    • Added unit tests ensuring clear errors when BA_API_KEY_ENCRYPTION_KEY is unset across encrypt, decrypt, and key rotation.
  • Chores
    • Set explicit defaults in test settings: BA_API_KEY_REQUEST_HEADER defaults to "HTTP_API_KEY" and BA_API_KEY_ENCRYPTION_KEY defaults to None.

@coderabbitai
Copy link

coderabbitai bot commented Oct 7, 2025

Walkthrough

Centralizes encryption key resolution into a new helper in BaseAPIKeyManager, updates encrypt/decrypt to use it, and adds tests ensuring errors when the key is unset. Test project settings now provide explicit defaults for related env vars. The .env file adds a comment clarifying a dummy key is for local development.

Changes

Cohort / File(s) Summary
API key manager logic
baseapp_api_key/managers.py
Added private method _get_encryption_key(encryption_key: str | None = None) -> str. encrypt and decrypt now delegate key resolution to the helper, consolidating fallback logic and raising when unset. Public API unchanged.
Unit tests for missing key
baseapp_api_key/tests/unit/test_api_key.py
Added tests asserting encrypt, decrypt, and rotate_encryption_key raise ValueError with message "BA_API_KEY_ENCRYPTION_KEY is not set" under override_settings(...=None).
Test project settings defaults
testproject/settings.py
Added explicit defaults for env lookups: BA_API_KEY_REQUEST_HEADER defaults to "HTTP_API_KEY", BA_API_KEY_ENCRYPTION_KEY defaults to None.
Environment file note
.env
Added a comment noting the dummy encryption key is for local development.

Sequence Diagram(s)

sequenceDiagram
  autonumber
  actor Client
  participant Manager as BaseAPIKeyManager
  participant Helper as _get_encryption_key
  participant Settings as Django settings/env

  rect rgb(245,245,255)
    Client->>Manager: encrypt(data, encryption_key=None)
    Manager->>Helper: resolve key
    Helper->>Settings: read BA_API_KEY_ENCRYPTION_KEY (or use provided)
    alt key unset
      Helper-->>Manager: raise ValueError("BA_API_KEY_ENCRYPTION_KEY is not set")
      Manager-->>Client: exception
    else key present
      Helper-->>Manager: return key
      Manager-->>Client: ciphertext
    end
  end

  note over Manager,Helper: decrypt(...) and rotate_encryption_key(...) follow the same key resolution path
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I stash my keys where carrots hide,
A cipher nibble, hop with pride.
If key’s not set, I thump—abort!
No secrets cracked in this warren court.
New helper burrows, finds the way,
Tests keep watch—hip hop, hooray! 🥕🔑

Pre-merge checks and finishing touches

❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Title Check ⚠️ Warning The current title only highlights the addition of default values for API key environment variables but omits the significant change of centralizing encryption key resolution in the BaseAPIKeyManager and the accompanying tests for missing keys, so it does not fully capture the main changes introduced by the PR. Please update the title to reflect both the new helper method for encryption key resolution and the default environment variable updates, for example: “Centralize API key encryption key resolution and add default env var values.”
✅ Passed checks (2 passed)
Check name Status Explanation
Docstring Coverage ✅ Passed No functions found in the changes. Docstring coverage check skipped.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch hotfix/api-key-env-vars

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.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think a ImproperlyConfigured exception would be more appropriate

@vitorguima vitorguima self-requested a review October 15, 2025 13:44
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.

6 participants