-
Notifications
You must be signed in to change notification settings - Fork 52.9k
feat: support Bitwarden env alias mapping #55886
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Kaluanbernardo
wants to merge
1
commit into
NousResearch:main
from
Kaluanbernardo:feat/bitwarden-env-map
+197
−0
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| """Tests for Bitwarden env alias mapping in Hermes env loader.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import os | ||
|
|
||
| from agent.secret_sources.bitwarden import FetchResult | ||
| from hermes_cli import env_loader | ||
| from agent.secret_sources import bitwarden as bw | ||
|
|
||
|
|
||
| def test_bitwarden_env_map_materializes_target_and_prunes_aliases(tmp_path, monkeypatch): | ||
| home = tmp_path / ".hermes" | ||
| home.mkdir() | ||
| (home / "config.yaml").write_text( | ||
| "secrets:\n" | ||
| " bitwarden:\n" | ||
| " enabled: true\n" | ||
| " access_token_env: BWS_ACCESS_TOKEN\n" | ||
| " project_id: project-123\n" | ||
| " env_map:\n" | ||
| " DISCORD_BOT_TOKEN_MEOS_DEV: DISCORD_BOT_TOKEN\n" | ||
| " prune_env_prefixes:\n" | ||
| " - DISCORD_BOT_TOKEN_\n", | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| def fake_apply_bitwarden_secrets(**kwargs): | ||
| assert kwargs["project_id"] == "project-123" | ||
| secrets = { | ||
| "DISCORD_BOT_TOKEN_MEOS_DEV": "dev-token", | ||
| "DISCORD_BOT_TOKEN_MEOS_ACADEMIC": "academic-token", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This mocks the legacy |
||
| "OPENAI_API_KEY": "openai-key", | ||
| } | ||
| for key, value in secrets.items(): | ||
| os.environ[key] = value | ||
| return FetchResult( | ||
| secrets=secrets, | ||
| applied=list(secrets), | ||
| ) | ||
|
|
||
| monkeypatch.setattr(bw, "apply_bitwarden_secrets", fake_apply_bitwarden_secrets) | ||
| monkeypatch.setenv("BWS_ACCESS_TOKEN", "access") | ||
| monkeypatch.delenv("DISCORD_BOT_TOKEN", raising=False) | ||
| monkeypatch.delenv("DISCORD_BOT_TOKEN_MEOS_DEV", raising=False) | ||
| monkeypatch.delenv("DISCORD_BOT_TOKEN_MEOS_ACADEMIC", raising=False) | ||
| monkeypatch.delenv("OPENAI_API_KEY", raising=False) | ||
| env_loader.reset_secret_source_cache() | ||
| env_loader._SECRET_SOURCES.clear() | ||
|
|
||
| env_loader._apply_external_secret_sources(home) | ||
|
|
||
| assert env_loader.os.environ["DISCORD_BOT_TOKEN"] == "dev-token" | ||
| assert env_loader.os.environ["OPENAI_API_KEY"] == "openai-key" | ||
| assert "DISCORD_BOT_TOKEN_MEOS_DEV" not in env_loader.os.environ | ||
| assert "DISCORD_BOT_TOKEN_MEOS_ACADEMIC" not in env_loader.os.environ | ||
| assert env_loader.get_secret_source("DISCORD_BOT_TOKEN") == "bitwarden" | ||
| assert env_loader.get_secret_source("OPENAI_API_KEY") == "bitwarden" | ||
| assert env_loader.get_secret_source("DISCORD_BOT_TOKEN_MEOS_DEV") is None | ||
|
|
||
|
|
||
| def test_bitwarden_env_map_respects_existing_target_without_override(tmp_path, monkeypatch): | ||
| home = tmp_path / ".hermes" | ||
| home.mkdir() | ||
| (home / "config.yaml").write_text( | ||
| "secrets:\n" | ||
| " bitwarden:\n" | ||
| " enabled: true\n" | ||
| " access_token_env: BWS_ACCESS_TOKEN\n" | ||
| " project_id: project-123\n" | ||
| " override_existing: false\n" | ||
| " env_map:\n" | ||
| " DISCORD_BOT_TOKEN_MEOS_DEV: DISCORD_BOT_TOKEN\n", | ||
| encoding="utf-8", | ||
| ) | ||
|
|
||
| def fake_apply_bitwarden_secrets(**kwargs): | ||
| os.environ["DISCORD_BOT_TOKEN_MEOS_DEV"] = "dev-token" | ||
| return FetchResult( | ||
| secrets={"DISCORD_BOT_TOKEN_MEOS_DEV": "dev-token"}, | ||
| applied=["DISCORD_BOT_TOKEN_MEOS_DEV"], | ||
| ) | ||
|
|
||
| monkeypatch.setattr(bw, "apply_bitwarden_secrets", fake_apply_bitwarden_secrets) | ||
| monkeypatch.setenv("BWS_ACCESS_TOKEN", "access") | ||
| monkeypatch.setenv("DISCORD_BOT_TOKEN", "existing-token") | ||
| env_loader.reset_secret_source_cache() | ||
| env_loader._SECRET_SOURCES.clear() | ||
|
|
||
| env_loader._apply_external_secret_sources(home) | ||
|
|
||
| assert env_loader.os.environ["DISCORD_BOT_TOKEN"] == "existing-token" | ||
| assert "DISCORD_BOT_TOKEN_MEOS_DEV" not in env_loader.os.environ | ||
| assert env_loader.get_secret_source("DISCORD_BOT_TOKEN_MEOS_DEV") is None | ||
| assert env_loader.get_secret_source("DISCORD_BOT_TOKEN") is None | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Current main no longer has this direct Bitwarden-result path:
hermes_cli/env_loader.py:341-346callsregistry.apply_all(), which owns the actual environment writes. Please move aliasing into the active SecretSource/registry flow so it preserves precedence, protected-variable handling, and provenance.