This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch fix/CAMEL-28109 in repository https://gitbox.apache.org/repos/asf/camel.git
commit 2458840a047be1303c03ad5ec77ba835222a6a95 Author: Claus Ibsen <[email protected]> AuthorDate: Fri Dec 4 12:33:44 2026 +0100 CAMEL-28109: camel-snmp - SNMPv3 endpoints use their own USM, not the SNMP4J global one The SNMPv3 producers, the trap producer and the POLL consumer registered their USM in SNMP4J's SecurityModels.getInstance(), which holds one USM per security model. With several SNMPv3 endpoints in one application, the endpoint that started last decided the user of all of them, and stopping a producer removed the USM for the endpoints still running. - SnmpHelper.createMessageDispatcher builds a message dispatcher with only the message processing models of the snmpVersion, and MPv3 with the USM of the endpoint (moved from SnmpTrapConsumer). All endpoints use it, and Camel no longer registers anything in SecurityModels.getInstance(). - createUSM registers the default security protocols of SNMP4J, as new Snmp(dispatcher, transport) does not. - The POLL consumer closes its Snmp session when it stops. - authenticationProtocol accepts the SHA-2 protocols HMAC128SHA224, HMAC192SHA256, HMAC256SHA384 and HMAC384SHA512. - Upgrade guide entry in camel-4x-upgrade-guide-4_25.adoc. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Claus Ibsen <[email protected]> --- .../org/apache/camel/catalog/components/snmp.json | 2 +- .../apache/camel/catalog/docs/snmp-component.adoc | 7 +- .../org/apache/camel/component/snmp/snmp.json | 2 +- .../camel-snmp/src/main/docs/snmp-component.adoc | 7 +- .../camel/component/snmp/SnmpAuthProtocolType.java | 6 +- .../apache/camel/component/snmp/SnmpEndpoint.java | 10 +- .../apache/camel/component/snmp/SnmpHelper.java | 58 +++++++--- .../apache/camel/component/snmp/SnmpOIDPoller.java | 7 +- .../apache/camel/component/snmp/SnmpProducer.java | 19 +-- .../camel/component/snmp/SnmpTrapConsumer.java | 27 +---- .../camel/component/snmp/SnmpTrapProducer.java | 19 +-- .../apache/camel/component/snmp/ProducerTest.java | 35 ++++++ .../component/snmp/TwoSnmpV3EndpointsTest.java | 127 +++++++++++++++++++++ .../ROOT/pages/camel-4x-upgrade-guide-4_25.adoc | 12 ++ .../endpoint/dsl/SnmpEndpointBuilderFactory.java | 33 ++++-- 15 files changed, 283 insertions(+), 88 deletions(-) diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/snmp.json b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/snmp.json index 3cedefa2c18b..2059f3994020 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/snmp.json +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/components/snmp.json @@ -74,7 +74,7 @@ "timeUnit": { "index": 31, "kind": "parameter", "displayName": "Time Unit", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "enum", "javaType": "java.util.concurrent.TimeUnit", "enum": [ "NANOSECONDS", "MICROSECONDS", "MILLISECONDS", "SECONDS", "MINUTES", "HOURS", "DAYS" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "MILLISECONDS", "description": "Time unit for initialDelay and delay options." }, "useFixedDelay": { "index": 32, "kind": "parameter", "displayName": "Use Fixed Delay", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Controls if fixed delay or fixed rate is used. See ScheduledExecutorService in JDK for details." }, "authenticationPassphrase": { "index": 33, "kind": "parameter", "displayName": "Authentication Passphrase", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "security": "secret", "description": "The SNMPv3 authentication passphrase of the user, used with the authenticationProtocol. SNMP4J requires at least 8 bytes (RFC 3414 section 11.2) when an authenticationProtoco [...] - "authenticationProtocol": { "index": 34, "kind": "parameter", "displayName": "Authentication Protocol", "group": "security", "label": "security", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "MD5", "SHA1" ], "deprecated": false, "autowired": false, "secret": false, "description": "The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96) or SHA1 (HMAC-SHA-96). Without it, the user has no authentication. SNMP4J does not register these protocols [...] + "authenticationProtocol": { "index": 34, "kind": "parameter", "displayName": "Authentication Protocol", "group": "security", "label": "security", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "MD5", "SHA1", "HMAC128SHA224", "HMAC192SHA256", "HMAC256SHA384", "HMAC384SHA512" ], "deprecated": false, "autowired": false, "secret": false, "description": "The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96), SHA1 (HMAC-SHA-96), HMAC128SHA224 (HMAC [...] "privacyPassphrase": { "index": 35, "kind": "parameter", "displayName": "Privacy Passphrase", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "security": "secret", "description": "The SNMPv3 privacy passphrase of the user, used with the privacyProtocol. SNMP4J requires at least 8 bytes (RFC 3414 section 11.2) when a privacyProtocol is set, and otherwise fails with a [...] "privacyProtocol": { "index": 36, "kind": "parameter", "displayName": "Privacy Protocol", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "security": "secret", "description": "The SNMPv3 privacy (encryption) protocol of the user: DES, TRIDES (Triple DES), AES128, AES192 or AES256. Without it, the user supports only unencrypted messages." }, "securityLevel": { "index": 37, "kind": "parameter", "displayName": "Security Level", "group": "security", "label": "security", "required": false, "type": "enum", "javaType": "int", "enum": [ "1", "2", "3" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": 3, "description": "The SNMPv3 security level: 1 (noAuthNoPriv) means no authentication and no privacy, 2 (authNoPriv) authentication without privacy, and 3 (authPriv) authentication and privacy. The produc [...] diff --git a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/snmp-component.adoc b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/snmp-component.adoc index 45359d25e727..11af5d749fb0 100644 --- a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/snmp-component.adoc +++ b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/snmp-component.adoc @@ -138,9 +138,14 @@ can check the community in the `securityName` header. * With `snmpVersion` 3, it receives only the SNMPv3 traps of the user set in `securityName`, with at least the `securityLevel` of the endpoint (the default is 3, authentication and privacy). The traps are checked and decrypted with the `authenticationProtocol`, `authenticationPassphrase`, `privacyProtocol` and `privacyPassphrase` of the endpoint. -These protocols must be registered in the `org.snmp4j.security.SecurityProtocols` of SNMP4J, for example with +SNMP4J registers the SHA-2 authentication protocols (`HMAC128SHA224`, `HMAC192SHA256`, `HMAC256SHA384` and +`HMAC384SHA512`) and the privacy protocols by default. The `MD5` and `SHA1` authentication protocols must be registered +in the `org.snmp4j.security.SecurityProtocols` of SNMP4J, for example with `SecurityProtocols.getInstance().addAuthenticationProtocol(new AuthSHA())`. +Each SNMPv3 endpoint (producer, `POLL` consumer and `TRAP` consumer) has a USM of its own that holds only the user of +the endpoint, so several SNMPv3 endpoints with different users can be used side by side. + ------------------------------------------------------------------------------------------------------------------ snmp:0.0.0.0:162?protocol=udp&type=TRAP&snmpVersion=3&securityName=camel&securityLevel=3 &authenticationProtocol=SHA1&authenticationPassphrase=RAW(...)&privacyProtocol=AES128&privacyPassphrase=RAW(...) diff --git a/components/camel-snmp/src/generated/resources/META-INF/org/apache/camel/component/snmp/snmp.json b/components/camel-snmp/src/generated/resources/META-INF/org/apache/camel/component/snmp/snmp.json index 3cedefa2c18b..2059f3994020 100644 --- a/components/camel-snmp/src/generated/resources/META-INF/org/apache/camel/component/snmp/snmp.json +++ b/components/camel-snmp/src/generated/resources/META-INF/org/apache/camel/component/snmp/snmp.json @@ -74,7 +74,7 @@ "timeUnit": { "index": 31, "kind": "parameter", "displayName": "Time Unit", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "enum", "javaType": "java.util.concurrent.TimeUnit", "enum": [ "NANOSECONDS", "MICROSECONDS", "MILLISECONDS", "SECONDS", "MINUTES", "HOURS", "DAYS" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": "MILLISECONDS", "description": "Time unit for initialDelay and delay options." }, "useFixedDelay": { "index": 32, "kind": "parameter", "displayName": "Use Fixed Delay", "group": "scheduler", "label": "consumer,scheduler", "required": false, "type": "boolean", "javaType": "boolean", "deprecated": false, "autowired": false, "secret": false, "defaultValue": true, "description": "Controls if fixed delay or fixed rate is used. See ScheduledExecutorService in JDK for details." }, "authenticationPassphrase": { "index": 33, "kind": "parameter", "displayName": "Authentication Passphrase", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "security": "secret", "description": "The SNMPv3 authentication passphrase of the user, used with the authenticationProtocol. SNMP4J requires at least 8 bytes (RFC 3414 section 11.2) when an authenticationProtoco [...] - "authenticationProtocol": { "index": 34, "kind": "parameter", "displayName": "Authentication Protocol", "group": "security", "label": "security", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "MD5", "SHA1" ], "deprecated": false, "autowired": false, "secret": false, "description": "The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96) or SHA1 (HMAC-SHA-96). Without it, the user has no authentication. SNMP4J does not register these protocols [...] + "authenticationProtocol": { "index": 34, "kind": "parameter", "displayName": "Authentication Protocol", "group": "security", "label": "security", "required": false, "type": "enum", "javaType": "java.lang.String", "enum": [ "MD5", "SHA1", "HMAC128SHA224", "HMAC192SHA256", "HMAC256SHA384", "HMAC384SHA512" ], "deprecated": false, "autowired": false, "secret": false, "description": "The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96), SHA1 (HMAC-SHA-96), HMAC128SHA224 (HMAC [...] "privacyPassphrase": { "index": 35, "kind": "parameter", "displayName": "Privacy Passphrase", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "security": "secret", "description": "The SNMPv3 privacy passphrase of the user, used with the privacyProtocol. SNMP4J requires at least 8 bytes (RFC 3414 section 11.2) when a privacyProtocol is set, and otherwise fails with a [...] "privacyProtocol": { "index": 36, "kind": "parameter", "displayName": "Privacy Protocol", "group": "security", "label": "security", "required": false, "type": "string", "javaType": "java.lang.String", "deprecated": false, "autowired": false, "secret": true, "security": "secret", "description": "The SNMPv3 privacy (encryption) protocol of the user: DES, TRIDES (Triple DES), AES128, AES192 or AES256. Without it, the user supports only unencrypted messages." }, "securityLevel": { "index": 37, "kind": "parameter", "displayName": "Security Level", "group": "security", "label": "security", "required": false, "type": "enum", "javaType": "int", "enum": [ "1", "2", "3" ], "deprecated": false, "autowired": false, "secret": false, "defaultValue": 3, "description": "The SNMPv3 security level: 1 (noAuthNoPriv) means no authentication and no privacy, 2 (authNoPriv) authentication without privacy, and 3 (authPriv) authentication and privacy. The produc [...] diff --git a/components/camel-snmp/src/main/docs/snmp-component.adoc b/components/camel-snmp/src/main/docs/snmp-component.adoc index 45359d25e727..11af5d749fb0 100644 --- a/components/camel-snmp/src/main/docs/snmp-component.adoc +++ b/components/camel-snmp/src/main/docs/snmp-component.adoc @@ -138,9 +138,14 @@ can check the community in the `securityName` header. * With `snmpVersion` 3, it receives only the SNMPv3 traps of the user set in `securityName`, with at least the `securityLevel` of the endpoint (the default is 3, authentication and privacy). The traps are checked and decrypted with the `authenticationProtocol`, `authenticationPassphrase`, `privacyProtocol` and `privacyPassphrase` of the endpoint. -These protocols must be registered in the `org.snmp4j.security.SecurityProtocols` of SNMP4J, for example with +SNMP4J registers the SHA-2 authentication protocols (`HMAC128SHA224`, `HMAC192SHA256`, `HMAC256SHA384` and +`HMAC384SHA512`) and the privacy protocols by default. The `MD5` and `SHA1` authentication protocols must be registered +in the `org.snmp4j.security.SecurityProtocols` of SNMP4J, for example with `SecurityProtocols.getInstance().addAuthenticationProtocol(new AuthSHA())`. +Each SNMPv3 endpoint (producer, `POLL` consumer and `TRAP` consumer) has a USM of its own that holds only the user of +the endpoint, so several SNMPv3 endpoints with different users can be used side by side. + ------------------------------------------------------------------------------------------------------------------ snmp:0.0.0.0:162?protocol=udp&type=TRAP&snmpVersion=3&securityName=camel&securityLevel=3 &authenticationProtocol=SHA1&authenticationPassphrase=RAW(...)&privacyProtocol=AES128&privacyPassphrase=RAW(...) diff --git a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpAuthProtocolType.java b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpAuthProtocolType.java index 48aaed826e70..d1b0c2ca6ba6 100644 --- a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpAuthProtocolType.java +++ b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpAuthProtocolType.java @@ -18,5 +18,9 @@ package org.apache.camel.component.snmp; public enum SnmpAuthProtocolType { MD5, - SHA1 + SHA1, + HMAC128SHA224, + HMAC192SHA256, + HMAC256SHA384, + HMAC384SHA512 } diff --git a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpEndpoint.java b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpEndpoint.java index 62549c904642..a9d5ad1f4ed4 100644 --- a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpEndpoint.java +++ b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpEndpoint.java @@ -121,10 +121,12 @@ public class SnmpEndpoint extends DefaultPollingEndpoint implements EndpointServ description = "The SNMPv3 security name (the user of the USM) of the requests and traps. Required with" + " snmpVersion 3, where the TRAP consumer receives only the traps of this user.") private String securityName; - @UriParam(enums = "MD5,SHA1", label = "security", - description = "The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96) or SHA1 (HMAC-SHA-96)." - + " Without it, the user has no authentication. SNMP4J does not register these protocols by" - + " default: add them to the SecurityProtocols of SNMP4J, for example with" + @UriParam(enums = "MD5,SHA1,HMAC128SHA224,HMAC192SHA256,HMAC256SHA384,HMAC384SHA512", label = "security", + description = "The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96), SHA1 (HMAC-SHA-96)," + + " HMAC128SHA224 (HMAC-SHA-224), HMAC192SHA256 (HMAC-SHA-256), HMAC256SHA384 (HMAC-SHA-384)" + + " or HMAC384SHA512 (HMAC-SHA-512), see RFC 7860. Without it, the user has no authentication." + + " SNMP4J registers only the SHA-2 protocols by default: to use MD5 or SHA1, add them to the" + + " SecurityProtocols of SNMP4J, for example with" + " SecurityProtocols.getInstance().addAuthenticationProtocol(new AuthSHA()).") private String authenticationProtocol; @UriParam(label = "security", security = "secret", diff --git a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpHelper.java b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpHelper.java index 7521c611047e..545cca6fafb5 100644 --- a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpHelper.java +++ b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpHelper.java @@ -17,13 +17,20 @@ package org.apache.camel.component.snmp; import org.snmp4j.CommunityTarget; +import org.snmp4j.MessageDispatcherImpl; import org.snmp4j.PDU; import org.snmp4j.PDUv1; import org.snmp4j.ScopedPDU; import org.snmp4j.Target; import org.snmp4j.UserTarget; +import org.snmp4j.mp.MPv1; +import org.snmp4j.mp.MPv2c; import org.snmp4j.mp.MPv3; import org.snmp4j.mp.SnmpConstants; +import org.snmp4j.security.AuthHMAC128SHA224; +import org.snmp4j.security.AuthHMAC192SHA256; +import org.snmp4j.security.AuthHMAC256SHA384; +import org.snmp4j.security.AuthHMAC384SHA512; import org.snmp4j.security.AuthMD5; import org.snmp4j.security.AuthSHA; import org.snmp4j.security.Priv3DES; @@ -31,7 +38,6 @@ import org.snmp4j.security.PrivAES128; import org.snmp4j.security.PrivAES192; import org.snmp4j.security.PrivAES256; import org.snmp4j.security.PrivDES; -import org.snmp4j.security.SecurityModels; import org.snmp4j.security.SecurityProtocols; import org.snmp4j.security.USM; import org.snmp4j.security.UsmUser; @@ -69,27 +75,45 @@ final class SnmpHelper { }; } - static USM createAndSetUSM(SnmpEndpoint endpoint) { - return switch (endpoint.getSnmpVersion()) { - case SnmpConstants.version3 -> internalCreateAndSetUSM(endpoint); - default -> null; - }; - } - - private static USM internalCreateAndSetUSM(SnmpEndpoint endpoint) { - USM usm = createUSM(endpoint); - SecurityModels.getInstance().addSecurityModel(usm); - return usm; + /** + * Creates the message dispatcher of an SNMP session of the endpoint, with only the message processing models of the + * configured snmpVersion: SNMPv1 and SNMPv2c for the versions 0 and 1, and SNMPv3 with the given USM for the + * version 3. Each endpoint thereby uses its own USM, and not the one of the JVM-wide {@code SecurityModels} of + * SNMP4J. + * + * @param endpoint the endpoint + * @param usm the USM of the endpoint (see {@link #createUSM(SnmpEndpoint)}), used only with SNMPv3 + */ + static MessageDispatcherImpl createMessageDispatcher(SnmpEndpoint endpoint, USM usm) { + MessageDispatcherImpl dispatcher = new MessageDispatcherImpl(); + int version = endpoint.getSnmpVersion(); + if (version == SnmpConstants.version1 || version == SnmpConstants.version2c) { + dispatcher.addMessageProcessingModel(new MPv1()); + dispatcher.addMessageProcessingModel(new MPv2c()); + } else if (version == SnmpConstants.version3) { + dispatcher.addMessageProcessingModel(new MPv3(usm)); + } else { + throw new IllegalArgumentException("Unknown snmpVersion: " + version); + } + return dispatcher; } /** - * Creates a USM with the user of the endpoint, without registering it in the JVM-wide {@link SecurityModels}. + * Creates a USM with the user of an SNMPv3 endpoint, without registering it in the JVM-wide {@code SecurityModels} + * of SNMP4J. + * + * @return the USM, or {@code null} when the endpoint does not use SNMPv3 */ static USM createUSM(SnmpEndpoint endpoint) { + if (endpoint.getSnmpVersion() != SnmpConstants.version3) { + return null; + } if (endpoint.getSecurityName() == null) { throw new IllegalArgumentException("SecurityName is required for SNMP v3"); } + // an Snmp created with its own message dispatcher does not register the default security protocols of SNMP4J + SecurityProtocols.getInstance().addDefaultProtocols(); USM usm = new USM(SecurityProtocols.getInstance(), new OctetString(MPv3.createLocalEngineID()), 0); OID authProtocol = convertAuthenticationProtocol(endpoint.getAuthenticationProtocol()); @@ -160,6 +184,14 @@ final class SnmpHelper { return AuthMD5.ID; } else if ("SHA1".equals(authenticationProtocol)) { return AuthSHA.ID; + } else if ("HMAC128SHA224".equals(authenticationProtocol)) { + return AuthHMAC128SHA224.ID; + } else if ("HMAC192SHA256".equals(authenticationProtocol)) { + return AuthHMAC192SHA256.ID; + } else if ("HMAC256SHA384".equals(authenticationProtocol)) { + return AuthHMAC256SHA384.ID; + } else if ("HMAC384SHA512".equals(authenticationProtocol)) { + return AuthHMAC384SHA512.ID; } else { throw new IllegalArgumentException("Unknown authentication protocol: " + authenticationProtocol); } diff --git a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpOIDPoller.java b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpOIDPoller.java index 93cc3c4dbd17..c9109e8a3f0b 100644 --- a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpOIDPoller.java +++ b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpOIDPoller.java @@ -21,6 +21,7 @@ import java.util.List; import org.apache.camel.Exchange; import org.apache.camel.Processor; import org.apache.camel.support.ScheduledPollConsumer; +import org.apache.camel.util.IOHelper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.snmp4j.PDU; @@ -67,8 +68,8 @@ public class SnmpOIDPoller extends ScheduledPollConsumer { throw new IllegalArgumentException("Unknown protocol: " + endpoint.getProtocol()); } - this.snmp = new Snmp(this.transport); - SnmpHelper.createAndSetUSM(endpoint); + // NOTE: the object is going to be closed by stop() lifecycle. + this.snmp = new Snmp(SnmpHelper.createMessageDispatcher(endpoint, SnmpHelper.createUSM(endpoint)), this.transport); // NOSONAR this.target = SnmpHelper.createTarget(endpoint); // listen to the transport @@ -92,6 +93,8 @@ public class SnmpOIDPoller extends ScheduledPollConsumer { this.transport.close(); LOG.info("Stopped OID poller on {}", targetAddress); } + // closing the SNMP session also stops its timer + IOHelper.close(snmp); super.doStop(); } diff --git a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpProducer.java b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpProducer.java index 9696c73c5e12..4b13ff34ac36 100644 --- a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpProducer.java +++ b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpProducer.java @@ -31,11 +31,9 @@ import org.snmp4j.Target; import org.snmp4j.TransportMapping; import org.snmp4j.event.ResponseEvent; import org.snmp4j.mp.SnmpConstants; -import org.snmp4j.security.SecurityModels; import org.snmp4j.security.USM; import org.snmp4j.smi.Address; import org.snmp4j.smi.GenericAddress; -import org.snmp4j.smi.Integer32; import org.snmp4j.smi.OID; import org.snmp4j.smi.VariableBinding; import org.snmp4j.transport.DefaultTcpTransportMapping; @@ -68,7 +66,8 @@ public class SnmpProducer extends DefaultProducer { this.targetAddress = GenericAddress.parse(this.endpoint.getServerAddress()); LOG.debug("targetAddress: {}", targetAddress); - this.usm = SnmpHelper.createAndSetUSM(endpoint); + // the USM of the endpoint, used only by the SNMP sessions of this producer + this.usm = SnmpHelper.createUSM(endpoint); this.target = SnmpHelper.createTarget(endpoint); } @@ -103,15 +102,9 @@ public class SnmpProducer extends DefaultProducer { protected void doStop() throws Exception { super.doStop(); - try { - if (this.usm != null) { - SecurityModels.getInstance().removeSecurityModel(new Integer32(this.usm.getID())); - } - } finally { - this.targetAddress = null; - this.usm = null; - this.target = null; - } + this.targetAddress = null; + this.usm = null; + this.target = null; } @Override @@ -132,7 +125,7 @@ public class SnmpProducer extends DefaultProducer { throw new IllegalArgumentException("Unknown protocol: " + this.endpoint.getProtocol()); } - snmp = new Snmp(transport); + snmp = new Snmp(SnmpHelper.createMessageDispatcher(endpoint, usm), transport); LOG.debug("Snmp: i am sending"); diff --git a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpTrapConsumer.java b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpTrapConsumer.java index 4899cdedb3a6..20a9be9dd000 100644 --- a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpTrapConsumer.java +++ b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpTrapConsumer.java @@ -26,19 +26,14 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.snmp4j.CommandResponder; import org.snmp4j.CommandResponderEvent; -import org.snmp4j.MessageDispatcherImpl; import org.snmp4j.MessageException; import org.snmp4j.PDU; import org.snmp4j.Snmp; import org.snmp4j.TransportMapping; -import org.snmp4j.mp.MPv1; -import org.snmp4j.mp.MPv2c; -import org.snmp4j.mp.MPv3; import org.snmp4j.mp.MessageProcessingModel; import org.snmp4j.mp.SnmpConstants; import org.snmp4j.mp.StateReference; import org.snmp4j.mp.StatusInformation; -import org.snmp4j.security.USM; import org.snmp4j.smi.Address; import org.snmp4j.smi.GenericAddress; import org.snmp4j.smi.OctetString; @@ -81,7 +76,7 @@ public class SnmpTrapConsumer extends DefaultConsumer implements CommandResponde } // NOTE: the object is going to be closed by stop() lifecycle. - this.snmp = new Snmp(createMessageDispatcher(), transport); // NOSONAR + this.snmp = new Snmp(SnmpHelper.createMessageDispatcher(endpoint, SnmpHelper.createUSM(endpoint)), transport); // NOSONAR snmp.addCommandResponder(this); // listen to the transport @@ -94,26 +89,6 @@ public class SnmpTrapConsumer extends DefaultConsumer implements CommandResponde } } - /** - * Creates a message dispatcher with only the message processing models of the configured snmpVersion: SNMPv1 and - * SNMPv2c for the versions 0 and 1, and SNMPv3 with a USM of its own that holds only the user of the endpoint for - * the version 3. - */ - private MessageDispatcherImpl createMessageDispatcher() { - MessageDispatcherImpl dispatcher = new MessageDispatcherImpl(); - int version = endpoint.getSnmpVersion(); - if (version == SnmpConstants.version1 || version == SnmpConstants.version2c) { - dispatcher.addMessageProcessingModel(new MPv1()); - dispatcher.addMessageProcessingModel(new MPv2c()); - } else if (version == SnmpConstants.version3) { - USM usm = SnmpHelper.createUSM(endpoint); - dispatcher.addMessageProcessingModel(new MPv3(usm)); - } else { - throw new IllegalArgumentException("Unknown snmpVersion: " + version); - } - return dispatcher; - } - /** * Whether the event matches the configured snmpVersion and, for SNMPv3, the security name and the minimum security * level of the endpoint. diff --git a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpTrapProducer.java b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpTrapProducer.java index 3ec2b15a732c..77e83e3b6afa 100644 --- a/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpTrapProducer.java +++ b/components/camel-snmp/src/main/java/org/apache/camel/component/snmp/SnmpTrapProducer.java @@ -25,11 +25,9 @@ import org.snmp4j.Snmp; import org.snmp4j.Target; import org.snmp4j.TransportMapping; import org.snmp4j.mp.SnmpConstants; -import org.snmp4j.security.SecurityModels; import org.snmp4j.security.USM; import org.snmp4j.smi.Address; import org.snmp4j.smi.GenericAddress; -import org.snmp4j.smi.Integer32; import org.snmp4j.transport.DefaultTcpTransportMapping; import org.snmp4j.transport.DefaultUdpTransportMapping; @@ -58,7 +56,8 @@ public class SnmpTrapProducer extends DefaultProducer { this.targetAddress = GenericAddress.parse(this.endpoint.getServerAddress()); LOG.debug("targetAddress: {}", targetAddress); - this.usm = SnmpHelper.createAndSetUSM(endpoint); + // the USM of the endpoint, used only by the SNMP sessions of this producer + this.usm = SnmpHelper.createUSM(endpoint); this.target = SnmpHelper.createTarget(endpoint); } @@ -66,15 +65,9 @@ public class SnmpTrapProducer extends DefaultProducer { protected void doStop() throws Exception { super.doStop(); - try { - if (this.usm != null) { - SecurityModels.getInstance().removeSecurityModel(new Integer32(this.usm.getID())); - } - } finally { - this.targetAddress = null; - this.usm = null; - this.target = null; - } + this.targetAddress = null; + this.usm = null; + this.target = null; } @Override @@ -95,7 +88,7 @@ public class SnmpTrapProducer extends DefaultProducer { throw new IllegalArgumentException("Unknown protocol: " + this.endpoint.getProtocol()); } - snmp = new Snmp(transport); + snmp = new Snmp(SnmpHelper.createMessageDispatcher(endpoint, usm), transport); LOG.debug("SnmpTrap: getting pdu from body"); PDU trap = exchange.getIn().getMandatoryBody(PDU.class); diff --git a/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/ProducerTest.java b/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/ProducerTest.java index 609ca5ad652e..96a6ecdd69b7 100644 --- a/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/ProducerTest.java +++ b/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/ProducerTest.java @@ -19,14 +19,28 @@ package org.apache.camel.component.snmp; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.component.mock.MockEndpoint; import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.MethodSource; import org.snmp4j.mp.SnmpConstants; +import org.snmp4j.security.AuthHMAC192SHA256; +import org.snmp4j.security.UsmUser; +import org.snmp4j.smi.OctetString; public class ProducerTest extends SnmpRespondTestSupport { + private static final String SHA256_SECURITY_NAME = "sha256"; + private static final String SHA256_PASSPHRASE = "sha256pass"; + private String oids = "1.3.6.1.2.1.1.3.0,1.3.6.1.2.1.25.3.2.1.5.1,1.3.6.1.2.1.25.3.5.1.1.1,1.3.6.1.2.1.43.5.1.1.11.1"; + @BeforeAll + void addSha256User() { + snmpResponder.getUSM().addUser(new UsmUser( + new OctetString(SHA256_SECURITY_NAME), AuthHMAC192SHA256.ID, new OctetString(SHA256_PASSPHRASE), null, null)); + } + @ParameterizedTest @MethodSource("supportedVersions") public void testSnmpProducer(int version) throws Exception { @@ -44,6 +58,21 @@ public class ProducerTest extends SnmpRespondTestSupport { "Expected string matching '" + responseToMatch + "'. Got: " + receivedMessage); } + @Test + public void testSnmpProducerWithSha256Authentication() throws Exception { + MockEndpoint mock = getMockEndpoint("mock:resultSha256"); + mock.expectedMessageCount(1); + + template.sendBody("direct:inSha256", ""); + + mock.assertIsSatisfied(); + + SnmpMessage snmpMessage = mock.getReceivedExchanges().get(0).getIn().getBody(SnmpMessage.class); + String receivedMessage = snmpMessage.getSnmpMessage().getVariable(SnmpConstants.sysDescr).toString(); + Assertions.assertTrue(receivedMessage.matches("My Printer - response #\\d+, using version: 3"), + "Unexpected response: " + receivedMessage); + } + @Override protected RouteBuilder createRouteBuilder() { return new RouteBuilder() { @@ -62,6 +91,12 @@ public class ProducerTest extends SnmpRespondTestSupport { .to("snmp://" + getListeningAddress() + "?securityName=test&securityLevel=1&snmpVersion=3&oids=" + oids) .log("V0: ${body}") .to("mock:resultV3"); + + from("direct:inSha256") + .to("snmp://" + getListeningAddress() + "?securityName=" + SHA256_SECURITY_NAME + + "&securityLevel=2&snmpVersion=3&authenticationProtocol=HMAC192SHA256" + + "&authenticationPassphrase=" + SHA256_PASSPHRASE + "&oids=" + oids) + .to("mock:resultSha256"); } }; } diff --git a/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/TwoSnmpV3EndpointsTest.java b/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/TwoSnmpV3EndpointsTest.java new file mode 100644 index 000000000000..72f2e16becca --- /dev/null +++ b/components/camel-snmp/src/test/java/org/apache/camel/component/snmp/TwoSnmpV3EndpointsTest.java @@ -0,0 +1,127 @@ +/* + * 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.camel.component.snmp; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.snmp4j.mp.SnmpConstants; +import org.snmp4j.security.SecurityModel; +import org.snmp4j.security.SecurityModels; +import org.snmp4j.security.UsmUser; +import org.snmp4j.smi.Integer32; +import org.snmp4j.smi.OctetString; + +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Several SNMPv3 endpoints with different users in one CamelContext: each producer and poll consumer sends its requests + * with its own user, whatever the order in which the endpoints start and stop. + */ +public class TwoSnmpV3EndpointsTest extends SnmpRespondTestSupport { + + private static final String OTHER_SECURITY_NAME = "other"; + + @BeforeAll + void addOtherUser() { + // the responder knows both users, without authentication and privacy + snmpResponder.getUSM().addUser(new UsmUser(new OctetString(OTHER_SECURITY_NAME), null, null, null, null)); + } + + @BeforeEach + void resetMocks() { + // the CamelContext is shared by the tests of the class + MockEndpoint.resetMocks(context); + } + + @Test + public void eachEndpointUsesItsOwnUser() throws Exception { + MockEndpoint poll = getMockEndpoint("mock:poll"); + poll.expectedMinimumMessageCount(1); + MockEndpoint test = getMockEndpoint("mock:test"); + test.expectedMessageCount(1); + MockEndpoint other = getMockEndpoint("mock:other"); + other.expectedMessageCount(1); + + // the producer of the user "other" started last + template.sendBody("direct:test", ""); + template.sendBody("direct:other", ""); + + MockEndpoint.assertIsSatisfied(context); + assertSnmpV3Response(poll); + assertSnmpV3Response(test); + assertSnmpV3Response(other); + // the endpoints do not register their users in the JVM-wide SecurityModels of SNMP4J + assertNull(SecurityModels.getInstance().getSecurityModel(new Integer32(SecurityModel.SECURITY_MODEL_USM))); + } + + @Test + public void stoppingOneProducerKeepsTheOtherEndpointsWorking() throws Exception { + context.getRouteController().stopRoute(OTHER_SECURITY_NAME); + try { + MockEndpoint.resetMocks(context); + MockEndpoint test = getMockEndpoint("mock:test"); + test.expectedMessageCount(1); + // the poll consumer still receives responses + MockEndpoint poll = getMockEndpoint("mock:poll"); + poll.expectedMinimumMessageCount(1); + + template.sendBody("direct:test", ""); + + MockEndpoint.assertIsSatisfied(context); + assertSnmpV3Response(test); + assertSnmpV3Response(poll); + } finally { + context.getRouteController().startRoute(OTHER_SECURITY_NAME); + } + } + + private static void assertSnmpV3Response(MockEndpoint mock) { + SnmpMessage snmpMessage = mock.getReceivedExchanges().get(0).getIn().getBody(SnmpMessage.class); + String receivedMessage = snmpMessage.getSnmpMessage().getVariable(SnmpConstants.sysDescr).toString(); + assertTrue(receivedMessage.matches("My Printer - response #\\d+, using version: 3"), + "Unexpected response: " + receivedMessage); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + public void configure() { + from(String.format( + "snmp:%s?protocol=udp&type=POLL&snmpVersion=3&securityName=%s&securityLevel=1&oids=%s&delay=100", + getListeningAddress(), SECURITY_NAME, SnmpConstants.sysName)) + .routeId("poll") + .to("mock:poll"); + + from("direct:test") + .routeId(SECURITY_NAME) + .to(String.format("snmp:%s?snmpVersion=3&securityName=%s&securityLevel=1&oids=%s", + getListeningAddress(), SECURITY_NAME, SnmpConstants.sysName)) + .to("mock:test"); + + from("direct:other") + .routeId(OTHER_SECURITY_NAME) + .to(String.format("snmp:%s?snmpVersion=3&securityName=%s&securityLevel=1&oids=%s", + getListeningAddress(), OTHER_SECURITY_NAME, SnmpConstants.sysName)) + .to("mock:other"); + } + }; + } +} diff --git a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc index 5b6a1d50e997..384964dfb87f 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_25.adoc @@ -503,6 +503,18 @@ If you rely on `pkChunking*` options being silently ignored — for example if y intended jobs to run without chunking — remove those options or set `pkChunking=false` to restore the previous behaviour. +=== camel-snmp - SNMPv3 endpoints no longer use the SecurityModels of SNMP4J + +The SNMPv3 producers and the `POLL` consumer now use a USM of their own, which holds only the user of the endpoint, as +the `TRAP` consumer already did. They no longer register a USM in the JVM-wide `SecurityModels.getInstance()` of SNMP4J +when they start, and no longer remove it when they stop. A USM or users that the application adds to +`SecurityModels.getInstance()` are not used by the Camel endpoints: configure the user on the endpoint with +`securityName`, `authenticationProtocol`, `authenticationPassphrase`, `privacyProtocol` and `privacyPassphrase`. +Several SNMPv3 endpoints with different users can now be used in the same application. + +All SNMPv3 endpoints, the `TRAP` consumer included, now register the default security protocols of SNMP4J when they +start, so the SHA-2 authentication protocols and the privacy protocols are available without registering them. + === camel-spring-redis - SpringRedisIdempotentRepository.clear() deletes only the set of the repository `SpringRedisIdempotentRepository.clear()` (also the `clear` JMX operation) now deletes only the Redis set named by diff --git a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/SnmpEndpointBuilderFactory.java b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/SnmpEndpointBuilderFactory.java index 59371f7d517c..096a975a1c1e 100644 --- a/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/SnmpEndpointBuilderFactory.java +++ b/dsl/camel-endpointdsl/src/generated/java/org/apache/camel/builder/endpoint/dsl/SnmpEndpointBuilderFactory.java @@ -877,10 +877,13 @@ public interface SnmpEndpointBuilderFactory { return this; } /** - * The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96) or - * SHA1 (HMAC-SHA-96). Without it, the user has no authentication. - * SNMP4J does not register these protocols by default: add them to the - * SecurityProtocols of SNMP4J, for example with + * The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96), + * SHA1 (HMAC-SHA-96), HMAC128SHA224 (HMAC-SHA-224), HMAC192SHA256 + * (HMAC-SHA-256), HMAC256SHA384 (HMAC-SHA-384) or HMAC384SHA512 + * (HMAC-SHA-512), see RFC 7860. Without it, the user has no + * authentication. SNMP4J registers only the SHA-2 protocols by default: + * to use MD5 or SHA1, add them to the SecurityProtocols of SNMP4J, for + * example with * SecurityProtocols.getInstance().addAuthenticationProtocol(new * AuthSHA()). * @@ -1428,10 +1431,13 @@ public interface SnmpEndpointBuilderFactory { return this; } /** - * The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96) or - * SHA1 (HMAC-SHA-96). Without it, the user has no authentication. - * SNMP4J does not register these protocols by default: add them to the - * SecurityProtocols of SNMP4J, for example with + * The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96), + * SHA1 (HMAC-SHA-96), HMAC128SHA224 (HMAC-SHA-224), HMAC192SHA256 + * (HMAC-SHA-256), HMAC256SHA384 (HMAC-SHA-384) or HMAC384SHA512 + * (HMAC-SHA-512), see RFC 7860. Without it, the user has no + * authentication. SNMP4J registers only the SHA-2 protocols by default: + * to use MD5 or SHA1, add them to the SecurityProtocols of SNMP4J, for + * example with * SecurityProtocols.getInstance().addAuthenticationProtocol(new * AuthSHA()). * @@ -1872,10 +1878,13 @@ public interface SnmpEndpointBuilderFactory { return this; } /** - * The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96) or - * SHA1 (HMAC-SHA-96). Without it, the user has no authentication. - * SNMP4J does not register these protocols by default: add them to the - * SecurityProtocols of SNMP4J, for example with + * The SNMPv3 authentication protocol of the user: MD5 (HMAC-MD5-96), + * SHA1 (HMAC-SHA-96), HMAC128SHA224 (HMAC-SHA-224), HMAC192SHA256 + * (HMAC-SHA-256), HMAC256SHA384 (HMAC-SHA-384) or HMAC384SHA512 + * (HMAC-SHA-512), see RFC 7860. Without it, the user has no + * authentication. SNMP4J registers only the SHA-2 protocols by default: + * to use MD5 or SHA1, add them to the SecurityProtocols of SNMP4J, for + * example with * SecurityProtocols.getInstance().addAuthenticationProtocol(new * AuthSHA()). *
