Search in sources :

Example 1 with LeaderConfigurationLoader

use of horse.wtf.nzyme.configuration.leader.LeaderConfigurationLoader in project nzyme by lennartkoopmann.

the class Main method main.

public static void main(String[] argv) {
    final CLIArguments cliArguments = new CLIArguments();
    // Parse CLI arguments.
    JCommander.newBuilder().addObject(cliArguments).build().parse(argv);
    // Override log level if requested.
    if (cliArguments.isDebugMode()) {
        Logging.setRootLoggerLevel(Level.DEBUG);
    }
    if (cliArguments.isTraceMode()) {
        Logging.setRootLoggerLevel(Level.TRACE);
    }
    // Parse configuration.
    BaseConfiguration baseConfiguration = null;
    try {
        baseConfiguration = new BaseConfigurationLoader(new File(cliArguments.getConfigFilePath())).get();
    } catch (InvalidConfigurationException | ConfigException e) {
        LOG.error("Invalid baseconfiguration. Please refer to the example configuration file or documentation.", e);
        System.exit(FAILURE);
    } catch (IncompleteConfigurationException e) {
        LOG.error("Incomplete base configuration. Please refer to the example configuration file or documentation.", e);
        System.exit(FAILURE);
    } catch (FileNotFoundException e) {
        LOG.error("Could not read configuration file.", e);
        System.exit(FAILURE);
    }
    switch(baseConfiguration.mode()) {
        case LEADER:
            LeaderConfiguration leaderConfiguration = null;
            try {
                leaderConfiguration = new LeaderConfigurationLoader(new File(cliArguments.getConfigFilePath()), false).get();
            } catch (InvalidConfigurationException | ConfigException e) {
                LOG.error("Invalid configuration. Please refer to the example configuration file or documentation.", e);
                System.exit(FAILURE);
            } catch (IncompleteConfigurationException e) {
                LOG.error("Incomplete configuration. Please refer to the example configuration file or documentation.", e);
                System.exit(FAILURE);
            } catch (FileNotFoundException e) {
                LOG.error("Could not read configuration file.", e);
                System.exit(FAILURE);
            }
            // Database.
            Database database = new Database(leaderConfiguration);
            try {
                database.initializeAndMigrate();
            } catch (LiquibaseException e) {
                LOG.fatal("Error during database initialization and migration.", e);
                System.exit(FAILURE);
            }
            NzymeLeader nzyme = new NzymeLeaderImpl(baseConfiguration, leaderConfiguration, database);
            try {
                nzyme.initialize();
            } catch (Exception e) {
                LOG.fatal("Could not initialize nzyme.", e);
                System.exit(FAILURE);
            }
            Runtime.getRuntime().addShutdownHook(new Thread(() -> {
                Thread.currentThread().setName("shutdown-hook");
                nzyme.shutdown();
            }));
            break;
        case TRACKER:
            TrackerConfiguration trackerConfiguration = null;
            try {
                trackerConfiguration = new TrackerConfigurationLoader(new File(cliArguments.getConfigFilePath())).get();
            } catch (InvalidConfigurationException | ConfigException e) {
                LOG.error("Invalid configuration. Please refer to the example configuration file or documentation.", e);
                System.exit(FAILURE);
            } catch (IncompleteConfigurationException e) {
                LOG.error("Incomplete configuration. Please refer to the example configuration file or documentation.", e);
                System.exit(FAILURE);
            } catch (FileNotFoundException e) {
                LOG.error("Could not read configuration file.", e);
                System.exit(FAILURE);
            }
            NzymeTracker tracker = new NzymeTrackerImpl(baseConfiguration, trackerConfiguration);
            try {
                tracker.initialize();
            } catch (Exception e) {
                LOG.fatal("Could not initialize nzyme.", e);
                System.exit(FAILURE);
            }
    }
    while (true) {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            break;
        /* nein */
        }
    }
}
Also used : BaseConfigurationLoader(horse.wtf.nzyme.configuration.base.BaseConfigurationLoader) CLIArguments(horse.wtf.nzyme.configuration.CLIArguments) FileNotFoundException(java.io.FileNotFoundException) ConfigException(com.typesafe.config.ConfigException) TrackerConfigurationLoader(horse.wtf.nzyme.configuration.tracker.TrackerConfigurationLoader) IncompleteConfigurationException(horse.wtf.nzyme.configuration.IncompleteConfigurationException) IncompleteConfigurationException(horse.wtf.nzyme.configuration.IncompleteConfigurationException) FileNotFoundException(java.io.FileNotFoundException) InvalidConfigurationException(horse.wtf.nzyme.configuration.InvalidConfigurationException) ConfigException(com.typesafe.config.ConfigException) LiquibaseException(liquibase.exception.LiquibaseException) TrackerConfiguration(horse.wtf.nzyme.configuration.tracker.TrackerConfiguration) InvalidConfigurationException(horse.wtf.nzyme.configuration.InvalidConfigurationException) LeaderConfiguration(horse.wtf.nzyme.configuration.leader.LeaderConfiguration) BaseConfiguration(horse.wtf.nzyme.configuration.base.BaseConfiguration) Database(horse.wtf.nzyme.database.Database) LiquibaseException(liquibase.exception.LiquibaseException) File(java.io.File) LeaderConfigurationLoader(horse.wtf.nzyme.configuration.leader.LeaderConfigurationLoader)

