Skip to content

Commit

Permalink
Pushing current state. All sorts of work.
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartkoopmann committed Apr 18, 2022
1 parent 511bad5 commit 0322426
Show file tree
Hide file tree
Showing 54 changed files with 1,089 additions and 135 deletions.
13 changes: 11 additions & 2 deletions src/main/java/horse/wtf/nzyme/MockNzyme.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import horse.wtf.nzyme.bandits.trackers.TrackerManager;
import horse.wtf.nzyme.configuration.IncompleteConfigurationException;
import horse.wtf.nzyme.configuration.InvalidConfigurationException;
import horse.wtf.nzyme.configuration.db.BaseConfigurationService;
import horse.wtf.nzyme.configuration.leader.LeaderConfiguration;
import horse.wtf.nzyme.configuration.leader.LeaderConfigurationLoader;
import horse.wtf.nzyme.database.Database;
Expand All @@ -50,7 +51,6 @@
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import liquibase.exception.LiquibaseException;
import org.joda.time.DateTime;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -59,7 +59,7 @@
import java.util.Collections;
import java.util.List;

public class MockNzyme implements NzymeLeader {
public class MockNzyme implements NzymeLeader {

private File loadFromResourceFile(String name) {
URL resource = getClass().getClassLoader().getResource(name);
Expand Down Expand Up @@ -91,6 +91,7 @@ private File loadFromResourceFile(String name) {
private final Anonymizer anonymizer;
private final Sentry sentry;
private final EventService eventService;
private final BaseConfigurationService configurationService;

public MockNzyme() {
this(0);
Expand Down Expand Up @@ -128,6 +129,9 @@ public MockNzyme(int sentryInterval) {

this.database.useHandle(handle -> handle.execute("TRUNCATE sentry_ssids"));

this.configurationService = new BaseConfigurationService(this);
this.configurationService.initialize();

this.metricRegistry = new MetricRegistry();
this.registry = new Registry();
this.systemStatus = new SystemStatus();
Expand Down Expand Up @@ -214,6 +218,11 @@ public LeaderConfiguration getConfiguration() {
return configuration;
}

@Override
public BaseConfigurationService getConfigurationService() {
return configurationService;
}

@Override
public MetricRegistry getMetrics() {
return metricRegistry;
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/horse/wtf/nzyme/NzymeLeader.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import horse.wtf.nzyme.bandits.engine.ContactManager;
import horse.wtf.nzyme.bandits.trackers.GroundStation;
import horse.wtf.nzyme.bandits.trackers.TrackerManager;
import horse.wtf.nzyme.configuration.db.BaseConfigurationService;
import horse.wtf.nzyme.configuration.leader.LeaderConfiguration;
import horse.wtf.nzyme.database.Database;
import horse.wtf.nzyme.dot11.anonymization.Anonymizer;
Expand Down Expand Up @@ -55,7 +56,9 @@ public interface NzymeLeader extends RemoteConnector {

void registerUplink(Uplink uplink);

LeaderConfiguration getConfiguration();
LeaderConfiguration getConfiguration(); // TODO remove after switch to DB config

BaseConfigurationService getConfigurationService();

MetricRegistry getMetrics();

Expand Down
20 changes: 17 additions & 3 deletions src/main/java/horse/wtf/nzyme/NzymeLeaderImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import horse.wtf.nzyme.bandits.trackers.TrackerManager;
import horse.wtf.nzyme.configuration.*;
import horse.wtf.nzyme.configuration.base.BaseConfiguration;
import horse.wtf.nzyme.configuration.db.BaseConfigurationService;
import horse.wtf.nzyme.configuration.leader.LeaderConfiguration;
import horse.wtf.nzyme.database.Database;
import horse.wtf.nzyme.dot11.Dot11MetaInformation;
Expand Down Expand Up @@ -69,14 +70,16 @@
import horse.wtf.nzyme.remote.forwarders.Forwarder;
import horse.wtf.nzyme.remote.forwarders.ForwarderFactory;
import horse.wtf.nzyme.remote.inputs.RemoteFrameInput;
import horse.wtf.nzyme.rest.authentication.RESTAuthenticationFilter;
import horse.wtf.nzyme.rest.authentication.TapAuthenticationFilter;
import horse.wtf.nzyme.rest.resources.taps.StatusResource;
import horse.wtf.nzyme.rest.resources.taps.TablesResource;
import horse.wtf.nzyme.rest.resources.taps.TapsResource;
import horse.wtf.nzyme.scheduler.SchedulingService;
import horse.wtf.nzyme.rest.CORSFilter;
import horse.wtf.nzyme.rest.NzymeLeaderInjectionBinder;
import horse.wtf.nzyme.rest.NzymeExceptionMapper;
import horse.wtf.nzyme.rest.ObjectMapperProvider;
import horse.wtf.nzyme.rest.authentication.AuthenticationFilter;
import horse.wtf.nzyme.rest.resources.*;
import horse.wtf.nzyme.rest.resources.assets.WebInterfaceAssetsResource;
import horse.wtf.nzyme.rest.resources.authentication.AuthenticationResource;
Expand Down Expand Up @@ -107,7 +110,6 @@
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Consumer;
import java.util.logging.Level;

public class NzymeLeaderImpl implements NzymeLeader {
Expand All @@ -120,6 +122,7 @@ public class NzymeLeaderImpl implements NzymeLeader {

private final LeaderConfiguration configuration;
private final Database database;
private final BaseConfigurationService configurationService;
private final ExecutorService probeExecutor;
private final MetricRegistry metrics;
private final Registry registry;
Expand Down Expand Up @@ -163,6 +166,7 @@ public NzymeLeaderImpl(BaseConfiguration baseConfiguration, LeaderConfiguration
this.database = database;
this.uplinks = Lists.newArrayList();
this.forwarders = Lists.newArrayList();
this.configurationService = new BaseConfigurationService(this);

this.frameProcessor = new FrameProcessor();

Expand Down Expand Up @@ -235,6 +239,9 @@ public void initialize() {

eventService.recordEvent(new StartupEvent());

LOG.info("Reading configuration from database.");
this.configurationService.initialize();

LOG.info("Active alerts: {}", configuration.dot11Alerts());

// Initial OUI fetch. Not in periodical because this needs to be blocking.
Expand Down Expand Up @@ -307,7 +314,8 @@ public void initialize() {
// Spin up REST API and web interface.
java.util.logging.Logger.getLogger("org.glassfish.grizzly").setLevel(Level.SEVERE);
ResourceConfig resourceConfig = new ResourceConfig();
resourceConfig.register(new AuthenticationFilter(this));
resourceConfig.register(new RESTAuthenticationFilter(this));
resourceConfig.register(new TapAuthenticationFilter(this));
resourceConfig.register(new CORSFilter());
resourceConfig.register(new NzymeLeaderInjectionBinder(this));
resourceConfig.register(new ObjectMapperProvider());
Expand All @@ -329,6 +337,7 @@ public void initialize() {
resourceConfig.register(ReportsResource.class);
resourceConfig.register(StatusResource.class);
resourceConfig.register(TablesResource.class);
resourceConfig.register(TapsResource.class);

// Enable GZIP.
resourceConfig.registerClasses(EncodingFilter.class, GZipEncoder.class, DeflateEncoder.class);
Expand Down Expand Up @@ -627,6 +636,11 @@ public LeaderConfiguration getConfiguration() {
return configuration;
}

@Override
public BaseConfigurationService getConfigurationService() {
return configurationService;
}

@Override
public MetricRegistry getMetrics() {
return metrics;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import horse.wtf.nzyme.NzymeTracker;
import horse.wtf.nzyme.bandits.trackers.hid.webhid.WebHID;
import horse.wtf.nzyme.bandits.trackers.hid.webhid.rest.responses.StateResponse;
import horse.wtf.nzyme.rest.authentication.Secured;
import horse.wtf.nzyme.rest.authentication.RESTSecured;
import org.joda.time.DateTime;

import javax.inject.Inject;
Expand All @@ -31,7 +31,7 @@
import javax.ws.rs.core.Response;

@Path("/api/state")
@Secured
@RESTSecured
@Produces(MediaType.APPLICATION_JSON)
public class TrackerWebHIDResource {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package horse.wtf.nzyme.configuration.db;

import com.google.auto.value.AutoValue;
import org.joda.time.DateTime;

@AutoValue
public abstract class BaseConfiguration {

public abstract String tapSecret();
public abstract DateTime updatedAt();

public static BaseConfiguration create(String tapSecret, DateTime updatedAt) {
return builder()
.tapSecret(tapSecret)
.updatedAt(updatedAt)
.build();
}

public static Builder builder() {
return new AutoValue_BaseConfiguration.Builder();
}

@AutoValue.Builder
public abstract static class Builder {
public abstract Builder tapSecret(String tapSecret);

public abstract Builder updatedAt(DateTime updatedAt);

public abstract BaseConfiguration build();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package horse.wtf.nzyme.configuration.db;

import org.jdbi.v3.core.mapper.RowMapper;
import org.jdbi.v3.core.statement.StatementContext;
import org.joda.time.DateTime;

import java.sql.ResultSet;
import java.sql.SQLException;

public class BaseConfigurationMapper implements RowMapper<BaseConfiguration> {

@Override
public BaseConfiguration map(ResultSet rs, StatementContext ctx) throws SQLException {
return BaseConfiguration.create(
rs.getString("tap_secret"),
new DateTime(rs.getTimestamp("updated_at"))
);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package horse.wtf.nzyme.configuration.db;

import horse.wtf.nzyme.NzymeLeader;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.joda.time.DateTime;

import java.util.concurrent.atomic.AtomicReference;

public class BaseConfigurationService {

private static final Logger LOG = LogManager.getLogger(BaseConfigurationService.class);

private final NzymeLeader nzyme;
private final AtomicReference<BaseConfiguration> cache;

private boolean initialized = false;

public BaseConfigurationService(NzymeLeader nzyme) {
this.nzyme = nzyme;

this.cache = new AtomicReference<>(null);
}

public void initialize() {
// There should always only be one row.
long count = nzyme.getDatabase().withHandle(handle ->
handle.createQuery("SELECT COUNT(*) FROM base_configuration")
.mapTo(Long.class)
.first()
);

if (count == 0) {
// Create initial row.
LOG.info("Creating initial default base configuration.");

nzyme.getDatabase().useHandle(handle ->
handle.createUpdate("INSERT INTO base_configuration(tap_secret, updated_at) " +
"VALUES(:tap_secret, :timestamp)")
.bind("tap_secret", generateTapSecret())
.bind("timestamp", DateTime.now())
.execute()
);
}

if (count > 1) {
LOG.warn("More than one base configuration detected. This should never happen and can cause issues.");
}

invalidateAndUpdateCache();

initialized = true;
}

public BaseConfiguration getConfiguration() {
return cache.get();
}

public void setTapSecret(String secret) {
nzyme.getDatabase().useHandle(handle -> {
handle.createUpdate("UPDATE base_configuration SET " +
"tap_secret = :tap_secret, " +
"updated_at = (current_timestamp at time zone 'UTC')")
.bind("tap_secret", secret)
.execute();
});

invalidateAndUpdateCache();
}

public boolean isInitialized() {
return initialized;
}

public String generateTapSecret() {
return RandomStringUtils.random(64, true, true);
}

private void invalidateAndUpdateCache() {
BaseConfiguration config = nzyme.getDatabase().withHandle(handle ->
handle.createQuery("SELECT tap_secret, updated_at FROM base_configuration LIMIT 1")
.mapTo(BaseConfiguration.class)
.one()
);

if (config == null) {
LOG.fatal("!!!!!!!! No base configuration found. This is an unrecoverable error. !!!!!!!!");
}

this.cache.set(config);
}

}
6 changes: 5 additions & 1 deletion src/main/java/horse/wtf/nzyme/database/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import horse.wtf.nzyme.alerts.service.AlertDatabaseEntryMapper;
import horse.wtf.nzyme.bandits.database.*;
import horse.wtf.nzyme.configuration.db.BaseConfigurationMapper;
import horse.wtf.nzyme.configuration.leader.LeaderConfiguration;
import horse.wtf.nzyme.dot11.deauth.db.DeauthenticationMonitorRecordingMapper;
import horse.wtf.nzyme.dot11.networks.beaconrate.BeaconRateMapper;
Expand All @@ -11,6 +12,7 @@
import horse.wtf.nzyme.measurements.mappers.MeasurementMapper;
import horse.wtf.nzyme.reporting.db.ExecutionLogEntryMapper;
import horse.wtf.nzyme.reporting.db.ScheduledReportEntryMapper;
import horse.wtf.nzyme.taps.db.TapMapper;
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
Expand Down Expand Up @@ -71,7 +73,9 @@ public void initializeAndMigrate() throws LiquibaseException {
.registerRowMapper(new ExecutionLogEntryMapper())
.registerRowMapper(new ContactRecordMapper())
.registerRowMapper(new ContactRecordValueAggregationMapper())
.registerRowMapper(new ContactRecorderHistogramEntryMapper());
.registerRowMapper(new ContactRecorderHistogramEntryMapper())
.registerRowMapper(new TapMapper())
.registerRowMapper(new BaseConfigurationMapper());

// Run migrations against underlying JDBC connection.
JdbcConnection connection = new JdbcConnection(jdbi.open().getConnection());
Expand Down
Loading

0 comments on commit 0322426

Please sign in to comment.