turcsanyip commented on code in PR #9824:
URL: https://github.com/apache/nifi/pull/9824#discussion_r2022773338
##########
nifi-extension-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/operations/SNMPTrapReceiverHandler.java:
##########
@@ -95,10 +101,17 @@ public void close() {
}
private void addUsmUsers() {
- if (configuration.getVersion() == SnmpConstants.version3) {
- USM usm = new USM(SecurityProtocols.getInstance(), new
OctetString(MPv3.createLocalEngineID()), 0);
- SecurityModels.getInstance().addSecurityModel(usm);
- usmUsers.forEach(user -> snmpManager.getUSM().addUser(user));
+ USM usm = new USM(SecurityProtocols.getInstance(), new
OctetString(MPv3.createLocalEngineID()), 0);
+ SecurityModels.getInstance().addSecurityModel(usm);
+ usmUsers.forEach(user -> snmpManager.getUSM().addUser(user));
+ }
+
+ private void removeMessageProcessingModel(final int modelId) {
Review Comment:
Unused method. Seems to be a leftover from a previous commit in the PR.
##########
nifi-extension-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/processors/properties/PrivacyProtocol.java:
##########
@@ -0,0 +1,77 @@
+/*
+ * 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.processors.properties;
+
+import org.apache.nifi.components.DescribedValue;
+import org.snmp4j.security.PrivAES128;
+import org.snmp4j.security.PrivAES192;
+import org.snmp4j.security.PrivAES256;
+import org.snmp4j.security.PrivDES;
+import org.snmp4j.smi.OID;
+
+import java.util.Arrays;
+
+public enum PrivacyProtocol implements DescribedValue {
+ DES("DES", "DES",
+ "Data Encryption Standard (DES) is an older symmetric-key
algorithm used for encrypting digital data. DES is considered insecure due" +
+ " to its short 56-bit key length, which is vulnerable to
brute-force attacks. It is now generally replaced by stronger encryption
protocols.",
+ PrivDES.ID),
+ AES128("AES128", "AES128",
+ "Advanced Encryption Standard (AES) with a 128-bit key length,
offering a balance between speed and security. AES128 is widely considered" +
+ " to be secure and is commonly used in various
cryptographic applications.",
+ PrivAES128.ID),
+ AES192("AES192", "AES192",
+ "Advanced Encryption Standard (AES) with a 192-bit key length,
providing a higher level of security than AES128, suitable for applications" +
+ " that require stronger encryption.",
+ PrivAES192.ID),
+ AES256("AES256", "AES256",
+ "Advanced Encryption Standard (AES) with a 256-bit key length,
offering the highest level of encryption strength. AES256 is the" +
+ " recommended choice for high-security applications and is
considered highly resistant to brute-force attacks.",
+ PrivAES256.ID);
+
+ private final String value;
+ private final String displayName;
+ private final String description;
+ private final OID oid;
+
+ PrivacyProtocol(final String value, final String displayName, final String
description, final OID oid) {
+ this.value = value;
+ this.displayName = displayName;
+ this.description = description;
+ this.oid = oid;
+ }
+
+ public String getValue() {
+ return value;
+ }
+
+ public String getDisplayName() {
+ return displayName;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+
+ public OID getOid() {
+ return oid;
+ }
+
+ public static boolean isValid(String protocol) {
Review Comment:
Unused. Please remove it.
##########
nifi-extension-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/utils/UsmUserDeserializer.java:
##########
@@ -34,49 +37,48 @@ public UsmUserDeserializer() {
@Override
public UsmUser deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
- JsonNode node = jp.getCodec().readTree(jp);
- String securityName = node.get("securityName").asText();
+ final JsonNode node = jp.getCodec().readTree(jp);
- OID authProtocol = null;
- final JsonNode authProtocolNode = node.get("authProtocol");
- if (authProtocolNode != null) {
- authProtocol = SNMPUtils.getAuth(authProtocolNode.asText());
- }
+ final String securityName = node.get("securityName").asText();
+ final OID authProtocol = getAuthProtocolOid(node);
+ final OctetString authPassphrase = getOctetString(node,
"authPassphrase");
- OctetString authPassphrase = null;
- final JsonNode authPassphraseNode = node.get("authPassphrase");
- if (authPassphraseNode != null) {
- authPassphrase = new OctetString(authPassphraseNode.asText());
- }
+ validatePassphrase(authProtocol, authPassphrase, "Authentication");
- if (authProtocol != null && authPassphrase == null) {
- throw new IllegalArgumentException("Authentication passphrase must
be set and at least 8 bytes long if" +
- "authentication protocol is specified.");
- }
+ final OID privProtocol = getPrivProtocolOid(node);
+ final OctetString privPassphrase = getOctetString(node,
"privPassphrase");
- OID privProtocol = null;
- final JsonNode privProtocolNode = node.get("privProtocol");
- if (privProtocolNode != null) {
- privProtocol = SNMPUtils.getPriv(privProtocolNode.asText());
- }
+ validatePassphrase(privProtocol, privPassphrase, "Privacy");
- OctetString privPassphrase = null;
- final JsonNode privPassphraseNode = node.get("privPassphrase");
- if (privPassphraseNode != null) {
- privPassphrase = new OctetString(privPassphraseNode.asText());
- }
+ return new UsmUser(new OctetString(securityName), authProtocol,
authPassphrase, privProtocol, privPassphrase);
+ }
- if (privProtocol != null && privPassphrase == null) {
- throw new IllegalArgumentException("Privacy passphrase must be set
and at least 8 bytes long if" +
- "authentication protocol is specified.");
- }
+ private OID getAuthProtocolOid(final JsonNode node) {
+ return Optional.ofNullable(node.get("authProtocol"))
+ .map(JsonNode::asText)
+ .map(AuthenticationProtocol::valueOf)
+ .map(AuthenticationProtocol::getOid)
+ .orElseThrow(() -> new IllegalArgumentException("Invalid
protocol: " + node.get("authProtocol")));
+ }
Review Comment:
This requires `authProtocol` to be present in the USM JSON even if
`noAuthNoPriv` is used.
The old implementation allowed the following USM JSON which now leads to
error:
```
[
{
"securityName": "user1"
}
]
```
##########
nifi-extension-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/utils/UsmUserDeserializer.java:
##########
@@ -34,49 +37,48 @@ public UsmUserDeserializer() {
@Override
public UsmUser deserialize(JsonParser jp, DeserializationContext ctxt)
throws IOException {
- JsonNode node = jp.getCodec().readTree(jp);
- String securityName = node.get("securityName").asText();
+ final JsonNode node = jp.getCodec().readTree(jp);
- OID authProtocol = null;
- final JsonNode authProtocolNode = node.get("authProtocol");
- if (authProtocolNode != null) {
- authProtocol = SNMPUtils.getAuth(authProtocolNode.asText());
- }
+ final String securityName = node.get("securityName").asText();
+ final OID authProtocol = getAuthProtocolOid(node);
+ final OctetString authPassphrase = getOctetString(node,
"authPassphrase");
- OctetString authPassphrase = null;
- final JsonNode authPassphraseNode = node.get("authPassphrase");
- if (authPassphraseNode != null) {
- authPassphrase = new OctetString(authPassphraseNode.asText());
- }
+ validatePassphrase(authProtocol, authPassphrase, "Authentication");
- if (authProtocol != null && authPassphrase == null) {
- throw new IllegalArgumentException("Authentication passphrase must
be set and at least 8 bytes long if" +
- "authentication protocol is specified.");
- }
+ final OID privProtocol = getPrivProtocolOid(node);
+ final OctetString privPassphrase = getOctetString(node,
"privPassphrase");
- OID privProtocol = null;
- final JsonNode privProtocolNode = node.get("privProtocol");
- if (privProtocolNode != null) {
- privProtocol = SNMPUtils.getPriv(privProtocolNode.asText());
- }
+ validatePassphrase(privProtocol, privPassphrase, "Privacy");
- OctetString privPassphrase = null;
- final JsonNode privPassphraseNode = node.get("privPassphrase");
- if (privPassphraseNode != null) {
- privPassphrase = new OctetString(privPassphraseNode.asText());
- }
+ return new UsmUser(new OctetString(securityName), authProtocol,
authPassphrase, privProtocol, privPassphrase);
+ }
- if (privProtocol != null && privPassphrase == null) {
- throw new IllegalArgumentException("Privacy passphrase must be set
and at least 8 bytes long if" +
- "authentication protocol is specified.");
- }
+ private OID getAuthProtocolOid(final JsonNode node) {
+ return Optional.ofNullable(node.get("authProtocol"))
+ .map(JsonNode::asText)
+ .map(AuthenticationProtocol::valueOf)
+ .map(AuthenticationProtocol::getOid)
+ .orElseThrow(() -> new IllegalArgumentException("Invalid
protocol: " + node.get("authProtocol")));
+ }
+
+ private OID getPrivProtocolOid(final JsonNode node) {
+ return Optional.ofNullable(node.get("privProtocol"))
+ .map(JsonNode::asText)
+ .map(PrivacyProtocol::valueOf)
+ .map(PrivacyProtocol::getOid)
+ .orElseThrow(() -> new IllegalArgumentException("Invalid
protocol: " + node.get("privProtocol")));
+ }
Review Comment:
Similar to `authProtocol`, `privProtocol` is now mandatory even when
`noAuthNoPriv` or `authNoPriv` is used.
The following USM JSONs should be valid for `noAuthNoPriv` and `authNoPriv`,
respectively:
```
[
{
"securityName": "user1"
}
]
```
```
[
{
"securityName": "user1",
"authProtocol": "HMAC384SHA512",
"authPassphrase": "authPassphrase1"
}
]
```
##########
nifi-extension-bundles/nifi-snmp-bundle/nifi-snmp-processors/src/main/java/org/apache/nifi/snmp/processors/properties/AuthenticationProtocol.java:
##########
@@ -0,0 +1,76 @@
+/*
+ * 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.processors.properties;
+
+import org.apache.nifi.components.DescribedValue;
+import org.snmp4j.security.AuthHMAC128SHA224;
+import org.snmp4j.security.AuthHMAC192SHA256;
+import org.snmp4j.security.AuthHMAC256SHA384;
+import org.snmp4j.security.AuthHMAC384SHA512;
+import org.snmp4j.smi.OID;
+
+import java.util.Arrays;
+
+public enum AuthenticationProtocol implements DescribedValue {
+ HMAC128SHA224("HMAC128SHA224", "SHA224",
+ "HMAC with SHA224, a variant of SHA-2, used for ensuring data
integrity and authenticity. It combines the HMAC construction with the SHA224
hash function.",
+ AuthHMAC128SHA224.ID),
+ HMAC192SHA256("HMAC192SHA256", "SHA256",
+ "HMAC with SHA256, a widely used secure hash function in the SHA-2
family, providing strong data integrity and authenticity guarantees.",
+ AuthHMAC192SHA256.ID),
+ HMAC256SHA384("HMAC256SHA384", "SHA384",
+ "HMAC with SHA384, a stronger variant of SHA-2 providing a 384-bit
hash for increased security in data integrity and authenticity.",
+ AuthHMAC256SHA384.ID),
+ HMAC384SHA512("HMAC384SHA512", "SHA512",
+ "HMAC with SHA512, using the SHA-2 family with a 512-bit hash,
providing the highest level of security for data integrity and authenticity.",
+ AuthHMAC384SHA512.ID);
+
+ private final String value;
+ private final String displayName;
+ private final String description;
+ private final OID oid;
+
+ AuthenticationProtocol(final String value, final String displayName, final
String description, final OID oid) {
+ this.value = value;
+ this.displayName = displayName;
+ this.description = description;
+ this.oid = oid;
+ }
+
+ @Override
+ public String getValue() {
+ return value;
+ }
+
+ @Override
+ public String getDisplayName() {
+ return displayName;
+ }
+
+ @Override
+ public String getDescription() {
+ return description;
+ }
+
+ public OID getOid() {
+ return oid;
+ }
+
+ public static boolean isValid(String protocol) {
Review Comment:
Unused. Please remove it.
--
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]