Mask Passwords with Logback?

后端 未结 2 803
一向
一向 2021-02-03 11:56

We currently generically log all XML documents coming in and going out of our system, and some of them contain passwords in the clear. We would like to be able to configure the

相关标签:
2条回答
  • 2021-02-03 11:58

    The logback version 0.9.27 introduced replacement capability. Replacements support regular expressions. For example, if the logged message was "userid=alice, pswd='my secret'", and the output pattern was

      "%d [%t] $logger - %msg%n",
    

    you just modify the pattern to

     "%d [%t] $logger - %replace(%msg){"pswd='.*'", "pswd='xxx'"}%n"
    

    Note that the above makes use of option quoting.

    The previous log message would be output as "userid=alice, pswd='xxx'"

    For blazing performance, you could also mark the log statement as CONFIDENTIAL and instruct %replace to perform replacement only for log statements marked as CONFIDENTIAL. Example,

     Marker confidential = MarkerFactory.getMarker("CONFIDENTIAL");
     logger.info(confidential, "userid={}, password='{}'", userid, password);
    

    Unfortunately, the current version of logback does not yet support conditional replacements (based on markers or otherwise). However, you could easily write your own replacement code by extending ReplacingCompositeConverter. Shout on the logback-user mailing list if you need further assistance.

    0 讨论(0)
  • 2021-02-03 12:03

    I believe Masking is an aspect of your business, not the aspect of any technology or logging system. There are situations where the passwords, national identities etc should be masked while storing them in the DB as well due to legal reasons. You should be able to mask the xml before giving it to the logger.

    One way to do it is to run the XML through XSLT that does that making and then give it to logger for logging.

    If you doesn't want to do this then LogBack has Filters support that is one of the option (not the right one though).

    But understand that any generic out of the box solution you are trying to find at the logging infrastructure level is going to be suboptimal as every log message is going to be checked for masking.

    0 讨论(0)
提交回复
热议问题