Skip to content

Add support for SNMPv3 context engine ID and context name to snmptrap #76

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 4.1.0
- Add support for SNMPv3 `context engine ID` and `context name` to the `snmptrap` input[xxx]()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- Add support for SNMPv3 `context engine ID` and `context name` to the `snmptrap` input[xxx]()
- Add support for SNMPv3 `context engine ID` and `context name` to the `snmptrap` input [#76](https://github.com/logstash-plugins/logstash-integration-snmp/pull/76)


## 4.0.7
- FIX: The `snmptrap` input now correctly enforces the user security level set by `security_level` config, and drops received events that do not match the configured value [#75](https://github.com/logstash-plugins/logstash-integration-snmp/pull/75)

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0.7
4.1.0
2 changes: 2 additions & 0 deletions docs/input-snmptrap.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ The value is stored in the `@metadata` where it can be used by other plugins in
|ECS disabled, v1, v8 |Availability|Description
|[@metadata][input][snmptrap][pdu][agent_addr]|`SNMPv1`|Network address of the object generating the trap
|[@metadata][input][snmptrap][pdu][community]|`SNMPv1` `SNMPv2c`|SNMP community
|[@metadata][input][snmptrap][pdu][context_engine_id]|`SNMPv3`|SNMP context engine id
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
|[@metadata][input][snmptrap][pdu][context_engine_id]|`SNMPv3`|SNMP context engine id
|[@metadata][input][snmptrap][pdu][context_engine_id]|`SNMPv3`|SNMP context engine ID

|[@metadata][input][snmptrap][pdu][context_name]|`SNMPv3`|SNMP context name
|[@metadata][input][snmptrap][pdu][enterprise]|`SNMPv1`|Type of object generating the trap
|[@metadata][input][snmptrap][pdu][error_index]|`SNMPv2c` `SNMPv3`|Provides additional information by identifying
which variable binding in the list caused the error
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/org/logstash/snmp/SnmpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,6 @@ public <A extends Address> void processPdu(final CommandResponderEvent<A> event)
if (!validateTrapMessage(version, securityName, event.getSecurityLevel(), allowedCommunities)) {
return;
}

final Map<String, Object> trapEvent = createTrapEvent(version, securityName, event.getPDU());
final Map<String, Object> formattedVarBindings = new HashMap<>(event.getPDU().getVariableBindings().size());
for (VariableBinding binding : event.getPDU().getVariableBindings()) {
Expand Down Expand Up @@ -306,6 +305,10 @@ private Map<String, Object> createTrapEvent(int version, final String securityNa
trapEvent.put("error_status", pdu.getErrorStatus());
trapEvent.put("error_status_text", pdu.getErrorStatusText());
trapEvent.put("error_index", pdu.getErrorIndex());
if (pdu instanceof ScopedPDU) {
trapEvent.put("context_engine_id", ((ScopedPDU)pdu).getContextEngineID());
trapEvent.put("context_name", ((ScopedPDU)pdu).getContextName());
Comment on lines +308 to +310
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (pdu instanceof ScopedPDU) {
trapEvent.put("context_engine_id", ((ScopedPDU)pdu).getContextEngineID());
trapEvent.put("context_name", ((ScopedPDU)pdu).getContextName());
if (pdu instanceof ScopedPDU) {
final ScopedPDU scopedPDU = (ScopedPDU) pdu;
trapEvent.put("context_engine_id", String.valueOf(scopedPDU.getContextEngineID()));
trapEvent.put("context_name", scopedPDU.getContextName());

Both fields are OctetString and need to be parse to string before setting them as a Logstash event field, otherwise a Java::OrgLogstash::MissingConverterException is raised.

}
} else if (pdu instanceof PDUv1) {
final PDUv1 pdUv1 = (PDUv1) pdu;
trapEvent.put("enterprise", String.valueOf(pdUv1.getEnterprise()));
Expand Down
6 changes: 5 additions & 1 deletion src/test/java/org/logstash/snmp/SnmpClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1083,10 +1083,12 @@ void trapShouldProperlyCreateV2cTrapMessage() throws IOException {

@Test
void trapShouldProperlyCreateV3TrapMessage() throws IOException {
final PDU scopedPDU = new PDU(PDU.TRAP, List.of(
final ScopedPDU scopedPDU = new ScopedPDU();
scopedPDU.setVariableBindings(List.of(
new VariableBinding(new OID("1.1"), new OctetString("foo")),
new VariableBinding(new OID("1.2"), new OctetString("bar"))
));
scopedPDU.setType(ScopedPDU.TRAP);
scopedPDU.setRequestID(new Integer32(123));

when(mibManager.map(any(OID.class)))
Expand All @@ -1105,6 +1107,8 @@ void trapShouldProperlyCreateV3TrapMessage() throws IOException {
assertEquals(scopedPDU.getErrorStatus(), trapEvent.remove("error_status"));
assertEquals(scopedPDU.getErrorStatusText(), trapEvent.remove("error_status_text"));
assertEquals(scopedPDU.getErrorIndex(), trapEvent.remove("error_index"));
assertEquals(scopedPDU.getContextEngineID(), trapEvent.remove("context_engine_id"));
assertEquals(scopedPDU.getContextName(), trapEvent.remove("context_name"));

@SuppressWarnings("unchecked") final Map<String, Object> variableBindings = (Map<String, Object>) trapEvent.remove("variable_bindings");
scopedPDU.getVariableBindings().forEach(binding -> assertEquals(
Expand Down