|
| 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 | +package org.apache.servicecomb.authentication.provider; |
| 18 | + |
| 19 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 20 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 21 | +import static org.mockito.Mockito.when; |
| 22 | + |
| 23 | +import io.swagger.v3.oas.models.OpenAPI; |
| 24 | +import io.swagger.v3.oas.models.servers.Server; |
| 25 | +import org.apache.servicecomb.swagger.SwaggerUtils; |
| 26 | +import org.junit.jupiter.api.BeforeEach; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.mockito.Mockito; |
| 29 | +import org.springframework.core.env.Environment; |
| 30 | + |
| 31 | +import java.util.ArrayList; |
| 32 | +import java.util.List; |
| 33 | +import java.util.stream.Collectors; |
| 34 | + |
| 35 | +public class TestPathCheckUtils { |
| 36 | + private static final String KEY_INCLUDE_PATH = "servicecomb.publicKey.accessControl.includePathPatterns"; |
| 37 | + |
| 38 | + private static final String KEY_EXCLUDE_PATH = "servicecomb.publicKey.accessControl.excludePathPatterns"; |
| 39 | + |
| 40 | + private static final String BASE_PATH = "/api/v1"; |
| 41 | + |
| 42 | + private Environment environment; |
| 43 | + |
| 44 | + private OpenAPI swagger; |
| 45 | + |
| 46 | + @BeforeEach |
| 47 | + public void setUp() { |
| 48 | + environment = Mockito.mock(Environment.class); |
| 49 | + swagger = new OpenAPI(); |
| 50 | + swagger.setServers(new ArrayList<>()); |
| 51 | + swagger.getServers().add(new Server().url(BASE_PATH)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testExcludePathWithBasePathAndExactMatch() { |
| 56 | + String operationPath = "/public"; |
| 57 | + when(environment.getProperty(KEY_EXCLUDE_PATH, "")).thenReturn(BASE_PATH + operationPath); |
| 58 | + |
| 59 | + String fullPath = SwaggerUtils.concatAbsolutePath(swagger, operationPath); |
| 60 | + assertTrue(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 61 | + "Should not require auth for excluded path with exact match"); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testExcludePathWithBasePathAndWildcard() { |
| 66 | + when(environment.getProperty(KEY_EXCLUDE_PATH, "")).thenReturn(BASE_PATH + "/public/*"); |
| 67 | + |
| 68 | + String fullPath = SwaggerUtils.concatAbsolutePath(swagger, "/public/test"); |
| 69 | + assertTrue(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 70 | + "Should not require auth for excluded path with wildcard"); |
| 71 | + |
| 72 | + fullPath = SwaggerUtils.concatAbsolutePath(swagger, "/public/nested/path"); |
| 73 | + assertTrue(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 74 | + "Should not require auth for excluded nested path with wildcard"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void testIncludePathWithBasePath() { |
| 79 | + when(environment.getProperty(KEY_EXCLUDE_PATH, "")).thenReturn(""); |
| 80 | + when(environment.getProperty(KEY_INCLUDE_PATH, "")).thenReturn(BASE_PATH + "/private/*"); |
| 81 | + |
| 82 | + String fullPath = SwaggerUtils.concatAbsolutePath(swagger, "/private/resource"); |
| 83 | + assertFalse(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 84 | + "Should require auth for included path"); |
| 85 | + |
| 86 | + fullPath = SwaggerUtils.concatAbsolutePath(swagger, "/public/resource"); |
| 87 | + assertTrue(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 88 | + "Should not require auth for non-included path"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testExcludeOverrideIncludePath() { |
| 93 | + when(environment.getProperty(KEY_EXCLUDE_PATH, "")).thenReturn(BASE_PATH + "/resource"); |
| 94 | + when(environment.getProperty(KEY_INCLUDE_PATH, "")).thenReturn(BASE_PATH + "/*"); |
| 95 | + |
| 96 | + String fullPath = SwaggerUtils.concatAbsolutePath(swagger, "/resource"); |
| 97 | + assertTrue(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 98 | + "Exclude patterns should override include patterns"); |
| 99 | + } |
| 100 | + |
| 101 | + @Test |
| 102 | + public void testMultipleExcludePaths() { |
| 103 | + List<String> operationPaths = List.of("/public", "/health", "/metrics/*"); |
| 104 | + when(environment.getProperty(KEY_EXCLUDE_PATH, "")).thenReturn(concatPath(BASE_PATH, operationPaths)); |
| 105 | + |
| 106 | + String fullPath = SwaggerUtils.concatAbsolutePath(swagger, "/public"); |
| 107 | + assertTrue(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 108 | + "Should not require auth for first exclude path"); |
| 109 | + |
| 110 | + fullPath = SwaggerUtils.concatAbsolutePath(swagger, "/health"); |
| 111 | + assertTrue(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 112 | + "Should not require auth for second exclude path"); |
| 113 | + |
| 114 | + fullPath = SwaggerUtils.concatAbsolutePath(swagger, "/metrics/jvm"); |
| 115 | + assertTrue(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 116 | + "Should not require auth for wildcard exclude path"); |
| 117 | + } |
| 118 | + |
| 119 | + @Test |
| 120 | + public void testDifferentBasePath() { |
| 121 | + String basePath = "/different/base"; |
| 122 | + String publicOperationPath = "/public"; |
| 123 | + String privateOperationPath = "/private"; |
| 124 | + swagger.getServers().clear(); |
| 125 | + swagger.getServers().add(new Server().url(basePath)); |
| 126 | + when(environment.getProperty(KEY_EXCLUDE_PATH, "")).thenReturn(basePath + publicOperationPath); |
| 127 | + |
| 128 | + String fullPath = SwaggerUtils.concatAbsolutePath(swagger, publicOperationPath); |
| 129 | + assertTrue(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 130 | + "Should not require auth with different base path"); |
| 131 | + |
| 132 | + fullPath = SwaggerUtils.concatAbsolutePath(swagger, privateOperationPath); |
| 133 | + assertFalse(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 134 | + "Should require auth for non-excluded path with different base path"); |
| 135 | + } |
| 136 | + |
| 137 | + @Test |
| 138 | + public void testNoBasePath() { |
| 139 | + String operationPath = "/public"; |
| 140 | + swagger.setServers(null); |
| 141 | + when(environment.getProperty(KEY_EXCLUDE_PATH, "")).thenReturn(operationPath); |
| 142 | + |
| 143 | + String fullPath = SwaggerUtils.concatAbsolutePath(swagger, operationPath); |
| 144 | + assertTrue(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 145 | + "Should not require auth when no base path is set"); |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + public void testEmptyConfiguration() { |
| 150 | + when(environment.getProperty(KEY_EXCLUDE_PATH, "")).thenReturn(""); |
| 151 | + when(environment.getProperty(KEY_INCLUDE_PATH, "")).thenReturn(""); |
| 152 | + |
| 153 | + String fullPath = SwaggerUtils.concatAbsolutePath(swagger, "/any/path"); |
| 154 | + assertFalse(PathCheckUtils.isNotRequiredAuth(fullPath, environment), |
| 155 | + "Should require auth by default when no patterns are configured"); |
| 156 | + } |
| 157 | + |
| 158 | + private String concatPath(String basePath, List<String> paths) { |
| 159 | + return paths.stream().map(path -> basePath + path).collect(Collectors.joining(",")); |
| 160 | + } |
| 161 | +} |
0 commit comments