use of org.opennms.netmgt.snmp.SnmpV3User in project opennms by OpenNMS.
the class Snmp4jTrapReceiverIT method testTrapReceiverWithOpenNMSAuthPriv.
@Test
public void testTrapReceiverWithOpenNMSAuthPriv() {
final Snmp4JStrategy strategy = new Snmp4JStrategy();
assertEquals(0, m_trapCount);
LOG.debug("ONMS: Register for Traps");
final TestTrapListener trapListener = new TestTrapListener();
SnmpV3User user = new SnmpV3User("opennmsUser", "MD5", "0p3nNMSv3", "DES", "0p3nNMSv3");
try {
strategy.registerForTraps(trapListener, getAgentAddress(), 9162, Collections.singletonList(user));
sendTraps(strategy, SnmpConfiguration.AUTH_PRIV);
await().atMost(5, SECONDS).until(() -> m_trapCount, equalTo(2));
} catch (final IOException e) {
LOG.debug("Failed to register for traps.", e);
} catch (final Exception e) {
LOG.debug("Failed to send traps.", e);
} finally {
LOG.debug("ONMS: Unregister for Traps");
try {
strategy.unregisterForTraps(trapListener, 9162);
} catch (final IOException e) {
LOG.debug("Failed to unregister for traps.", e);
}
}
LOG.debug("ONMS: Checking Trap status");
assertFalse(trapListener.hasError());
assertEquals(2, trapListener.getReceivedTrapCount());
strategy.clearUsers();
}
use of org.opennms.netmgt.snmp.SnmpV3User in project opennms by OpenNMS.
the class TrapdConfigBean method toSnmpV3User.
private static SnmpV3User toSnmpV3User(Snmpv3User snmpv3User) {
SnmpV3User snmpV3User = new SnmpV3User();
snmpV3User.setAuthPassPhrase(snmpv3User.getAuthPassphrase());
snmpV3User.setAuthProtocol(snmpv3User.getAuthProtocol());
snmpV3User.setEngineId(snmpv3User.getEngineId());
snmpV3User.setPrivPassPhrase(snmpv3User.getPrivacyPassphrase());
snmpV3User.setPrivProtocol(snmpv3User.getPrivacyProtocol());
snmpV3User.setSecurityName(snmpv3User.getSecurityName());
return snmpV3User;
}
use of org.opennms.netmgt.snmp.SnmpV3User in project opennms by OpenNMS.
the class Snmp4JStrategy method registerForTraps.
@Override
public void registerForTraps(final TrapNotificationListener listener, InetAddress address, int snmpTrapPort, List<SnmpV3User> snmpUsers) throws IOException {
final RegistrationInfo info = new RegistrationInfo(listener, address, snmpTrapPort);
final Snmp4JTrapNotifier trapNotifier = new Snmp4JTrapNotifier(listener);
info.setHandler(trapNotifier);
final UdpAddress udpAddress;
if (address == null) {
udpAddress = new UdpAddress(snmpTrapPort);
} else {
udpAddress = new UdpAddress(address, snmpTrapPort);
}
// Set socket option SO_REUSEADDR so that we can bind to the port even if it
// has recently been closed by passing 'true' as the second argument here.
final DefaultUdpTransportMapping transport = new DefaultUdpTransportMapping(udpAddress, true);
// Increase the receive buffer for the socket
LOG.debug("Attempting to set receive buffer size to {}", Integer.MAX_VALUE);
transport.setReceiveBufferSize(Integer.MAX_VALUE);
LOG.debug("Actual receive buffer size is {}", transport.getReceiveBufferSize());
info.setTransportMapping(transport);
Snmp snmp = new Snmp(transport);
snmp.addCommandResponder(trapNotifier);
if (snmpUsers != null) {
for (SnmpV3User user : snmpUsers) {
SnmpAgentConfig config = new SnmpAgentConfig();
config.setVersion(SnmpConfiguration.VERSION3);
config.setSecurityName(user.getSecurityName());
config.setAuthProtocol(user.getAuthProtocol());
config.setAuthPassPhrase(user.getAuthPassPhrase());
config.setPrivProtocol(user.getPrivProtocol());
config.setPrivPassPhrase(user.getPrivPassPhrase());
Snmp4JAgentConfig agentConfig = new Snmp4JAgentConfig(config);
UsmUser usmUser = new UsmUser(agentConfig.getSecurityName(), agentConfig.getAuthProtocol(), agentConfig.getAuthPassPhrase(), agentConfig.getPrivProtocol(), agentConfig.getPrivPassPhrase());
/* This doesn't work as expected. Basically SNMP4J is ignoring the engineId
if (user.getEngineId() == null) {
snmp.getUSM().addUser(agentConfig.getSecurityName(), usmUser);
} else {
snmp.getUSM().addUser(agentConfig.getSecurityName(), new OctetString(user.getEngineId()), usmUser);
}
*/
snmp.getUSM().addUser(agentConfig.getSecurityName(), usmUser);
}
}
info.setSession(snmp);
s_registrations.put(listener, info);
snmp.listen();
}
use of org.opennms.netmgt.snmp.SnmpV3User in project opennms by OpenNMS.
the class Snmp4jTrapReceiverIT method testTrapReceiverWithOpenNMSNoAuthNoPriv.
@Test
public void testTrapReceiverWithOpenNMSNoAuthNoPriv() {
final Snmp4JStrategy strategy = new Snmp4JStrategy();
assertEquals(0, m_trapCount);
LOG.debug("ONMS: Register for Traps");
final TestTrapListener trapListener = new TestTrapListener();
//SnmpV3User user = new SnmpV3User("opennmsUser", "MD5", "0p3nNMSv3", "DES", "0p3nNMSv3");
SnmpV3User user = new SnmpV3User("noAuthUser", null, null, null, null);
try {
long start = System.currentTimeMillis();
strategy.registerForTraps(trapListener, getAgentAddress(), 9162, Collections.singletonList(user));
sendTraps(strategy, SnmpConfiguration.NOAUTH_NOPRIV);
await().atMost(5, SECONDS).until(() -> m_trapCount, equalTo(2));
} catch (final IOException e) {
LOG.debug("Failed to register for traps.", e);
} catch (final Exception e) {
LOG.debug("Failed to send traps.", e);
} finally {
LOG.debug("ONMS: Unregister for Traps");
try {
strategy.unregisterForTraps(trapListener, 9162);
} catch (final IOException e) {
LOG.debug("Failed to unregister for traps.", e);
}
}
LOG.debug("ONMS: Checking Trap status");
assertFalse(trapListener.hasError());
assertEquals(2, trapListener.getReceivedTrapCount());
strategy.clearUsers();
}
use of org.opennms.netmgt.snmp.SnmpV3User in project opennms by OpenNMS.
the class AbstractTrapReceiverTest method setUp.
/**
* Sets up the test (initialize a trap listener).
*
* @throws Exception the exception
*/
@Before
public void setUp() throws Exception {
MockLogAppender.setupLogging();
resetTrapsReceived();
System.setProperty("opennms.home", "src/test/resources");
SnmpPeerFactory.init();
Assert.assertEquals("Snmp4JStrategy", SnmpUtils.getStrategy().getClass().getSimpleName());
SnmpAgentConfig config = SnmpPeerFactory.getInstance().getAgentConfig(TRAP_DESTINATION);
SnmpV3User user = new SnmpV3User(config.getSecurityName(), config.getAuthProtocol(), config.getAuthPassPhrase(), config.getPrivProtocol(), config.getPrivPassPhrase());
SnmpUtils.registerForTraps(this, TRAP_DESTINATION, TRAP_PORT, Collections.singletonList(user));
LOG.info("Registered Trap Listener for {} on port {}", TRAP_DESTINATION, TRAP_PORT);
}
Aggregations