Skip to content

Commit

Permalink
Update NarayanaPropertiesInitializer.java
Browse files Browse the repository at this point in the history
Generate right-length nodeIdentifier
if nodeIdentifier is longer than 28 bytes, the XID generated by narayana ist too long, and it breaks.
This shortens it, while nodeIdentifier remains unique.

Fix ported from quarkus
quarkusio/quarkus#30491
  • Loading branch information
deepred-dev authored Feb 6, 2024
1 parent b4064de commit 17a8e1e
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@

package dev.snowdrop.boot.narayana.core.properties;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import com.arjuna.ats.arjuna.common.CoordinatorEnvironmentBean;
import com.arjuna.ats.arjuna.common.CoreEnvironmentBean;
import com.arjuna.ats.arjuna.common.CoreEnvironmentBeanException;
Expand All @@ -34,6 +36,8 @@
*/
public class NarayanaPropertiesInitializer implements InitializingBean {

private static final String HASH_ALGORITHM_FOR_SHORTENING = "SHA-224";

private final NarayanaProperties properties;

public NarayanaPropertiesInitializer(NarayanaProperties narayanaProperties) {
Expand All @@ -59,13 +63,24 @@ public void afterPropertiesSet() {
}

private void setNodeIdentifier(String nodeIdentifier) {
String verifiedNodeIdentifier = nodeIdentifier;
try {
getPopulator(CoreEnvironmentBean.class).setNodeIdentifier(nodeIdentifier);
} catch (CoreEnvironmentBeanException e) {
if (nodeIdentifier != null && nodeIdentifier.getBytes(StandardCharsets.UTF_8).length > 28) {
verifiedNodeIdentifier = shortenNodeIdentifier(nodeIdentifier);
}

getPopulator(CoreEnvironmentBean.class).setNodeIdentifier(verifiedNodeIdentifier);
} catch (CoreEnvironmentBeanException | NoSuchAlgorithmException e) {
throw new IllegalArgumentException(e);
}
}

private String shortenNodeIdentifier(String nodeIdentifier) throws NoSuchAlgorithmException {
final byte[] nodeIdentifierAsBytes = nodeIdentifier.getBytes();
MessageDigest messageDigest224 = MessageDigest.getInstance(HASH_ALGORITHM_FOR_SHORTENING);
return new String(messageDigest224.digest(nodeIdentifierAsBytes), StandardCharsets.UTF_8);
}

private void setXARecoveryNodes(List<String> xaRecoveryNodes) {
getPopulator(JTAEnvironmentBean.class).setXaRecoveryNodes(xaRecoveryNodes);
}
Expand Down

0 comments on commit 17a8e1e

Please sign in to comment.