|
| 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.connector.paimon; |
| 19 | + |
| 20 | +import org.apache.doris.connector.api.ConnectorValidationContext; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Assertions; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +/** |
| 32 | + * Tests for {@link PaimonConnector#preCreateValidation} (rereview2 B-8b): a JDBC-flavor catalog |
| 33 | + * with a {@code driver_url} must route it through the engine's |
| 34 | + * {@link ConnectorValidationContext#validateAndResolveDriverPath} security gate at CREATE CATALOG, |
| 35 | + * before the jar is ever loaded into the FE JVM. Mirrors {@code JdbcDorisConnector.preCreateValidation}. |
| 36 | + * |
| 37 | + * <p>Offline: a hand-written {@link RecordingValidationContext} fake records each validated url and |
| 38 | + * can simulate a rejected url. {@link RecordingConnectorContext} supplies the (unused-by-this-path) |
| 39 | + * {@code ConnectorContext}. |
| 40 | + */ |
| 41 | +public class PaimonConnectorPreCreateValidationTest { |
| 42 | + |
| 43 | + private static PaimonConnector connector(Map<String, String> props) { |
| 44 | + return new PaimonConnector(props, new RecordingConnectorContext()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void validatesJdbcDriverUrl() throws Exception { |
| 49 | + Map<String, String> props = new HashMap<>(); |
| 50 | + props.put("paimon.catalog.type", "jdbc"); |
| 51 | + props.put("jdbc.driver_url", "mysql.jar"); |
| 52 | + RecordingValidationContext ctx = new RecordingValidationContext(); |
| 53 | + |
| 54 | + connector(props).preCreateValidation(ctx); |
| 55 | + |
| 56 | + // WHY (BLOCKER B-8b): a jdbc driver_url is loaded into the FE JVM (URLClassLoader); CREATE |
| 57 | + // CATALOG must route it through the engine's format / white-list / secure-path gate. MUTATION: |
| 58 | + // dropping the preCreateValidation override -> validateAndResolveDriverPath never called -> red. |
| 59 | + Assertions.assertEquals(Collections.singletonList("mysql.jar"), ctx.validatedDriverUrls); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + public void validatesPaimonJdbcDriverUrlAlias() throws Exception { |
| 64 | + Map<String, String> props = new HashMap<>(); |
| 65 | + props.put("paimon.catalog.type", "jdbc"); |
| 66 | + props.put("paimon.jdbc.driver_url", "mysql.jar"); |
| 67 | + RecordingValidationContext ctx = new RecordingValidationContext(); |
| 68 | + |
| 69 | + connector(props).preCreateValidation(ctx); |
| 70 | + |
| 71 | + Assertions.assertEquals(Collections.singletonList("mysql.jar"), ctx.validatedDriverUrls, |
| 72 | + "the paimon.jdbc.driver_url alias must also be validated"); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void skipsValidationForNonJdbcFlavor() throws Exception { |
| 77 | + Map<String, String> props = new HashMap<>(); |
| 78 | + props.put("paimon.catalog.type", "filesystem"); |
| 79 | + props.put("jdbc.driver_url", "mysql.jar"); |
| 80 | + RecordingValidationContext ctx = new RecordingValidationContext(); |
| 81 | + |
| 82 | + connector(props).preCreateValidation(ctx); |
| 83 | + |
| 84 | + Assertions.assertTrue(ctx.validatedDriverUrls.isEmpty(), |
| 85 | + "non-JDBC flavors must not trigger driver-url validation"); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void skipsValidationWhenNoDriverUrl() throws Exception { |
| 90 | + Map<String, String> props = new HashMap<>(); |
| 91 | + props.put("paimon.catalog.type", "jdbc"); |
| 92 | + RecordingValidationContext ctx = new RecordingValidationContext(); |
| 93 | + |
| 94 | + connector(props).preCreateValidation(ctx); |
| 95 | + |
| 96 | + Assertions.assertTrue(ctx.validatedDriverUrls.isEmpty(), |
| 97 | + "a jdbc catalog without a driver_url uses the platform driver -> nothing to validate"); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void propagatesRejectedDriverUrl() { |
| 102 | + Map<String, String> props = new HashMap<>(); |
| 103 | + props.put("paimon.catalog.type", "jdbc"); |
| 104 | + props.put("jdbc.driver_url", "http://evil.test/x.jar"); |
| 105 | + RecordingValidationContext ctx = new RecordingValidationContext(); |
| 106 | + ctx.reject = true; |
| 107 | + |
| 108 | + // WHY (BLOCKER B-8b): a disallowed url must FAIL CREATE CATALOG — the hook throws and the |
| 109 | + // connector must let it propagate, not swallow it. MUTATION: catching the exception -> no |
| 110 | + // throw -> red. |
| 111 | + Assertions.assertThrows(IllegalArgumentException.class, |
| 112 | + () -> connector(props).preCreateValidation(ctx)); |
| 113 | + } |
| 114 | + |
| 115 | + /** Hand-written {@link ConnectorValidationContext} test double (no Mockito). */ |
| 116 | + private static final class RecordingValidationContext implements ConnectorValidationContext { |
| 117 | + final List<String> validatedDriverUrls = new ArrayList<>(); |
| 118 | + boolean reject; |
| 119 | + |
| 120 | + @Override |
| 121 | + public long getCatalogId() { |
| 122 | + return 0; |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public String getProperty(String key) { |
| 127 | + return null; |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void storeProperty(String key, String value) { |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public String validateAndResolveDriverPath(String driverUrl) throws Exception { |
| 136 | + validatedDriverUrls.add(driverUrl); |
| 137 | + if (reject) { |
| 138 | + throw new IllegalArgumentException("disallowed driver url: " + driverUrl); |
| 139 | + } |
| 140 | + return "file:///resolved/" + driverUrl; |
| 141 | + } |
| 142 | + |
| 143 | + @Override |
| 144 | + public String computeDriverChecksum(String driverUrl) { |
| 145 | + return "deadbeef"; |
| 146 | + } |
| 147 | + |
| 148 | + @Override |
| 149 | + public void requestBeConnectivityTest(byte[] serializedDescriptor, int connectionTypeValue, |
| 150 | + String testQuery) { |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments