Skip to content

update ConfigModel to prevent api key output in a debug log#29

Merged
hmasdev merged 8 commits intomainfrom
fix/fix-the-issue-of-log-content
Nov 16, 2025
Merged

update ConfigModel to prevent api key output in a debug log#29
hmasdev merged 8 commits intomainfrom
fix/fix-the-issue-of-log-content

Conversation

@hmasdev
Copy link
Copy Markdown
Owner

@hmasdev hmasdev commented Nov 15, 2025

I found that debug logs might contains the OPENAI API KEY like

DEBUG:simple_typing_application.config:config: sentence_generator_type=<ESentenceGeneratorType.OPENAI: 'OPENAI'> sentence_generator_config={'model': 'gpt-5-nano', 'temperature': 0.7, 'openai_api_key': 'sk-proj-hogehoge', 'memory_size': 0, 'max_retry': 5} user_interface_type=<EUserInterfaceType.CONSOLE: 'CONSOLE'> user_interface_config={} key_monitor_type=<EKeyMonitorType.PYNPUT: 'PYNPUT'> key_monitor_config={} record_direc='./record'

(b82784b). This is a security concern because sensitive configuration values such as openai_api_key are being logged in plain text via the debug logger.

This pull request refactors the configuration handling throughout the application to use strongly-typed Pydantic model instances instead of generic dictionaries. This change improves type safety, code clarity, and maintainability across the core factory functions and their corresponding tests. The update also modifies how configuration models are passed and processed, requiring updates to parameter types and test setups.

Refactoring configuration handling

  • Updated the ConfigModel in simple_typing_application/models/config_models/general_config_model.py to use specific Pydantic model instances for sentence_generator_config, user_interface_config, and key_monitor_config fields, replacing the previous use of generic dictionaries.

  • Refactored factory functions (create_key_monitor, create_sentence_generator, and create_user_interface) to accept configuration model instances instead of dictionaries, and adjusted their internal logic to use .model_dump() for serialization. Changes applied in simple_typing_application/key_monitor/factory.py, simple_typing_application/sentence_generator/factory.py, and simple_typing_application/ui/factory.py. [1] [2] [3] [4] [5] [6]

Test updates

  • Updated all relevant test parameterizations and function signatures to pass configuration model instances instead of dictionaries, ensuring tests match the new function interfaces. Changes made in tests/key_monitor/test_factory.py, tests/sentence_generator/test_factory.py, and tests/ui/test_factory.py. [1] [2] [3] [4] [5] [6] [7] [8] [9] [10]

  • Modified the configuration comparison logic in tests/test_config.py to exclude sensitive fields (like openai_api_key) from equality checks, reflecting the new model-based configuration structure.

Check

uv run python -m simple_typing_application outputs a debug log:

DEBUG:simple_typing_application.config:config: sentence_generator_type=<ESentenceGeneratorType.OPENAI: 'OPENAI'> sentence_generator_config=OpenAISentenceGeneratorConfigModel(model='gpt-5-nano', temperature=0.7, openai_api_key=SecretStr('**********'), memory_size=0, max_retry=5) user_interface_type=<EUserInterfaceType.CONSOLE: 'CONSOLE'> user_interface_config=BaseUserInterfaceConfigModel() key_monitor_type=<EKeyMonitorType.PYNPUT: 'PYNPUT'> key_monitor_config=BaseKeyMonitorConfigModel() record_direc='./record'

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request refactors configuration handling to use strongly-typed Pydantic model instances instead of generic dictionaries throughout the application. The changes improve type safety by requiring factory functions to accept specific configuration model types rather than untyped dictionaries.

Key changes:

  • Updated ConfigModel to use Pydantic model instances for nested configuration fields instead of dictionaries
  • Modified factory functions to accept typed configuration models and use .model_dump() for serialization
  • Updated all test cases to pass model instances instead of dictionaries to factory functions

