nsivabalan commented on a change in pull request #3097: URL: https://github.com/apache/hudi/pull/3097#discussion_r659090902
########## File path: hudi-utilities/src/main/java/org/apache/hudi/utilities/schema/SchemaRegistryProvider.java ########## @@ -48,19 +54,40 @@ "hoodie.deltastreamer.schemaprovider.registry.targetUrl"; } - private static String fetchSchemaFromRegistry(String registryUrl) throws IOException { - URL registry = new URL(registryUrl); + public String fetchSchemaFromRegistry(String registryUrl) throws IOException { + URL registry; + HttpURLConnection connection; + Matcher matcher = Pattern.compile("://(.*?)@").matcher(registryUrl); + if (matcher.find()) { + String creds = matcher.group(1); + String urlWithoutCreds = registryUrl.replace(creds + "@", ""); + registry = new URL(urlWithoutCreds); + connection = (HttpURLConnection) registry.openConnection(); + setAuthorizationHeader(matcher.group(1), connection); + } else { + registry = new URL(registryUrl); + connection = (HttpURLConnection) registry.openConnection(); + } ObjectMapper mapper = new ObjectMapper(); - JsonNode node = mapper.readTree(registry.openStream()); + JsonNode node = mapper.readTree(getStream(connection)); return node.get("schema").asText(); } + public void setAuthorizationHeader(String creds, HttpURLConnection connection) { + String encodedAuth = Base64.getEncoder().encodeToString(creds.getBytes(StandardCharsets.UTF_8)); + connection.setRequestProperty("Authorization", "Basic " + encodedAuth); + } + + public InputStream getStream(HttpURLConnection connection) throws IOException { + return connection.getInputStream(); Review comment: do we need a method for one line code. also, I don't see it being used in more than 1 place. ########## File path: hudi-utilities/src/test/java/org/apache/hudi/utilities/schema/SchemaRegistryProviderTest.java ########## @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hudi.utilities.schema; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.avro.Schema; +import org.apache.hudi.common.config.TypedProperties; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +class SchemaRegistryProviderTest { + + private final String basicAuth = "foo:bar"; + + private final String json = "{\"schema\":\"{\\\"type\\\": \\\"record\\\", \\\"namespace\\\": \\\"example\\\", " + + "\\\"name\\\": \\\"FullName\\\",\\\"fields\\\": [{ \\\"name\\\": \\\"first\\\", \\\"type\\\": " + + "\\\"string\\\" }]}\"}"; + + private TypedProperties getProps() { + return new TypedProperties() {{ + put("hoodie.deltastreamer.schemaprovider.registry.baseUrl", "http://" + basicAuth + "@localhost"); + put("hoodie.deltastreamer.schemaprovider.registry.urlSuffix", "-value"); + put("hoodie.deltastreamer.schemaprovider.registry.url", "http://foo:bar@localhost"); + put("hoodie.deltastreamer.source.kafka.topic", "foo"); + }}; + } + + Schema getExpectedSchema(String response) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + JsonNode node = mapper.readTree(new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8))); + return (new Schema.Parser()).parse(node.get("schema").asText()); + } + + @Test + public void testGetSourceSchemaShouldRequestSchemaWithCreds() throws IOException { + InputStream is = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); + SchemaRegistryProvider underTest = new SchemaRegistryProvider(getProps(), null); + SchemaRegistryProvider spyUnderTest = Mockito.spy(underTest); + Mockito.doReturn(is).when(spyUnderTest).getStream(Mockito.any()); + Schema actual = spyUnderTest.getSourceSchema(); + assertNotNull(actual); + assertEquals(actual, getExpectedSchema(json)); + verify(spyUnderTest, times(1)).setAuthorizationHeader(eq(basicAuth), + Mockito.any(HttpURLConnection.class)); + } + + @Test + public void testGetTargetSchemaShouldRequestSchemaWithCreds() throws IOException { + InputStream is = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); Review comment: Can we re-use code across previous test and this one. except for 1 or 2 lines, mostly its same. we can create a private method w/ an argument for whether its source schema or target schema ########## File path: hudi-utilities/src/main/java/org/apache/hudi/utilities/schema/SchemaRegistryProvider.java ########## @@ -48,19 +54,40 @@ "hoodie.deltastreamer.schemaprovider.registry.targetUrl"; } - private static String fetchSchemaFromRegistry(String registryUrl) throws IOException { - URL registry = new URL(registryUrl); + public String fetchSchemaFromRegistry(String registryUrl) throws IOException { + URL registry; + HttpURLConnection connection; + Matcher matcher = Pattern.compile("://(.*?)@").matcher(registryUrl); + if (matcher.find()) { + String creds = matcher.group(1); Review comment: would be nice to have some docs here to explain what we are doing w/ this regex matcher etc, which explains each part etc. May be if you can give an example, would be easy to comprehend ########## File path: hudi-utilities/src/main/java/org/apache/hudi/utilities/schema/SchemaRegistryProvider.java ########## @@ -48,19 +54,40 @@ "hoodie.deltastreamer.schemaprovider.registry.targetUrl"; } - private static String fetchSchemaFromRegistry(String registryUrl) throws IOException { - URL registry = new URL(registryUrl); + public String fetchSchemaFromRegistry(String registryUrl) throws IOException { + URL registry; + HttpURLConnection connection; + Matcher matcher = Pattern.compile("://(.*?)@").matcher(registryUrl); + if (matcher.find()) { + String creds = matcher.group(1); + String urlWithoutCreds = registryUrl.replace(creds + "@", ""); + registry = new URL(urlWithoutCreds); + connection = (HttpURLConnection) registry.openConnection(); + setAuthorizationHeader(matcher.group(1), connection); + } else { + registry = new URL(registryUrl); + connection = (HttpURLConnection) registry.openConnection(); + } ObjectMapper mapper = new ObjectMapper(); - JsonNode node = mapper.readTree(registry.openStream()); + JsonNode node = mapper.readTree(getStream(connection)); return node.get("schema").asText(); } + public void setAuthorizationHeader(String creds, HttpURLConnection connection) { Review comment: may I know why this needs to be public? ########## File path: hudi-utilities/src/test/java/org/apache/hudi/utilities/schema/SchemaRegistryProviderTest.java ########## @@ -0,0 +1,116 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hudi.utilities.schema; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.avro.Schema; +import org.apache.hudi.common.config.TypedProperties; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +class SchemaRegistryProviderTest { + + private final String basicAuth = "foo:bar"; + + private final String json = "{\"schema\":\"{\\\"type\\\": \\\"record\\\", \\\"namespace\\\": \\\"example\\\", " + + "\\\"name\\\": \\\"FullName\\\",\\\"fields\\\": [{ \\\"name\\\": \\\"first\\\", \\\"type\\\": " + + "\\\"string\\\" }]}\"}"; + + private TypedProperties getProps() { + return new TypedProperties() {{ + put("hoodie.deltastreamer.schemaprovider.registry.baseUrl", "http://" + basicAuth + "@localhost"); + put("hoodie.deltastreamer.schemaprovider.registry.urlSuffix", "-value"); + put("hoodie.deltastreamer.schemaprovider.registry.url", "http://foo:bar@localhost"); + put("hoodie.deltastreamer.source.kafka.topic", "foo"); + }}; + } + + Schema getExpectedSchema(String response) throws IOException { + ObjectMapper mapper = new ObjectMapper(); + JsonNode node = mapper.readTree(new ByteArrayInputStream(response.getBytes(StandardCharsets.UTF_8))); + return (new Schema.Parser()).parse(node.get("schema").asText()); + } + + @Test + public void testGetSourceSchemaShouldRequestSchemaWithCreds() throws IOException { + InputStream is = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); + SchemaRegistryProvider underTest = new SchemaRegistryProvider(getProps(), null); + SchemaRegistryProvider spyUnderTest = Mockito.spy(underTest); + Mockito.doReturn(is).when(spyUnderTest).getStream(Mockito.any()); + Schema actual = spyUnderTest.getSourceSchema(); + assertNotNull(actual); + assertEquals(actual, getExpectedSchema(json)); + verify(spyUnderTest, times(1)).setAuthorizationHeader(eq(basicAuth), + Mockito.any(HttpURLConnection.class)); + } + + @Test + public void testGetTargetSchemaShouldRequestSchemaWithCreds() throws IOException { + InputStream is = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); + SchemaRegistryProvider underTest = new SchemaRegistryProvider(getProps(), null); + SchemaRegistryProvider spyUnderTest = Mockito.spy(underTest); + Mockito.doReturn(is).when(spyUnderTest).getStream(Mockito.any()); + Schema actual = spyUnderTest.getTargetSchema(); + assertNotNull(actual); + assertEquals(actual, getExpectedSchema(json)); + verify(spyUnderTest, times(1)).setAuthorizationHeader(eq(basicAuth), + Mockito.any(HttpURLConnection.class)); + } + + @Test + public void testGetSourceSchemaShouldRequestSchemaWithoutCreds() throws IOException { + TypedProperties props = getProps(); + props.put("hoodie.deltastreamer.schemaprovider.registry.url", "http://localhost"); + InputStream is = new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)); + SchemaRegistryProvider underTest = new SchemaRegistryProvider(props, null); + SchemaRegistryProvider spyUnderTest = Mockito.spy(underTest); + Mockito.doReturn(is).when(spyUnderTest).getStream(Mockito.any()); + Schema actual = spyUnderTest.getSourceSchema(); + assertNotNull(actual); + assertEquals(actual, getExpectedSchema(json)); + verify(spyUnderTest, times(0)).setAuthorizationHeader(Mockito.any(), Mockito.any()); + } + + @Test + public void testGetTargetSchemaShouldRequestSchemaWithoutCreds() throws IOException { + TypedProperties props = getProps(); + props.put("hoodie.deltastreamer.schemaprovider.registry.url", "http://localhost"); Review comment: same comment as above. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@hudi.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org