-
Notifications
You must be signed in to change notification settings - Fork 64
Bind to configured IP address on netty servers #320
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,8 @@ | |
|
|
||
| package org.springframework.grpc.server; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.net.SocketAddress; | ||
| import java.util.LinkedHashSet; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
|
|
@@ -31,6 +33,7 @@ | |
|
|
||
| import org.springframework.grpc.internal.GrpcUtils; | ||
| import org.springframework.grpc.server.service.ServerInterceptorFilter; | ||
| import org.springframework.util.StringUtils; | ||
|
|
||
| import io.grpc.Grpc; | ||
| import io.grpc.InsecureServerCredentials; | ||
|
|
@@ -190,4 +193,26 @@ protected void configureServices(T builder, List<ServerServiceDefinition> servic | |
| }); | ||
| } | ||
|
|
||
| protected String hostName() { | ||
| return address().split(":")[0].trim(); | ||
| } | ||
|
|
||
| protected SocketAddress socketAddress() { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer moving this to |
||
| String address = address(); | ||
| if (address.startsWith("unix:")) { | ||
| throw new UnsupportedOperationException("Unix socket addresses not supported"); | ||
| } | ||
| if (address.startsWith("in-process:")) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think |
||
| throw new UnsupportedOperationException("In-Process addresses not supported"); | ||
| } | ||
|
|
||
| var host = hostName(); | ||
| if (StringUtils.hasText(host) && !Objects.equals(host, "*")) { | ||
| return new InetSocketAddress(host, port()); | ||
| } | ||
| else { | ||
| return new InetSocketAddress(port()); | ||
| } | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -55,7 +55,7 @@ protected NettyServerBuilder newServerBuilder() { | |
| .bossEventLoopGroup(new EpollEventLoopGroup(1)) | ||
| .workerEventLoopGroup(new EpollEventLoopGroup()); | ||
| } | ||
| return super.newServerBuilder(); | ||
| return NettyServerBuilder.forAddress(socketAddress(), credentials()); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this replacement makes sense and is "safe" as the previous code called: Grpc.newServerBuilderForPort(port(), credentials());which ultimately called: ProtocolNegotiators.FromServerCredentialsResult result = ProtocolNegotiators.from(creds);
if (result.error != null) {
return NewServerBuilderResult.error(result.error);
}
return NewServerBuilderResult.serverBuilder(
new NettyServerBuilder(new InetSocketAddress(port), result.negotiator));and the new code Previously we were simply disregarding any host info (as noted in your issue). |
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,13 +17,18 @@ | |
| package org.springframework.grpc.server; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.assertj.core.api.Assertions.assertThatExceptionOfType; | ||
| import static org.mockito.Mockito.mock; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.net.SocketAddress; | ||
| import java.util.List; | ||
|
|
||
| import org.assertj.core.api.InstanceOfAssertFactories; | ||
| import org.junit.jupiter.api.DynamicTest; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.TestFactory; | ||
|
|
||
| import io.grpc.ServerServiceDefinition; | ||
|
|
||
|
|
@@ -83,4 +88,37 @@ void whenFilterAllowsOneThenOneServiceAdded() { | |
|
|
||
| } | ||
|
|
||
| @Nested | ||
| class AddressParser { | ||
|
|
||
| @TestFactory | ||
| List<DynamicTest> ipAddress() { | ||
| return List.of(testIpAddress(":9999", new InetSocketAddress(9999)), | ||
| testIpAddress("localhost:9999", new InetSocketAddress("localhost", 9999)), | ||
| testIpAddress("localhost", new InetSocketAddress("localhost", 9090)), | ||
| testIpAddress("*", new InetSocketAddress(9090)), | ||
| testIpAddress("*:8888", new InetSocketAddress(8888)), | ||
| testIpAddress("", new InetSocketAddress(9090))); | ||
| } | ||
|
|
||
| private DynamicTest testIpAddress(String address, SocketAddress expected) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎉 nice use of |
||
| return DynamicTest.dynamicTest("Socket address: " + address, () -> { | ||
| var factory = new DefaultGrpcServerFactory<>(address, List.of()); | ||
| assertThat(factory.socketAddress()).isEqualTo(expected); | ||
| }); | ||
| } | ||
|
|
||
| @TestFactory | ||
| List<DynamicTest> unsupportedAddress() { | ||
| return List.of(testThrows("unix:dummy"), testThrows("in-process:")); | ||
| } | ||
|
|
||
| private DynamicTest testThrows(String address) { | ||
| return DynamicTest.dynamicTest("Socket address: " + address, | ||
| () -> assertThatExceptionOfType(UnsupportedOperationException.class) | ||
| .isThrownBy(() -> new DefaultGrpcServerFactory<>(address, List.of()).socketAddress())); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think this will handle all cases as users could pass in arbitrary addresses (e.g.
12::34::....). I think we need to use a regex to cover all cases. Wdyt?