如何屏蔽xml文件中的敏感数据?

时间:2019-01-25 09:17:59

标签: xml groovy

我要在xml文件中屏蔽的敏感数据列表。它能够屏蔽这些列表数据,但是在创建新的xml(Masked XML)时,实际XML中的“ Pack” xml标签内容未如预期的那样在(Masked XML)中回写,仅回写了值,并且“打包” xml标记被省略。

请帮助解决这里的问题,即使某个地方的ipAddres也没有被掩盖。

我将下面的代码附加到一部分代码中,并提供了这两个xml内容。

import groovy.xml.XmlUtil
import java.util.regex.Pattern;

public class SensitiveDatamasking { 
    private static String masked = "####"   
    def attributeList = ["username", "password","ipAddress"]    
    private static final String IPADDRESS_PATTERN = "(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
    String removeSensetiveData(File file){
        removeSensetiveData(file.text)
    }       
    String removeSensetiveData(String xmlText){     
        def root = new XmlParser().parseText(xmlText);      
        nameValuePair(root)
        attributeValue(root)        
        nodeValue(root)     
        XmlUtil.serialize(root)
    }   
    private void nodeValue(def root) {      
        def elements = root."**".findAll{
            it.text().size() > 0 && it.children().size() == 1
        }       
        Pattern  pattern = Pattern.compile(IPADDRESS_PATTERN);      
        elements.each {             
            it.value =  it.text().replaceAll(pattern, masked)
        }
    }   
    private void nameValuePair(def root) {      
        def nodes = root."**".findAll{
             it?.@name?.toString()?.toLowerCase()?.contains("username" ) ||
             it?.@name?.toString()?.toLowerCase()?.contains( "password") ||
             it?.@name?.toString()?.toLowerCase()?.contains( "ipAddress") }     
        maskValue(nodes)
    }   
    private void attributeValue(def root) {     
        attributeList.each {
            maskAttribute( root, it)
        }
    }   
    private def maskAttribute(def root, String attribute) {
        def nodes =  root."**".findAll{ it."""@$attribute"""}
        maskAttributes( nodes, attribute)
    }

    private void maskValue(def nodes) {     
        nodes.each {  if (it.@value?.size() > 0) {
            it.@value = masked }
        }       
    }   
    private void maskAttributes(def nodes, String keyName) {
        nodes.each { it."""@$keyName""" = masked }
    }   
    static main(args) { 
        File file = new File("C:\\shared\\test\\zenderJan2019.xml")     
        SensitiveDatamasking xml = new SensitiveDatamasking()
        String maskedXml = xml.removeSensetiveData(file)        
        File output = new File("C:\\shared\\test\\zenderJan2019_masked.xml")
        output.write(maskedXml)
    }
}

实际xml (预期输出应与此xml内容相匹配,并且用户名,密码和ipAddress的值应仅进行掩码更改)

<?xml version="1.0" encoding="UTF-8"?>
<org_dump date="1/25/19 8:17 AM" version="1.1">
  <DETAIL>
    <osname>Linux</osname>
    <hostname>zion.zender.com</hostname>
    <release>3.10.0-693.el7.x86_64</release>
  </DETAIL>
  <INFO>
    <version>1.10.5 beta</version>
    <Pack>
      <INFO>
        <NUM>1.1</NUM>
        <PREVIOUS_PACK_APPLIED/>
        <RELEASEDATETIME>Jan 17, 2019</RELEASEDATETIME>
        <APPLIEDDATETIME>Thu Jan 24 14:28:52 IST 2019</APPLIEDDATETIME>
        <PRODUCT>zender</PRODUCT>
        <VERSION>1.10.5 beta</VERSION>
        <SYMPTOMS>1.10.5 beta jag</SYMPTOMS>
      </INFO>
    </Pack>
    <MetaData>
      <Gateways>
        <Node port="14528" httpPort="14527" host="zion.zender.com" name="node01" ipAddress="127.0.1.1"/>
      </Gateways>
      <TeamGroup name="DomainTeams">
        <Team name="username" value="andysimmons"/>
        <Team name="password" value="123456"/>
        <Team name="ipAddress" value="127.0.1.1"/>
      </TeamGroup>
      <Service name="Jag_Service" version="">
        <ServiceProcess>
          <Node name="node01"/>
          <PreStartCommand/>
          <PostStartCommand/>
          <JvmTeams/>
          <TeamGroup name="CUSTOM_PROPERTIES"/>
          <TeamGroup name="REPOSITORY">
            <Team name="LoggingDirectory" value=""/>
            <Team name="LogLevel" value="INFO"/>
            <Team name="ActivateDumpPersistenceConfigurationToFile" value="false"/>
            <Team name="ActivateLogPersistenceSQLToFile" value="false"/>
          </TeamGroup>
        </ServiceProcess>
        <TeamGroup name="CACHE">
          <Team name="EnableCache" value="false"/>
          <Team name="CacheJVMTeams" value="-Xmx128m"/>
        </TeamGroup>
      </Service>
    </MetaData>
  </INFO>
