|
| 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.mysql.privilege.AccessControllerManager; |
| 22 | +import org.apache.doris.mysql.privilege.PrivPredicate; |
| 23 | +import org.apache.doris.nereids.exceptions.AnalysisException; |
| 24 | +import org.apache.doris.qe.ConnectContext; |
| 25 | +import org.apache.doris.rpc.BackendServiceProxy; |
| 26 | +import org.apache.doris.system.SystemInfoService; |
| 27 | + |
| 28 | +import com.google.common.collect.ImmutableMap; |
| 29 | +import org.junit.jupiter.api.AfterEach; |
| 30 | +import org.junit.jupiter.api.Assertions; |
| 31 | +import org.junit.jupiter.api.BeforeEach; |
| 32 | +import org.junit.jupiter.api.Test; |
| 33 | +import org.mockito.MockedConstruction; |
| 34 | +import org.mockito.MockedStatic; |
| 35 | +import org.mockito.Mockito; |
| 36 | + |
| 37 | +import java.util.Map; |
| 38 | + |
| 39 | +public class LocalTableValuedFunctionDisabledTest { |
| 40 | + private static final Map<String, String> PROPERTIES = ImmutableMap.of( |
| 41 | + "file_path", "local_disabled_test.csv", "backend_id", "1", "format", "csv"); |
| 42 | + |
| 43 | + private final Env env = Mockito.mock(Env.class); |
| 44 | + private final ConnectContext context = Mockito.mock(ConnectContext.class); |
| 45 | + private final AccessControllerManager accessManager = Mockito.mock(AccessControllerManager.class); |
| 46 | + private final SystemInfoService systemInfo = Mockito.mock(SystemInfoService.class); |
| 47 | + private final BackendServiceProxy proxy = Mockito.mock(BackendServiceProxy.class); |
| 48 | + private MockedStatic<Env> mockedEnv; |
| 49 | + private MockedStatic<ConnectContext> mockedContext; |
| 50 | + private MockedStatic<BackendServiceProxy> mockedProxy; |
| 51 | + |
| 52 | + @BeforeEach |
| 53 | + public void setUp() { |
| 54 | + mockedEnv = Mockito.mockStatic(Env.class); |
| 55 | + mockedEnv.when(Env::getCurrentEnv).thenReturn(env); |
| 56 | + mockedEnv.when(Env::getCurrentSystemInfo).thenReturn(systemInfo); |
| 57 | + mockedContext = Mockito.mockStatic(ConnectContext.class); |
| 58 | + mockedContext.when(ConnectContext::get).thenReturn(context); |
| 59 | + mockedProxy = Mockito.mockStatic(BackendServiceProxy.class); |
| 60 | + mockedProxy.when(BackendServiceProxy::getInstance).thenReturn(proxy); |
| 61 | + Mockito.when(env.getAccessManager()).thenReturn(accessManager); |
| 62 | + } |
| 63 | + |
| 64 | + @AfterEach |
| 65 | + public void tearDown() { |
| 66 | + mockedProxy.close(); |
| 67 | + mockedContext.close(); |
| 68 | + mockedEnv.close(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testLocalDisabledBeforePropertyParsing() { |
| 73 | + AnalysisException exception = Assertions.assertThrows(AnalysisException.class, |
| 74 | + () -> new LocalTableValuedFunction(ImmutableMap.of())); |
| 75 | + Assertions.assertTrue(exception.getMessage().contains("LOCAL TVF is disabled")); |
| 76 | + Mockito.verifyNoInteractions(accessManager, systemInfo, proxy); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testFormatsDisabledBeforeFileAccess() { |
| 81 | + for (String format : new String[] {"csv", "json", "lance"}) { |
| 82 | + Map<String, String> properties = ImmutableMap.of( |
| 83 | + "file_path", "local_disabled_test", "backend_id", "1", "format", format); |
| 84 | + AnalysisException exception = Assertions.assertThrows(AnalysisException.class, |
| 85 | + () -> new LocalTableValuedFunction(properties)); |
| 86 | + Assertions.assertTrue(exception.getMessage().contains("LOCAL TVF is disabled")); |
| 87 | + } |
| 88 | + Mockito.verifyNoInteractions(accessManager, systemInfo, proxy); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testFileLocalDisabledBeforeFileAccess() { |
| 93 | + AnalysisException exception = Assertions.assertThrows(AnalysisException.class, |
| 94 | + () -> new FileTableValuedFunction(PROPERTIES)); |
| 95 | + Assertions.assertTrue(exception.getMessage().contains("LOCAL TVF is disabled")); |
| 96 | + Mockito.verifyNoInteractions(accessManager, systemInfo, proxy); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testPrivilegedUsersCannotAccessLocalFiles() { |
| 101 | + for (PrivPredicate privilege : new PrivPredicate[] {PrivPredicate.ADMIN, PrivPredicate.OPERATOR}) { |
| 102 | + Mockito.when(accessManager.checkGlobalPriv(context, privilege)).thenReturn(true); |
| 103 | + Assertions.assertThrows(AnalysisException.class, () -> new LocalTableValuedFunction(PROPERTIES)); |
| 104 | + Assertions.assertThrows(AnalysisException.class, () -> new FileTableValuedFunction(PROPERTIES)); |
| 105 | + } |
| 106 | + Mockito.verifyNoInteractions(accessManager, systemInfo, proxy); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testFileS3RemainsAvailable() { |
| 111 | + Map<String, String> properties = ImmutableMap.<String, String>builder() |
| 112 | + .put("uri", "s3://test-bucket/local_disabled_test.csv") |
| 113 | + .put("s3.endpoint", "s3.us-east-1.amazonaws.com") |
| 114 | + .put("s3.region", "us-east-1") |
| 115 | + .put("s3.access_key", "test-access-key") |
| 116 | + .put("s3.secret_key", "test-secret-key") |
| 117 | + .put("format", "csv") |
| 118 | + .build(); |
| 119 | + try (MockedConstruction<S3TableValuedFunction> mockedS3 = |
| 120 | + Mockito.mockConstruction(S3TableValuedFunction.class)) { |
| 121 | + Assertions.assertDoesNotThrow(() -> new FileTableValuedFunction(properties)); |
| 122 | + Assertions.assertEquals(1, mockedS3.constructed().size()); |
| 123 | + } |
| 124 | + Mockito.verifyNoInteractions(accessManager, systemInfo, proxy); |
| 125 | + } |
| 126 | +} |
0 commit comments