nandorsoma commented on code in PR #6034: URL: https://github.com/apache/nifi/pull/6034#discussion_r881784199
########## nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/utils/UsmReader.java: ########## @@ -0,0 +1,145 @@ +/* + * 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.snmp.utils; + +import com.fasterxml.jackson.core.JsonParser; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.DeserializationContext; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.deser.std.StdDeserializer; +import com.fasterxml.jackson.databind.module.SimpleModule; +import org.apache.nifi.processor.exception.ProcessException; +import org.snmp4j.security.UsmUser; +import org.snmp4j.smi.OID; +import org.snmp4j.smi.OctetString; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; +import java.util.stream.Collectors; + +public interface UsmReader { + + List<UsmUser> readUsm(String usmUsers); + + static UsmReader jsonFileUsmReader() { + return usmUsersFilePath -> { + final List<UsmUser> userDetails; + try (Scanner scanner = new Scanner(new File(usmUsersFilePath))) { + final String content = scanner.useDelimiter("\\Z").next(); + ObjectMapper mapper = new ObjectMapper(); + SimpleModule module = new SimpleModule(); + module.addDeserializer(UsmUser.class, new UsmUserDeserializer()); + mapper.registerModule(module); + userDetails = mapper.readValue(content, new TypeReference<List<UsmUser>>() { + }); + } catch (FileNotFoundException e) { + throw new ProcessException("USM user file not found, please check the file path and file permissions.", e); + } catch (JsonProcessingException e) { + throw new ProcessException("Could not parse USM user file, please check the processor details for examples.", e); + } + return userDetails; + }; + } + + static UsmReader jsonUsmReader() { + return usmUsersJson -> { + try { + ObjectMapper mapper = new ObjectMapper(); + SimpleModule module = new SimpleModule(); + module.addDeserializer(UsmUser.class, new UsmUserDeserializer()); + mapper.registerModule(module); + return mapper.readValue(usmUsersJson, new TypeReference<List<UsmUser>>() { + }); + } catch (JsonProcessingException e) { + throw new ProcessException("Could not parse USM user file, please check the processor details for examples.", e); + } + }; + } + + static UsmReader securityNamesUsmReader() { + return usmSecurityNames -> Arrays.stream(usmSecurityNames.trim().split(",")) + .map(securityName -> new UsmUser( + new OctetString(securityName), null, null, null, null) + ) + .collect(Collectors.toList()); + } + + class UsmUserDeserializer extends StdDeserializer<UsmUser> { + + public UsmUserDeserializer() { + this(null); + } + + public UsmUserDeserializer(Class<?> vc) { + super(vc); + } + + @Override + public UsmUser deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { + JsonNode node = jp.getCodec().readTree(jp); Review Comment: As we agreed, this can be moved to validation, since we know this information during the processor configuration and expression language is not supported for these fields. ########## nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/processors/ListenTrapSNMP.java: ########## @@ -65,14 +75,45 @@ public class ListenTrapSNMP extends AbstractSessionFactoryProcessor { .addValidator(StandardValidators.PORT_VALIDATOR) .build(); - public static final PropertyDescriptor SNMP_USM_USERS_FILE_PATH = new PropertyDescriptor.Builder() + public static final PropertyDescriptor SNMP_USM_USER_SOURCE = new PropertyDescriptor.Builder() Review Comment: As we agreed, I'd only show this field when the SNMP Version is v3. ########## nifi-nar-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/processors/ListenTrapSNMP.java: ########## @@ -56,6 +61,11 @@ @RequiresInstanceClassLoading public class ListenTrapSNMP extends AbstractSessionFactoryProcessor { + public static final AllowableValue USM_JSON_FILE = new AllowableValue("usm-json-file", "Json File Path", "The path of the JSON file containing the USM users"); + public static final AllowableValue USM_JSON = new AllowableValue("usm-json", "Json File", "The JSON file containing the USM users"); Review Comment: As we agreed, I'd use JSON File Content displayName for this value. (probably I'd rename the variable name too and I'd add _PATH to the variable above for better clarity. -- 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]
