-
Notifications
You must be signed in to change notification settings - Fork 291
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace UUID.randomUUID() with RandomUtils.randomUUID() (#8267)
* Replace UUID.randomUUID() with RandomUtils.randomUUID() This avoids a potential side-effect of loading 'java.util.logging' too early when the Amazon Corretto Crypto Provider (ACCP) is plugged into SecureRandom * Add RandomUtils.secureRandomUUID()
- Loading branch information
Showing
14 changed files
with
82 additions
and
23 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
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
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
29 changes: 29 additions & 0 deletions
29
internal-api/src/main/java/datadog/trace/util/RandomUtils.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,29 @@ | ||
package datadog.trace.util; | ||
|
||
import de.thetaphi.forbiddenapis.SuppressForbidden; | ||
import java.util.Random; | ||
import java.util.UUID; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
|
||
public final class RandomUtils { | ||
private RandomUtils() {} | ||
|
||
/** Returns a random UUID. */ | ||
public static UUID randomUUID() { | ||
Random rnd = ThreadLocalRandom.current(); | ||
long msb = (rnd.nextLong() & 0xffff_ffff_ffff_0fffL) | 0x0000_0000_0000_4000L; | ||
long lsb = (rnd.nextLong() & 0x3fff_ffff_ffff_ffffL) | 0x8000_0000_0000_0000L; | ||
return new UUID(msb, lsb); | ||
} | ||
|
||
/** | ||
* Returns a cryptographically strong random UUID. | ||
* | ||
* <p>Note on some systems this may have a side effect of initializing java.util.logging, so its | ||
* use should be avoided during premain. | ||
*/ | ||
@SuppressForbidden | ||
public static UUID secureRandomUUID() { | ||
return UUID.randomUUID(); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
internal-api/src/test/java/datadog/trace/util/RandomUtilsTest.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,25 @@ | ||
package datadog.trace.util; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.util.regex.Pattern; | ||
import org.junit.jupiter.api.Test; | ||
|
||
public class RandomUtilsTest { | ||
private static final Pattern VERSION_4_UUID = | ||
Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[8-9a-f][0-9a-f]{3}-[0-9a-f]{12}"); | ||
|
||
@Test | ||
public void testRandomUUIDMatchesSpec() { | ||
for (int i = 0; i < 8; i++) { | ||
assertThat(RandomUtils.randomUUID().toString()).matches(VERSION_4_UUID); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSecureRandomUUIDMatchesSpec() { | ||
for (int i = 0; i < 8; i++) { | ||
assertThat(RandomUtils.secureRandomUUID().toString()).matches(VERSION_4_UUID); | ||
} | ||
} | ||
} |
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