</org_dump>

屏蔽的XML

<?xml version="1.0" encoding="UTF-8"?>
<org_dump date="1/25/19 8:17 AM" version="1.1">
  <DETAIL>
    <osname>Linux</osname>
    <hostname>zion.zender.com</hostname>
    <release>3.10.0-693.el7.x86_64</release>
  </DETAIL>
  <INFO>
    <version>1.10.5 beta</version>
    <Pack>1.1Jan 17, 2019Thu Jan 24 14:28:52 IST 2019zender1.10.5 beta1.10.5 beta jag</Pack>
    <MetaData>
      <Gateways>
        <Node port="14528" httpPort="14527" host="zion.zender.com" name="node01" ipAddress="####"/>
      </Gateways>
      <TeamGroup name="DomainTeams">
        <Team name="username" value="####"/>
        <Team name="password" value="####"/>
        <Team name="ipAddress" value="127.1.1.1"/>
      </TeamGroup>
      <Service name="Jag_Service" version="">
        <ServiceProcess>
          <Node name="node01"/>
          <PreStartCommand/>
          <PostStartCommand/>
          <JvmTeams/>
          <TeamGroup name="CUSTOM_PROPERTIES"/>
          <TeamGroup name="REPOSITORY">
            <Team name="LoggingDirectory" value=""/>
            <Team name="LogLevel" value="INFO"/>
            <Team name="ActivateDumpPersistenceConfigurationToFile" value="false"/>
            <Team name="ActivateLogPersistenceSQLToFile" value="false"/>
          </TeamGroup>
        </ServiceProcess>
        <TeamGroup name="CACHE">
          <Team name="EnableCache" value="false"/>
          <Team name="CacheJVMTeams" value="-Xmx128m"/>
        </TeamGroup>
      </Service>
    </MetaData>
  </INFO>
</org_dump>

1 个答案:

答案 0 :(得分:1)

您应该能够做到:

File file = new File("C:\\shared\\test\\zenderJan2019.xml")     
def xml = new XmlParser().parse(file)
xml.'**'.findAll { it.@name in ['username', 'password', 'ipAddress'] }*.@value = '####'
println XmlUtil.serialize(xml)

希望这会有所帮助

已更新以掩盖我错过的更多内容:

def masked = ['username', 'password', 'ipAddress']
File file = new File("C:\\shared\\test\\zenderJan2019.xml")     
def xml = new XmlParser().parse(file)
xml.'**'.each { node ->
    if (node.@name in masked) {
        node.@value = '####'
    }
    masked.each { m -> if (node.@"$m") node.@"$m" = '####' }
}
println XmlUtil.serialize(xml)

如果您将println XmlUtil.serialize(xml)更改为:

StringWriter writer = new StringWriter()
new XmlNodePrinter(new IndentPrinter(new PrintWriter(writer), '', false)).print(xml)
println writer.toString()

它将以“非漂亮”(最小化)的方式输出XML