Skip to content

Commit

Permalink
Fixes replacement of masked secrets (microsoft#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
markwaddle authored Oct 18, 2024
1 parent b2a4555 commit 37dab15
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 16 deletions.
2 changes: 0 additions & 2 deletions libraries/python/chat-driver/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
"python.analysis.typeCheckingMode": "basic",
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"python.testing.cwd": "${workspaceFolder}",
"python.testing.pytestArgs": ["--color", "yes"],
"python.testing.pytestEnabled": true,
"search.exclude": {
"**/.venv": true,
"**/data": true
Expand Down
2 changes: 0 additions & 2 deletions libraries/python/events/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
"python.analysis.typeCheckingMode": "basic",
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"python.testing.cwd": "${workspaceFolder}",
"python.testing.pytestArgs": ["--color", "yes"],
"python.testing.pytestEnabled": true,
"search.exclude": {
"**/.venv": true,
"**/data": true
Expand Down
2 changes: 0 additions & 2 deletions libraries/python/guided-conversation/.vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
"python.analysis.typeCheckingMode": "basic",
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"python.testing.cwd": "${workspaceFolder}",
"python.testing.pytestArgs": ["--color", "yes"],
"python.testing.pytestEnabled": true,
"search.exclude": {
"**/.venv": true,
"**/data": true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import inspect
import os
import re
import types
from collections import ChainMap
from enum import StrEnum
Expand Down Expand Up @@ -212,7 +213,7 @@ def replace_config_secret_str_masked_values(model_values: ModelT, original_model
continue

if field_info.annotation is ConfigSecretStr:
if getattr(updated_model_values, field_name) == _mask(getattr(original_model_values, field_name)):
if hasattr(original_model_values, field_name) and re.match(r"^[*]+$", getattr(updated_model_values, field_name)):
setattr(updated_model_values, field_name, getattr(original_model_values, field_name))
continue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,32 @@ class TestModel(BaseModel):

def test_config_secret_str_deserialization() -> None:
class SubModel1(BaseModel):
secret: ConfigSecretStr
submodel1_secret: ConfigSecretStr

class SubModel2(BaseModel):
secret: ConfigSecretStr
submodel2_secret: ConfigSecretStr

class TestModel(BaseModel):
secret: ConfigSecretStr
sub_model: SubModel1 | SubModel2

secret_value = uuid.uuid4().hex

model = TestModel(secret=secret_value, sub_model=SubModel2(secret=secret_value))
sub_model = SubModel2(submodel2_secret=secret_value)
model = TestModel(secret=secret_value, sub_model=sub_model)
assert model.secret == secret_value

serialized_config = model.model_dump(mode="json")

assert serialized_config["secret"] == "*" * len(secret_value)
assert serialized_config["sub_model"]["secret"] == "*" * len(secret_value)
assert serialized_config["sub_model"]["submodel2_secret"] == "*" * len(secret_value)

deserialized_config = TestModel.model_validate(serialized_config)

masked_reverted = replace_config_secret_str_masked_values(deserialized_config, model)
assert masked_reverted.secret == model.secret
assert masked_reverted.sub_model.secret == model.sub_model.secret
assert isinstance(masked_reverted.sub_model, SubModel2)
assert masked_reverted.sub_model.submodel2_secret == sub_model.submodel2_secret

deserialized_model = TestModel.model_validate(masked_reverted)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
"python.analysis.typeCheckingMode": "basic",
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"python.testing.cwd": "${workspaceFolder}",
"python.testing.pytestArgs": ["--color", "yes"],
"python.testing.pytestEnabled": true,
"search.exclude": {
"**/.venv": true,
"**/data": true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
"python.analysis.typeCheckingMode": "basic",
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python",
"python.testing.cwd": "${workspaceFolder}",
"python.testing.pytestArgs": ["--color", "yes"],
"python.testing.pytestEnabled": true,
"search.exclude": {
"**/.venv": true,
"**/data": true
Expand Down

0 comments on commit 37dab15

Please sign in to comment.