Skip to content
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
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package dev.aikido.agent_api.background.cloud;

import dev.aikido.agent_api.Config;
import dev.aikido.agent_api.helpers.net.Hostname;
import dev.aikido.agent_api.helpers.net.IPAddress;
import dev.aikido.agent_api.storage.ServiceConfigStore;
import dev.aikido.agent_api.storage.ServiceConfiguration;

import java.util.List;
import java.util.Map;

import static dev.aikido.agent_api.helpers.net.Hostname.getHostname;
import static dev.aikido.agent_api.helpers.net.IPAddress.getIpAddress;

/**
* Class to give you the "agent" info, which is the CloudConnectionManager in Java.
*/
Expand All @@ -29,37 +28,40 @@ public record ManagerInfo(
String nodeEnv,
Platform platform
) {}


public record OS(String name, String version) {}
private static final OS OS_INFO;
static {
String osName = System.getProperty("os.name");
String osVersion = System.getProperty("os.version");
OS_INFO = new OS(osName, osVersion);
}

public record Platform(String name, String version) {}
private static final Platform PLATFORM_INFO;
static {
String jvmName = System.getProperty("java.vm.name");
String jvmVersion = System.getProperty("java.version");
PLATFORM_INFO = new Platform(jvmName, jvmVersion);
}


public static ManagerInfo getManagerInfo() {
ServiceConfiguration serviceConfig = ServiceConfigStore.getConfig();
return new ManagerInfo(
!serviceConfig.isBlockingEnabled(), // dryMode
getHostname(), // hostname
Hostname.get(), // hostname
Config.pkgVersion, // version
"firewall-java", // library
getIpAddress(), // ipAddress
IPAddress.get(), // ipAddress
Map.of(), // packages (FIX LATER)
null, // serverless is not supported for Java
List.of(), // stack
getOSInfo(), // os
OS_INFO, // os
false, // preventedPrototypePollution, should be removed from API
"", // nodeEnv
getPlatformInfo() // platform info
PLATFORM_INFO // platform info
);
}

private static OS getOSInfo() {
String name = System.getProperty("os.name");
String version = System.getProperty("os.version");
return new OS(name, version);
}

private static Platform getPlatformInfo() {
String name = System.getProperty("java.vm.name");
String version = System.getProperty("java.version");
return new Platform(name, version);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,30 @@

import dev.aikido.agent_api.helpers.logging.LogManager;
import dev.aikido.agent_api.helpers.logging.Logger;

import java.io.IOException;
import java.util.Scanner;


public final class Hostname {
private Hostname() {}

private static final Logger logger = LogManager.getLogger(Hostname.class);
private static final String HOSTNAME;

public static String getHostname() {
// getHostName function seem unreliable, so using "hostname" command which works for both UNIX(-like) systems and Windows
// See https://stackoverflow.com/a/7800008 for more info.
// getHostName function seem unreliable, so using "hostname" command which works for both UNIX(-like) systems and Windows
// See https://stackoverflow.com/a/7800008 for more info.
static {

Choose a reason for hiding this comment

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

Static initializer in Hostname replaces earlier immediate return inside try-block with post-try assignment, removing an early-return guard and increasing control flow complexity

Details

✨ AI Reasoning
​​1) Hostname.java change: previously the method returned early from within the try-block when a hostname was read; the change replaces those early returns with a static initializer that assigns to a local variable and sets a final field after the try/catch.
​2) This change removed a clear early-return/guard style and forces continued execution and assignment after the nested try/if instead of exiting as soon as a value is available, slightly increasing cognitive overhead.
​3) The issue harms readability/maintainability by replacing an immediate return with additional control flow and state assignment in a static initializer, which is less direct for a simple operation.

🔧 How do I fix it?
Place parameter validation and guard clauses at the function start. Use early returns to reduce nesting levels and improve readability.

More info - Comment @AikidoSec feedback: [FEEDBACK] to get better review comments in the future.

String hostname = "unknown";
try (Scanner s = new Scanner(Runtime.getRuntime().exec("hostname").getInputStream()).useDelimiter("\\A")) {
if (s.hasNext()) {
return s.next().trim();
hostname = s.next().trim();
}
} catch (IOException e) {
logger.debug(e);
}
return "unknown";
HOSTNAME = hostname;
}

public static String get() {

Choose a reason for hiding this comment

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

Method Hostname.get() has a vague name ('get') that does not clearly convey it returns the system hostname

Details

🔧 How do I fix it?
Use descriptive verb-noun function names, add docstrings explaining the function's purpose, or provide meaningful return type hints.

More info - Comment @AikidoSec feedback: [FEEDBACK] to get better review comments in the future.

return HOSTNAME;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,23 @@ public final class IPAddress {
private IPAddress() {
}

public static String getIpAddress() {
try {
String hostAddress = InetAddress.getLocalHost().getHostAddress();
private static final String IP_ADDRESS;

static {

Choose a reason for hiding this comment

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

Static initializer in IPAddress replaces immediate return inside try-block with post-try assignment, removing an early-return guard and increasing control flow complexity

Details

✨ AI Reasoning
​​1) IPAddress.java change: previously the method returned the computed host address immediately inside the try-block, but the change converts this into a static initializer that assigns to a variable and sets a final field after the try/catch.
​2) This removes the direct early-return behavior and replaces it with later assignment, adding an extra layer of state and flow for a simple retrieval.
​3) That worsens clarity for a simple operation by avoiding the straightforward early-return pattern.

🔧 How do I fix it?
Place parameter validation and guard clauses at the function start. Use early returns to reduce nesting levels and improve readability.

More info - Comment @AikidoSec feedback: [FEEDBACK] to get better review comments in the future.

String hostAddress = "0.0.0.0";
try {
hostAddress = InetAddress.getLocalHost().getHostAddress();
// Remove the zone index if present
if (hostAddress.contains("%")) {
hostAddress = hostAddress.substring(0, hostAddress.indexOf('%'));
}

return hostAddress;
} catch (UnknownHostException ignored) {
// pass-through
}
return "0.0.0.0";
IP_ADDRESS = hostAddress;
}

public static String get() {

Choose a reason for hiding this comment

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

Method IPAddress.get() has a vague name ('get') that does not clearly convey it returns the host IP address

Details

🔧 How do I fix it?
Use descriptive verb-noun function names, add docstrings explaining the function's purpose, or provide meaningful return type hints.

More info - Comment @AikidoSec feedback: [FEEDBACK] to get better review comments in the future.

return IP_ADDRESS;
}
}