Skip to content

Commit 9ba4872

Browse files
authored
Avoid runtime attach user conflicts (#1704)
* allow multiple users with same agent version
1 parent 93e301e commit 9ba4872

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

CHANGELOG.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ any further configuration. Supports log4j1, log4j2 and Logback. {pull}1261[#1261
5252
* fix sample rate rounded to zero when lower than precision - {pull}1655[#1655]
5353
* fixed a couple of bugs with the external plugin mechanism (not documented until now) - {pull}1660[#1660]
5454
* Update to async-profiler 1.8.4 to prevent crashes on Java 7 - {pull}1678[#1678]
55+
* Fix runtime attach conflict with multiple users - {pull}1704[#1704]
5556
5657
[[release-notes-1.x]]
5758
=== Java Agent version 1.x

apm-agent-attach/src/main/java/co/elastic/apm/attach/ElasticApmAttacher.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,12 @@ private static File getAgentJarFile() {
202202
throw new IllegalStateException("Agent jar not found");
203203
}
204204
String hash = md5Hash(ElasticApmAttacher.class.getResourceAsStream("/elastic-apm-agent.jar"));
205-
File tempAgentJar = new File(System.getProperty("java.io.tmpdir"), "elastic-apm-agent-" + hash + ".jar");
205+
206+
// we have to include current user name as multiple copies of the same agent could be attached
207+
// to multiple JVMs, each running under a different user. Also, we have to make it path-friendly.
208+
String user = md5Hash(System.getProperty("user.name"));
209+
210+
File tempAgentJar = new File(System.getProperty("java.io.tmpdir"), String.format("elastic-apm-agent-%s-%s.jar", user, hash));
206211
if (!tempAgentJar.exists()) {
207212
try (FileOutputStream out = new FileOutputStream(tempAgentJar)) {
208213
FileChannel channel = out.getChannel();
@@ -238,4 +243,10 @@ static String md5Hash(InputStream resourceAsStream) throws IOException, NoSuchAl
238243
return String.format("%032x", new BigInteger(1, md.digest()));
239244
}
240245
}
246+
247+
static String md5Hash(String s) throws NoSuchAlgorithmException {
248+
MessageDigest md = MessageDigest.getInstance("MD5");
249+
md.update(s.getBytes());
250+
return String.format("%032x", new BigInteger(1, md.digest()));
251+
}
241252
}

0 commit comments

Comments
 (0)