exceptionfactory commented on code in PR #6054: URL: https://github.com/apache/nifi/pull/6054#discussion_r880712490
########## nifi-commons/nifi-flow-encryptor/src/main/java/org/apache/nifi/flow/encryptor/XmlFlowEncryptor.java: ########## @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "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. + */ +package org.apache.nifi.flow.encryptor; + +import org.apache.nifi.encrypt.PropertyEncryptor; +import org.apache.nifi.xml.processing.stream.StandardXMLEventReaderProvider; +import org.apache.nifi.xml.processing.stream.XMLEventReaderProvider; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import javax.xml.stream.XMLEventFactory; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLEventWriter; +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.Characters; +import javax.xml.stream.events.StartDocument; +import javax.xml.stream.events.XMLEvent; +import javax.xml.transform.stream.StreamSource; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.UncheckedIOException; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +public class XmlFlowEncryptor implements FlowEncryptor { + private static final Pattern ENCRYPTED_PATTERN = Pattern.compile("enc\\{([^\\}]+?)\\}"); + + private static final int FIRST_GROUP = 1; + + private static final String ENCRYPTED_FORMAT = "enc{%s}"; + + private static final XMLEventReaderProvider eventReaderProvider = new StandardXMLEventReaderProvider(); + + private final Logger logger = LoggerFactory.getLogger(XmlFlowEncryptor.class); + + @Override + public void processFlow(InputStream inputStream, OutputStream outputStream, PropertyEncryptor inputEncryptor, PropertyEncryptor outputEncryptor) { + XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance(); + XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); + final XMLEventFactory eventFactory = XMLEventFactory.newInstance(); + try { + final XMLEventReader reader = eventReaderProvider.getEventReader(new StreamSource(inputStream)); + final XMLEventWriter writer = xmlOutputFactory.createXMLEventWriter(outputStream, "UTF-8"); + while (reader.hasNext()) { + final XMLEvent event = reader.nextEvent(); + if (event.getEventType() == XMLEvent.START_DOCUMENT) { + final StartDocument startDocument = (StartDocument) event; + handleXmlStartDocument(startDocument, outputStream); + } else if (event.getEventType() == XMLEvent.CHARACTERS) { + final Characters characters = event.asCharacters(); + final String value = characters.getData(); + final Matcher matcher = ENCRYPTED_PATTERN.matcher(value); + if (matcher.matches()) { + final String processedValue = getOutputEncrypted(matcher.group(FIRST_GROUP), inputEncryptor, outputEncryptor); + writer.add(eventFactory.createCharacters(processedValue)); + } else { + writer.add(characters); + } + } else if (event.getEventType() == XMLEvent.COMMENT) { + writer.add(event); + final Characters newLine = eventFactory.createCharacters("\n"); + writer.add(newLine); + } else if (event.getEventType() == XMLEvent.END_DOCUMENT) { + final Characters newLine = eventFactory.createCharacters("\n"); + writer.add(newLine); + } else { + writer.add(event); + } + } + writer.flush(); + writer.close(); + reader.close(); + outputStream.close(); + inputStream.close(); + } catch (XMLStreamException e) { + logger.error("Failed Processing Flow Configuration: XMLStreamException occurred"); + e.printStackTrace(); + } catch (IOException e) { + throw new UncheckedIOException("Failed Processing Flow Configuration", e); + } + } + + private String getOutputEncrypted(final String inputEncrypted, final PropertyEncryptor inputEncryptor, final PropertyEncryptor outputEncryptor) { + final String inputDecrypted = inputEncryptor.decrypt(inputEncrypted); + final String outputEncrypted = outputEncryptor.encrypt(inputDecrypted); + return String.format(ENCRYPTED_FORMAT, outputEncrypted); + } + + private void handleXmlStartDocument(final StartDocument startDocument, final OutputStream outputStream) { + final PrintWriter writer = new PrintWriter(new OutputStreamWriter(outputStream)); + final String version = startDocument.getVersion(); + final String encoding = startDocument.getCharacterEncodingScheme(); + String standalone = ""; + if (startDocument.standaloneSet()) { + standalone = startDocument.isStandalone() ? " standalone=\"yes\"" : " standalone=\"no\""; + } + final String line = String.format("<?xml version=\"%s\" encoding=\"%s\"%s?>", version, encoding, standalone); + writer.println(line); + writer.flush(); + } Review Comment: Thanks for the explanation. Since `standalone="no"` is the default value when not specified, and that is always the preferred setting for the flow configuration, it seems better to just remove the custom handling in this method. ########## nifi-commons/nifi-flow-encryptor/src/main/java/org/apache/nifi/flow/encryptor/AbstractFlowEncryptor.java: ########## @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "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. + */ +package org.apache.nifi.flow.encryptor; + +import org.apache.nifi.encrypt.PropertyEncryptor; + +import java.io.InputStream; +import java.io.OutputStream; +import java.util.regex.Pattern; + +public abstract class AbstractFlowEncryptor implements FlowEncryptor { + protected static final Pattern ENCRYPTED_PATTERN = Pattern.compile("^enc\\{([^\\}]+?)\\}$"); + + protected static final int FIRST_GROUP = 1; + + protected static final String ENCRYPTED_FORMAT = "enc{%s}"; + + @Override + public abstract void processFlow(final InputStream inputStream, final OutputStream outputStream, + final PropertyEncryptor inputEncryptor, final PropertyEncryptor outputEncryptor); Review Comment: Since this interface method is already declared, it is not necessary to declare it again in this abstract class. ########## nifi-commons/nifi-flow-encryptor/src/main/java/org/apache/nifi/flow/encryptor/XmlFlowEncryptor.java: ########## @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "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. + */ +package org.apache.nifi.flow.encryptor; + +import org.apache.nifi.encrypt.PropertyEncryptor; +import org.apache.nifi.xml.processing.stream.StandardXMLEventReaderProvider; +import org.apache.nifi.xml.processing.stream.XMLEventReaderProvider; + +import javax.xml.stream.XMLEventFactory; +import javax.xml.stream.XMLEventReader; +import javax.xml.stream.XMLEventWriter; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.events.Characters; +import javax.xml.stream.events.StartDocument; +import javax.xml.stream.events.XMLEvent; +import javax.xml.transform.stream.StreamSource; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.PrintWriter; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.util.regex.Matcher; + +public class XmlFlowEncryptor extends AbstractFlowEncryptor { + private static final XMLEventReaderProvider eventReaderProvider = new StandardXMLEventReaderProvider(); + + @Override + public void processFlow(final InputStream inputStream, final OutputStream outputStream, + final PropertyEncryptor inputEncryptor, final PropertyEncryptor outputEncryptor) { + XMLOutputFactory xmlOutputFactory = XMLOutputFactory.newInstance(); + final XMLEventFactory eventFactory = XMLEventFactory.newInstance(); + try { + final XMLEventReader reader = eventReaderProvider.getEventReader(new StreamSource(inputStream)); + final XMLEventWriter writer = xmlOutputFactory.createXMLEventWriter(outputStream, StandardCharsets.UTF_8.name()); + while (reader.hasNext()) { + final XMLEvent event = reader.nextEvent(); + if (event.getEventType() == XMLEvent.START_DOCUMENT) { + final StartDocument startDocument = (StartDocument) event; + handleXmlStartDocument(startDocument, outputStream); + } else if (event.getEventType() == XMLEvent.CHARACTERS) { + final Characters characters = event.asCharacters(); + final String value = characters.getData(); + final Matcher matcher = ENCRYPTED_PATTERN.matcher(value); + if (matcher.matches()) { + final String processedValue = getOutputEncrypted(matcher.group(FIRST_GROUP), inputEncryptor, outputEncryptor); + writer.add(eventFactory.createCharacters(processedValue)); + } else { + writer.add(characters); + } + } else if (event.getEventType() == XMLEvent.COMMENT) { + writer.add(event); + final Characters newLine = eventFactory.createCharacters("\n"); + writer.add(newLine); + } else if (event.getEventType() == XMLEvent.END_DOCUMENT) { + final Characters newLine = eventFactory.createCharacters("\n"); + writer.add(newLine); + } else { + writer.add(event); + } + } + writer.flush(); + writer.close(); + reader.close(); + outputStream.close(); + inputStream.close(); + } catch (XMLStreamException e) { + final String msg = String.format("XMLStreamException occurred - %s", e.getMessage()); + throw new RuntimeException(msg, e); Review Comment: Instead of including and repeating the exception message, in this case it is better to have a simple message. The stack trace would include the message from the wrapped exception: ```suggestion throw new RuntimeException("Flow XML Processing Failed", e); ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
