|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.tablefunction; |
| 19 | + |
| 20 | +import org.apache.doris.catalog.Env; |
| 21 | +import org.apache.doris.common.Config; |
| 22 | +import org.apache.doris.mysql.privilege.AccessControllerManager; |
| 23 | +import org.apache.doris.mysql.privilege.PrivPredicate; |
| 24 | +import org.apache.doris.nereids.exceptions.AnalysisException; |
| 25 | +import org.apache.doris.proto.InternalService.PGlobResponse; |
| 26 | +import org.apache.doris.proto.Types.PStatus; |
| 27 | +import org.apache.doris.qe.ConnectContext; |
| 28 | +import org.apache.doris.qe.SessionVariable; |
| 29 | +import org.apache.doris.resource.computegroup.ComputeGroup; |
| 30 | +import org.apache.doris.rpc.BackendServiceProxy; |
| 31 | +import org.apache.doris.system.Backend; |
| 32 | +import org.apache.doris.system.SystemInfoService; |
| 33 | +import org.apache.doris.thrift.TNetworkAddress; |
| 34 | + |
| 35 | +import com.google.common.collect.ImmutableMap; |
| 36 | +import org.junit.jupiter.api.AfterEach; |
| 37 | +import org.junit.jupiter.api.Assertions; |
| 38 | +import org.junit.jupiter.api.BeforeEach; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | +import org.mockito.MockedConstruction; |
| 41 | +import org.mockito.MockedStatic; |
| 42 | +import org.mockito.Mockito; |
| 43 | + |
| 44 | +import java.util.Collections; |
| 45 | +import java.util.Map; |
| 46 | +import java.util.concurrent.CompletableFuture; |
| 47 | + |
| 48 | +public class LocalTableValuedFunctionEnableTest { |
| 49 | + private static final Map<String, String> PROPERTIES = ImmutableMap.of( |
| 50 | + "file_path", "local_privilege_test.csv", "backend_id", "1", "format", "csv"); |
| 51 | + |
| 52 | + private final Env env = Mockito.mock(Env.class); |
| 53 | + private final ConnectContext context = Mockito.mock(ConnectContext.class); |
| 54 | + private final AccessControllerManager accessManager = Mockito.mock(AccessControllerManager.class); |
| 55 | + private final SystemInfoService systemInfo = Mockito.mock(SystemInfoService.class); |
| 56 | + private final BackendServiceProxy proxy = Mockito.mock(BackendServiceProxy.class); |
| 57 | + private boolean originalEnableLocalTvf; |
| 58 | + private MockedStatic<Env> mockedEnv; |
| 59 | + private MockedStatic<ConnectContext> mockedContext; |
| 60 | + private MockedStatic<BackendServiceProxy> mockedProxy; |
| 61 | + |
| 62 | + @BeforeEach |
| 63 | + public void setUp() throws Exception { |
| 64 | + originalEnableLocalTvf = Config.enable_local_tvf; |
| 65 | + Config.enable_local_tvf = false; |
| 66 | + mockedEnv = Mockito.mockStatic(Env.class); |
| 67 | + mockedEnv.when(Env::getCurrentEnv).thenReturn(env); |
| 68 | + mockedEnv.when(Env::getCurrentSystemInfo).thenReturn(systemInfo); |
| 69 | + mockedContext = Mockito.mockStatic(ConnectContext.class); |
| 70 | + mockedContext.when(ConnectContext::get).thenReturn(context); |
| 71 | + mockedProxy = Mockito.mockStatic(BackendServiceProxy.class); |
| 72 | + mockedProxy.when(BackendServiceProxy::getInstance).thenReturn(proxy); |
| 73 | + Mockito.when(env.getAccessManager()).thenReturn(accessManager); |
| 74 | + Mockito.when(context.getSessionVariable()).thenReturn(new SessionVariable()); |
| 75 | + Mockito.when(context.getQueryTimeoutS()).thenReturn(60); |
| 76 | + |
| 77 | + Backend backend = Mockito.mock(Backend.class); |
| 78 | + Mockito.when(backend.getId()).thenReturn(1L); |
| 79 | + Mockito.when(backend.getHost()).thenReturn("127.0.0.1"); |
| 80 | + Mockito.when(backend.isAlive()).thenReturn(true); |
| 81 | + Mockito.when(backend.isQueryAvailable()).thenReturn(true); |
| 82 | + Mockito.when(backend.isLoadAvailable()).thenReturn(true); |
| 83 | + ComputeGroup computeGroup = Mockito.mock(ComputeGroup.class); |
| 84 | + Mockito.when(context.getComputeGroup()).thenReturn(computeGroup); |
| 85 | + Mockito.when(computeGroup.getBackendList()).thenReturn(Collections.singletonList(backend)); |
| 86 | + Mockito.when(systemInfo.getBackend(1L)).thenReturn(backend); |
| 87 | + Mockito.when(backend.getBrpcAddress()).thenReturn(new TNetworkAddress("127.0.0.1", 8060)); |
| 88 | + Mockito.when(proxy.glob(Mockito.any(), Mockito.any())).thenReturn(CompletableFuture.completedFuture( |
| 89 | + PGlobResponse.newBuilder().setStatus(PStatus.newBuilder().setStatusCode(0)).build())); |
| 90 | + } |
| 91 | + |
| 92 | + @AfterEach |
| 93 | + public void tearDown() { |
| 94 | + Config.enable_local_tvf = originalEnableLocalTvf; |
| 95 | + mockedProxy.close(); |
| 96 | + mockedContext.close(); |
| 97 | + mockedEnv.close(); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testDisabledRejectsLocalBeforePropertyParsing() { |
| 102 | + AnalysisException exception = Assertions.assertThrows(AnalysisException.class, |
| 103 | + () -> new LocalTableValuedFunction(ImmutableMap.of())); |
| 104 | + Assertions.assertTrue(exception.getMessage().contains("enable_local_tvf=true")); |
| 105 | + Mockito.verifyNoInteractions(accessManager, systemInfo, proxy); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + public void testDisabledRejectsCsvAndLanceBeforeFileAccess() { |
| 110 | + for (String format : new String[] {"csv", "lance"}) { |
| 111 | + Map<String, String> properties = ImmutableMap.of( |
| 112 | + "file_path", "local_enable_test.csv", "backend_id", "1", "format", format); |
| 113 | + AnalysisException exception = Assertions.assertThrows(AnalysisException.class, |
| 114 | + () -> new LocalTableValuedFunction(properties)); |
| 115 | + Assertions.assertTrue(exception.getMessage().contains("LOCAL TVF is disabled")); |
| 116 | + } |
| 117 | + Mockito.verifyNoInteractions(accessManager, systemInfo, proxy); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testEnabledLocalReachesExistingFileAccess() throws Exception { |
| 122 | + Config.enable_local_tvf = true; |
| 123 | + Mockito.when(accessManager.checkGlobalPriv(context, PrivPredicate.ADMIN)).thenReturn(true); |
| 124 | + new LocalTableValuedFunction(PROPERTIES); |
| 125 | + Mockito.verify(proxy).glob(Mockito.any(), Mockito.any()); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void testDisabledRejectsFileLocalBeforeFileAccess() { |
| 130 | + AnalysisException exception = Assertions.assertThrows(AnalysisException.class, |
| 131 | + () -> new FileTableValuedFunction(PROPERTIES)); |
| 132 | + Assertions.assertTrue(exception.getMessage().contains("LOCAL TVF is disabled")); |
| 133 | + Mockito.verifyNoInteractions(accessManager, systemInfo, proxy); |
| 134 | + } |
| 135 | + |
| 136 | + @Test |
| 137 | + public void testEnabledFileLocalReachesExistingFileAccess() throws Exception { |
| 138 | + Config.enable_local_tvf = true; |
| 139 | + Mockito.when(accessManager.checkGlobalPriv(context, PrivPredicate.ADMIN)).thenReturn(true); |
| 140 | + new FileTableValuedFunction(PROPERTIES); |
| 141 | + Mockito.verify(proxy).glob(Mockito.any(), Mockito.any()); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void testDisabledLocalDoesNotBlockFileS3() { |
| 146 | + Map<String, String> properties = ImmutableMap.<String, String>builder() |
| 147 | + .put("uri", "s3://test-bucket/local_enable_test.csv") |
| 148 | + .put("s3.endpoint", "s3.us-east-1.amazonaws.com") |
| 149 | + .put("s3.region", "us-east-1") |
| 150 | + .put("s3.access_key", "test-access-key") |
| 151 | + .put("s3.secret_key", "test-secret-key") |
| 152 | + .put("format", "csv") |
| 153 | + .build(); |
| 154 | + try (MockedConstruction<S3TableValuedFunction> mockedS3 = |
| 155 | + Mockito.mockConstruction(S3TableValuedFunction.class)) { |
| 156 | + Assertions.assertDoesNotThrow(() -> new FileTableValuedFunction(properties)); |
| 157 | + Assertions.assertEquals(1, mockedS3.constructed().size()); |
| 158 | + } |
| 159 | + Mockito.verifyNoInteractions(accessManager, systemInfo, proxy); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testEnabledLocalRejectsUnprivilegedUserBeforeFileAccess() { |
| 164 | + Config.enable_local_tvf = true; |
| 165 | + AnalysisException exception = Assertions.assertThrows(AnalysisException.class, |
| 166 | + () -> new LocalTableValuedFunction(PROPERTIES)); |
| 167 | + Assertions.assertTrue(exception.getMessage().contains("ADMIN/OPERATOR")); |
| 168 | + Mockito.verifyNoInteractions(systemInfo, proxy); |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + public void testOperatorCanAccessLocalFiles() throws Exception { |
| 173 | + Config.enable_local_tvf = true; |
| 174 | + Mockito.when(accessManager.checkGlobalPriv(context, PrivPredicate.OPERATOR)).thenReturn(true); |
| 175 | + new LocalTableValuedFunction(PROPERTIES); |
| 176 | + Mockito.verify(proxy).glob(Mockito.any(), Mockito.any()); |
| 177 | + } |
| 178 | + |
| 179 | + @Test |
| 180 | + public void testEnabledLancePreservesExistingBackendSelection() throws Exception { |
| 181 | + Config.enable_local_tvf = true; |
| 182 | + Mockito.when(accessManager.checkGlobalPriv(context, PrivPredicate.ADMIN)).thenReturn(true); |
| 183 | + new LocalTableValuedFunction(ImmutableMap.of( |
| 184 | + "file_path", "local_enable_test.lance", "backend_id", "1", "format", "lance")); |
| 185 | + Mockito.verifyNoInteractions(proxy); |
| 186 | + } |
| 187 | +} |
0 commit comments