Add support for events and anonymous

This commit is contained in:
Dave Syer
2025-01-23 13:03:07 +00:00
parent ff40f5b323
commit 5433d422c0
5 changed files with 91 additions and 10 deletions

View File

@@ -44,8 +44,10 @@ public class GrpcServerApplication {
.hasAuthority("ROLE_ADMIN")
.methods("Simple/SayHello")
.hasAuthority("ROLE_USER")
.methods("grpc.*/*")
.permitAll()
.allRequests()
.permitAll())
.denyAll())
.httpBasic(withDefaults())
.preauth(withDefaults())
.authenticationExtractor((headers, attributes) -> {

View File

@@ -4,7 +4,10 @@ import static org.junit.Assert.assertThrows;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.List;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import org.awaitility.Awaitility;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
@@ -30,6 +33,10 @@ import io.grpc.ForwardingClientCall.SimpleForwardingClientCall;
import io.grpc.MethodDescriptor;
import io.grpc.Status.Code;
import io.grpc.StatusRuntimeException;
import io.grpc.reflection.v1.ServerReflectionGrpc;
import io.grpc.reflection.v1.ServerReflectionRequest;
import io.grpc.reflection.v1.ServerReflectionResponse;
import io.grpc.stub.StreamObserver;
@SpringBootTest(properties = { "spring.grpc.server.port=0",
"spring.grpc.client.channels.stub.address=static://0.0.0.0:${local.grpc.port}",
@@ -45,6 +52,10 @@ public class GrpcServerApplicationTests {
@Qualifier("stub")
private SimpleGrpc.SimpleBlockingStub stub;
@Autowired
@Qualifier("reflect")
private ServerReflectionGrpc.ServerReflectionStub reflect;
@Autowired
@Qualifier("secure")
private SimpleGrpc.SimpleBlockingStub secure;
@@ -66,6 +77,32 @@ public class GrpcServerApplicationTests {
assertEquals(Code.UNAUTHENTICATED, exception.getStatus().getCode());
}
@Test
@DirtiesContext
void anonymous() throws Exception {
AtomicReference<ServerReflectionResponse> response = new AtomicReference<>();
AtomicBoolean error = new AtomicBoolean();
StreamObserver<ServerReflectionResponse> responses = new StreamObserver<ServerReflectionResponse>() {
@Override
public void onNext(ServerReflectionResponse value) {
response.set(value);
}
@Override
public void onError(Throwable t) {
error.set(true);
}
@Override
public void onCompleted() {
}
};
StreamObserver<ServerReflectionRequest> request = reflect.serverReflectionInfo(responses);
request.onNext(ServerReflectionRequest.newBuilder().setListServices("").build());
request.onCompleted();
Awaitility.await().until(() -> response.get() != null || error.get());
}
@Test
@DirtiesContext
void unauthauthorized() {
@@ -114,7 +151,7 @@ public class GrpcServerApplicationTests {
@Lazy
SimpleGrpc.SimpleBlockingStub basic(GrpcChannelFactory channels) {
return SimpleGrpc.newBlockingStub(channels.createChannel("basic", ChannelBuilderOptions.defaults()
.withInterceptors(List.of(new BasicAuthenticationInterceptor("user", "user")))));
.withInterceptors(List.of(new BasicAuthenticationInterceptor("user", "user")))));
}
@Bean
@@ -123,6 +160,12 @@ public class GrpcServerApplicationTests {
return SimpleGrpc.newBlockingStub(channels.createChannel("stub"));
}
@Bean
@Lazy
ServerReflectionGrpc.ServerReflectionStub reflect(GrpcChannelFactory channels, @LocalGrpcPort int port) {
return ServerReflectionGrpc.newStub(channels.createChannel("stub"));
}
}
}