Skip to content

Commit

Permalink
quarkusio#30491: If the node name is longer than 28 bytes, shorten it…
Browse files Browse the repository at this point in the history
… with SHA-224
  • Loading branch information
turing85 committed Oct 31, 2023
1 parent d110270 commit 07b3a4c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package io.quarkus.narayana.jta.runtime;

import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -29,6 +32,7 @@

@Recorder
public class NarayanaJtaRecorder {
public static final String HASH_ALGORITHM_FOR_SHORTENING = "SHA-224";

private static Properties defaultProperties;

Expand All @@ -37,14 +41,28 @@ public class NarayanaJtaRecorder {
public void setNodeName(final TransactionManagerConfiguration transactions) {

try {
if (transactions.nodeName.getBytes(StandardCharsets.UTF_8).length > 28
&& transactions.shortenNodeNameIfNecessary) {
shortenNodeName(transactions);
}
arjPropertyManager.getCoreEnvironmentBean().setNodeIdentifier(transactions.nodeName);
jtaPropertyManager.getJTAEnvironmentBean().setXaRecoveryNodes(Collections.singletonList(transactions.nodeName));
TxControl.setXANodeName(transactions.nodeName);
} catch (CoreEnvironmentBeanException e) {
e.printStackTrace();
} catch (CoreEnvironmentBeanException | NoSuchAlgorithmException e) {
log.error("Could not set node name", e);
}
}

private static void shortenNodeName(TransactionManagerConfiguration transactions) throws NoSuchAlgorithmException {
String originalNodeName = transactions.nodeName;
log.warnf("Node name \"%s\" is longer than 28 bytes, shortening it by using %s.", originalNodeName,
HASH_ALGORITHM_FOR_SHORTENING);
final byte[] nodeNameAsBytes = originalNodeName.getBytes();
MessageDigest messageDigest224 = MessageDigest.getInstance(HASH_ALGORITHM_FOR_SHORTENING);
transactions.nodeName = new String(messageDigest224.digest(nodeNameAsBytes), StandardCharsets.UTF_8);
log.warnf("New node name is \"%s\"", transactions.nodeName);
}

public void setDefaultProperties(Properties properties) {
//TODO: this is a huge hack to avoid loading XML parsers
//this needs a proper SPI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,24 @@
public final class TransactionManagerConfiguration {
/**
* The node name used by the transaction manager.
* Must not exceed a length of 28 bytes.
*
* @see #shortenNodeNameIfNecessary
*/
@ConfigItem(defaultValue = "quarkus")
public String nodeName;

/**
* Whether the node name should be shortened if necessary.
* The node name must not exceed a length of 28 bytes. If this property is set to {@code true}, and the node name exceeds 28
* bytes, the node name is shortened by calculating the <a href="https://en.wikipedia.org/wiki/SHA-2">SHA-224</a> hash,
* which has a length of 28 bytes.
*
* @see #nodeName
*/
@ConfigItem(defaultValue = "false")
public boolean shortenNodeNameIfNecessary;

/**
* The default transaction timeout.
*/
Expand Down

0 comments on commit 07b3a4c

Please sign in to comment.