|
| 1 | +# coding: utf-8 |
| 2 | + |
| 3 | +# Copyright 2024 IBM All Rights Reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | + |
| 17 | +# pylint: disable=missing-docstring |
| 18 | + |
| 19 | +import logging |
| 20 | + |
| 21 | +from ibm_cloud_sdk_core import base_service |
| 22 | +from ibm_cloud_sdk_core.authenticators import NoAuthAuthenticator |
| 23 | +from ibm_cloud_sdk_core.logger import LoggingFilter |
| 24 | +from .utils.http_utils import local_server |
| 25 | + |
| 26 | + |
| 27 | +def test_redact_secrets(): |
| 28 | + redact_secrets = LoggingFilter.redact_secrets |
| 29 | + |
| 30 | + assert "secret" not in redact_secrets("Authorization: Bearer secret") |
| 31 | + assert "secret" not in redact_secrets("Authorization: Basic secret") |
| 32 | + assert "secret" not in redact_secrets("X-Authorization: secret") |
| 33 | + assert "secret" not in redact_secrets("ApIKey=secret") |
| 34 | + assert "secret" not in redact_secrets("ApI_Key=secret") |
| 35 | + assert "secret" not in redact_secrets("passCode=secret") |
| 36 | + assert "secret" not in redact_secrets("PASSword=secret") |
| 37 | + assert "secret" not in redact_secrets("toKen=secret") |
| 38 | + assert "secret" not in redact_secrets("client_id=secret") |
| 39 | + assert "secret" not in redact_secrets("client_x509_cert_url=secret") |
| 40 | + assert "secret" not in redact_secrets("client_id=secret") |
| 41 | + assert "secret" not in redact_secrets("key=secret") |
| 42 | + assert "secret" not in redact_secrets("project_id=secret") |
| 43 | + assert "DaSecret" not in redact_secrets("secret=DaSecret") |
| 44 | + assert "secret" not in redact_secrets("subscriptionId=secret") |
| 45 | + assert "secret" not in redact_secrets("tenantId=secret") |
| 46 | + assert "secret" not in redact_secrets("thumbprint=secret") |
| 47 | + assert "secret" not in redact_secrets("token_uri=secret") |
| 48 | + assert "secret" not in redact_secrets('xxx "apIKEy": "secret",xxx') |
| 49 | + assert "secret" not in redact_secrets('xxx "apI_KEy": "secret",xxx') |
| 50 | + assert "secret" not in redact_secrets('xxx "pAsSCoDe": "secret",xxx') |
| 51 | + assert "secret" not in redact_secrets('xxx "passWORD": "secret",xxx') |
| 52 | + assert "secret" not in redact_secrets('xxx {"token": "secret"},xxx') |
| 53 | + assert "secret" not in redact_secrets('xxx "aadClientId": "secret",xxx') |
| 54 | + assert "secret" not in redact_secrets('xxx "aadClientSecret": "secret",xxx') |
| 55 | + assert "secret" not in redact_secrets('xxx "auth": "secret",xxx') |
| 56 | + assert "secret" not in redact_secrets('xxx "auth_provider_x509_cert_url": "secret",xxx') |
| 57 | + assert "secret" not in redact_secrets('xxx "auth_uri": "secret",xxx') |
| 58 | + assert "secret" not in redact_secrets('xxx "client_email": "secret",xxx') |
| 59 | + # Now use a real-world example, to validate the correct behavior. |
| 60 | + assert ( |
| 61 | + redact_secrets( |
| 62 | + 'GET / HTTP/1.1\r\nHost: localhost:3335\r\n' |
| 63 | + + 'User-Agent: ibm-python-sdk-core-3.20.6 os.name=Darwin os.version=23.6.0 python.version=3.11.10\r\n' |
| 64 | + + 'Accept-Encoding: gzip, deflate\r\nAccept: */*\r\nAuthorization: token-foo-bar-123\r\n' |
| 65 | + + 'Connection: keep-alive\r\n\r\n' |
| 66 | + ) |
| 67 | + == 'GET / HTTP/1.1\r\nHost: localhost:3335\r\n' |
| 68 | + + 'User-Agent: ibm-python-sdk-core-3.20.6 os.name=Darwin os.version=23.6.0 python.version=3.11.10\r\n' |
| 69 | + + 'Accept-Encoding: gzip, deflate\r\nAccept: */*\r\nAuthorization: [redacted]\r\nConnection: keep-alive\r\n\r\n' |
| 70 | + ) |
| 71 | + |
| 72 | + |
| 73 | +# Simulate a real-world scenario. |
| 74 | +@local_server(3335) |
| 75 | +def test_redact_secrets_log(caplog): |
| 76 | + # Since we use a real BaseService here, we need to set the logging level |
| 77 | + # to DEBUG in its module, to simulate the real behavior. |
| 78 | + original_logging_level = base_service.logger.level |
| 79 | + base_service.logger.setLevel(logging.DEBUG) |
| 80 | + |
| 81 | + try: |
| 82 | + service = base_service.BaseService(service_url="http://localhost:3335", authenticator=NoAuthAuthenticator()) |
| 83 | + prepped = service.prepare_request('GET', url='/', headers={'Authorization': 'token-foo-bar-123'}) |
| 84 | + res = service.send(prepped) |
| 85 | + except Exception as ex: |
| 86 | + raise ex |
| 87 | + finally: |
| 88 | + # And now we restore the logger's level to the original value. |
| 89 | + base_service.logger.setLevel(original_logging_level) |
| 90 | + |
| 91 | + assert res is not None |
| 92 | + # Make sure the request has been logged and the token is redacted. |
| 93 | + assert "Authorization" in caplog.text |
| 94 | + assert "token" not in caplog.text |
0 commit comments