Skip to content

Commit

Permalink
Merge fd621d6 into 0419543
Browse files Browse the repository at this point in the history
  • Loading branch information
EricGao888 committed Apr 3, 2024
2 parents 0419543 + fd621d6 commit 4e2dd67
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 4 deletions.
@@ -0,0 +1,46 @@
/*
* 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.dolphinscheduler.common.utils;

import org.yaml.snakeyaml.LoaderOptions;
import org.yaml.snakeyaml.constructor.Constructor;

/**
* Whitelist constructor implementation for YAML snake.
* Copied from Apache ShardingSphere and Apache Skywalking.
*/
public final class ClassFilterConstructor extends Constructor {

private final Class<?>[] acceptClasses;

public ClassFilterConstructor(final Class<?>[] acceptClasses) {
super(new LoaderOptions());
this.acceptClasses = acceptClasses;
}

@Override
protected Class<?> getClassForName(final String name) throws ClassNotFoundException {
for (Class<? extends Object> each : acceptClasses) {
if (name.equals(each.getName())) {
return super.getClassForName(name);
}
}
throw new IllegalArgumentException(String.format("Class is not accepted: %s", name));
}
}
Expand Up @@ -17,12 +17,14 @@

package org.apache.dolphinscheduler.plugin.task.api.k8s;

import org.apache.dolphinscheduler.common.utils.ClassFilterConstructor;
import org.apache.dolphinscheduler.plugin.task.api.TaskException;
import org.apache.dolphinscheduler.plugin.task.api.TaskExecutionContext;
import org.apache.dolphinscheduler.plugin.task.api.model.TaskResponse;
import org.apache.dolphinscheduler.plugin.task.api.utils.K8sUtils;

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

import org.yaml.snakeyaml.Yaml;
Expand All @@ -36,7 +38,9 @@ public abstract class AbstractK8sTaskExecutor {
protected AbstractK8sTaskExecutor(TaskExecutionContext taskRequest) {
this.taskRequest = taskRequest;
this.k8sUtils = new K8sUtils();
this.yaml = new Yaml();
this.yaml = new Yaml(new ClassFilterConstructor(new Class[]{
List.class
}));
this.taskOutputParams = new HashMap<>();
}
public Map<String, String> getTaskOutputParams() {
Expand Down
Expand Up @@ -17,6 +17,7 @@

package org.apache.dolphinscheduler.plugin.task.api.loop.template.http.parser;

import org.apache.dolphinscheduler.common.utils.ClassFilterConstructor;
import org.apache.dolphinscheduler.plugin.task.api.loop.template.LoopTaskYamlDefinition;
import org.apache.dolphinscheduler.plugin.task.api.loop.template.TaskDefinitionParser;
import org.apache.dolphinscheduler.plugin.task.api.loop.template.http.HttpLoopTaskDefinition;
Expand All @@ -32,7 +33,6 @@
import lombok.NonNull;

import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import com.google.common.base.Preconditions;

Expand Down Expand Up @@ -60,9 +60,9 @@ public class HttpTaskDefinitionParser implements TaskDefinitionParser<HttpLoopTa
}

protected @NonNull LoopTaskYamlDefinition parseYamlConfigFile(@NonNull String yamlConfigFile) throws IOException {
Yaml yaml = new Yaml(new Constructor(LoopTaskYamlDefinition.class));
try (FileReader fileReader = new FileReader(yamlConfigFile)) {
return yaml.load(fileReader);
return new Yaml(new ClassFilterConstructor(new Class[]{LoopTaskYamlDefinition.class}))
.loadAs(fileReader, LoopTaskYamlDefinition.class);
}
}

Expand Down

0 comments on commit 4e2dd67

Please sign in to comment.