From fe207e5c72433270efdda22982dcd3fb75e2c55a Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 24 Jan 2025 09:22:18 +0000 Subject: [PATCH] Simplify test (no need for custom extractor) --- .../grpc/sample/GrpcServerApplication.java | 23 +++-------- .../sample/GrpcServerApplicationTests.java | 39 ++----------------- 2 files changed, 9 insertions(+), 53 deletions(-) diff --git a/samples/grpc-secure/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java b/samples/grpc-secure/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java index a875d4d..4a88659 100644 --- a/samples/grpc-secure/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java +++ b/samples/grpc-secure/src/main/java/org/springframework/grpc/sample/GrpcServerApplication.java @@ -10,10 +10,8 @@ import org.springframework.grpc.server.GlobalServerInterceptor; import org.springframework.grpc.server.security.GrpcSecurity; import org.springframework.security.config.annotation.authentication.configuration.AuthenticationConfiguration; import org.springframework.security.config.annotation.method.configuration.EnableMethodSecurity; -import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.userdetails.User; import org.springframework.security.provisioning.InMemoryUserDetailsManager; -import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken; import io.grpc.Metadata; import io.grpc.ServerInterceptor; @@ -40,24 +38,13 @@ public class GrpcServerApplication { @GlobalServerInterceptor public ServerInterceptor securityInterceptor(GrpcSecurity security) throws Exception { return security - .authorizeRequests(requests -> requests.methods("Simple/StreamHello") - .hasAuthority("ROLE_ADMIN") - .methods("Simple/SayHello") - .hasAuthority("ROLE_USER") - .methods("grpc.*/*") - .permitAll() - .allRequests() - .denyAll()) + .authorizeRequests(requests -> requests + .methods("Simple/StreamHello").hasAuthority("ROLE_ADMIN") + .methods("Simple/SayHello").hasAuthority("ROLE_USER") + .methods("grpc.*/*").permitAll() + .allRequests().denyAll()) .httpBasic(withDefaults()) .preauth(withDefaults()) - .authenticationExtractor((headers, attributes) -> { - String user = headers.get(USER_KEY); - if (user != null) { - return new PreAuthenticatedAuthenticationToken(user, "N/A", - AuthorityUtils.createAuthorityList("ROLE_" + user.toUpperCase())); - } - return null; - }) .build(); } diff --git a/samples/grpc-secure/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java b/samples/grpc-secure/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java index 913ecbf..3db8970 100644 --- a/samples/grpc-secure/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java +++ b/samples/grpc-secure/src/test/java/org/springframework/grpc/sample/GrpcServerApplicationTests.java @@ -25,12 +25,6 @@ import org.springframework.grpc.sample.proto.SimpleGrpc; import org.springframework.grpc.test.LocalGrpcPort; import org.springframework.test.annotation.DirtiesContext; -import io.grpc.CallOptions; -import io.grpc.Channel; -import io.grpc.ClientCall; -import io.grpc.ClientInterceptor; -import io.grpc.ForwardingClientCall.SimpleForwardingClientCall; -import io.grpc.MethodDescriptor; import io.grpc.Status.Code; import io.grpc.StatusRuntimeException; import io.grpc.reflection.v1.ServerReflectionGrpc; @@ -39,9 +33,7 @@ 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}", - "spring.grpc.client.channels.basic.address=static://0.0.0.0:${local.grpc.port}", - "spring.grpc.client.channels.secure.address=static://0.0.0.0:${local.grpc.port}" }) + "spring.grpc.client.channels.stub.address=static://0.0.0.0:${local.grpc.port}" }) public class GrpcServerApplicationTests { public static void main(String[] args) { @@ -56,10 +48,6 @@ public class GrpcServerApplicationTests { @Qualifier("reflect") private ServerReflectionGrpc.ServerReflectionStub reflect; - @Autowired - @Qualifier("secure") - private SimpleGrpc.SimpleBlockingStub secure; - @Autowired @Qualifier("basic") private SimpleGrpc.SimpleBlockingStub basic; @@ -107,14 +95,14 @@ public class GrpcServerApplicationTests { @DirtiesContext void unauthauthorized() { StatusRuntimeException exception = assertThrows(StatusRuntimeException.class, - () -> secure.streamHello(HelloRequest.newBuilder().setName("Alien").build()).next()); + () -> basic.streamHello(HelloRequest.newBuilder().setName("Alien").build()).next()); assertEquals(Code.PERMISSION_DENIED, exception.getStatus().getCode()); } @Test @DirtiesContext void authenticated() { - HelloReply response = secure.sayHello(HelloRequest.newBuilder().setName("Alien").build()); + HelloReply response = basic.sayHello(HelloRequest.newBuilder().setName("Alien").build()); assertEquals("Hello ==> Alien", response.getMessage()); } @@ -128,29 +116,10 @@ public class GrpcServerApplicationTests { @TestConfiguration static class ExtraConfiguration { - @Bean - @Lazy - SimpleGrpc.SimpleBlockingStub secure(GrpcChannelFactory channels) { - return SimpleGrpc.newBlockingStub(channels.createChannel("secure", - ChannelBuilderOptions.defaults().withInterceptors(List.of(new ClientInterceptor() { - @Override - public ClientCall interceptCall(MethodDescriptor method, - CallOptions callOptions, Channel next) { - return new SimpleForwardingClientCall(next.newCall(method, callOptions)) { - public void start(ClientCall.Listener responseListener, - io.grpc.Metadata headers) { - headers.put(GrpcServerApplication.USER_KEY, "user"); - super.start(responseListener, headers); - }; - }; - } - })))); - } - @Bean @Lazy SimpleGrpc.SimpleBlockingStub basic(GrpcChannelFactory channels) { - return SimpleGrpc.newBlockingStub(channels.createChannel("basic", ChannelBuilderOptions.defaults() + return SimpleGrpc.newBlockingStub(channels.createChannel("stub", ChannelBuilderOptions.defaults() .withInterceptors(List.of(new BasicAuthenticationInterceptor("user", "user"))))); }