Example #1
0
  def test_redact_empty_string(self):
    engine = RedactionEngine([
        RedactionRule('password='******'password="******"', 'password="******"'),
    ])

    assert_equal(engine.redact(None), None)
    assert_equal(engine.redact(''), '')
Example #2
0
  def test_redact_empty_string(self):
    engine = RedactionEngine([
        RedactionRule('password='******'password="******"', 'password="******"'),
    ])

    assert_equal(engine.redact(None), None)
    assert_equal(engine.redact(''), '')
Example #3
0
  def test_equality(self):
    engine1 = RedactionEngine([
        RedactionRule('password='******'password="******"', 'password="******"'),
    ])
    engine2 = RedactionEngine([
        RedactionRule('password='******'password="******"', 'password="******"'),
    ])
    engine3 = RedactionEngine([
        RedactionRule('ssn=', 'ssn=\d{3}-\d{2}-\d{4}', 'ssn=XXX-XX-XXXX'),
    ])

    assert_equal(engine1, engine2)
    assert_not_equal(engine1, engine3)
Example #4
0
  def test_redaction_works(self):
    redaction_engine = RedactionEngine([
      RedactionRule('password='******'password="******"', 'password="******"'),
      RedactionRule('ssn=', 'ssn=\d{3}-\d{2}-\d{4}', 'ssn=XXX-XX-XXXX'),
    ])

    test_strings = [
        ('message', 'message'),
        ('password="******"', 'password="******"'),
        ('before password="******" after', 'before password="******" after'),
        ('an ssn=123-45-6789', 'an ssn=XXX-XX-XXXX'),
    ]

    for message, redacted_message in test_strings:
      assert_equal(redaction_engine.redact(message), redacted_message)
Example #5
0
  def test_redaction_works(self):
    redaction_engine = RedactionEngine([
      RedactionRule('password='******'password="******"', 'password="******"'),
      RedactionRule('ssn=', 'ssn=\d{3}-\d{2}-\d{4}', 'ssn=XXX-XX-XXXX'),
    ])

    test_strings = [
        ('message', 'message'),
        ('password="******"', 'password="******"'),
        ('before password="******" after', 'before password="******" after'),
        ('an ssn=123-45-6789', 'an ssn=XXX-XX-XXXX'),
    ]

    for message, redacted_message in test_strings:
      assert_equal(redaction_engine.redact(message), redacted_message)
Example #6
0
  def setUpClass(cls):
    cls.logger = logging.getLogger(cls.__name__)

    cls.handler = MockLoggingHandler()
    cls.logger.addHandler(cls.handler)

    engine = RedactionEngine([
      RedactionRule('password='******'password="******"', 'password="******"'),
      RedactionRule('ssn=', 'ssn=\d{3}-\d{2}-\d{4}', 'ssn=XXX-XX-XXXX'),
    ])

    add_log_redaction_filter_to_logger(engine, cls.logger)
Example #7
0
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import logging
from desktop.redaction import logfilter
from desktop.redaction.engine import RedactionEngine

global_redaction_engine = RedactionEngine()


def redact(string):
    """
  Redact a string using the global redaction engine.
  """

    return global_redaction_engine.redact(string)


def register_log_filtering(policy):
    """
  `add_redaction_filter` injects the redaction filter into all of the `logger`
  handlers. This must be called after all of the handlers have been added to
  `logger`, otherwise those handlers may expose unredacted strings.