From 156b28b0fe1b1aa1df69029dc687cac2fcd41d76 Mon Sep 17 00:00:00 2001 From: Tomas Langer Date: Fri, 3 Jun 2022 21:46:14 +0200 Subject: [PATCH] Deserialization disabled by default. (#4334) Added serial config where required. MP Config uses known class for converters (to avoid lambdas and complex deserialization) --- .../helidon/common/SerializationConfig.java | 12 ++++-- .../common/SerializationConfigTest.java | 8 ++-- .../io/helidon/config/mp/MpConfigBuilder.java | 39 ++++++++++++++++++- .../io/helidon/config/mp/MpConfigImpl.java | 15 +------ .../META-INF/helidon/serial-config.properties | 19 +++++++++ .../src/main/resources/logging.properties | 7 ---- .../META-INF/helidon/serial-config.properties | 18 +++++++++ .../META-INF/helidon/serial-config.properties | 18 +++++++++ 8 files changed, 104 insertions(+), 32 deletions(-) create mode 100644 examples/jbatch/src/main/resources/META-INF/helidon/serial-config.properties create mode 100644 microprofile/config/src/main/resources/META-INF/helidon/serial-config.properties create mode 100644 microprofile/tests/tck/tck-config/src/test/resources/META-INF/helidon/serial-config.properties diff --git a/common/common/src/main/java/io/helidon/common/SerializationConfig.java b/common/common/src/main/java/io/helidon/common/SerializationConfig.java index 0212818cb6b..4b6582931d6 100644 --- a/common/common/src/main/java/io/helidon/common/SerializationConfig.java +++ b/common/common/src/main/java/io/helidon/common/SerializationConfig.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Oracle and/or its affiliates. + * Copyright (c) 2021, 2022 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -126,7 +126,7 @@ public static Builder builder() { * This is a one-off call to set up global filter. */ public static void configureRuntime() { - builder().build().configure(); + builder().onNoConfig(Action.CONFIGURE).build().configure(); } /** @@ -366,8 +366,8 @@ public enum TraceOption { * {@link SerializationConfig#configureRuntime()} directly. */ public static class Builder implements io.helidon.common.Builder { - private Action onWrongConfig = configuredAction(PROP_WRONG_CONFIG_ACTION, Action.WARN); - private Action onNoConfig = configuredAction(PROP_NO_CONFIG_ACTION, Action.WARN); + private Action onWrongConfig = configuredAction(PROP_WRONG_CONFIG_ACTION, Action.FAIL); + private Action onNoConfig = configuredAction(PROP_NO_CONFIG_ACTION, Action.FAIL); private String filterPattern = System.getProperty(PROP_PATTERN); private TraceOption traceSerialization = configuredTrace(TraceOption.NONE); private boolean ignoreFiles = Boolean.getBoolean(PROP_IGNORE_FILES); @@ -621,6 +621,10 @@ public Status checkInput(FilterInfo filterInfo) { } Status result = delegate.checkInput(filterInfo); + if (clazz == null) { + return result; + } + if (!reportedClasses.add(clazz)) { if (basic) { return result; diff --git a/common/common/src/test/java/io/helidon/common/SerializationConfigTest.java b/common/common/src/test/java/io/helidon/common/SerializationConfigTest.java index 982dfe634fa..c182e27b27d 100644 --- a/common/common/src/test/java/io/helidon/common/SerializationConfigTest.java +++ b/common/common/src/test/java/io/helidon/common/SerializationConfigTest.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Oracle and/or its affiliates. + * Copyright (c) 2021, 2022 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -34,10 +34,8 @@ void testDefaults() { SerializationConfig.ConfigOptions options = serializationConfig.options(); assertThat(options.traceSerialization(), is(SerializationConfig.TraceOption.NONE)); - // TODO this will change in 3.0.0 - assertThat(options.onNoConfig(), is(SerializationConfig.Action.WARN)); - // TODO this will change in 3.0.0 - assertThat(options.onWrongConfig(), is(SerializationConfig.Action.WARN)); + assertThat(options.onNoConfig(), is(SerializationConfig.Action.FAIL)); + assertThat(options.onWrongConfig(), is(SerializationConfig.Action.FAIL)); assertThat(options.filterPattern(), is("!*")); } diff --git a/config/config-mp/src/main/java/io/helidon/config/mp/MpConfigBuilder.java b/config/config-mp/src/main/java/io/helidon/config/mp/MpConfigBuilder.java index a32511cb44e..26e6e339efb 100644 --- a/config/config-mp/src/main/java/io/helidon/config/mp/MpConfigBuilder.java +++ b/config/config-mp/src/main/java/io/helidon/config/mp/MpConfigBuilder.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2021 Oracle and/or its affiliates. + * Copyright (c) 2020, 2022 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -668,11 +668,46 @@ private static class OrdinalConverter { private OrdinalConverter(Converter converter, Class aClass, int ordinal) { this.ordinal = ordinal; this.type = aClass; - this.converter = converter; + this.converter = new NullCheckingConverter<>(aClass, converter); } private OrdinalConverter(Converter converter) { this(converter, getConverterType(converter.getClass()), Priorities.find(converter, 100)); } + + @Override + public String toString() { + return type.getName() + "->" + converter; + } + } + + private static final class NullCheckingConverter implements Converter { + private final Converter delegate; + private final Class type; + + private NullCheckingConverter(Class type, Converter delegate) { + this.delegate = delegate; + this.type = type; + } + + @Override + public T convert(String value) throws IllegalArgumentException, NullPointerException { + if (value == null) { + throw new NullPointerException("Null not allowed in MP converters. Converter for type " + type.getName()); + } + + try { + return delegate.convert(value); + } catch (IllegalArgumentException | NullPointerException e) { + throw e; + } catch (Exception e) { + throw new IllegalArgumentException("Cannot convert value", e); + } + } + + @Override + public String toString() { + return type.getName() + "->" + delegate; + } } } diff --git a/config/config-mp/src/main/java/io/helidon/config/mp/MpConfigImpl.java b/config/config-mp/src/main/java/io/helidon/config/mp/MpConfigImpl.java index 6de36a0a5c0..3b57c7f108b 100644 --- a/config/config-mp/src/main/java/io/helidon/config/mp/MpConfigImpl.java +++ b/config/config-mp/src/main/java/io/helidon/config/mp/MpConfigImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020, 2021 Oracle and/or its affiliates. + * Copyright (c) 2020, 2022 Oracle and/or its affiliates. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -260,19 +260,6 @@ private Optional> findComponentConverter(Class type) { .findFirst() .map(Map.Entry::getValue) .map(it -> (Converter) it) - .map(it -> (Converter) value -> { - if (value == null) { - throw new NullPointerException("Null not allowed in MP converters. Converter for type " + forType - .getName()); - } - try { - return it.convert(value); - } catch (IllegalArgumentException e) { - throw e; - } catch (Exception e) { - throw new IllegalArgumentException("Cannot convert value", e); - } - }) .or(() -> findImplicit(forType)); } diff --git a/examples/jbatch/src/main/resources/META-INF/helidon/serial-config.properties b/examples/jbatch/src/main/resources/META-INF/helidon/serial-config.properties new file mode 100644 index 00000000000..8aad930d0ed --- /dev/null +++ b/examples/jbatch/src/main/resources/META-INF/helidon/serial-config.properties @@ -0,0 +1,19 @@ +# +# Copyright (c) 2022 Oracle and/or its affiliates. +# +# Licensed 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. +# + +# The JBatch uses Serialization a lot, and these are all required +pattern=com.ibm.jbatch.**;jakarta.batch.runtime.BatchStatus;java.lang.Enum;\ + java.util.Properties;java.util.Hashtable;java.util.Map$Entry diff --git a/examples/jbatch/src/main/resources/logging.properties b/examples/jbatch/src/main/resources/logging.properties index 8fad3a4fb05..930fbd49781 100644 --- a/examples/jbatch/src/main/resources/logging.properties +++ b/examples/jbatch/src/main/resources/logging.properties @@ -28,10 +28,3 @@ java.util.logging.SimpleFormatter.format=%1$tY.%1$tm.%1$td %1$tH:%1$tM:%1$tS %4$ # Quiet Weld org.jboss.level=WARNING - -# Component specific log levels -#io.helidon.webserver.level=INFO -#io.helidon.config.level=INFO -#io.helidon.security.level=INFO -#io.helidon.common.level=INFO -#io.netty.level=INFO diff --git a/microprofile/config/src/main/resources/META-INF/helidon/serial-config.properties b/microprofile/config/src/main/resources/META-INF/helidon/serial-config.properties new file mode 100644 index 00000000000..ff0a8d4cef0 --- /dev/null +++ b/microprofile/config/src/main/resources/META-INF/helidon/serial-config.properties @@ -0,0 +1,18 @@ +# +# Copyright (c) 2022 Oracle and/or its affiliates. +# +# Licensed 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. +# + +pattern=org.jboss.weld.bean.proxy.util.SerializableClientProxy;org.jboss.weld.bean.StringBeanIdentifier;\ + org.eclipse.microprofile.config.Config$_$$_WeldClientProxy;io.helidon.config.mp.MpConfigBuilder$NullCheckingConverter diff --git a/microprofile/tests/tck/tck-config/src/test/resources/META-INF/helidon/serial-config.properties b/microprofile/tests/tck/tck-config/src/test/resources/META-INF/helidon/serial-config.properties new file mode 100644 index 00000000000..af211a1e196 --- /dev/null +++ b/microprofile/tests/tck/tck-config/src/test/resources/META-INF/helidon/serial-config.properties @@ -0,0 +1,18 @@ +# +# Copyright (c) 2022 Oracle and/or its affiliates. +# +# Licensed 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. +# + +# TCK converter to validate deserialization +pattern=org.eclipse.microprofile.config.tck.converters.DuckConverter;org.eclipse.microprofile.config.tck.converters.Duck