|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.streampark.console.core.service.impl; |
| 19 | + |
| 20 | +import org.apache.streampark.console.base.exception.ApiAlertException; |
| 21 | +import org.apache.streampark.console.core.entity.Application; |
| 22 | +import org.apache.streampark.console.core.entity.Project; |
| 23 | +import org.apache.streampark.console.core.service.ProjectService; |
| 24 | + |
| 25 | +import org.junit.jupiter.api.BeforeEach; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 28 | +import org.junit.jupiter.api.io.TempDir; |
| 29 | +import org.mockito.Mock; |
| 30 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 31 | +import org.springframework.test.util.ReflectionTestUtils; |
| 32 | + |
| 33 | +import java.io.File; |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.Path; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 39 | +import static org.mockito.Mockito.when; |
| 40 | + |
| 41 | +@ExtendWith(MockitoExtension.class) |
| 42 | +class ApplicationServiceImplTest { |
| 43 | + |
| 44 | + @Mock private ProjectService projectService; |
| 45 | + |
| 46 | + private final ApplicationServiceImpl applicationService = new ApplicationServiceImpl(); |
| 47 | + |
| 48 | + @TempDir private Path tempDir; |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + void setUp() { |
| 52 | + ReflectionTestUtils.setField(applicationService, "projectService", projectService); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + void getReadableConfFileShouldAllowConfigUnderProjectModuleConf() throws Exception { |
| 57 | + Path distHome = Files.createDirectory(tempDir.resolve("dist")); |
| 58 | + Path configFile = |
| 59 | + Files.createDirectories(distHome.resolve("app").resolve("conf")) |
| 60 | + .resolve("application.yaml"); |
| 61 | + Files.write(configFile, "key: value".getBytes()); |
| 62 | + |
| 63 | + when(projectService.getById(1L)).thenReturn(project(1L, 10L, distHome)); |
| 64 | + |
| 65 | + Application application = application(1L, 10L, "app.tar.gz", configFile); |
| 66 | + |
| 67 | + assertThat(applicationService.getReadableConfFile(application)) |
| 68 | + .isEqualTo(configFile.toFile().getCanonicalFile()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void getReadableConfFileShouldRejectConfigOutsideProjectModuleConf() throws Exception { |
| 73 | + Path distHome = Files.createDirectory(tempDir.resolve("dist")); |
| 74 | + Files.createDirectories(distHome.resolve("app").resolve("conf")); |
| 75 | + Path secretFile = Files.write(tempDir.resolve("secret.txt"), "secret".getBytes()); |
| 76 | + |
| 77 | + when(projectService.getById(1L)).thenReturn(project(1L, 10L, distHome)); |
| 78 | + |
| 79 | + Application application = application(1L, 10L, "app.tar.gz", secretFile); |
| 80 | + |
| 81 | + assertThatThrownBy(() -> applicationService.getReadableConfFile(application)) |
| 82 | + .isInstanceOf(ApiAlertException.class) |
| 83 | + .hasMessage("Invalid config."); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void getReadableConfFileShouldRejectModuleTraversal() throws Exception { |
| 88 | + Path distHome = Files.createDirectory(tempDir.resolve("dist")); |
| 89 | + Path outsideModule = Files.createDirectories(tempDir.resolve("outside").resolve("conf")); |
| 90 | + Path configFile = |
| 91 | + Files.write(outsideModule.resolve("application.yaml"), "key: value".getBytes()); |
| 92 | + |
| 93 | + Application application = application(1L, 10L, "../outside.tar.gz", configFile); |
| 94 | + |
| 95 | + assertThatThrownBy(() -> applicationService.getReadableConfFile(application)) |
| 96 | + .isInstanceOf(ApiAlertException.class) |
| 97 | + .hasMessage("Invalid module."); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + void getReadableConfFileShouldRejectModulePathAlias() throws Exception { |
| 102 | + Path distHome = Files.createDirectory(tempDir.resolve("dist")); |
| 103 | + Path configFile = |
| 104 | + Files.createDirectories(distHome.resolve("app").resolve("conf")) |
| 105 | + .resolve("application.yaml"); |
| 106 | + Files.write(configFile, "key: value".getBytes()); |
| 107 | + |
| 108 | + Application application = application(1L, 10L, "app/conf/..", configFile); |
| 109 | + |
| 110 | + assertThatThrownBy(() -> applicationService.getReadableConfFile(application)) |
| 111 | + .isInstanceOf(ApiAlertException.class) |
| 112 | + .hasMessage("Invalid module."); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void getReadableConfFileShouldRejectProjectFromOtherTeam() throws Exception { |
| 117 | + Path distHome = Files.createDirectory(tempDir.resolve("dist")); |
| 118 | + Path configFile = |
| 119 | + Files.createDirectories(distHome.resolve("app").resolve("conf")) |
| 120 | + .resolve("application.yaml"); |
| 121 | + Files.write(configFile, "key: value".getBytes()); |
| 122 | + |
| 123 | + when(projectService.getById(1L)).thenReturn(project(1L, 20L, distHome)); |
| 124 | + |
| 125 | + Application application = application(1L, 10L, "app.tar.gz", configFile); |
| 126 | + |
| 127 | + assertThatThrownBy(() -> applicationService.getReadableConfFile(application)) |
| 128 | + .isInstanceOf(ApiAlertException.class) |
| 129 | + .hasMessage("Invalid project."); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + void getYarnNameShouldReadOnlyValidatedConfig() throws Exception { |
| 134 | + Path distHome = Files.createDirectory(tempDir.resolve("dist")); |
| 135 | + Path configFile = |
| 136 | + Files.createDirectories(distHome.resolve("app").resolve("conf")) |
| 137 | + .resolve("application.properties"); |
| 138 | + Files.write(configFile, "flink.property.pipeline.name=test-app".getBytes()); |
| 139 | + |
| 140 | + when(projectService.getById(1L)).thenReturn(project(1L, 10L, distHome)); |
| 141 | + |
| 142 | + Application application = application(1L, 10L, "app.tar.gz", configFile); |
| 143 | + |
| 144 | + assertThat(applicationService.getYarnName(application)).isEqualTo("test-app"); |
| 145 | + } |
| 146 | + |
| 147 | + private Application application(Long projectId, Long teamId, String module, Path configFile) { |
| 148 | + Application application = new Application(); |
| 149 | + application.setProjectId(projectId); |
| 150 | + application.setTeamId(teamId); |
| 151 | + application.setModule(module); |
| 152 | + application.setConfig(configFile.toString()); |
| 153 | + return application; |
| 154 | + } |
| 155 | + |
| 156 | + private Project project(Long id, Long teamId, Path distHome) { |
| 157 | + return new Project() { |
| 158 | + { |
| 159 | + setId(id); |
| 160 | + setTeamId(teamId); |
| 161 | + } |
| 162 | + |
| 163 | + @Override |
| 164 | + public File getDistHome() { |
| 165 | + return distHome.toFile(); |
| 166 | + } |
| 167 | + }; |
| 168 | + } |
| 169 | +} |
0 commit comments