Skip to content
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

Remove SecretsFilter from log after disconnection #3369

Merged
merged 2 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion netmiko/base_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,8 @@ def __init__(
if self.secret:
no_log["secret"] = self.secret
# Always sanitize username and password
log.addFilter(SecretsFilter(no_log=no_log))
self._secrets_filter = SecretsFilter(no_log=no_log)
log.addFilter(self._secrets_filter)

# Netmiko will close the session_log if we open the file
if session_log is not None:
Expand Down Expand Up @@ -2480,6 +2481,7 @@ def disconnect(self) -> None:
self.remote_conn = None
if self.session_log:
self.session_log.close()
log.removeFilter(self._secrets_filter)

def commit(self) -> str:
"""Commit method for platforms that support this."""
Expand Down
12 changes: 11 additions & 1 deletion tests/unit/test_base_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from os.path import dirname, join
from threading import Lock

from netmiko import NetmikoTimeoutException
from netmiko import NetmikoTimeoutException, log
from netmiko.base_connection import BaseConnection

RESOURCE_FOLDER = join(dirname(dirname(__file__)), "etc")
Expand Down Expand Up @@ -480,3 +480,13 @@ def test_strip_ansi_codes():

# code_next_line must be substituted with a return
assert connection.strip_ansi_escape_codes("\x1bE") == "\n"


def test_remove_SecretsFilter_after_disconnection():
connection = BaseConnection(
host="testhost", # Enter the hostname to pass initialization
auto_connect=False, # No need to connect for the test purposes
)
connection.disconnect()

assert not log.filters
Loading