This is an automated email from the ASF dual-hosted git repository. jamesnetherton pushed a commit to branch camel-4.4.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-4.4.x by this push: new 9885daa22ed CAMEL-20534: Fix gRPC producer / consumer port validation 9885daa22ed is described below commit 9885daa22eda077144ab4fb93664d2df6b4a9796 Author: James Netherton <jamesnether...@gmail.com> AuthorDate: Thu Mar 7 09:54:37 2024 +0000 CAMEL-20534: Fix gRPC producer / consumer port validation --- .../camel/component/grpc/GrpcConfiguration.java | 2 +- .../apache/camel/component/grpc/GrpcConsumer.java | 2 +- .../apache/camel/component/grpc/GrpcProducer.java | 2 +- .../grpc/GrpcConsumerConfigurationTest.java | 47 ++++++++++++++++++++++ .../grpc/GrpcProducerConfigurationTest.java | 42 +++++++++++++++++++ 5 files changed, 92 insertions(+), 3 deletions(-) diff --git a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConfiguration.java b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConfiguration.java index 098759b145e..41067736feb 100644 --- a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConfiguration.java +++ b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConfiguration.java @@ -40,7 +40,7 @@ public class GrpcConfiguration { @UriPath @Metadata(required = true) - private int port; + private int port = -1; @UriPath @Metadata(required = true) diff --git a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConsumer.java b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConsumer.java index d41ef7a1756..93ecb2cace2 100644 --- a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConsumer.java +++ b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcConsumer.java @@ -93,7 +93,7 @@ public class GrpcConsumer extends DefaultConsumer { BindableService bindableService = getBindableServiceFactory().createBindableService(this); ServerInterceptor headerInterceptor = new GrpcHeaderInterceptor(); - if (!ObjectHelper.isEmpty(configuration.getHost()) && !ObjectHelper.isEmpty(configuration.getPort())) { + if (ObjectHelper.isNotEmpty(configuration.getHost()) && configuration.getPort() > 0) { LOG.debug("Building gRPC server on {}:{}", configuration.getHost(), configuration.getPort()); serverBuilder = NettyServerBuilder.forAddress(new InetSocketAddress(configuration.getHost(), configuration.getPort())); diff --git a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcProducer.java b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcProducer.java index 3290221debd..b6b7fc64d35 100644 --- a/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcProducer.java +++ b/components/camel-grpc/src/main/java/org/apache/camel/component/grpc/GrpcProducer.java @@ -139,7 +139,7 @@ public class GrpcProducer extends DefaultAsyncProducer { protected void initializeChannel() throws Exception { NettyChannelBuilder channelBuilder; - if (!ObjectHelper.isEmpty(configuration.getHost()) && !ObjectHelper.isEmpty(configuration.getPort())) { + if (ObjectHelper.isNotEmpty(configuration.getHost()) && configuration.getPort() > 0) { LOG.info("Creating channel to the remote gRPC server {}:{}", configuration.getHost(), configuration.getPort()); channelBuilder = NettyChannelBuilder.forAddress(configuration.getHost(), configuration.getPort()); } else { diff --git a/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcConsumerConfigurationTest.java b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcConsumerConfigurationTest.java new file mode 100644 index 00000000000..0b8b1c38908 --- /dev/null +++ b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcConsumerConfigurationTest.java @@ -0,0 +1,47 @@ +/* + * 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.camel.component.grpc; + +import org.apache.camel.FailedToCreateConsumerException; +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class GrpcConsumerConfigurationTest extends CamelTestSupport { + @Test + void emptyHostPort() { + FailedToCreateConsumerException exception = assertThrows(FailedToCreateConsumerException.class, + () -> consumer.receive("grpc:/org.apache.camel.component.grpc.PingPong")); + assertInstanceOf(IllegalArgumentException.class, exception.getCause()); + } + + @Test + void emptyPort() { + FailedToCreateConsumerException exception = assertThrows(FailedToCreateConsumerException.class, + () -> consumer.receive("grpc:localhost/org.apache.camel.component.grpc.PingPong")); + assertInstanceOf(IllegalArgumentException.class, exception.getCause()); + } + + @Test + void invalidPort() { + FailedToCreateConsumerException exception = assertThrows(FailedToCreateConsumerException.class, + () -> consumer.receive("grpc:localhost:0/org.apache.camel.component.grpc.PingPong")); + assertInstanceOf(IllegalArgumentException.class, exception.getCause()); + } +} diff --git a/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerConfigurationTest.java b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerConfigurationTest.java new file mode 100644 index 00000000000..f42b64d1cf9 --- /dev/null +++ b/components/camel-grpc/src/test/java/org/apache/camel/component/grpc/GrpcProducerConfigurationTest.java @@ -0,0 +1,42 @@ +/* + * 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.camel.component.grpc; + +import org.apache.camel.test.junit5.CamelTestSupport; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +class GrpcProducerConfigurationTest extends CamelTestSupport { + @Test + void emptyHostPort() { + assertThrows(IllegalArgumentException.class, + () -> template.requestBody("grpc:/org.apache.camel.component.grpc.PingPong")); + } + + @Test + void emptyPort() { + assertThrows(IllegalArgumentException.class, + () -> template.requestBody("grpc:localhost/org.apache.camel.component.grpc.PingPong")); + } + + @Test + void invalidPort() { + assertThrows(IllegalArgumentException.class, + () -> template.requestBody("grpc:localhost:0/org.apache.camel.component.grpc.PingPong")); + } +}