Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue#120 fix memory leak in busmap #129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/no/difi/oxalis/as4/util/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public class Constants {

public static final String TEST_SERVICE = "http://docs.oasis-open.org/ebxml-msg/ebms/v3.0/ns/core/200704/service";
public static final String TEST_ACTION = "http://docs.oasis-open.org/ebxml-msg/ebms/v3.0/ns/core/200704/test";

public static final String OXALIS_ALGORITHM_NAMESPACE = "http://oxalis.difi.no/custom/security-policy";
public static final String BASIC_128_GCM_SHA_256 = "Basic128GCMSha256";
public static final String BASIC_128_GCM_SHA_256_MGF_SHA_256 = "Basic128GCMSha256MgfSha256";
}
Original file line number Diff line number Diff line change
@@ -1,55 +1,32 @@
package no.difi.oxalis.as4.util;

import static no.difi.oxalis.as4.util.Constants.BASIC_128_GCM_SHA_256;
import static no.difi.oxalis.as4.util.Constants.BASIC_128_GCM_SHA_256_MGF_SHA_256;
import static no.difi.oxalis.as4.util.Constants.OXALIS_ALGORITHM_NAMESPACE;
import static org.apache.wss4j.common.WSS4JConstants.MGF_SHA256;

import lombok.extern.slf4j.Slf4j;
import org.apache.cxf.Bus;
import org.apache.cxf.ws.policy.AssertionBuilderRegistry;
import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion;
import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertionBuilder;
import org.apache.cxf.ws.security.policy.custom.AlgorithmSuiteLoader;
import org.apache.neethi.Assertion;
import org.apache.neethi.AssertionBuilderFactory;
import org.apache.neethi.Policy;
import org.apache.neethi.builders.xml.XMLPrimitiveAssertionBuilder;
import org.apache.wss4j.common.WSS4JConstants;
import org.apache.wss4j.policy.SPConstants;
import org.apache.wss4j.policy.model.AbstractSecurityAssertion;
import org.apache.wss4j.policy.model.AlgorithmSuite;
import org.w3c.dom.Element;

import javax.xml.namespace.QName;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static org.apache.wss4j.common.WSS4JConstants.MGF_SHA256;


