|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.flink.table.client; |
| 20 | + |
| 21 | +import org.apache.flink.configuration.Configuration; |
| 22 | +import org.apache.flink.configuration.SecurityOptions; |
| 23 | +import org.apache.flink.table.gateway.rest.util.SqlGatewayRestEndpointExtension; |
| 24 | +import org.apache.flink.table.gateway.service.utils.SqlGatewayServiceExtension; |
| 25 | + |
| 26 | +import org.junit.jupiter.api.Order; |
| 27 | +import org.junit.jupiter.api.Test; |
| 28 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 29 | + |
| 30 | +import java.io.IOException; |
| 31 | +import java.net.InetSocketAddress; |
| 32 | +import java.net.URL; |
| 33 | +import java.nio.file.Files; |
| 34 | +import java.nio.file.Path; |
| 35 | +import java.util.stream.Collectors; |
| 36 | + |
| 37 | +import static org.assertj.core.api.Assertions.assertThat; |
| 38 | + |
| 39 | +/** Test that {@link SqlClient} works normally when SSL is enabled. */ |
| 40 | +class SqlClientSSLTest extends SqlClientTestBase { |
| 41 | + @RegisterExtension |
| 42 | + @Order(1) |
| 43 | + public static final SqlGatewayServiceExtension SQL_GATEWAY_SERVICE_EXTENSION = |
| 44 | + new SqlGatewayServiceExtension(Configuration::new); |
| 45 | + |
| 46 | + @RegisterExtension |
| 47 | + @Order(2) |
| 48 | + private static final SqlGatewayRestEndpointExtension SQL_GATEWAY_REST_ENDPOINT_EXTENSION = |
| 49 | + new SqlGatewayRestEndpointExtension( |
| 50 | + SQL_GATEWAY_SERVICE_EXTENSION::getService, SqlClientSSLTest::withSSL); |
| 51 | + |
| 52 | + private static final String truststorePath = getTestResource("ssl/local127.truststore"); |
| 53 | + |
| 54 | + private static final String keystorePath = getTestResource("ssl/local127.keystore"); |
| 55 | + |
| 56 | + @Test |
| 57 | + void testEmbeddedMode() throws Exception { |
| 58 | + String[] args = new String[] {"embedded"}; |
| 59 | + String actual = runSqlClient(args, String.join("\n", "SET;", "QUIT;"), false); |
| 60 | + assertThat(actual).contains(SecurityOptions.SSL_REST_ENABLED.key(), "true"); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testGatewayMode() throws Exception { |
| 65 | + String[] args = |
| 66 | + new String[] { |
| 67 | + "gateway", |
| 68 | + "-e", |
| 69 | + InetSocketAddress.createUnresolved( |
| 70 | + SQL_GATEWAY_REST_ENDPOINT_EXTENSION.getTargetAddress(), |
| 71 | + SQL_GATEWAY_REST_ENDPOINT_EXTENSION.getTargetPort()) |
| 72 | + .toString() |
| 73 | + }; |
| 74 | + String actual = runSqlClient(args, String.join("\n", "SET;", "QUIT;"), false); |
| 75 | + assertThat(actual).contains(SecurityOptions.SSL_REST_ENABLED.key(), "true"); |
| 76 | + } |
| 77 | + |
| 78 | + private static void withSSL(Configuration configuration) { |
| 79 | + configuration.set(SecurityOptions.SSL_REST_ENABLED, true); |
| 80 | + configuration.set(SecurityOptions.SSL_REST_TRUSTSTORE, truststorePath); |
| 81 | + configuration.set(SecurityOptions.SSL_REST_TRUSTSTORE_PASSWORD, "password"); |
| 82 | + configuration.set(SecurityOptions.SSL_REST_KEYSTORE, keystorePath); |
| 83 | + configuration.set(SecurityOptions.SSL_REST_KEYSTORE_PASSWORD, "password"); |
| 84 | + configuration.set(SecurityOptions.SSL_REST_KEY_PASSWORD, "password"); |
| 85 | + } |
| 86 | + |
| 87 | + @Override |
| 88 | + protected void writeConfigOptionsToConfYaml(Path confYamlPath) throws IOException { |
| 89 | + Configuration configuration = new Configuration(); |
| 90 | + withSSL(configuration); |
| 91 | + Files.write( |
| 92 | + confYamlPath, |
| 93 | + configuration.toMap().entrySet().stream() |
| 94 | + .map(entry -> entry.getKey() + ": " + entry.getValue()) |
| 95 | + .collect(Collectors.toList())); |
| 96 | + } |
| 97 | + |
| 98 | + private static String getTestResource(final String fileName) { |
| 99 | + final ClassLoader classLoader = ClassLoader.getSystemClassLoader(); |
| 100 | + final URL resource = classLoader.getResource(fileName); |
| 101 | + if (resource == null) { |
| 102 | + throw new IllegalArgumentException( |
| 103 | + String.format("Test resource %s does not exist", fileName)); |
| 104 | + } |
| 105 | + return resource.getFile(); |
| 106 | + } |
| 107 | +} |
0 commit comments