martinzink commented on code in PR #2225: URL: https://github.com/apache/nifi-minifi-cpp/pull/2225#discussion_r4046159782
########## minifi_rust/extensions/minifi_pgp/src/processors/decrypt_content.rs: ########## @@ -0,0 +1,399 @@ +// 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 +// +// https://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. + +use proc_def::*; + +use crate::controller_services::private_key_service::PGPPrivateKeyService; + +use minifi_native::macros::ComponentIdentifier; +use minifi_native::{ + FlowFileStreamTransform, GetControllerService, GetProperty, InputStream, Logger, MinifiError, + OutputStream, ProcessError, RouteErrorExt, Schedule, TransformStreamResult, +}; +use pgp::composed::{Message, TheRing}; + +#[derive(Debug, ComponentIdentifier)] +pub(crate) struct DecryptContentPGP { + symmetric_password: Option<pgp::types::Password>, +} + +impl Schedule for DecryptContentPGP { + fn schedule<P: GetProperty + GetControllerService, L>( + context: &P, + _logger: &L, + ) -> Result<Self, MinifiError> + where + Self: Sized, + L: Logger, + { + let symmetric_password = context.get_property(&SYMMETRIC_PASSWORD)?; + let private_key_service = context.get_controller_service(&PRIVATE_KEY_SERVICE)?; + if private_key_service.is_none() && symmetric_password.is_none() { + Err(MinifiError::validation( + "Either Symmetric Password or Private Key Service must be set", + )) + } else { + Ok(DecryptContentPGP { symmetric_password }) + } + } +} + +impl DecryptContentPGP { + fn decrypt_msg<'a>( + &'a self, + msg: Message<'a>, + private_key_service: Option<&'a PGPPrivateKeyService>, + ) -> pgp::errors::Result<Message<'a>> { + let mut ring = if let Some(pks) = private_key_service { + pks.get_the_ring() + } else { + TheRing::default() + }; + + ring.decrypt_options = ring.decrypt_options.enable_gnupg_aead(); + + if let Some(sym_passwd) = &self.symmetric_password { + ring.message_password.push(sym_passwd); + } + let (decrypted_msg, _ring_result) = msg.decrypt_the_ring(ring, false)?; + Ok(decrypted_msg) + } + + fn extract_attributes_from_decrypted_message( + decrypted_msg: &Message, + ) -> Vec<(&'static str, String)> { + let mut res = Vec::new(); + if let Some(literal_data_header) = decrypted_msg.literal_data_header() { + if let Ok(file_name) = str::from_utf8(literal_data_header.file_name()) { + res.push((LITERAL_DATA_FILENAME.name, file_name.to_string())); + } + // NiFi uses ms timestamp + res.push(( + LITERAL_DATA_MODIFIED.name, + (1000u64 * literal_data_header.created().as_secs() as u64).to_string(), + )); + } + res + } +} + +impl FlowFileStreamTransform for DecryptContentPGP { + fn transform<Ctx: GetProperty + GetControllerService, LoggerImpl: Logger>( + &self, + context: &Ctx, + input_stream: &mut dyn InputStream, + output_stream: &mut dyn OutputStream, + _logger: &LoggerImpl, + ) -> Result<TransformStreamResult, ProcessError> { + let private_key_service = context.get_controller_service(&PRIVATE_KEY_SERVICE)?; + + let msg = Message::from_reader(input_stream) + .map(|(msg, _header)| msg) + .route_err_to_failure()?; + + let mut decrypted_msg = self + .decrypt_msg(msg, private_key_service) + .route_err_to_failure()?; + + if decrypted_msg.is_compressed() { + decrypted_msg = decrypted_msg + .decompress() + .map_err(MinifiError::other) + .route_err_to_failure()? + }; + + let attributes_to_add = Self::extract_attributes_from_decrypted_message(&decrypted_msg); + let _written_bytes = + std::io::copy(&mut decrypted_msg.into_inner(), output_stream).route_err_to_failure()?; + + Ok(TransformStreamResult::new(&SUCCESS).with_attributes(attributes_to_add)) + } +} + +mod proc_def { + use super::*; + use crate::controller_services::private_key_service::PGPPrivateKeyService; + use crate::utils; + use minifi_native::{ + OutputAttribute, ProcessorDefinition, ProcessorInputRequirement, Property, + PropertyDefinition, Relationship, property_definitions, + }; + + pub(super) const LITERAL_DATA_FILENAME: OutputAttribute = OutputAttribute { + name: "pgp.literal.data.filename", + relationships: &["success"], + description: "Filename from decrypted Literal Data (Note that OpenPGP signatures do not include the formatting octet, the file name, and the date field of the Literal Data packet in a signature hash; therefore, those fields are not protected against tampering in a signed document. Therefore a lot of implementations omit these inherently malleable metadata)", Review Comment: https://github.com/apache/nifi-minifi-cpp/pull/2225/changes/dd9c84f9e0e120ee3430f61aeae6aebb7a0e26e4#diff-04a53c5b836714443ecb6da3dd630ee5c5ca30adff3012af7a13f5f71dcd7f19L174-L175 ########## minifi_rust/extensions/minifi_pgp/src/processors/decrypt_content.rs: ########## @@ -0,0 +1,399 @@ +// 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 +// +// https://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. + +use proc_def::*; + +use crate::controller_services::private_key_service::PGPPrivateKeyService; + +use minifi_native::macros::ComponentIdentifier; +use minifi_native::{ + FlowFileStreamTransform, GetControllerService, GetProperty, InputStream, Logger, MinifiError, + OutputStream, ProcessError, RouteErrorExt, Schedule, TransformStreamResult, +}; +use pgp::composed::{Message, TheRing}; + +#[derive(Debug, ComponentIdentifier)] +pub(crate) struct DecryptContentPGP { + symmetric_password: Option<pgp::types::Password>, +} + +impl Schedule for DecryptContentPGP { + fn schedule<P: GetProperty + GetControllerService, L>( + context: &P, + _logger: &L, + ) -> Result<Self, MinifiError> + where + Self: Sized, + L: Logger, + { + let symmetric_password = context.get_property(&SYMMETRIC_PASSWORD)?; + let private_key_service = context.get_controller_service(&PRIVATE_KEY_SERVICE)?; + if private_key_service.is_none() && symmetric_password.is_none() { + Err(MinifiError::validation( + "Either Symmetric Password or Private Key Service must be set", + )) + } else { + Ok(DecryptContentPGP { symmetric_password }) + } + } +} + +impl DecryptContentPGP { + fn decrypt_msg<'a>( + &'a self, + msg: Message<'a>, + private_key_service: Option<&'a PGPPrivateKeyService>, + ) -> pgp::errors::Result<Message<'a>> { + let mut ring = if let Some(pks) = private_key_service { + pks.get_the_ring() + } else { + TheRing::default() + }; + + ring.decrypt_options = ring.decrypt_options.enable_gnupg_aead(); + + if let Some(sym_passwd) = &self.symmetric_password { + ring.message_password.push(sym_passwd); + } + let (decrypted_msg, _ring_result) = msg.decrypt_the_ring(ring, false)?; + Ok(decrypted_msg) + } + + fn extract_attributes_from_decrypted_message( + decrypted_msg: &Message, + ) -> Vec<(&'static str, String)> { + let mut res = Vec::new(); + if let Some(literal_data_header) = decrypted_msg.literal_data_header() { + if let Ok(file_name) = str::from_utf8(literal_data_header.file_name()) { + res.push((LITERAL_DATA_FILENAME.name, file_name.to_string())); + } + // NiFi uses ms timestamp + res.push(( + LITERAL_DATA_MODIFIED.name, + (1000u64 * literal_data_header.created().as_secs() as u64).to_string(), + )); + } + res + } +} + +impl FlowFileStreamTransform for DecryptContentPGP { + fn transform<Ctx: GetProperty + GetControllerService, LoggerImpl: Logger>( + &self, + context: &Ctx, + input_stream: &mut dyn InputStream, + output_stream: &mut dyn OutputStream, + _logger: &LoggerImpl, + ) -> Result<TransformStreamResult, ProcessError> { + let private_key_service = context.get_controller_service(&PRIVATE_KEY_SERVICE)?; + + let msg = Message::from_reader(input_stream) + .map(|(msg, _header)| msg) + .route_err_to_failure()?; + + let mut decrypted_msg = self + .decrypt_msg(msg, private_key_service) + .route_err_to_failure()?; + + if decrypted_msg.is_compressed() { + decrypted_msg = decrypted_msg + .decompress() + .map_err(MinifiError::other) + .route_err_to_failure()? + }; + + let attributes_to_add = Self::extract_attributes_from_decrypted_message(&decrypted_msg); + let _written_bytes = + std::io::copy(&mut decrypted_msg.into_inner(), output_stream).route_err_to_failure()?; + + Ok(TransformStreamResult::new(&SUCCESS).with_attributes(attributes_to_add)) + } +} + +mod proc_def { + use super::*; + use crate::controller_services::private_key_service::PGPPrivateKeyService; + use crate::utils; + use minifi_native::{ + OutputAttribute, ProcessorDefinition, ProcessorInputRequirement, Property, + PropertyDefinition, Relationship, property_definitions, + }; + + pub(super) const LITERAL_DATA_FILENAME: OutputAttribute = OutputAttribute { + name: "pgp.literal.data.filename", + relationships: &["success"], + description: "Filename from decrypted Literal Data (Note that OpenPGP signatures do not include the formatting octet, the file name, and the date field of the Literal Data packet in a signature hash; therefore, those fields are not protected against tampering in a signed document. Therefore a lot of implementations omit these inherently malleable metadata)", + }; + + pub(super) const LITERAL_DATA_MODIFIED: OutputAttribute = OutputAttribute { + name: "pgp.literal.data.modified", + relationships: &["success"], + description: "Modified Date from decrypted Literal Data (Note that OpenPGP signatures do not include the formatting octet, the file name, and the date field of the Literal Data packet in a signature hash; therefore, those fields are not protected against tampering in a signed document. Therefore a lot of implementations omit these inherently malleable metadata)", Review Comment: https://github.com/apache/nifi-minifi-cpp/pull/2225/changes/dd9c84f9e0e120ee3430f61aeae6aebb7a0e26e4#diff-04a53c5b836714443ecb6da3dd630ee5c5ca30adff3012af7a13f5f71dcd7f19L174-L175 -- 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]