// Based on from CEF e-delivery Domibus
// https://ec.europa.eu/cefdigital/code/projects/EDELIVERY/repos/domibus/browse/Domibus-MSH/src/main/java/eu/domibus/ebms3/security/custom/DomibusAlgorithmSuiteLoader.java
@Slf4j
public class OxalisAlgorithmSuiteLoader implements AlgorithmSuiteLoader {

public static final String OXALIS_ALGORITHM_NAMESPACE = "http://oxalis.difi.no/custom/security-policy";
public static final String AES128_GCM_ALGORITHM = "http://www.w3.org/2009/xmlenc11#aes128-gcm";
public static final String BASIC_128_GCM_SHA_256 = "Basic128GCMSha256";
public static final String BASIC_128_GCM_SHA_256_MGF_SHA_256 = "Basic128GCMSha256MgfSha256";

private static final Map<String, Bus> BUS_MAP = new ConcurrentHashMap<>();

public OxalisAlgorithmSuiteLoader(final Bus bus) {
BUS_MAP.computeIfAbsent(bus.getId(), id -> {
AlgorithmSuiteLoader algorithmSuiteLoader = bus.getExtension(AlgorithmSuiteLoader.class);

if (algorithmSuiteLoader instanceof OxalisAlgorithmSuiteLoader) {
log.info("Cached OxalisAlgorithmSuite on bus {}", bus.getId());
} else {
log.info("Registering OxalisAlgorithmSuite on bus {}", bus.getId());
bus.setExtension(this, AlgorithmSuiteLoader.class);
register(bus);
}

return bus;
});
bus.setExtension(this, AlgorithmSuiteLoader.class);
register(bus);
}

public AlgorithmSuite getAlgorithmSuite(final Bus bus, final SPConstants.SPVersion version, final Policy nestedPolicy) {
Expand All @@ -59,23 +36,7 @@ public AlgorithmSuite getAlgorithmSuite(final Bus bus, final SPConstants.SPVersi
private void register(final Bus bus) {
final AssertionBuilderRegistry reg = bus.getExtension(AssertionBuilderRegistry.class);
if (reg != null) {
final Map<QName, Assertion> assertions = new HashMap<>();
QName qName = new QName(OXALIS_ALGORITHM_NAMESPACE, BASIC_128_GCM_SHA_256);
assertions.put(qName, new PrimitiveAssertion(qName));
qName = new QName(OXALIS_ALGORITHM_NAMESPACE, BASIC_128_GCM_SHA_256_MGF_SHA_256);
assertions.put(qName, new PrimitiveAssertion(qName));

reg.registerBuilder(new PrimitiveAssertionBuilder(assertions.keySet()) {
@Override
public Assertion build(final Element element, final AssertionBuilderFactory fact) {
if (XMLPrimitiveAssertionBuilder.isOptional(element)
|| XMLPrimitiveAssertionBuilder.isIgnorable(element)) {
return super.build(element, fact);
}
final QName q = new QName(element.getNamespaceURI(), element.getLocalName());
return assertions.get(q);
}
});
reg.registerBuilder(new OxalisAssertionBuilder());
}
}

Expand Down Expand Up @@ -127,7 +88,7 @@ protected AbstractSecurityAssertion cloneAssertion(final Policy nestedPolicy) {
protected void parseCustomAssertion(final Assertion assertion) {
final String assertionName = assertion.getName().getLocalPart();
final String assertionNamespace = assertion.getName().getNamespaceURI();
if (!OxalisAlgorithmSuiteLoader.OXALIS_ALGORITHM_NAMESPACE.equals(assertionNamespace)) {
if (!OXALIS_ALGORITHM_NAMESPACE.equals(assertionNamespace)) {
return;
}

Expand Down
41 changes: 41 additions & 0 deletions src/main/java/no/difi/oxalis/as4/util/OxalisAssertionBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package no.difi.oxalis.as4.util;

import static no.difi.oxalis.as4.util.Constants.BASIC_128_GCM_SHA_256;
import static no.difi.oxalis.as4.util.Constants.BASIC_128_GCM_SHA_256_MGF_SHA_256;
import static no.difi.oxalis.as4.util.Constants.OXALIS_ALGORITHM_NAMESPACE;

import java.util.HashMap;
import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertion;
import org.apache.cxf.ws.policy.builder.primitive.PrimitiveAssertionBuilder;
import org.apache.neethi.Assertion;
import org.apache.neethi.AssertionBuilderFactory;
import org.apache.neethi.builders.xml.XMLPrimitiveAssertionBuilder;
import org.w3c.dom.Element;


/**
* @author Jonas Hysing Øvrebø (pearl consulting)
*/
public class OxalisAssertionBuilder extends PrimitiveAssertionBuilder {
private static final Map<QName, Assertion> ASSERTION_MAP = new HashMap<>(2);
static {
final QName basic128GCMSha256QName = new QName(OXALIS_ALGORITHM_NAMESPACE, BASIC_128_GCM_SHA_256);
final QName basic128GCMSha256MgfSha256QName = new QName(OXALIS_ALGORITHM_NAMESPACE, BASIC_128_GCM_SHA_256_MGF_SHA_256);

ASSERTION_MAP.put(basic128GCMSha256QName, new PrimitiveAssertion(basic128GCMSha256QName));
ASSERTION_MAP.put(basic128GCMSha256MgfSha256QName, new PrimitiveAssertion(basic128GCMSha256MgfSha256QName));
}

@Override
public Assertion build(final Element element, final AssertionBuilderFactory fact) {
if (XMLPrimitiveAssertionBuilder.isOptional(element) || XMLPrimitiveAssertionBuilder.isIgnorable(element)) {
return super.build(element, fact);
}
final QName q = new QName(element.getNamespaceURI(), element.getLocalName());
return ASSERTION_MAP.get(q);
}
}