-
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.
feature: Add IdentityManager and create agentId based on PID and host…
…name (#26) * Add IdentityManager and create agentId based on PID and hostname * Fix review findings * Fix remaining review findings * Fix AgentInfoTest
- Loading branch information
1 parent
6fc5c59
commit 7fc92b8
Showing
7 changed files
with
136 additions
and
19 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
...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,58 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.internal.identity; | ||
|
||
import java.lang.management.ManagementFactory; | ||
import java.lang.management.RuntimeMXBean; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Objects; | ||
import rocks.inspectit.gepard.agent.internal.identity.model.IdentityInfo; | ||
|
||
/** Responsible for generating the agentId. */ | ||
public class IdentityManager { | ||
|
||
private static IdentityManager instance; | ||
|
||
private final IdentityInfo identityInfo; | ||
|
||
private IdentityManager() { | ||
RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean(); | ||
String vmId = runtime.getName(); | ||
this.identityInfo = new IdentityInfo(vmId, hash(vmId)); | ||
} | ||
|
||
public static IdentityManager getInstance() { | ||
if (Objects.isNull(instance)) instance = new IdentityManager(); | ||
return instance; | ||
} | ||
|
||
public IdentityInfo getIdentityInfo() { | ||
return this.identityInfo; | ||
} | ||
|
||
/** | ||
* 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(); | ||
} | ||
} |
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
21 changes: 21 additions & 0 deletions
21
...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,21 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.internal.identity.model; | ||
|
||
public final class IdentityInfo { | ||
|
||
private final String vmId; | ||
private final String agentId; | ||
|
||
public IdentityInfo(String vmId, String agentId) { | ||
this.vmId = vmId; | ||
this.agentId = agentId; | ||
} | ||
|
||
public String vmId() { | ||
return vmId; | ||
} | ||
|
||
public String agentId() { | ||
return 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
41 changes: 41 additions & 0 deletions
41
...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,41 @@ | ||
/* (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 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() { | ||
RuntimeMXBean mockRuntimeMXBean = mock(RuntimeMXBean.class); | ||
when(mockRuntimeMXBean.getName()).thenReturn("12345@mockedHostName"); | ||
|
||
try (MockedStatic<ManagementFactory> managementFactoryMockedStatic = | ||
Mockito.mockStatic(ManagementFactory.class)) { | ||
managementFactoryMockedStatic | ||
.when(ManagementFactory::getRuntimeMXBean) | ||
.thenReturn(mockRuntimeMXBean); | ||
|
||
IdentityManager identityManager = IdentityManager.getInstance(); | ||
IdentityInfo identityInfo = identityManager.getIdentityInfo(); | ||
|
||
assertNotNull(identityInfo); | ||
assertEquals("12345@mockedHostName", identityInfo.vmId()); | ||
assertEquals( | ||
"d29aca592fc2071bcef6577d649071d4d54a8ae6cd5c0be0e51f28af2867f207", | ||
identityInfo.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
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