-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IdentityManager and create agentId based on PID and hostname
- Loading branch information
1 parent
6fc5c59
commit f8e931b
Showing
5 changed files
with
159 additions
and
13 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
...d-agent/src/main/java/rocks/inspectit/gepard/agent/internal/identity/IdentityManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.internal.identity; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.RuntimeMXBean; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import rocks.inspectit.gepard.agent.internal.identity.model.IdentityInfo; | ||
|
||
public class IdentityManager { | ||
|
||
private static final Logger log = LoggerFactory.getLogger(IdentityManager.class); | ||
|
||
private final IdentityInfo identityInfo; | ||
|
||
private IdentityManager() { | ||
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean(); | ||
long pid = runtime.getPid(); | ||
String localHostName = getLocalHostname(); | ||
this.identityInfo = new IdentityInfo(pid, localHostName, hash(pid + localHostName)); | ||
} | ||
|
||
public static IdentityManager getInstance() { | ||
return new IdentityManager(); | ||
} | ||
|
||
public IdentityInfo getIdentityInfo() { | ||
return this.identityInfo; | ||
} | ||
|
||
/** | ||
* Determines the current hostname. | ||
* | ||
* @return the hostname or if the operation is not allowed by the security check, the textual | ||
* representation of the IP address. The default value is "0.0.0.0". | ||
*/ | ||
private String getLocalHostname() { | ||
String localHostName = "0.0.0.0"; | ||
try { | ||
localHostName = InetAddress.getLocalHost().getHostName(); | ||
} catch (UnknownHostException e) { | ||
log.info("Could not determine hostname", e); | ||
} | ||
return localHostName; | ||
} | ||
|
||
/** | ||
* Hashes the given input with SHA3-256. | ||
* | ||
* @param input the <code>String</code> to be hashed. | ||
* @return the SHA3-256 hashed <code>String</code> | ||
*/ | ||
private static String hash(String input) { | ||
MessageDigest messageDigest; | ||
try { | ||
messageDigest = MessageDigest.getInstance("SHA3-256"); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new UnsupportedOperationException("SHA3-256 not supported", e); | ||
} | ||
byte[] bytes = messageDigest.digest(input.getBytes(StandardCharsets.UTF_8)); | ||
StringBuilder hexString = new StringBuilder(2 * bytes.length); | ||
for (byte b : bytes) { | ||
String hex = Integer.toHexString(0xff & b); | ||
if (hex.length() == 1) { | ||
hexString.append('0'); | ||
} | ||
hexString.append(hex); | ||
} | ||
return hexString.toString(); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
...gent/src/main/java/rocks/inspectit/gepard/agent/internal/identity/model/IdentityInfo.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.internal.identity.model; | ||
|
||
public record IdentityInfo(long pid, String hostname, String agentId) {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...ent/src/test/java/rocks/inspectit/gepard/agent/internal/identity/IdentityManagerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.internal.identity; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.RuntimeMXBean; | ||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.Mockito; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import rocks.inspectit.gepard.agent.internal.identity.model.IdentityInfo; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class IdentityManagerTest { | ||
|
||
@Test | ||
void testCreateIdentityManagerSuccessfully() { | ||
InetAddress mockInetAddress = mock(InetAddress.class); | ||
when(mockInetAddress.getHostName()).thenReturn("mockedHostName"); | ||
|
||
RuntimeMXBean mockRuntimeMXBean = mock(RuntimeMXBean.class); | ||
when(mockRuntimeMXBean.getPid()).thenReturn(12345L); | ||
|
||
try (MockedStatic<InetAddress> mockedStatic = Mockito.mockStatic(InetAddress.class); | ||
MockedStatic<ManagementFactory> managementFactoryMockedStatic = | ||
Mockito.mockStatic(ManagementFactory.class)) { | ||
mockedStatic.when(InetAddress::getLocalHost).thenReturn(mockInetAddress); | ||
managementFactoryMockedStatic | ||
.when(ManagementFactory::getRuntimeMXBean) | ||
.thenReturn(mockRuntimeMXBean); | ||
|
||
IdentityManager identityManager = IdentityManager.getInstance(); | ||
IdentityInfo identityInfo = identityManager.getIdentityInfo(); | ||
|
||
assertNotNull(identityInfo); | ||
assertEquals("mockedHostName", identityInfo.hostname()); | ||
assertEquals(12345L, identityInfo.pid()); | ||
assertEquals( | ||
"ef138ea8d422d1df09c3f94b675a99bb475cdae966d067f58a0887ab54ab35e0", | ||
identityInfo.agentId()); | ||
} | ||
} | ||
|
||
@Test | ||
void testCreateIdentityManagerWithUnknownHostException() { | ||
try (MockedStatic<InetAddress> mockedStatic = Mockito.mockStatic(InetAddress.class)) { | ||
mockedStatic | ||
.when(InetAddress::getLocalHost) | ||
.thenThrow(new UnknownHostException("Mocked Exception")); | ||
|
||
IdentityManager identityManager = IdentityManager.getInstance(); | ||
IdentityInfo identityInfo = identityManager.getIdentityInfo(); | ||
|
||
assertNotNull(identityInfo); | ||
assertEquals("0.0.0.0", identityInfo.hostname()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters