forked from LogNet/grpc-spring-boot-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Example of method security & exception translation
- Loading branch information
Pavel Grushetzky
committed
Oct 24, 2017
1 parent
7dfd3e8
commit 15ab3c0
Showing
5 changed files
with
166 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...t-starter-demo/src/main/java/org/lognet/springboot/grpc/demo/AuthExceptionTranslator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package org.lognet.springboot.grpc.demo; | ||
|
||
import io.grpc.*; | ||
import org.springframework.security.access.AccessDeniedException; | ||
import org.springframework.security.core.AuthenticationException; | ||
|
||
public class AuthExceptionTranslator implements ServerInterceptor { | ||
public static final String DEFAULT_DESCRIPTION = "Authentication/Authorization failed"; | ||
|
||
@Override | ||
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { | ||
|
||
ServerCall.Listener<ReqT> listener = null; | ||
try { | ||
// streaming calls may error out in startCall | ||
listener = next.startCall(call, headers); | ||
} catch (AuthenticationException | AccessDeniedException aex) { | ||
closeCallAndRethrow(call, aex); | ||
} | ||
|
||
ForwardingServerCallListener.SimpleForwardingServerCallListener exceptionTranslationListener = new ForwardingServerCallListener.SimpleForwardingServerCallListener<ReqT>(listener) { | ||
@Override | ||
public void onHalfClose() { | ||
try { | ||
// unary calls may error out here | ||
super.onHalfClose(); | ||
} catch (AuthenticationException | AccessDeniedException aex) { | ||
closeCallAndRethrow(call, aex); | ||
} | ||
} | ||
}; | ||
|
||
return exceptionTranslationListener; | ||
} | ||
|
||
private <ReqT, RespT> void closeCallAndRethrow(ServerCall<ReqT, RespT> call, RuntimeException aex) { | ||
call.close(Status.PERMISSION_DENIED.withCause(aex).withDescription(DEFAULT_DESCRIPTION), new Metadata()); | ||
throw aex; | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
...boot-starter-demo/src/main/java/org/lognet/springboot/grpc/demo/GrantRoleInterceptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package org.lognet.springboot.grpc.demo; | ||
|
||
import io.grpc.*; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
|
||
import java.util.Collections; | ||
|
||
public class GrantRoleInterceptor implements ServerInterceptor { | ||
private final String role; | ||
|
||
public GrantRoleInterceptor(String role) { | ||
this.role = role; | ||
} | ||
|
||
@Override | ||
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) { | ||
final Authentication authentication = new UsernamePasswordAuthenticationToken("user", "user", | ||
Collections.singletonList(new SimpleGrantedAuthority(role))); | ||
|
||
ServerCall.Listener<ReqT> listener = next.startCall(call, headers); | ||
ForwardingServerCallListener.SimpleForwardingServerCallListener authEnabledListener = new ForwardingServerCallListener.SimpleForwardingServerCallListener(listener) { | ||
@Override | ||
public void onHalfClose() { | ||
SecurityContextHolder.getContext().setAuthentication(authentication); | ||
try { | ||
super.onHalfClose(); | ||
} finally { | ||
SecurityContextHolder.getContext().setAuthentication(null); | ||
} | ||
} | ||
|
||
}; | ||
|
||
return authEnabledListener; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
...pring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/GreeterWithAuthTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.lognet.springboot.grpc; | ||
|
||
import io.grpc.ManagedChannel; | ||
import io.grpc.ManagedChannelBuilder; | ||
import io.grpc.Status; | ||
import io.grpc.StatusRuntimeException; | ||
import io.grpc.examples.GreeterGrpc; | ||
import io.grpc.examples.GreeterOuterClass; | ||
import org.hamcrest.Matchers; | ||
import org.junit.After; | ||
import org.junit.Before; | ||
import org.junit.Rule; | ||
import org.junit.Test; | ||
import org.junit.rules.ExpectedException; | ||
import org.junit.runner.RunWith; | ||
import org.lognet.springboot.grpc.demo.AuthExceptionTranslator; | ||
import org.lognet.springboot.grpc.demo.DemoApp; | ||
import org.lognet.springboot.grpc.demo.GrantRoleInterceptor; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
|
||
import static org.hamcrest.Matchers.hasProperty; | ||
import static org.hamcrest.Matchers.is; | ||
|
||
@RunWith(SpringRunner.class) | ||
@SpringBootTest(classes = {DemoApp.class, GreeterWithAuthTest.Config.class}, | ||
webEnvironment = SpringBootTest.WebEnvironment.NONE, | ||
properties = {"grpc.port=8081", | ||
"spring.aop.proxy-target-class=true" | ||
}) | ||
public class GreeterWithAuthTest { | ||
private ManagedChannel channel; | ||
private GreeterGrpc.GreeterBlockingStub client; | ||
@Rule | ||
public ExpectedException exception = ExpectedException.none(); | ||
|
||
@Before | ||
public void setupChannel() { | ||
channel = ManagedChannelBuilder.forAddress("localhost", 8081) | ||
.usePlaintext(true).build(); | ||
client = GreeterGrpc.newBlockingStub(channel); | ||
} | ||
|
||
@After | ||
public void shutdownChannel() { | ||
channel.shutdownNow(); | ||
} | ||
|
||
@Test | ||
public void securityExceptionPropagatesToClient() { | ||
// given | ||
exception.expect(StatusRuntimeException.class); | ||
exception.expect(hasProperty("status", hasProperty("code", is(Status.Code.PERMISSION_DENIED)))); | ||
exception.expectMessage(AuthExceptionTranslator.DEFAULT_DESCRIPTION); | ||
|
||
// when | ||
GreeterOuterClass.HelloRequest request = GreeterOuterClass.HelloRequest.newBuilder().setName("world").build(); | ||
GreeterOuterClass.HelloReply helloReply = client.sayHello(request); | ||
|
||
// then expected exception occurs | ||
} | ||
|
||
@Configuration | ||
@EnableGlobalMethodSecurity(securedEnabled = true) | ||
static class Config { | ||
@Bean | ||
@GRpcGlobalInterceptor | ||
public AuthExceptionTranslator authExceptionTranslator() { | ||
return new AuthExceptionTranslator(); | ||
} | ||
|
||
@Bean | ||
@GRpcGlobalInterceptor | ||
public GrantRoleInterceptor grantRole() { | ||
return new GrantRoleInterceptor("NON_ADMIN"); | ||
} | ||
|
||
} | ||
} |
15ab3c0
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.
WARNING: This example is vulnerable to concurrency issues!
See also: LogNet#61 (comment)