Example 2 with LeaderConfigurationLoader

use of horse.wtf.nzyme.configuration.leader.LeaderConfigurationLoader in project nzyme by lennartkoopmann.

the class NzymeLeaderConfigurationLoaderTest method testGetValidConfig.

@Test
public void testGetValidConfig() throws InvalidConfigurationException, IncompleteConfigurationException, FileNotFoundException {
    String configFile = "nzyme-test-complete-valid.conf.test";
    if (System.getProperty("os.name").startsWith("Windows")) {
        configFile = "nzyme-test-complete-valid-windows.conf.test";
        System.out.println("loading Windows nzyme configuration file");
    }
    LeaderConfiguration c = new LeaderConfigurationLoader(loadFromResourceFile(configFile), false).get();
    assertEquals(c.role(), Role.LEADER);
    // This one is different based on ENV vars
    assertFalse(c.databasePath().isEmpty());
    assertEquals(c.pythonScriptPrefix(), "nzyme_");
    assertEquals(c.alertingTrainingPeriodSeconds(), 300);
    assertTrue(c.fetchOuis());
    assertTrue(c.versionchecksEnabled());
    assertEquals(c.restListenUri(), URI.create("http://127.0.0.1:22900/"));
    assertEquals(c.dot11Monitors(), new ArrayList<Dot11MonitorDefinition>() {

        {
            add(Dot11MonitorDefinition.create("wlx00c0ca8fd89a", ImmutableList.of(1, 2, 3, 4, 5, 6), "sudo /sbin/iwconfig {interface} channel {channel}", 1, false, 60));
            add(Dot11MonitorDefinition.create("wlx00c0ca971216", ImmutableList.of(7, 8, 9, 10, 11), "sudo /sbin/iwconfig {interface} channel {channel}", 3, false, 60));
        }
    });
    assertEquals(c.dot11Networks(), new ArrayList<Dot11NetworkDefinition>() {

        {
            add(Dot11NetworkDefinition.create("United_Wi-Fi", new ArrayList<Dot11BSSIDDefinition>() {

                {
                    add(Dot11BSSIDDefinition.create("06:0d:2d:c9:36:23", ImmutableList.of("c9ed4adc12dc3e17208446b6a10070b70a73b9ce3a99215e05426faea6de91c7"), null));
                    add(Dot11BSSIDDefinition.create("24:a4:3c:7d:01:cc", ImmutableList.of("def456"), null));
                }
            }, new ArrayList<Integer>() {

                {
                    add(1);
                    add(3);
                    add(6);
                    add(11);
                }
            }, new ArrayList<String>() {

                {
                    add("NONE");
                }
            }, 40));
            add(Dot11NetworkDefinition.create("WTF", new ArrayList<Dot11BSSIDDefinition>() {

                {
                    add(Dot11BSSIDDefinition.create("00:c0:ca:95:68:3b", ImmutableList.of("dfac3abce0c722f9609343f7dfa208afa51a1c7decbd2eb6f96c78051f0a594b"), null));
                }
            }, new ArrayList<Integer>() {

                {
                    add(1);
                    add(11);
                }
            }, new ArrayList<String>() {

                {
                    add("WPA1-EAM-PSK-CCMP");
                    add("WPA2-EAM-PSK-CCMP");
                }
            }, 40));
        }
    });
    assertEquals(c.dot11Alerts(), new ArrayList<Alert.TYPE_WIDE>() {

        {
            add(Alert.TYPE_WIDE.UNEXPECTED_BSSID);
            add(Alert.TYPE_WIDE.UNEXPECTED_SSID);
            add(Alert.TYPE_WIDE.UNEXPECTED_CHANNEL);
            add(Alert.TYPE_WIDE.CRYPTO_CHANGE);
            add(Alert.TYPE_WIDE.DEAUTH_FLOOD);
        }
    });
    assertEquals(c.uplinks().size(), 4);
    assertEquals(c.uplinks().get(0).type(), "syslog_udp_rfc5424");
    assertEquals(c.uplinks().get(0).configuration().getString(ConfigurationKeys.HOST), "localhost");
    assertEquals(c.uplinks().get(0).configuration().getInt(ConfigurationKeys.PORT), 5516);
    assertEquals(c.uplinks().get(1).type(), "graylog");
    assertEquals(c.uplinks().get(1).configuration().getString(ConfigurationKeys.HOST), "example.org");
    assertEquals(c.uplinks().get(1).configuration().getInt(ConfigurationKeys.PORT), 5517);
    assertEquals(c.uplinks().get(2).type(), "graylog");
    assertEquals(c.uplinks().get(2).configuration().getString(ConfigurationKeys.HOST), "10.243.255.10");
    assertEquals(c.uplinks().get(2).configuration().getInt(ConfigurationKeys.PORT), 33001);
    assertEquals(c.uplinks().get(3).type(), "graylog");
    assertEquals(c.uplinks().get(3).configuration().getString(ConfigurationKeys.HOST), "127.0.0.1");
    assertEquals(c.uplinks().get(3).configuration().getInt(ConfigurationKeys.PORT), 9001);
    assertEquals(c.remoteInputAddress(), new InetSocketAddress("0.0.0.0", 9002));
    assertEquals(c.reporting().email().transportStrategy(), TransportStrategy.SMTP_TLS);
    assertEquals(c.reporting().email().host(), "smtp.example.org");
    assertEquals(c.reporting().email().port(), 587);
    assertEquals(c.reporting().email().username(), "your_username");
    assertEquals(c.reporting().email().password(), "your_password");
    assertEquals(c.reporting().email().from(), new Recipient("nzyme", "nzyme@example.org", Message.RecipientType.TO));
    assertEquals(c.reporting().email().subjectPrefix(), "[NZYME]");
    assertEquals(c.deauth().globalThreshold(), 10);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Recipient(org.simplejavamail.api.email.Recipient) LeaderConfiguration(horse.wtf.nzyme.configuration.leader.LeaderConfiguration) LeaderConfigurationLoader(horse.wtf.nzyme.configuration.leader.LeaderConfigurationLoader) Test(org.testng.annotations.Test) ResourcesAccessingTest(horse.wtf.nzyme.ResourcesAccessingTest)

Aggregations

LeaderConfiguration (horse.wtf.nzyme.configuration.leader.LeaderConfiguration)2 LeaderConfigurationLoader (horse.wtf.nzyme.configuration.leader.LeaderConfigurationLoader)2 ConfigException (com.typesafe.config.ConfigException)1 ResourcesAccessingTest (horse.wtf.nzyme.ResourcesAccessingTest)1 CLIArguments (horse.wtf.nzyme.configuration.CLIArguments)1 IncompleteConfigurationException (horse.wtf.nzyme.configuration.IncompleteConfigurationException)1 InvalidConfigurationException (horse.wtf.nzyme.configuration.InvalidConfigurationException)1 BaseConfiguration (horse.wtf.nzyme.configuration.base.BaseConfiguration)1 BaseConfigurationLoader (horse.wtf.nzyme.configuration.base.BaseConfigurationLoader)1 TrackerConfiguration (horse.wtf.nzyme.configuration.tracker.TrackerConfiguration)1 TrackerConfigurationLoader (horse.wtf.nzyme.configuration.tracker.TrackerConfigurationLoader)1 Database (horse.wtf.nzyme.database.Database)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 InetSocketAddress (java.net.InetSocketAddress)1 LiquibaseException (liquibase.exception.LiquibaseException)1 Recipient (org.simplejavamail.api.email.Recipient)1 Test (org.testng.annotations.Test)1