-
Notifications
You must be signed in to change notification settings - Fork 3
Cache: Hostname, IPAddress, OS, And Platform info #252
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
| 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() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? More info - Comment |
||
| return HOSTNAME; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 🔧 How do I fix it? More info - Comment |
||
| 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() { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? More info - Comment |
||
| return IP_ADDRESS; | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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.