|
| 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 | + |
| 18 | +package org.apache.seatunnel.engine.e2e; |
| 19 | + |
| 20 | +import org.apache.seatunnel.engine.server.rest.RestConstant; |
| 21 | + |
| 22 | +import org.awaitility.Awaitility; |
| 23 | +import org.junit.jupiter.api.AfterEach; |
| 24 | +import org.junit.jupiter.api.BeforeEach; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.testcontainers.containers.GenericContainer; |
| 27 | + |
| 28 | +import io.restassured.http.ContentType; |
| 29 | +import io.restassured.response.Response; |
| 30 | + |
| 31 | +import java.io.IOException; |
| 32 | +import java.util.Base64; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +import static io.restassured.RestAssured.given; |
| 36 | +import static org.apache.seatunnel.e2e.common.util.ContainerUtil.PROJECT_ROOT_PATH; |
| 37 | +import static org.hamcrest.Matchers.containsString; |
| 38 | +import static org.hamcrest.Matchers.notNullValue; |
| 39 | + |
| 40 | +/** Integration test for basic authentication in SeaTunnel Engine. */ |
| 41 | +public class BasicAuthenticationIT extends SeaTunnelEngineContainer { |
| 42 | + |
| 43 | + private static final String HTTP = "http://"; |
| 44 | + private static final String COLON = ":"; |
| 45 | + private static final String USERNAME = "testuser"; |
| 46 | + private static final String PASSWORD = "testpassword"; |
| 47 | + private static final String BASIC_AUTH_HEADER = "Authorization"; |
| 48 | + private static final String BASIC_AUTH_PREFIX = "Basic "; |
| 49 | + |
| 50 | + @Override |
| 51 | + @BeforeEach |
| 52 | + public void startUp() throws Exception { |
| 53 | + // Create server with basic authentication enabled |
| 54 | + |
| 55 | + server = createSeaTunnelContainerWithBasicAuth(); |
| 56 | + // Wait for server to be ready |
| 57 | + Awaitility.await() |
| 58 | + .atMost(2, TimeUnit.MINUTES) |
| 59 | + .until( |
| 60 | + () -> { |
| 61 | + try { |
| 62 | + // Try to access with correct credentials |
| 63 | + String credentials = USERNAME + ":" + PASSWORD; |
| 64 | + String encodedCredentials = |
| 65 | + Base64.getEncoder().encodeToString(credentials.getBytes()); |
| 66 | + |
| 67 | + given().header( |
| 68 | + BASIC_AUTH_HEADER, |
| 69 | + BASIC_AUTH_PREFIX + encodedCredentials) |
| 70 | + .get( |
| 71 | + HTTP |
| 72 | + + server.getHost() |
| 73 | + + COLON |
| 74 | + + server.getMappedPort(8080) |
| 75 | + + "/") |
| 76 | + .then() |
| 77 | + .statusCode(200); |
| 78 | + return true; |
| 79 | + } catch (Exception e) { |
| 80 | + return false; |
| 81 | + } |
| 82 | + }); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + @AfterEach |
| 87 | + public void tearDown() throws Exception { |
| 88 | + super.tearDown(); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Test that accessing the web UI without authentication credentials returns 401 Unauthorized. |
| 93 | + */ |
| 94 | + @Test |
| 95 | + public void testAccessWithoutCredentials() { |
| 96 | + given().get(HTTP + server.getHost() + COLON + server.getMappedPort(8080) + "/") |
| 97 | + .then() |
| 98 | + .statusCode(401); |
| 99 | + } |
| 100 | + |
| 101 | + /** Test that accessing the web UI with incorrect credentials returns 401 Unauthorized. */ |
| 102 | + @Test |
| 103 | + public void testAccessWithIncorrectCredentials() { |
| 104 | + String credentials = "wronguser:wrongpassword"; |
| 105 | + String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes()); |
| 106 | + |
| 107 | + given().header(BASIC_AUTH_HEADER, BASIC_AUTH_PREFIX + encodedCredentials) |
| 108 | + .get(HTTP + server.getHost() + COLON + server.getMappedPort(8080) + "/") |
| 109 | + .then() |
| 110 | + .statusCode(401); |
| 111 | + } |
| 112 | + |
| 113 | + /** Test that accessing the web UI with correct credentials returns 200 OK. */ |
| 114 | + @Test |
| 115 | + public void testAccessWithCorrectCredentials() { |
| 116 | + String credentials = USERNAME + ":" + PASSWORD; |
| 117 | + String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes()); |
| 118 | + |
| 119 | + given().header(BASIC_AUTH_HEADER, BASIC_AUTH_PREFIX + encodedCredentials) |
| 120 | + .get(HTTP + server.getHost() + COLON + server.getMappedPort(8080) + "/") |
| 121 | + .then() |
| 122 | + .statusCode(200) |
| 123 | + .contentType(containsString("text/html")) |
| 124 | + .body(containsString("<title>Seatunnel Engine UI</title>")); |
| 125 | + } |
| 126 | + |
| 127 | + /** Test that accessing the REST API with correct credentials returns 200 OK. */ |
| 128 | + @Test |
| 129 | + public void testRestApiAccessWithCorrectCredentials() { |
| 130 | + String credentials = USERNAME + ":" + PASSWORD; |
| 131 | + String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes()); |
| 132 | + |
| 133 | + given().header(BASIC_AUTH_HEADER, BASIC_AUTH_PREFIX + encodedCredentials) |
| 134 | + .get( |
| 135 | + HTTP |
| 136 | + + server.getHost() |
| 137 | + + COLON |
| 138 | + + server.getMappedPort(8080) |
| 139 | + + RestConstant.REST_URL_OVERVIEW) |
| 140 | + .then() |
| 141 | + .statusCode(200) |
| 142 | + .body("projectVersion", notNullValue()); |
| 143 | + } |
| 144 | + |
| 145 | + /** Test that accessing the REST API with Incorrect credentials returns 200 OK. */ |
| 146 | + @Test |
| 147 | + public void testRestApiAccessWithIncorrectCredentials() { |
| 148 | + String credentials = "wronguser:wrongpassword"; |
| 149 | + String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes()); |
| 150 | + |
| 151 | + given().header(BASIC_AUTH_HEADER, BASIC_AUTH_PREFIX + encodedCredentials) |
| 152 | + .get( |
| 153 | + HTTP |
| 154 | + + server.getHost() |
| 155 | + + COLON |
| 156 | + + server.getMappedPort(8080) |
| 157 | + + RestConstant.REST_URL_OVERVIEW) |
| 158 | + .then() |
| 159 | + .statusCode(401); |
| 160 | + } |
| 161 | + |
| 162 | + /** Test submitting a job via REST API with correct credentials. */ |
| 163 | + @Test |
| 164 | + public void testSubmitJobWithCorrectCredentials() { |
| 165 | + String credentials = USERNAME + ":" + PASSWORD; |
| 166 | + String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes()); |
| 167 | + |
| 168 | + // Simple batch job configuration |
| 169 | + String jobConfig = |
| 170 | + "{\n" |
| 171 | + + " \"env\": {\n" |
| 172 | + + " \"job.mode\": \"batch\"\n" |
| 173 | + + " },\n" |
| 174 | + + " \"source\": [\n" |
| 175 | + + " {\n" |
| 176 | + + " \"plugin_name\": \"FakeSource\",\n" |
| 177 | + + " \"plugin_output\": \"fake\",\n" |
| 178 | + + " \"row.num\": 100,\n" |
| 179 | + + " \"schema\": {\n" |
| 180 | + + " \"fields\": {\n" |
| 181 | + + " \"name\": \"string\",\n" |
| 182 | + + " \"age\": \"int\",\n" |
| 183 | + + " \"card\": \"int\"\n" |
| 184 | + + " }\n" |
| 185 | + + " }\n" |
| 186 | + + " }\n" |
| 187 | + + " ],\n" |
| 188 | + + " \"transform\": [\n" |
| 189 | + + " ],\n" |
| 190 | + + " \"sink\": [\n" |
| 191 | + + " {\n" |
| 192 | + + " \"plugin_name\": \"InMemory\",\n" |
| 193 | + + " \"plugin_input\": \"fake\",\n" |
| 194 | + + " \"throw_exception\": true\n" |
| 195 | + + " }\n" |
| 196 | + + " ]\n" |
| 197 | + + "}"; |
| 198 | + |
| 199 | + Response response = |
| 200 | + given().header(BASIC_AUTH_HEADER, BASIC_AUTH_PREFIX + encodedCredentials) |
| 201 | + .contentType(ContentType.JSON) |
| 202 | + .body(jobConfig) |
| 203 | + .post( |
| 204 | + HTTP |
| 205 | + + server.getHost() |
| 206 | + + COLON |
| 207 | + + server.getMappedPort(8080) |
| 208 | + + RestConstant.REST_URL_SUBMIT_JOB); |
| 209 | + |
| 210 | + response.then().statusCode(200).body("jobId", notNullValue()); |
| 211 | + } |
| 212 | + |
| 213 | + /** Test submitting a job via REST API with incorrect credentials. */ |
| 214 | + @Test |
| 215 | + public void testSubmitJobWithIncorrectCredentials() { |
| 216 | + String credentials = "wronguser:wrongpassword"; |
| 217 | + String encodedCredentials = Base64.getEncoder().encodeToString(credentials.getBytes()); |
| 218 | + |
| 219 | + // Simple batch job configuration |
| 220 | + String jobConfig = |
| 221 | + "{\n" |
| 222 | + + " \"env\": {\n" |
| 223 | + + " \"job.mode\": \"BATCH\"\n" |
| 224 | + + " },\n" |
| 225 | + + " \"source\": {\n" |
| 226 | + + " \"FakeSource\": {\n" |
| 227 | + + " \"plugin_output\": \"fake\",\n" |
| 228 | + + " \"row.num\": 100,\n" |
| 229 | + + " \"schema\": {\n" |
| 230 | + + " \"fields\": {\n" |
| 231 | + + " \"id\": \"int\",\n" |
| 232 | + + " \"name\": \"string\"\n" |
| 233 | + + " }\n" |
| 234 | + + " }\n" |
| 235 | + + " }\n" |
| 236 | + + " },\n" |
| 237 | + + " \"sink\": {\n" |
| 238 | + + " \"Console\": {\n" |
| 239 | + + " \"plugin_input\": \"fake\"\n" |
| 240 | + + " }\n" |
| 241 | + + " }\n" |
| 242 | + + "}"; |
| 243 | + |
| 244 | + given().header(BASIC_AUTH_HEADER, BASIC_AUTH_PREFIX + encodedCredentials) |
| 245 | + .contentType(ContentType.JSON) |
| 246 | + .body(jobConfig) |
| 247 | + .post( |
| 248 | + HTTP |
| 249 | + + server.getHost() |
| 250 | + + COLON |
| 251 | + + server.getMappedPort(8080) |
| 252 | + + RestConstant.REST_URL_SUBMIT_JOB) |
| 253 | + .then() |
| 254 | + .statusCode(401); |
| 255 | + } |
| 256 | + |
| 257 | + /** Create a SeaTunnel container with basic authentication enabled. */ |
| 258 | + private GenericContainer<?> createSeaTunnelContainerWithBasicAuth() |
| 259 | + throws IOException, InterruptedException { |
| 260 | + String configPath = |
| 261 | + PROJECT_ROOT_PATH |
| 262 | + + "/seatunnel-e2e/seatunnel-engine-e2e/connector-seatunnel-e2e-base/src/test/resources/basic-auth/seatunnel.yaml"; |
| 263 | + |
| 264 | + return createSeaTunnelContainerWithFakeSourceAndInMemorySink(configPath); |
| 265 | + } |
| 266 | +} |
0 commit comments