Skip to content

Commit

Permalink
Connect status reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
lennartkoopmann committed Jul 7, 2024
1 parent ba76aa3 commit dca27c1
Show file tree
Hide file tree
Showing 21 changed files with 704 additions and 4 deletions.
6 changes: 6 additions & 0 deletions src/main/java/app/nzyme/core/MockNzyme.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package app.nzyme.core;

import app.nzyme.core.configuration.base.BaseConfiguration;
import app.nzyme.core.connect.ConnectService;
import app.nzyme.core.context.ContextService;
import app.nzyme.core.detection.alerts.DetectionAlertService;
import app.nzyme.core.distributed.ClusterManager;
Expand Down Expand Up @@ -319,6 +320,11 @@ public DetectionAlertService getDetectionAlertService() {
return null;
}

@Override
public ConnectService getConnect() {
return null;
}

@Override
public Version getVersion() {
return version;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/app/nzyme/core/NzymeNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package app.nzyme.core;

import app.nzyme.core.configuration.base.BaseConfiguration;
import app.nzyme.core.connect.ConnectService;
import app.nzyme.core.context.ContextService;
import app.nzyme.core.detection.alerts.DetectionAlertService;
import app.nzyme.core.distributed.ClusterManager;
Expand Down Expand Up @@ -95,6 +96,8 @@ public interface NzymeNode extends PluginEntryPoint, DatabaseProvider, NodeIdent

DetectionAlertService getDetectionAlertService();

ConnectService getConnect();

Version getVersion();

NzymeHttpServer getHttpServer();
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/app/nzyme/core/NzymeNodeImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package app.nzyme.core;

import app.nzyme.core.cache.CacheManager;
import app.nzyme.core.connect.ConnectService;
import app.nzyme.core.context.ContextService;
import app.nzyme.core.detection.alerts.DetectionAlertService;
import app.nzyme.core.distributed.ClusterManager;
Expand All @@ -31,6 +32,7 @@
import app.nzyme.core.events.EventEngineImpl;
import app.nzyme.core.integrations.geoip.GeoIpService;
import app.nzyme.core.monitoring.health.HealthMonitor;
import app.nzyme.core.periodicals.connect.ConnectStatusReporter;
import app.nzyme.core.periodicals.distributed.NodeUpdater;
import app.nzyme.core.registry.RegistryChangeMonitorImpl;
import app.nzyme.core.rest.server.NzymeHttpServer;
Expand Down Expand Up @@ -61,6 +63,7 @@
import app.nzyme.core.util.MetricNames;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.checkerframework.checker.units.qual.C;
import org.jetbrains.annotations.Nullable;

import java.io.File;
Expand Down Expand Up @@ -114,6 +117,7 @@ public class NzymeNodeImpl implements NzymeNode {
private final DetectionAlertService detectionAlertService;
private final EventEngine eventEngine;

private final ConnectService connect;
private final HealthMonitor healthMonitor;

private List<String> plugins;
Expand Down Expand Up @@ -166,6 +170,7 @@ public NzymeNodeImpl(BaseConfiguration baseConfiguration, NodeConfiguration conf
this.crypto = new Crypto(this);
this.objectMapper = new ObjectMapper();

this.connect = new ConnectService(this);
this.healthMonitor = new HealthMonitor(this);

// Register JVM metrics.
Expand Down Expand Up @@ -226,10 +231,11 @@ public void initialize() {
// Periodicals. (TODO: Replace with scheduler service)
PeriodicalManager periodicalManager = new PeriodicalManager();
periodicalManager.scheduleAtFixedRate(new NodeUpdater(this), 0, 5, TimeUnit.SECONDS);
periodicalManager.scheduleAtFixedRate(new ConnectStatusReporter(this), 0, 1, TimeUnit.MINUTES);
periodicalManager.scheduleAtFixedRate(new OUIUpdater(this), 12, 12, TimeUnit.HOURS);
periodicalManager.scheduleAtFixedRate(new Dot11SignalTrackMonitor(this), 1, 1, TimeUnit.MINUTES);
periodicalManager.scheduleAtFixedRate(new Dot11DiscoMonitor(this), 1, 1, TimeUnit.MINUTES);
if(configuration.versionchecksEnabled()) {
if (configuration.versionchecksEnabled()) {
periodicalManager.scheduleAtFixedRate(new VersioncheckThread(version, this), 0, 60, TimeUnit.MINUTES);
} else {
LOG.info("Versionchecks are disabled.");
Expand Down Expand Up @@ -434,6 +440,11 @@ public DetectionAlertService getDetectionAlertService() {
return detectionAlertService;
}

@Override
public ConnectService getConnect() {
return connect;
}

@Override
public NodeIdentification getNodeInformation() {
return nodeIdentification;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,7 @@ public class ConfigurationKeys {
public static final String MISC = "misc";
public static final String CUSTOM_TITLE = "custom_title";
public static final String CUSTOM_FAVICON_URL = "custom_favicon_url";
public static final String CONNECT_SERVER = "connect_server";


}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package app.nzyme.core.configuration.node;

import com.google.auto.value.AutoValue;
import jakarta.annotation.Nullable;

import java.net.URI;
import java.util.Optional;
Expand All @@ -25,10 +26,13 @@ public abstract class NodeConfiguration {

public abstract String ntpServer();

@Nullable
public abstract String connectApiUri();

public abstract PerformanceConfiguration performance();
public abstract MiscConfiguration misc();

public static NodeConfiguration create(boolean versionchecksEnabled, boolean fetchOuis, String databasePath, URI restListenUri, URI httpExternalUri, String pluginDirectory, String cryptoDirectory, Optional<Integer> slowQueryLogThreshold, String ntpServer, PerformanceConfiguration performance, MiscConfiguration misc) {
public static NodeConfiguration create(boolean versionchecksEnabled, boolean fetchOuis, String databasePath, URI restListenUri, URI httpExternalUri, String pluginDirectory, String cryptoDirectory, Optional<Integer> slowQueryLogThreshold, String ntpServer, String connectApiUri, PerformanceConfiguration performance, MiscConfiguration misc) {
return builder()
.versionchecksEnabled(versionchecksEnabled)
.fetchOuis(fetchOuis)
Expand All @@ -39,6 +43,7 @@ public static NodeConfiguration create(boolean versionchecksEnabled, boolean fet
.cryptoDirectory(cryptoDirectory)
.slowQueryLogThreshold(slowQueryLogThreshold)
.ntpServer(ntpServer)
.connectApiUri(connectApiUri)
.performance(performance)
.misc(misc)
.build();
Expand Down Expand Up @@ -68,6 +73,8 @@ public abstract static class Builder {

public abstract Builder ntpServer(String ntpServer);

public abstract Builder connectApiUri(String connectApiUri);

public abstract Builder performance(PerformanceConfiguration performance);

public abstract Builder misc(MiscConfiguration misc);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public NodeConfiguration get() {
parseCryptoDirectory(),
parseSlowQueryLogThreshold(),
parseNtpServer(),
parseConnectUri(),
parsePerformance(),
parseMisc()
);
Expand Down Expand Up @@ -109,6 +110,15 @@ private String parseCryptoDirectory() {
return general.getString(ConfigurationKeys.CRYPTO_DIRECTORY);
}

@Nullable
private String parseConnectUri() {
if (general.hasPath(ConfigurationKeys.CONNECT_SERVER)) {
return general.getString(ConfigurationKeys.CONNECT_SERVER);
} else {
return null;
}
}

private String parseNtpServer() {
return general.getString(ConfigurationKeys.NTP_SERVER);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package app.nzyme.core.connect;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import org.joda.time.DateTime;

@AutoValue
public abstract class ConnectHealthIndicatorReport {

@JsonProperty("name")
public abstract String indicatorName();

@JsonProperty("id")
public abstract String indicatorId();

@JsonProperty("last_checked")
public abstract DateTime lastChecked();

@JsonProperty("result_level")
public abstract String resultLevel();

@JsonProperty("active")
public abstract boolean active();

public static ConnectHealthIndicatorReport create(String indicatorName, String indicatorId, DateTime lastChecked, String resultLevel, boolean active) {
return builder()
.indicatorName(indicatorName)
.indicatorId(indicatorId)
.lastChecked(lastChecked)
.resultLevel(resultLevel)
.active(active)
.build();
}

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

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

public abstract Builder indicatorId(String indicatorId);

public abstract Builder lastChecked(DateTime lastChecked);

public abstract Builder resultLevel(String resultLevel);

public abstract Builder active(boolean active);

public abstract ConnectHealthIndicatorReport build();
}
}
29 changes: 29 additions & 0 deletions src/main/java/app/nzyme/core/connect/ConnectRegistryKeys.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package app.nzyme.core.connect;

import app.nzyme.plugin.EncryptedRegistryKey;
import app.nzyme.plugin.RegistryKey;
import app.nzyme.plugin.rest.configuration.ConfigurationEntryConstraint;

import java.util.ArrayList;
import java.util.Optional;

public class ConnectRegistryKeys {

public static final RegistryKey CONNECT_ENABLED = RegistryKey.create(
"connect_enabled",
Optional.of(new ArrayList<>() {{
add(ConfigurationEntryConstraint.createSimpleBooleanConstraint());
}}),
Optional.of("false"),
false
);

public static EncryptedRegistryKey CONNECT_API_KEY = EncryptedRegistryKey.create(
"connect_api_key",
Optional.of(new ArrayList<>() {{
add(ConfigurationEntryConstraint.createStringLengthConstraint(64, 64));
}}),
false
);

}
47 changes: 47 additions & 0 deletions src/main/java/app/nzyme/core/connect/ConnectService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package app.nzyme.core.connect;

import app.nzyme.core.NzymeNode;
import app.nzyme.plugin.RegistryCryptoException;
import com.google.common.base.Strings;
import jakarta.annotation.Nullable;

import java.net.URI;
import java.util.Optional;

public class ConnectService {

private final NzymeNode nzyme;

private static final String DEFAULT_API_URI = "https://connect.nzyme.org/";

public ConnectService(NzymeNode nzyme) {
this.nzyme = nzyme;
}

public boolean isEnabled() {
Optional<String> enabled = nzyme.getDatabaseCoreRegistry().getValue(ConnectRegistryKeys.CONNECT_ENABLED.key());

return enabled.isPresent() && enabled.get().equals("true") && getApiKey() != null;
}

public URI getApiUri() {
if (Strings.isNullOrEmpty(nzyme.getConfiguration().connectApiUri())) {
return URI.create(DEFAULT_API_URI);
} else {
return URI.create(nzyme.getConfiguration().connectApiUri());
}
}

@Nullable
public String getApiKey() {
Optional<String> apiKey;
try {
apiKey = nzyme.getDatabaseCoreRegistry().getEncryptedValue(ConnectRegistryKeys.CONNECT_API_KEY.key());
} catch(RegistryCryptoException e) {
throw new RuntimeException("Could not decrypt Connect API key.", e);
}

return apiKey.orElse(null);
}

}
73 changes: 73 additions & 0 deletions src/main/java/app/nzyme/core/connect/ConnectStatusReport.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package app.nzyme.core.connect;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.auto.value.AutoValue;
import org.joda.time.DateTime;

import java.util.List;

@AutoValue
public abstract class ConnectStatusReport {

@JsonProperty("version")
public abstract String version();

@JsonProperty("local_time")
public abstract DateTime localTime();

@JsonProperty("java_vendor")
public abstract String javaVendor();

@JsonProperty("java_version")
public abstract String javaVersion();

@JsonProperty("os_name")
public abstract String osName();

@JsonProperty("os_architecture")
public abstract String osArchitecture();

@JsonProperty("os_version")
public abstract String osVersion();

@JsonProperty("health_indicators")
public abstract List<ConnectHealthIndicatorReport> healthIndicators();

public static ConnectStatusReport create(String version, DateTime localTime, String javaVendor, String javaVersion, String osName, String osArchitecture, String osVersion, List<ConnectHealthIndicatorReport> healthIndicators) {
return builder()
.version(version)
.localTime(localTime)
.javaVendor(javaVendor)
.javaVersion(javaVersion)
.osName(osName)
.osArchitecture(osArchitecture)
.osVersion(osVersion)
.healthIndicators(healthIndicators)
.build();
}

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

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

public abstract Builder localTime(DateTime localTime);

public abstract Builder javaVendor(String javaVendor);

public abstract Builder javaVersion(String javaVersion);

public abstract Builder osName(String osName);

public abstract Builder osArchitecture(String osArchitecture);

public abstract Builder osVersion(String osVersion);

public abstract Builder healthIndicators(List<ConnectHealthIndicatorReport> healthIndicators);

public abstract ConnectStatusReport build();
}
}
Loading

0 comments on commit dca27c1

Please sign in to comment.