新增DM的TrustedDmFunctionEnum枚举

This commit is contained in:
zhuyijun 2024-08-22 18:14:44 +08:00
parent 528c33a9fb
commit b6b58ad584
3 changed files with 69 additions and 2 deletions

View File

@ -0,0 +1,66 @@
/*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* 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 com.alibaba.nacos.plugin.datasource.enums.dm;
import java.util.HashMap;
import java.util.Map;
/**
* The TrustedSqlFunctionEnum enum class is used to enumerate and manage a list of trusted built-in SQL functions.
* By using this enum, you can verify whether a given SQL function is part of the trusted functions list
* to avoid potential SQL injection risks.
*
* @author blake.qiu
*/
public enum TrustedDmFunctionEnum {
/**
* NOW().
*/
NOW("NOW()", "NOW(3)");
private static final Map<String, TrustedDmFunctionEnum> LOOKUP_MAP = new HashMap<>();
static {
for (TrustedDmFunctionEnum entry : TrustedDmFunctionEnum.values()) {
LOOKUP_MAP.put(entry.functionName, entry);
}
}
private final String functionName;
private final String function;
TrustedDmFunctionEnum(String functionName, String function) {
this.functionName = functionName;
this.function = function;
}
/**
* Get the function name.
*
* @param functionName function name
* @return function
*/
public static String getFunctionByName(String functionName) {
TrustedDmFunctionEnum entry = LOOKUP_MAP.get(functionName);
if (entry != null) {
return entry.function;
}
throw new IllegalArgumentException(String.format("Invalid function name: %s", functionName));
}
}

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.plugin.datasource.impl.dm;
import com.alibaba.nacos.plugin.datasource.enums.dm.TrustedDmFunctionEnum;
import com.alibaba.nacos.plugin.datasource.enums.mysql.TrustedMysqlFunctionEnum;
import com.alibaba.nacos.plugin.datasource.mapper.AbstractMapper;
@ -28,6 +29,6 @@ public abstract class AbstractMapperByDm extends AbstractMapper {
@Override
public String getFunction(String functionName) {
return TrustedMysqlFunctionEnum.getFunctionByName(functionName);
return TrustedDmFunctionEnum.getFunctionByName(functionName);
}
}

View File

@ -24,7 +24,7 @@ import org.junit.jupiter.api.Test;
*
* @author blake.qiu
*/
public class TrustedMysqlFunctionEnumTest {
public class TrustedDmFunctionEnumTest {
@Test
void testGetFunctionByName() {