|
| 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.cloud.security; |
| 19 | + |
| 20 | +import org.apache.doris.common.DdlException; |
| 21 | + |
| 22 | +import org.junit.jupiter.api.Assertions; |
| 23 | +import org.junit.jupiter.api.Test; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.util.ArrayList; |
| 27 | +import java.util.Arrays; |
| 28 | +import java.util.List; |
| 29 | + |
| 30 | +/** |
| 31 | + * {@link SecurityChecker#checkEndpointForSsrf} is the single primitive the catalog SSRF gate delegates to. |
| 32 | + * The real third-party SDK is operator-supplied and absent in CI, so these tests drive its branches through |
| 33 | + * the {@code doConnect} seam: the SDK signals an intranet target by throwing {@link SecurityException} from |
| 34 | + * inside the connect, a plain connectivity failure is {@link IOException}, and when no checker is installed |
| 35 | + * the whole probe is a no-op. |
| 36 | + */ |
| 37 | +public class SecurityCheckerTest { |
| 38 | + |
| 39 | + private enum ConnectBehavior { SUCCESS, SSRF, IO } |
| 40 | + |
| 41 | + /** A SecurityChecker whose net-hook bracket and connect are recorded, not real. */ |
| 42 | + private static final class RecordingChecker extends SecurityChecker { |
| 43 | + private boolean enabled = true; |
| 44 | + private boolean startThrows = false; |
| 45 | + private ConnectBehavior connectBehavior = ConnectBehavior.SUCCESS; |
| 46 | + private final List<String> events = new ArrayList<>(); |
| 47 | + |
| 48 | + @Override |
| 49 | + public String getSafeJdbcUrl(String originJdbcUrl) { |
| 50 | + return originJdbcUrl; |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public void startSSRFChecking(String originUri) throws Exception { |
| 55 | + start(); |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public void startSSRFChecking() throws Exception { |
| 60 | + start(); |
| 61 | + } |
| 62 | + |
| 63 | + private void start() throws Exception { |
| 64 | + events.add("start"); |
| 65 | + if (startThrows) { |
| 66 | + throw new Exception("cannot install net-hook"); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public void stopSSRFChecking() { |
| 72 | + events.add("stop"); |
| 73 | + } |
| 74 | + |
| 75 | + @Override |
| 76 | + public boolean isEnabled() { |
| 77 | + return enabled; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + protected void doConnect(String host, int port, int connectTimeoutMs) throws IOException { |
| 82 | + events.add("connect"); |
| 83 | + switch (connectBehavior) { |
| 84 | + case SSRF: |
| 85 | + throw new SecurityException("simulated SSRFUnsafeConnectionException for " + host); |
| 86 | + case IO: |
| 87 | + throw new IOException("connection refused"); |
| 88 | + default: |
| 89 | + break; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void disabledCheckerSkipsProbeEntirely() throws DdlException { |
| 96 | + RecordingChecker checker = new RecordingChecker(); |
| 97 | + checker.enabled = false; |
| 98 | + // Intranet-looking host, but a disabled checker must not even connect. |
| 99 | + checker.checkEndpointForSsrf("10.0.0.1", 9083); |
| 100 | + Assertions.assertTrue(checker.events.isEmpty(), "disabled checker must not touch the network"); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void successfulProbeBracketsConnectAndReturns() throws DdlException { |
| 105 | + RecordingChecker checker = new RecordingChecker(); |
| 106 | + checker.connectBehavior = ConnectBehavior.SUCCESS; |
| 107 | + checker.checkEndpointForSsrf("public.example.com", 9083); |
| 108 | + Assertions.assertEquals(Arrays.asList("start", "connect", "stop"), checker.events); |
| 109 | + } |
| 110 | + |
| 111 | + @Test |
| 112 | + public void intranetTargetIsRejectedAsDdlException() { |
| 113 | + RecordingChecker checker = new RecordingChecker(); |
| 114 | + checker.connectBehavior = ConnectBehavior.SSRF; |
| 115 | + DdlException e = Assertions.assertThrows(DdlException.class, |
| 116 | + () -> checker.checkEndpointForSsrf("10.0.0.1", 9083)); |
| 117 | + Assertions.assertTrue(e.getMessage().contains("SSRF policy"), e.getMessage()); |
| 118 | + // stop still runs in the finally block even when the connect throws. |
| 119 | + Assertions.assertEquals(Arrays.asList("start", "connect", "stop"), checker.events); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void unreachableEndpointIsNotAnSsrfRejection() throws DdlException { |
| 124 | + RecordingChecker checker = new RecordingChecker(); |
| 125 | + checker.connectBehavior = ConnectBehavior.IO; |
| 126 | + // A refused/timed-out connect is a reachability fact, not an SSRF signal: must not throw. |
| 127 | + checker.checkEndpointForSsrf("public-but-down.example.com", 9083); |
| 128 | + Assertions.assertEquals(Arrays.asList("start", "connect", "stop"), checker.events); |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + public void failedHookInstallSurfacesAsDdlExceptionWithoutConnecting() { |
| 133 | + RecordingChecker checker = new RecordingChecker(); |
| 134 | + checker.startThrows = true; |
| 135 | + DdlException e = Assertions.assertThrows(DdlException.class, |
| 136 | + () -> checker.checkEndpointForSsrf("some.host", 9083)); |
| 137 | + Assertions.assertTrue(e.getMessage().contains("Failed to initialize SSRF checking"), e.getMessage()); |
| 138 | + // No connect attempt, and nothing to stop since the hook was never installed. |
| 139 | + Assertions.assertEquals(Arrays.asList("start"), checker.events); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void dummyCheckerIsDisabled() { |
| 144 | + Assertions.assertFalse(new DummySecurityChecker().isEnabled()); |
| 145 | + } |
| 146 | +} |
0 commit comments