From 43fe2b2e060371536021c95272269cd8ecdf5dcd Mon Sep 17 00:00:00 2001 From: Name Date: Thu, 25 Apr 2024 14:11:37 +0800 Subject: [PATCH] [ISSUE #11967] Can not aquire the specific config --- .../dump/disk/ConfigRawDiskService.java | 13 ++++ .../dump/disk/ConfigRawDiskServiceTest.java | 78 +++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 config/src/test/java/com/alibaba/nacos/config/server/service/dump/disk/ConfigRawDiskServiceTest.java diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/disk/ConfigRawDiskService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/disk/ConfigRawDiskService.java index 099efeaebe9..a9e34250710 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/dump/disk/ConfigRawDiskService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/dump/disk/ConfigRawDiskService.java @@ -17,6 +17,7 @@ package com.alibaba.nacos.config.server.service.dump.disk; import com.alibaba.nacos.api.utils.StringUtils; +import com.alibaba.nacos.common.pathencoder.PathEncoderManager; import com.alibaba.nacos.common.utils.IoUtils; import com.alibaba.nacos.config.server.utils.LogUtil; import com.alibaba.nacos.sys.env.EnvUtil; @@ -65,6 +66,10 @@ public void saveToDisk(String dataId, String group, String tenant, String conten * Returns the path of the server cache file. */ private static File targetFile(String dataId, String group, String tenant) { + // fix https://github.com/alibaba/nacos/issues/10067 + dataId = PathEncoderManager.getInstance().encode(dataId); + group = PathEncoderManager.getInstance().encode(group); + tenant = PathEncoderManager.getInstance().encode(tenant); File file = null; if (StringUtils.isBlank(tenant)) { file = new File(EnvUtil.getNacosHome(), BASE_DIR); @@ -81,6 +86,10 @@ private static File targetFile(String dataId, String group, String tenant) { * Returns the path of cache file in server. */ private static File targetBetaFile(String dataId, String group, String tenant) { + // fix https://github.com/alibaba/nacos/issues/10067 + dataId = PathEncoderManager.getInstance().encode(dataId); + group = PathEncoderManager.getInstance().encode(group); + tenant = PathEncoderManager.getInstance().encode(tenant); File file = null; if (StringUtils.isBlank(tenant)) { file = new File(EnvUtil.getNacosHome(), BETA_DIR); @@ -97,6 +106,10 @@ private static File targetBetaFile(String dataId, String group, String tenant) { * Returns the path of the tag cache file in server. */ private static File targetTagFile(String dataId, String group, String tenant, String tag) { + // fix https://github.com/alibaba/nacos/issues/10067 + dataId = PathEncoderManager.getInstance().encode(dataId); + group = PathEncoderManager.getInstance().encode(group); + tenant = PathEncoderManager.getInstance().encode(tenant); File file = null; if (StringUtils.isBlank(tenant)) { file = new File(EnvUtil.getNacosHome(), TAG_DIR); diff --git a/config/src/test/java/com/alibaba/nacos/config/server/service/dump/disk/ConfigRawDiskServiceTest.java b/config/src/test/java/com/alibaba/nacos/config/server/service/dump/disk/ConfigRawDiskServiceTest.java new file mode 100644 index 00000000000..4e6b207ac9e --- /dev/null +++ b/config/src/test/java/com/alibaba/nacos/config/server/service/dump/disk/ConfigRawDiskServiceTest.java @@ -0,0 +1,78 @@ +package com.alibaba.nacos.config.server.service.dump.disk; + +import junit.framework.TestCase; +import java.io.File; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class ConfigRawDiskServiceTest extends TestCase { + + /** + * 测试获取beta文件路径. + */ + public void testTargetFile() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Method method = ConfigRawDiskService.class.getDeclaredMethod("targetFile", String.class, String.class, String.class); + method.setAccessible(true); + File result = (File) method.invoke(null, "aaaa\\dsaknkf", "aaaa/dsaknkf", "aaaa:dsaknkf"); + // 分解路径 + Path path = Paths.get(result.getPath()); + Path parent = path.getParent(); + Path grandParent = parent.getParent(); + // 获取最后三段路径 + String lastSegment = path.getFileName().toString(); + String secondLastSegment = parent.getFileName().toString(); + String thirdLastSegment = grandParent.getFileName().toString(); + assertEquals("aaaa%A3%dsaknkf", thirdLastSegment); + assertEquals("aaaa%A2%dsaknkf", secondLastSegment); + assertEquals("aaaa%A1%dsaknkf", lastSegment); + } + + /** + * 测试获取beta文件路径. + */ + public void testTargetBetaFile() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Method method = ConfigRawDiskService.class.getDeclaredMethod("targetBetaFile", String.class, String.class, String.class); + method.setAccessible(true); + File result = (File) method.invoke(null, "aaaa\\dsaknkf", "aaaa/dsaknkf", "aaaa:dsaknkf"); + // 分解路径 + Path path = Paths.get(result.getPath()); + Path parent = path.getParent(); + Path grandParent = parent.getParent(); + // 获取最后三段路径 + String lastSegment = path.getFileName().toString(); + String secondLastSegment = parent.getFileName().toString(); + String thirdLastSegment = grandParent.getFileName().toString(); + assertEquals("aaaa%A3%dsaknkf", thirdLastSegment); + assertEquals("aaaa%A2%dsaknkf", secondLastSegment); + assertEquals("aaaa%A1%dsaknkf", lastSegment); + + } + + /** + * 测试获取tag文件路径. + * @throws NoSuchMethodException 方法不存在异常 + * @throws IllegalAccessException 非法访问异常 + * @throws InvocationTargetException 目标异常 + */ + public void testTargetTagFile() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + Method method = ConfigRawDiskService.class.getDeclaredMethod("targetTagFile", String.class, String.class, String.class, String.class); + method.setAccessible(true); + File result = (File) method.invoke(null, "aaaa\\dsaknkf", "aaaa/dsaknkf", "aaaa:dsaknkf", "aaaadsaknkf"); + // 分解路径 + Path path = Paths.get(result.getPath()); + Path parent = path.getParent(); + Path grandParent = parent.getParent(); + Path greatGrandParent = grandParent.getParent(); + // 获取最后四段路径 + String secondLastSegment = parent.getFileName().toString(); + String thirdLastSegment = grandParent.getFileName().toString(); + String fourthLastSegment = greatGrandParent.getFileName().toString(); + assertEquals("aaaa%A3%dsaknkf", fourthLastSegment); + assertEquals("aaaa%A2%dsaknkf", thirdLastSegment); + assertEquals("aaaa%A1%dsaknkf", secondLastSegment); + String lastSegment = path.getFileName().toString(); + assertEquals("aaaadsaknkf", lastSegment); + } +}