Skip to content

Commit

Permalink
allow the same secret and identifier to be redacted multiple times
Browse files Browse the repository at this point in the history
  • Loading branch information
jeking3 committed Sep 24, 2020
1 parent abfd513 commit 255e6d5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
13 changes: 9 additions & 4 deletions interposer/tapedeck.py
Original file line number Diff line number Diff line change
Expand Up @@ -378,19 +378,24 @@ def redact(self, secret: str, identifier: str) -> str:
key = f"_redact_{identifier}"

if self.mode == Mode.Recording:
secretlen = len(secret)
redacted = (identifier + ("_" * secretlen))[:secretlen]
if self._redactions.get(secret) == redacted:
# calling it more than once for the same secret and ID is ok
return secret

if self._tape.get(key):
raise AttributeError(
f"{identifier} has already been used to redact a secret"
f"{identifier} has already been used to redact another secret"
)
secretlen = len(secret)
self._redactions[secret] = (identifier + ("_" * secretlen))[:secretlen]
self._redactions[secret] = redacted
self._tape[key] = secretlen
return secret
else:
secretlen = self._tape.get(key)
if not secretlen:
raise AttributeError(
f"{identifier} was not used during recording to redact a secret"
f"{identifier} was not used during recording to redact this secret"
)
return (identifier + ("_" * secretlen))[:secretlen]

Expand Down
2 changes: 2 additions & 0 deletions tests/tapedeck_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ def test_recording_secrets(self):
# a secret redaction identifier can only be used once in a recording
with self.assertRaises(AttributeError):
uut.redact("foo", "REDACTED_SMALLER_THAN_ORIGINAL")
# but if the secret is the same that is not an error
assert uut.redact(token, "REDACTED_SMALLER_THAN_ORIGINAL") == token

# now during playback see everything with a secret (token) has been redacted!

Expand Down

0 comments on commit 255e6d5

Please sign in to comment.