Skip to content

Commit 169a8a8

Browse files
authored
[#261] Format digests with java.util.HexFormat instead of JAXB (#262)
`openidm-script` and `openidm-maintenance` use `javax.xml.bind` without declaring JAXB: `ScriptRegistryService` names inline scripts with `DatatypeConverter.printHexBinary`, and `ChecksumFile` formats checksums with `HexBinaryAdapter.marshal`. JAXB reaches them only through commons `json-resource` → `api-descriptor`, which OpenIdentityPlatform/commons#362 removes JAXB from. Both calls now use `HexFormat.of().withUpperCase()`. The output keeps the upper case of the JAXB methods, so inline script names and the update checksums (including the `checksum` field of the update archive listing) do not change. These were the only uses of `javax.xml.bind` in the repository. ## Tests - `ChecksumFileTest.testGetCurrentDigestIsUpperCaseHex` (new) and `ScriptRegistryServiceTest.testInlineScriptNameIsSha256OfSourceAndType` compare against literals computed with `md5` and `shasum`. The script test used to compute its expected value with `HexFormat` itself, which after this change would only compare the code with itself. Both tests pass on the base code, and both fail if `withUpperCase()` is dropped. - With commons#362 simulated (managed exclusions of `jaxb-api`, `jaxb-core` and `jaxb-impl` on `api-descriptor` and `json-web-token`), the base fails to compile with `package javax.xml.bind does not exist` (`ScriptRegistryService.java:39`) and `package javax.xml.bind.annotation.adapters does not exist` (`ChecksumFile.java:32`). With this change both modules build and pass their tests (16 and 117) with and without the simulation. - The generated `Import-Package` of both bundles differs from the base only by the removed `javax.xml.bind;version="[2.3,3)"` and `javax.xml.bind.annotation.adapters;version="[2.3,3)"`. Fixes #261
1 parent 104e8f6 commit 169a8a8

4 files changed

Lines changed: 16 additions & 11 deletions

File tree

‎openidm-maintenance/src/main/java/org/forgerock/openidm/maintenance/upgrade/ChecksumFile.java‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
import java.security.MessageDigest;
2828
import java.security.NoSuchAlgorithmException;
2929
import java.util.HashMap;
30+
import java.util.HexFormat;
3031
import java.util.Map;
3132
import java.util.Set;
32-
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
3333

3434
/**
3535
* Map-backed checksum file abstraction of the format
@@ -41,8 +41,8 @@ public class ChecksumFile extends HashMap<Path, String> {
4141

4242
static final long serialVersionUID = 1L;
4343

44-
/** Hex adapter for converting hex sums in checksum file to byte arrays */
45-
private static final HexBinaryAdapter hexAdapter = new HexBinaryAdapter();
44+
/** Formats computed digests as upper-case hex, as {@code HexBinaryAdapter} did before */
45+
private static final HexFormat hexFormat = HexFormat.of().withUpperCase();
4646

4747
// Path to checksums file
4848
private final Path checksums;
@@ -109,7 +109,7 @@ public ChecksumFile clone() {
109109
}
110110

111111
private String computeDigest(byte[] data) {
112-
return hexAdapter.marshal(digest.digest(data));
112+
return hexFormat.formatHex(digest.digest(data));
113113
}
114114

115115
/**

‎openidm-maintenance/src/test/java/org/forgerock/openidm/maintenance/upgrade/ChecksumFileTest.java‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,11 @@ public void testGetFiles() throws URISyntaxException, IOException, NoSuchAlgorit
7070
assertThat(files.contains(Paths.get("file1"))).isTrue();
7171
assertThat(files.contains(Paths.get("file2"))).isTrue();
7272
}
73+
74+
@Test
75+
public void testGetCurrentDigestIsUpperCaseHex() throws URISyntaxException, IOException, NoSuchAlgorithmException {
76+
ChecksumFile checksumFile = new ChecksumFile(Paths.get(getClass().getResource("/checksums.csv").toURI()));
77+
// MD5 of "Hello, world!"; the hex digits are upper case, as HexBinaryAdapter produced them
78+
assertThat(checksumFile.getCurrentDigest(Paths.get("file1"))).isEqualTo("6CD3556DEB0DA54BCA060B4C39479839");
79+
}
7380
}

‎openidm-script/src/main/java/org/forgerock/openidm/script/impl/ScriptRegistryService.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Collections;
2929
import java.util.HashMap;
3030
import java.util.HashSet;
31+
import java.util.HexFormat;
3132
import java.util.List;
3233
import java.util.Map;
3334
import java.util.Set;
@@ -36,7 +37,6 @@
3637

3738
import javax.script.ScriptException;
3839
import javax.script.SimpleBindings;
39-
import javax.xml.bind.DatatypeConverter;
4040

4141
import org.apache.commons.lang3.StringUtils;
4242
import org.forgerock.audit.events.AuditEvent;
@@ -605,7 +605,7 @@ public ScriptEntry takeScript(JsonValue script) throws ScriptException {
605605
String type = scriptConfig.get(SourceUnit.ATTR_TYPE).asString();
606606
byte[] nameAndType = (source.asString() + type).getBytes();
607607
byte[] digest = md.digest(nameAndType);
608-
String name = DatatypeConverter.printHexBinary(digest);
608+
String name = HexFormat.of().withUpperCase().formatHex(digest);
609609

610610
scriptConfig.put(SourceUnit.ATTR_NAME, name);
611611
} catch (NoSuchAlgorithmException e) {

‎openidm-script/src/test/java/org/forgerock/openidm/script/impl/ScriptRegistryServiceTest.java‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,8 @@
4444
import org.testng.annotations.DataProvider;
4545
import org.testng.annotations.Test;
4646

47-
import java.security.MessageDigest;
4847
import java.util.Collections;
4948
import java.util.HashMap;
50-
import java.util.HexFormat;
5149
import java.util.Map;
5250

5351
public class ScriptRegistryServiceTest {
@@ -111,9 +109,9 @@ public void testInlineScriptNameIsSha256OfSourceAndType() throws Exception {
111109

112110
ScriptEntry scriptEntry = new ScriptRegistryService().takeScript(jsonScript);
113111

114-
String expected = HexFormat.of().withUpperCase().formatHex(
115-
MessageDigest.getInstance("SHA-256").digest((source + type).getBytes()));
116-
assertThat(scriptEntry.getName().getName()).isEqualTo(expected);
112+
// SHA-256 of "var inline = 1;ECMAScript"; the hex digits are upper case, as printHexBinary produced them
113+
assertThat(scriptEntry.getName().getName())
114+
.isEqualTo("038E87A28ED5024EFB95C64C48A888663DCD18789E2D91E182EFEC4733FA8232");
117115
}
118116

119117
@Test

0 commit comments

Comments
 (0)