-
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.
- Loading branch information
1 parent
6fc5c59
commit 966305c
Showing
7 changed files
with
132 additions
and
5 deletions.
There are no files selected for viewing
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
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
68 changes: 68 additions & 0 deletions
68
...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,68 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.internal.identity; | ||
|
||
import java.net.InetAddress; | ||
import java.net.UnknownHostException; | ||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Objects; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import rocks.inspectit.gepard.agent.internal.identity.type.IdGenerationType; | ||
import rocks.inspectit.gepard.agent.internal.properties.PropertiesResolver; | ||
|
||
public class IdentityManager { | ||
private static final Logger log = LoggerFactory.getLogger(IdentityManager.class); | ||
|
||
private String agentId; | ||
|
||
private static IdentityManager instance; | ||
|
||
private IdentityManager() {} | ||
|
||
public static IdentityManager getInstance() { | ||
if (Objects.isNull(instance)) instance = new IdentityManager(); | ||
return instance; | ||
} | ||
|
||
public String getAgentId() { | ||
return agentId; | ||
} | ||
|
||
public void setIdentity() { | ||
IdGenerationType idGenerationType = PropertiesResolver.getIdGenerationType(); | ||
try { | ||
switch (idGenerationType) { | ||
case FQDN -> agentId = hash(InetAddress.getLocalHost().getCanonicalHostName()); | ||
case IP_ADDRESS -> agentId = hash(InetAddress.getLocalHost().getHostAddress()); | ||
case SERVICE_PROCESS_NAME -> | ||
ProcessHandle.current() | ||
.info() | ||
.commandLine() | ||
.ifPresent(commandLine -> agentId = commandLine.trim()); | ||
} | ||
} catch (UnknownHostException e) { | ||
log.info("Could not determine hostname", e); | ||
} | ||
} | ||
|
||
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(); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
...t/src/main/java/rocks/inspectit/gepard/agent/internal/identity/type/IdGenerationType.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,8 @@ | ||
/* (C) 2024 */ | ||
package rocks.inspectit.gepard.agent.internal.identity.type; | ||
|
||
public enum IdGenerationType { | ||
IP_ADDRESS, | ||
FQDN, | ||
SERVICE_PROCESS_NAME | ||
} |
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