Skip to content

Commit

Permalink
Merge branch 'WDT-863' into 'main'
Browse files Browse the repository at this point in the history
Adding support for Jakarta version of JAXB

See merge request weblogic-cloud/weblogic-deploy-tooling!1760
  • Loading branch information
robertpatrick committed Dec 31, 2024
2 parents 6960150 + 2fa0a84 commit 27391f1
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.PBEKeySpec;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;

import oracle.weblogic.deploy.logging.PlatformLogger;
import oracle.weblogic.deploy.logging.WLSDeployLogFactory;
import oracle.weblogic.deploy.util.StringUtils;
import oracle.weblogic.deploy.util.JaxbDatatypeConverter;
import oracle.weblogic.deploy.util.WdtJaxbException;

import static java.nio.charset.StandardCharsets.US_ASCII;
import static java.nio.charset.StandardCharsets.UTF_8;
Expand Down Expand Up @@ -165,7 +166,7 @@ public static String encryptString(String clearText, final char[] userPassphrase
byte[] encrypted = cipher.doFinal(clearText.getBytes(UTF_8));
result = getEncryptedString(encrypted, nonce, salt);
} catch (InvalidKeyException | InvalidAlgorithmParameterException |
IllegalBlockSizeException | BadPaddingException ex) {
IllegalBlockSizeException | BadPaddingException | WdtJaxbException ex) {

EncryptionException ee =
new EncryptionException("WLSDPLY-04002", ex, JAVA_VERSION, ex.getLocalizedMessage());
Expand Down Expand Up @@ -218,31 +219,38 @@ private static Cipher getCipher() throws EncryptionException {
return cipher;
}

private static String getEncryptedString(byte[] cipher, byte[] nonce, byte[] salt) {
String cipherEncoded = DatatypeConverter.printBase64Binary(cipher);
String nonceEncoded = DatatypeConverter.printBase64Binary(nonce);
String saltEncoded = DatatypeConverter.printBase64Binary(salt);
private static String getEncryptedString(byte[] cipher, byte[] nonce, byte[] salt) throws WdtJaxbException{
String cipherEncoded = JaxbDatatypeConverter.printBase64Binary(cipher);
String nonceEncoded = JaxbDatatypeConverter.printBase64Binary(nonce);
String saltEncoded = JaxbDatatypeConverter.printBase64Binary(salt);
String all = cipherEncoded + SEP + nonceEncoded + SEP + saltEncoded;
String allEncoded = DatatypeConverter.printBase64Binary(all.getBytes(US_ASCII));
String allEncoded = JaxbDatatypeConverter.printBase64Binary(all.getBytes(US_ASCII));
return CIPHER_TEXT_PREFIX + allEncoded;
}

private static List<byte[]> getCipherComponents(String text) throws EncryptionException {
final String METHOD = "getCipherComponents";
LOGGER.entering(CLASS, METHOD);

List<byte[]> result = new ArrayList<>();
if (text.startsWith(CIPHER_TEXT_PREFIX) && text.length() > CIPHER_TEXT_PREFIX.length()) {
String all = text.substring(CIPHER_TEXT_PREFIX.length());
String allDecoded = new String(DatatypeConverter.parseBase64Binary(all), US_ASCII);
String[] parts = allDecoded.split(SEP);
if (parts.length != CIPHER_SECTIONS) {
EncryptionException ee = new EncryptionException("WLSDPLY-04006", parts.length);
try {
String all = text.substring(CIPHER_TEXT_PREFIX.length());
String allDecoded = new String(JaxbDatatypeConverter.parseBase64Binary(all), US_ASCII);
String[] parts = allDecoded.split(SEP);
if (parts.length != CIPHER_SECTIONS) {
EncryptionException ee = new EncryptionException("WLSDPLY-04006", parts.length);
LOGGER.throwing(CLASS, METHOD, ee);
throw ee;
}
result.add(JaxbDatatypeConverter.parseBase64Binary(parts[PWD_POS]));
result.add(JaxbDatatypeConverter.parseBase64Binary(parts[NONCE_POS]));
result.add(JaxbDatatypeConverter.parseBase64Binary(parts[SALT_POS]));
} catch (WdtJaxbException ex) {
EncryptionException ee = new EncryptionException("WLSDPLY-04007", ex, ex.getLocalizedMessage());
LOGGER.throwing(CLASS, METHOD, ee);
throw ee;
}
result.add(DatatypeConverter.parseBase64Binary(parts[PWD_POS]));
result.add(DatatypeConverter.parseBase64Binary(parts[NONCE_POS]));
result.add(DatatypeConverter.parseBase64Binary(parts[SALT_POS]));
}
return result;
}
Expand Down
9 changes: 4 additions & 5 deletions core/src/main/java/oracle/weblogic/deploy/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import javax.xml.bind.DatatypeConverter;

import oracle.weblogic.deploy.exception.ExceptionHelper;
import oracle.weblogic.deploy.logging.PlatformLogger;
Expand Down Expand Up @@ -477,7 +476,7 @@ public static void deleteDirectory(File directory) {
* @throws NoSuchAlgorithmException if an error occurs obtaining the hashing algorithm
* @throws IllegalArgumentException if the file is not a valid, existing file
*/
public static String computeHash(String fileName) throws IOException, NoSuchAlgorithmException {
public static String computeHash(String fileName) throws IOException, NoSuchAlgorithmException, WdtJaxbException {
final String METHOD = "computeHash";

LOGGER.entering(CLASS, METHOD, fileName);
Expand All @@ -497,7 +496,7 @@ public static String computeHash(String fileName) throws IOException, NoSuchAlgo
* @throws NoSuchAlgorithmException if an error occurs obtaining the hashing algorithm
* @throws IllegalArgumentException if the file is not a valid, existing file
*/
public static String computeHash(File file) throws IOException, NoSuchAlgorithmException {
public static String computeHash(File file) throws IOException, NoSuchAlgorithmException, WdtJaxbException {
final String METHOD = "computeHash";

LOGGER.entering(CLASS, METHOD, file);
Expand All @@ -516,10 +515,10 @@ public static String computeHash(File file) throws IOException, NoSuchAlgorithmE
* @return the Base64-encoded hash
* @throws NoSuchAlgorithmException if an error occurs obtaining the hashing algorithm
*/
public static String computeHash(byte[] bytes) throws NoSuchAlgorithmException {
public static String computeHash(byte[] bytes) throws NoSuchAlgorithmException, WdtJaxbException {
MessageDigest messageDigest = MessageDigest.getInstance("SHA-512");
byte[] hash = messageDigest.digest(bytes);
return DatatypeConverter.printBase64Binary(hash);
return JaxbDatatypeConverter.printBase64Binary(hash);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import oracle.weblogic.deploy.logging.PlatformLogger;
import oracle.weblogic.deploy.logging.WLSDeployLogFactory;

/**
* A utility class that acts as an abstraction layer to JAXB classes so that
* we can operate with either javax or jakarta package implementation classes.
*/
public class JaxbDatatypeConverter {
private static final String CLASS = JaxbDatatypeConverter.class.getName();
private static final PlatformLogger LOGGER = WLSDeployLogFactory.getLogger("wlsdeploy.util");

private static final String JAVAX_DATATYPE_CONVERTER_CLASS_NAME = "javax.xml.bind.DatatypeConverter";
private static final String JAKARTA_DATATYPE_CONVERTER_CLASS_NAME = "jakarta.xml.bind.DatatypeConverter";

private static final Class<?> DATATYPE_CONVERTER_CLASS;

static {
Class<?> datatypeConverterClass = null;
try {
datatypeConverterClass =
JaxbDatatypeConverter.class.getClassLoader().loadClass(JAVAX_DATATYPE_CONVERTER_CLASS_NAME);
} catch (ClassNotFoundException e) {
LOGGER.fine("WLSDPLY-08600", JAVAX_DATATYPE_CONVERTER_CLASS_NAME, e.getLocalizedMessage());

try {
datatypeConverterClass =
JaxbDatatypeConverter.class.getClassLoader().loadClass(JAKARTA_DATATYPE_CONVERTER_CLASS_NAME);
} catch (ClassNotFoundException e2) {
LOGGER.fine("WLSDPLY-08600", JAKARTA_DATATYPE_CONVERTER_CLASS_NAME, e.getLocalizedMessage());

WdtJaxbException ex = new WdtJaxbException("WLSDPLY-08601", JAVAX_DATATYPE_CONVERTER_CLASS_NAME,
JAKARTA_DATATYPE_CONVERTER_CLASS_NAME);
LOGGER.throwing(CLASS, "static initializer", ex);
}
}
DATATYPE_CONVERTER_CLASS = datatypeConverterClass;
}

public static String printBase64Binary(byte[] val) throws WdtJaxbException {
final String METHOD = "printBase64Binary";
LOGGER.entering(CLASS, METHOD);

String result = null;
try {
Method method = getPrintBase64BinaryMethod();
result = (String) method.invoke(null, (Object) val);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
WdtJaxbException ex = new WdtJaxbException("WLSDPLY-08603", e, METHOD, e.getLocalizedMessage());
LOGGER.throwing(CLASS, METHOD, ex);
throw ex;
}

LOGGER.exiting(CLASS, METHOD, result);
return result;
}

public static byte[] parseBase64Binary(String val) throws WdtJaxbException {
final String METHOD = "parseBase64Binary";
LOGGER.entering(CLASS, METHOD);

byte[] result = new byte[] {};
try {
Method method = getParseBase64BinaryMethod();
result = (byte[]) method.invoke(null, val);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
WdtJaxbException ex = new WdtJaxbException("WLSDPLY-08603", e, METHOD, e.getLocalizedMessage());
LOGGER.throwing(CLASS, METHOD, ex);
throw ex;
}

LOGGER.exiting(CLASS, METHOD, result);
return result;
}

public static String printHexBinary(byte[] val) throws WdtJaxbException {
final String METHOD = "printHexBinary";
LOGGER.entering(CLASS, METHOD);

String result = null;
try {
Method method = getPrintHexBinaryMethod();
result = (String) method.invoke(null, (Object) val);
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | SecurityException e) {
WdtJaxbException ex = new WdtJaxbException("WLSDPLY-08603", e, METHOD, e.getLocalizedMessage());
LOGGER.throwing(CLASS, METHOD, ex);
throw ex;
}

LOGGER.exiting(CLASS, METHOD, result);
return result;
}

private static Method getPrintBase64BinaryMethod() throws WdtJaxbException {
final String METHOD = "getPrintBase64BinaryMethod";
LOGGER.entering(CLASS, METHOD);

Method method;
try {
method = DATATYPE_CONVERTER_CLASS.getMethod("printBase64Binary", byte[].class);
} catch (NoSuchMethodException | SecurityException e) {
WdtJaxbException ex = new WdtJaxbException("WLSDPLY-08602", e, "printBase64Binary", e.getLocalizedMessage());
LOGGER.throwing(CLASS, METHOD, ex);
throw ex;
}
LOGGER.exiting(CLASS, METHOD, method);
return method;
}

private static Method getPrintHexBinaryMethod() throws WdtJaxbException {
final String METHOD = "getPrintHexBinaryMethod";
LOGGER.entering(CLASS, METHOD);

Method method;
try {
method = DATATYPE_CONVERTER_CLASS.getMethod("printHexBinary", byte[].class);
} catch (NoSuchMethodException | SecurityException e) {
WdtJaxbException ex = new WdtJaxbException("WLSDPLY-08602", e, "printHexBinary", e.getLocalizedMessage());
LOGGER.throwing(CLASS, METHOD, ex);
throw ex;
}
LOGGER.exiting(CLASS, METHOD, method);
return method;
}

private static Method getParseBase64BinaryMethod() throws WdtJaxbException {
final String METHOD = "getParseBase64BinaryMethod";
LOGGER.entering(CLASS, METHOD);

Method method;
try {
method = DATATYPE_CONVERTER_CLASS.getMethod("parseBase64Binary", String.class);
} catch (NoSuchMethodException | SecurityException e) {
WdtJaxbException ex = new WdtJaxbException("WLSDPLY-08602", e, "parseBase64Binary", e.getLocalizedMessage());
LOGGER.throwing(CLASS, METHOD, ex);
throw ex;
}
LOGGER.exiting(CLASS, METHOD, method);
return method;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -933,7 +933,7 @@ public String getFileHash(String path) throws WLSDeployArchiveIOException {
String result;
try {
result = FileUtils.computeHash(fileBytes);
} catch (NoSuchAlgorithmException e) {
} catch (NoSuchAlgorithmException | WdtJaxbException e) {
WLSDeployArchiveIOException aioe =
new WLSDeployArchiveIOException("WLSDPLY-01407", e, getArchiveFileName(), path,
e.getLocalizedMessage());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Copyright (c) 2024, Oracle Corporation and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.util;

import oracle.weblogic.deploy.exception.BundleAwareException;
import oracle.weblogic.deploy.exception.ExceptionHelper;

public class WdtJaxbException extends BundleAwareException {
private static final long serialVersionUID = 1L;

/**
* Constructs a default exception.
*/
public WdtJaxbException() {
// default constructor
}

/**
* Constructs a new exception with the specified message id.
*
* @param messageID the message ID
*/
public WdtJaxbException(String messageID) {
super(messageID);
}

/**
* Constructs a new exception with the specified message id and parameters.
*
* @param messageID the message ID
* @param params the parameters to use to fill in the message tokens
*/
public WdtJaxbException(String messageID, Object... params) {
super(messageID, params);
}

/**
* Constructs a new exception with the specified message id and cause.
*
* @param messageID the message ID
* @param cause the exception that triggered the creation of this exception
*/
public WdtJaxbException(String messageID, Throwable cause) {
super(messageID, cause);
}

/**
* Constructs a new exception with passed message id, cause, and parameters.
*
* @param messageID the message ID
* @param cause the exception that triggered the creation of this exception
* @param params the parameters to use to fill in the message tokens
*/
public WdtJaxbException(String messageID, Throwable cause, Object... params) {
super(messageID, cause, params);
}

/**
* Constructs a new exception with the specified cause.
*
* @param cause the exception that triggered the creation of this exception
*/
public WdtJaxbException(Throwable cause) {
super(cause);
}

/**
* {@inheritDoc}
*/
@Override
public String getBundleName() {
return ExceptionHelper.getResourceBundleName();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from java.security import NoSuchAlgorithmException
from oracle.weblogic.deploy.util import FileUtils
from oracle.weblogic.deploy.util import PyOrderedDict as OrderedDict
from oracle.weblogic.deploy.util import WdtJaxbException

from wlsdeploy.aliases import alias_utils
from wlsdeploy.aliases.location_context import LocationContext
Expand Down Expand Up @@ -644,7 +645,7 @@ def __get_file_hash(self, filename):
return None

hash_value = FileUtils.computeHash(filename)
except (IOException, NoSuchAlgorithmException), e:
except (IOException, NoSuchAlgorithmException, WdtJaxbException), e:
ex = exception_helper.create_deploy_exception('WLSDPLY-09309', filename, e.getLocalizedMessage(), error=e)
self.logger.throwing(ex, class_name=self._class_name, method_name=_method_name)
raise ex
Expand Down
3 changes: 2 additions & 1 deletion core/src/main/python/wlsdeploy/tool/deploy/deployer_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from oracle.weblogic.deploy.util import FileUtils
from oracle.weblogic.deploy.util import StringUtils
from oracle.weblogic.deploy.util import PyWLSTException
from oracle.weblogic.deploy.util import WdtJaxbException
from oracle.weblogic.deploy.util import WLSDeployArchive

from oracle.weblogic.deploy.exception import BundleAwareException
Expand Down Expand Up @@ -475,7 +476,7 @@ def get_file_hash(file_name):
_logger.entering(file_name, class_name=_class_name, method_name=_method_name)
try:
result = FileUtils.computeHash(file_name)
except (IOException, NoSuchAlgorithmException), e:
except (IOException, NoSuchAlgorithmException, WdtJaxbException), e:
ex = exception_helper.create_deploy_exception('WLSDPLY-09108', file_name, e.getLocalizedMessage(), error=e)
_logger.throwing(ex, class_name=_class_name, method_name=_method_name)
raise ex
Expand Down
Loading

0 comments on commit 27391f1

Please sign in to comment.