martinzink commented on code in PR #2225:
URL: https://github.com/apache/nifi-minifi-cpp/pull/2225#discussion_r4091233682


##########
minifi_rust/extensions/minifi_pgp/src/processors/encrypt_content.rs:
##########
@@ -0,0 +1,392 @@
+// 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 minifi_native::{
+    FlowFileStreamTransform, GetAttribute, GetControllerService, GetId, 
GetProperty, InputStream,
+    Logger, MinifiError, OutputStream, ProcessError, RouteErrorExt, Schedule,
+    TransformStreamResult,
+};
+use pgp::composed::{ArmorOptions, MessageBuilder, SignedPublicKey};
+use pgp::types::{Password, StringToKey};
+
+use proc_def::*;
+
+use minifi_native::macros::{ComponentIdentifier, PropertyType};
+use strum_macros::{Display, EnumString, IntoStaticStr, VariantNames};
+
+#[derive(
+    Debug, Clone, Copy, PartialEq, Display, EnumString, VariantNames, 
IntoStaticStr, PropertyType,
+)]
+#[strum(serialize_all = "UPPERCASE", const_into_str)]
+enum FileEncoding {
+    Ascii,
+    Binary,
+}
+
+#[derive(Debug, ComponentIdentifier)]
+pub(crate) struct EncryptContentPGP {
+    file_encoding: FileEncoding,
+    symmetric_password: Option<Password>,
+}
+
+#[cfg(not(test))]
+fn string_to_key() -> StringToKey {
+    StringToKey::new_argon2(rand::thread_rng(), 3, 4, 16) // 64 MiB with 
rpgp's recommended parameter choice
+}
+
+#[cfg(test)]
+fn string_to_key() -> StringToKey {
+    StringToKey::new_argon2(rand::thread_rng(), 1, 1, 10) // fast for unit 
tests
+}
+
+impl EncryptContentPGP {
+    fn encrypt_bytes(
+        &self,
+        input_stream: &mut dyn InputStream,
+        output_stream: &mut dyn OutputStream,
+        pub_key: Option<&SignedPublicKey>,
+        file_name: String,
+    ) -> Result<(), MinifiError> {
+        if pub_key.is_none() && self.symmetric_password.is_none() {
+            return Err(MinifiError::custom(
+                "No password or public key to encrypt with",
+            ));
+        }
+
+        let mut builder = MessageBuilder::from_reader(file_name, 
input_stream).seipd_v1(
+            rand::thread_rng(),
+            pgp::crypto::sym::SymmetricKeyAlgorithm::AES256,
+        );
+
+        if let Some(pub_key) = pub_key {
+            builder
+                .encrypt_to_key(rand::thread_rng(), pub_key)
+                .map_err(MinifiError::other)?;
+        }
+
+        if let Some(password) = &self.symmetric_password {
+            builder
+                .encrypt_with_password(string_to_key(), password)
+                .map_err(MinifiError::other)?;
+        }
+
+        match self.file_encoding {
+            FileEncoding::Ascii => builder
+                .to_armored_writer(rand::thread_rng(), 
ArmorOptions::default(), output_stream)
+                .map_err(MinifiError::other),
+            FileEncoding::Binary => builder
+                .to_writer(rand::thread_rng(), output_stream)
+                .map_err(MinifiError::other),
+        }
+    }
+}
+
+impl Schedule for EncryptContentPGP {
+    fn schedule<P: GetProperty + GetControllerService, L: Logger>(
+        context: &P,
+        _logger: &L,
+    ) -> Result<Self, MinifiError>
+    where
+        Self: Sized,
+    {
+        let file_encoding = context.get_property(&FILE_ENCODING)?;
+        let symmetric_password = context.get_property(&SYMMETRIC_PASSPHRASE)?;
+
+        let public_key_service = 
context.get_controller_service(&PUBLIC_KEY_SERVICE)?;
+        let public_key_search = context.get_raw_property(&PUBLIC_KEY_SEARCH)?;

Review Comment:
   yeah i was under the impression that it doesnt eval EL, but it does so thats 
not gonna work here, and in that case there is no point of haing 
get_raw_property anymore, maybe we could add some support to the c api, 
`minifi_process_context_is_property_set` ?



-- 
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]

Reply via email to