gnodet-bot commented on code in PR #26486: URL: https://github.com/apache/camel/pull/26486#discussion_r4059768964
########## components/camel-xmlsecurity/src/test/java/org/apache/camel/dataformat/xmlsecurity/XmlSecurityConstantNameTest.java: ########## @@ -0,0 +1,233 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.camel.dataformat.xmlsecurity; + +import javax.crypto.KeyGenerator; +import javax.crypto.SecretKey; + +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.converter.jaxp.XmlConverter; +import org.apache.camel.support.jsse.KeyStoreParameters; +import org.apache.camel.test.junit6.CamelTestSupport; +import org.apache.xml.security.encryption.XMLCipher; +import org.apache.xml.security.utils.EncryptionConstants; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Verifies that the Java constant names exposed by the model's {@code @Metadata(enums=...)} annotations (e.g. + * {@code "AES_256_GCM"}, {@code "RSA_OAEP"}, {@code "SHA256"}, {@code "MGF1_SHA256"}) are accepted by + * {@link XMLSecurityDataFormat} at runtime as aliases for the corresponding W3C URIs. + * + * <p> + * Before the fix, passing a constant name to {@link XMLSecurityDataFormat#setXmlCipherAlgorithm(String)} would store it + * verbatim and then cause {@code XMLCipher.getInstance("AES_256_GCM")} to throw + * {@code XMLEncryptionException: Null or empty transformation}, because XMLCipher only understands URIs. + */ +class XmlSecurityConstantNameTest extends CamelTestSupport { + + TestHelper xmlsecTestHelper = new TestHelper(); + + @Override + public boolean isUseRouteBuilder() { + return false; + } + + @Override + public void doPostSetup() { + context.getGlobalOptions().put(XmlConverter.OUTPUT_PROPERTIES_PREFIX + javax.xml.transform.OutputKeys.ENCODING, + "UTF-8"); + } + + // -- resolveAlgorithm unit tests ------------------------------------------------------- + + @Test + void resolveAlgorithmReturnsMappedUri() { + assertEquals(XMLCipher.AES_256_GCM, XMLSecurityDataFormat.resolveAlgorithm("AES_256_GCM")); + assertEquals(XMLCipher.AES_128_GCM, XMLSecurityDataFormat.resolveAlgorithm("AES_128_GCM")); + assertEquals(XMLCipher.AES_128, XMLSecurityDataFormat.resolveAlgorithm("AES_128")); + assertEquals(XMLCipher.AES_192, XMLSecurityDataFormat.resolveAlgorithm("AES_192")); + assertEquals(XMLCipher.TRIPLEDES, XMLSecurityDataFormat.resolveAlgorithm("TRIPLEDES")); + assertEquals(XMLCipher.SEED_128, XMLSecurityDataFormat.resolveAlgorithm("SEED_128")); + assertEquals(XMLCipher.CAMELLIA_128, XMLSecurityDataFormat.resolveAlgorithm("CAMELLIA_128")); + assertEquals(XMLCipher.RSA_v1dot5, XMLSecurityDataFormat.resolveAlgorithm("RSA_v1dot5")); + assertEquals(XMLCipher.RSA_OAEP, XMLSecurityDataFormat.resolveAlgorithm("RSA_OAEP")); + assertEquals(XMLCipher.RSA_OAEP_11, XMLSecurityDataFormat.resolveAlgorithm("RSA_OAEP_11")); + assertEquals(XMLCipher.SHA1, XMLSecurityDataFormat.resolveAlgorithm("SHA1")); + assertEquals(XMLCipher.SHA256, XMLSecurityDataFormat.resolveAlgorithm("SHA256")); + assertEquals(XMLCipher.SHA512, XMLSecurityDataFormat.resolveAlgorithm("SHA512")); + assertEquals(EncryptionConstants.MGF1_SHA1, XMLSecurityDataFormat.resolveAlgorithm("MGF1_SHA1")); + assertEquals(EncryptionConstants.MGF1_SHA256, XMLSecurityDataFormat.resolveAlgorithm("MGF1_SHA256")); + assertEquals(EncryptionConstants.MGF1_SHA512, XMLSecurityDataFormat.resolveAlgorithm("MGF1_SHA512")); + } + + @Test + void resolveAlgorithmPassesThroughUri() { + // raw W3C URIs must pass through unchanged so existing routes keep working + assertEquals(XMLCipher.AES_256_GCM, XMLSecurityDataFormat.resolveAlgorithm(XMLCipher.AES_256_GCM)); + assertEquals(XMLCipher.RSA_OAEP, XMLSecurityDataFormat.resolveAlgorithm(XMLCipher.RSA_OAEP)); + assertEquals(XMLCipher.SHA256, XMLSecurityDataFormat.resolveAlgorithm(XMLCipher.SHA256)); + } + + @Test + void resolveAlgorithmHandlesNull() { + assertEquals(null, XMLSecurityDataFormat.resolveAlgorithm(null)); + } + + // -- end-to-end encrypt/decrypt tests using constant names ---------------------------- + + /** + * Symmetric AES-256-GCM using the constant name "AES_256_GCM" (the YAML DSL schema value). + */ + @Test + void testAES256GCMByConstantName() throws Exception { + KeyGenerator keygen = KeyGenerator.getInstance("AES"); + keygen.init(256); + SecretKey key = keygen.generateKey(); + + XMLSecurityDataFormat df = new XMLSecurityDataFormat(); + df.setPassPhrase(key.getEncoded()); + df.setSecureTagContents(true); + df.setSecureTag("//cheesesites/italy/cheese"); + // Use the constant name, not the URI + df.setXmlCipherAlgorithm("AES_256_GCM"); + // In this backport the setter is a plain assignment; resolution happens lazily at crypto + // call-sites via the private resolvedXxx() helpers. The encrypt/decrypt at the end of + // this method is the real functional validation. + + context.addRoutes(new RouteBuilder() { + public void configure() { + from("direct:start") + .marshal(df).to("mock:encrypted") + .log("Body: + ${body}") Review Comment: **Nit: log message has a stray `+` literal.** `"Body: + ${body}"` produces output like `Body: + <xml...>` — the `+` is a literal character in the string, not a concatenation. Repeated on lines 117, 144, 184, and 227. ```suggestion .log("Body: ${body}") ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
