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

JEP-290, SerialConfig support #3201

Merged
merged 4 commits into from
Aug 17, 2021
Merged
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
637 changes: 637 additions & 0 deletions common/common/src/main/java/io/helidon/common/SerializationConfig.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"resources": [
{
"pattern": "META-INF/helidon/serial-config.properties"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/*
* Copyright (c) 2021 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.
*/

package io.helidon.common;

import java.util.Properties;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

/**
* Make sure you never call configure or configureRuntime, as that modifies the shape of
* current JRE without possibility to reverse it.
*/
class SerializationConfigTest {
@Test
void testDefaults() {
SerializationConfig serializationConfig = SerializationConfig.builder().build();

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.filterPattern(), is("!*"));
}

@Test
void testBuilder() {
SerializationConfig serializationConfig = SerializationConfig.builder()
.ignoreFiles(true)
.filterPattern(SerializationConfigTest.class.getName())
.onNoConfig(SerializationConfig.Action.IGNORE)
.onWrongConfig(SerializationConfig.Action.CONFIGURE)
.traceSerialization(SerializationConfig.TraceOption.FULL)
.build();

SerializationConfig.ConfigOptions options = serializationConfig.options();
assertThat(options.traceSerialization(), is(SerializationConfig.TraceOption.FULL));
assertThat(options.onNoConfig(), is(SerializationConfig.Action.IGNORE));
assertThat(options.onWrongConfig(), is(SerializationConfig.Action.CONFIGURE));
assertThat(options.filterPattern(), is(SerializationConfigTest.class.getName() + ";!*"));
}

@Test
void testSysProps() {
try {
System.setProperty(SerializationConfig.PROP_PATTERN, SerializationConfigTest.class.getName());
System.setProperty(SerializationConfig.PROP_NO_CONFIG_ACTION, "ignore");
System.setProperty(SerializationConfig.PROP_WRONG_CONFIG_ACTION, "configure");
System.setProperty(SerializationConfig.PROP_TRACE, "full");
System.setProperty(SerializationConfig.PROP_IGNORE_FILES, "true");

SerializationConfig serializationConfig = SerializationConfig.builder()
.ignoreFiles(true)
.filterPattern(SerializationConfigTest.class.getName())
.onNoConfig(SerializationConfig.Action.IGNORE)
.onWrongConfig(SerializationConfig.Action.CONFIGURE)
.traceSerialization(SerializationConfig.TraceOption.FULL)
.build();

SerializationConfig.ConfigOptions options = serializationConfig.options();
assertThat(options.traceSerialization(), is(SerializationConfig.TraceOption.FULL));
assertThat(options.onNoConfig(), is(SerializationConfig.Action.IGNORE));
assertThat(options.onWrongConfig(), is(SerializationConfig.Action.CONFIGURE));
assertThat(options.filterPattern(), is(SerializationConfigTest.class.getName() + ";!*"));
} finally {
Properties properties = System.getProperties();

properties.remove(SerializationConfig.PROP_PATTERN);
properties.remove(SerializationConfig.PROP_NO_CONFIG_ACTION);
properties.remove(SerializationConfig.PROP_WRONG_CONFIG_ACTION);
properties.remove(SerializationConfig.PROP_TRACE);
properties.remove(SerializationConfig.PROP_IGNORE_FILES);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ grpc:
java:
enabled: true

mp.initializer:
allow: true
no-warn: true
4 changes: 4 additions & 0 deletions integrations/db/h2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
</properties>

<dependencies>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions integrations/db/ojdbc/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
</properties>

<dependencies>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common</artifactId>
</dependency>
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8-production</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright (c) 2021 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=oracle.sql.converter.*
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import io.helidon.common.HelidonFeatures;
import io.helidon.common.HelidonFlavor;
import io.helidon.common.LogConfig;
import io.helidon.common.SerializationConfig;
import io.helidon.common.Version;
import io.helidon.common.context.Context;
import io.helidon.common.context.Contexts;
Expand Down Expand Up @@ -244,6 +245,7 @@ public SeContainer start() {
// already started
return cdi;
}
SerializationConfig.configureRuntime();
LogConfig.configureRuntime();
try {
Contexts.runInContext(ROOT_CONTEXT, this::doStart);
Expand Down
52 changes: 52 additions & 0 deletions tests/integration/jep290/check_f_f_ok/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Copyright (c) 2019, 2021 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.
-->

<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>io.helidon.tests.integration.jep290</groupId>
<artifactId>helidon-tests-integration-jep290-project</artifactId>
<version>2.4.0-SNAPSHOT</version>
</parent>

<artifactId>helidon-tests-integration-jep290-check_f_f_ok</artifactId>
<name>Helidon Tests Integration JEP-290 check_f_f_ok</name>

<description>
Explicit pattern configured using java, fail on wrong config and pattern is ok.
</description>

<dependencies>
<dependency>
<groupId>io.helidon.common</groupId>
<artifactId>helidon-common</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2021 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.
*/

package io.helidon.tests.integration.jep290.checkffok;

import java.io.Serializable;

class Configured implements Serializable {
private static final long serialVersionUID = 12L;

private final String text;

Configured(String text) {
this.text = text;
}

String text() {
return text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Copyright (c) 2021 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.
*/

package io.helidon.tests.integration.jep290.checkffok;

import java.io.Serializable;

class NotConfigured implements Serializable {
private static final long serialVersionUID = 12L;

private String text;

NotConfigured(String text) {
this.text = text;
}

String text() {
return text;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2021 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.
*/

package io.helidon.tests.integration.jep290.checkffok;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.io.ObjectInputFilter;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Random;

import io.helidon.common.SerializationConfig;

import org.hamcrest.CoreMatchers;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

class DeserializationTest {
private static final String TEST_STRING = "Hello_" + new Random().nextInt(10);

@BeforeAll
static void configureDeserialization() {

ObjectInputFilter myFilter = filterInfo -> {
if (filterInfo.serialClass() == null) {
return ObjectInputFilter.Status.UNDECIDED;
}
if (filterInfo.serialClass().equals(Configured.class)) {
return ObjectInputFilter.Status.ALLOWED;
}
return ObjectInputFilter.Status.REJECTED;
};
ObjectInputFilter.Config.setSerialFilter(myFilter);
SerializationConfig sc = SerializationConfig.builder()
.onWrongConfig(SerializationConfig.Action.FAIL)
.onNoConfig(SerializationConfig.Action.IGNORE)
.ignoreFiles(false)
.traceSerialization(SerializationConfig.TraceOption.NONE)
.build();

// should not fail, as we have a blacklist for all other
sc.configure();
}

@Test
void testConfigured() throws IOException, ClassNotFoundException {
Configured object = new Configured(TEST_STRING);

ByteArrayOutputStream ob = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ob);

oos.writeObject(object);
oos.close();

byte[] bytes = ob.toByteArray();

ByteArrayInputStream ib = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(ib);
Object o = ois.readObject();

assertThat(o, CoreMatchers.instanceOf(Configured.class));

object = (Configured) o;

assertThat(object.text(), is(TEST_STRING));
}

@Test
void testNotConfigured() throws IOException, ClassNotFoundException {
NotConfigured object = new NotConfigured(TEST_STRING);

ByteArrayOutputStream ob = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(ob);

oos.writeObject(object);
oos.close();

byte[] bytes = ob.toByteArray();

ByteArrayInputStream ib = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(ib);

// should be excluded
assertThrows(InvalidClassException.class, ois::readObject);
}
}
Loading