Skip to content

Commit

Permalink
Merge pull request quarkusio#36752 from turing85/feature/30491-shorte…
Browse files Browse the repository at this point in the history
…n-node-name-if-necessary

If the node name is longer than 28 bytes, shorten it with SHA-224
  • Loading branch information
gsmet authored Nov 6, 2023
2 parents 1e5a54e + 07b3a4c commit 3eda9e4
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;
@@ -29,6 +32,7 @@

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

private static Properties defaultProperties;

@@ -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
Original file line number Diff line number Diff line change
@@ -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.
*/

0 comments on commit 3eda9e4

Please sign in to comment.