Reviewed Changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
simple_typing_application/models/config_models/general_config_model.py Changed nested config fields from dict types to union types of specific Pydantic models
simple_typing_application/key_monitor/factory.py Updated create_key_monitor to accept BaseKeyMonitorConfigModel instead of dict
simple_typing_application/sentence_generator/factory.py Updated create_sentence_generator to accept BaseSentenceGeneratorConfigModel instead of dict
simple_typing_application/ui/factory.py Updated create_user_interface to accept BaseUserInterfaceConfigModel instead of dict
tests/key_monitor/test_factory.py Updated test parameterization to use model instances; removed unused mocker parameter
tests/sentence_generator/test_factory.py Updated test parameterization to use model instances
tests/ui/test_factory.py Updated test parameterization to use model instances; removed unused mocker parameter
tests/test_config.py Added API key handling logic to exclude sensitive fields from test assertions
Comments suppressed due to low confidence (6)

tests/ui/test_factory.py:90

  • The factory function signature was changed to accept a BaseUserInterfaceConfigModel instance instead of a dictionary. This test will fail because {} is not a valid BaseUserInterfaceConfigModel instance. Replace {} with BaseUserInterfaceConfigModel().
            {},

tests/ui/test_factory.py:105

  • The factory function signature was changed to accept a BaseUserInterfaceConfigModel instance instead of a dictionary. This test will fail because {} is not a valid BaseUserInterfaceConfigModel instance. Replace {} with BaseUserInterfaceConfigModel().
            {},

tests/sentence_generator/test_factory.py:153

  • The factory function signature was changed to accept a BaseSentenceGeneratorConfigModel instance instead of a dictionary. This test will fail because {} is not a valid BaseSentenceGeneratorConfigModel instance. Replace {} with BaseSentenceGeneratorConfigModel().
            {},

tests/sentence_generator/test_factory.py:168

  • The factory function signature was changed to accept a BaseSentenceGeneratorConfigModel instance instead of a dictionary. This test will fail because {} is not a valid BaseSentenceGeneratorConfigModel instance. Replace {} with BaseSentenceGeneratorConfigModel().
            {},

tests/key_monitor/test_factory.py:91

  • The factory function signature was changed to accept a BaseKeyMonitorConfigModel instance instead of a dictionary. This test will fail because {} is not a valid BaseKeyMonitorConfigModel instance. Replace {} with BaseKeyMonitorConfigModel().
            {},

tests/key_monitor/test_factory.py:106

  • The factory function signature was changed to accept a BaseKeyMonitorConfigModel instance instead of a dictionary. This test will fail because {} is not a valid BaseKeyMonitorConfigModel instance. Replace {} with BaseKeyMonitorConfigModel().
            {},

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

hmasdev and others added 5 commits November 16, 2025 02:24
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

key_monitor_type: EKeyMonitorType = EKeyMonitorType.PYNPUT
key_monitor_config: dict[str, str | float | int | None | dict | list] = BaseKeyMonitorConfigModel().model_dump() # noqa
key_monitor_config: (
BaseKeyMonitorConfigModel | PynputBasedKeyMonitorConfigModel | SSHKeyboardBasedKeyMonitorConfigModel
Copy link

Copilot AI Nov 15, 2025

Choose a reason for hiding this comment

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

[nitpick] Inconsistent formatting: The union types for key_monitor_config are all on one line (line 40), while sentence_generator_config splits each union type onto separate lines (lines 27-30). For consistency and readability, consider formatting key_monitor_config the same way:

key_monitor_config: (
    BaseKeyMonitorConfigModel 
    | PynputBasedKeyMonitorConfigModel 
    | SSHKeyboardBasedKeyMonitorConfigModel
) = BaseKeyMonitorConfigModel()
Suggested change
BaseKeyMonitorConfigModel | PynputBasedKeyMonitorConfigModel | SSHKeyboardBasedKeyMonitorConfigModel
BaseKeyMonitorConfigModel
| PynputBasedKeyMonitorConfigModel
| SSHKeyboardBasedKeyMonitorConfigModel

Copilot uses AI. Check for mistakes.
@hmasdev hmasdev deleted the branch main November 15, 2025 17:44
@hmasdev hmasdev closed this Nov 15, 2025
@hmasdev hmasdev reopened this Nov 15, 2025
@hmasdev hmasdev changed the base branch from chore/fix-code-style to main November 15, 2025 17:47
@hmasdev hmasdev changed the title log: update ConfigModel to prevent api key output in a debug log update ConfigModel to prevent api key output in a debug log Nov 15, 2025
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@hmasdev hmasdev merged commit eb89f78 into main Nov 16, 2025
29 checks passed
@hmasdev hmasdev deleted the fix/fix-the-issue-of-log-content branch November 16, 2025 02:17
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