Simplify test (no need for custom extractor)

This commit is contained in:
Dave Syer
2025-01-24 09:22:18 +00:00
parent 6cf4a74384
commit fe207e5c72
2 changed files with 9 additions and 53 deletions

View File

@@ -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();
}

View File

@@ -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 <ReqT, RespT> ClientCall<ReqT, RespT> interceptCall(MethodDescriptor<ReqT, RespT> method,
CallOptions callOptions, Channel next) {
return new SimpleForwardingClientCall<ReqT, RespT>(next.newCall(method, callOptions)) {
public void start(ClientCall.Listener<RespT> 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")))));
}