Skip to content

Commit 5ea3c6d

Browse files
author
Alexander Furer
committed
grpc security config fixes
1 parent 8cc833a commit 5ea3c6d

File tree

5 files changed

+31
-27
lines changed

5 files changed

+31
-27
lines changed

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/auth/DefaultAuthConfigTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,14 @@ static class TestCfg extends GrpcSecurityConfigurerAdapter {
3030

3131
}
3232

33+
public DefaultAuthConfigTest() {
34+
super(false);
35+
}
36+
3337
@Test
3438
public void securedServiceTest() {
3539

36-
final SecuredGreeterGrpc.SecuredGreeterBlockingStub securedFutureStub = SecuredGreeterGrpc.newBlockingStub(selectedChanel);
40+
final SecuredGreeterGrpc.SecuredGreeterBlockingStub securedFutureStub = SecuredGreeterGrpc.newBlockingStub(getChannel(true));
3741

3842
final String reply = securedFutureStub.sayAuthHello(Empty.getDefaultInstance()).getMessage();
3943
assertNotNull("Reply should not be null",reply);

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/auth/JwtAuthBaseTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,15 +37,22 @@ public JwtAuthBaseTest() {
3737

3838
@Override
3939
protected Channel getChannel() {
40-
final AuthClientInterceptor clientInterceptor = new AuthClientInterceptor(
41-
AuthHeader.builder().bearer().tokenSupplier(this::generateToken));
42-
return globalSecuredChannel ? ClientInterceptors.intercept(super.getChannel(), clientInterceptor)
43-
: super.getChannel();
40+
return getChannel(globalSecuredChannel);
41+
4442
}
43+
protected Channel getChannel(boolean authenticated){
44+
return authenticated ? ClientInterceptors.intercept(super.getChannel(), getAuthClientInterceptor())
45+
: super.getChannel();
4546

47+
}
4648
protected final static String USER_NAME = "keycloak-test";
4749

4850

51+
protected AuthClientInterceptor getAuthClientInterceptor() {
52+
return new AuthClientInterceptor(
53+
AuthHeader.builder().bearer().tokenSupplier(this::generateToken));
54+
}
55+
4956
protected ByteBuffer generateToken() {
5057
if (authServerUrl.isEmpty()) {
5158
return ByteBuffer.wrap(UUID.randomUUID().toString().getBytes());

grpc-spring-boot-starter-demo/src/test/java/org/lognet/springboot/grpc/auth/JwtRoleTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,6 @@ public class DemoGrpcSecurityConfig extends GrpcSecurityConfigurerAdapter {
5555

5656
@Override
5757
public void configure(GrpcSecurity builder) throws Exception {
58-
59-
super.configure(builder);
6058
builder.authorizeRequests()
6159
.methods(GreeterGrpc.getSayHelloMethod()).hasAnyRole("reader")
6260
.methods(CalculatorGrpc.getCalculateMethod()).hasAnyRole("anotherRole")
@@ -146,4 +144,5 @@ public void shouldFail() {
146144

147145
}
148146

147+
149148
}

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/security/AuthenticationSchemeService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,11 @@ public Optional<Authentication> getAuthScheme(CharSequence authorization) {
2323
.collect(Collectors.toList());
2424
switch (auth.size()){
2525
case 0:
26-
throw new IllegalStateException("Authentication scheme not supported");
26+
throw new IllegalStateException("Authentication scheme" +authorization.toString() +"not supported");
2727
case 1 :
2828
return Optional.of(auth.get(0));
2929
default:
30-
throw new IllegalStateException("Ambiguous authentication scheme");
30+
throw new IllegalStateException("Ambiguous authentication scheme "+authorization.toString());
3131
}
3232
}
3333

grpc-spring-boot-starter/src/main/java/org/lognet/springboot/grpc/security/GrpcServiceAuthorizationConfigurer.java

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import io.grpc.BindableService;
44
import io.grpc.MethodDescriptor;
55
import io.grpc.ServerInterceptor;
6+
import io.grpc.ServerMethodDefinition;
67
import io.grpc.ServerServiceDefinition;
78
import io.grpc.ServiceDescriptor;
89
import org.springframework.context.ApplicationContext;
@@ -115,24 +116,17 @@ public GrpcSecurity withSecuredAnnotation() {
115116
}
116117
}
117118
// method level security
118-
serverServiceDefinition.getMethods()
119-
.stream()
120-
.map(methodDefinition -> Stream.of(service.getClass().getMethods()) // get method from methodDefinition
121-
.filter(m -> {
122-
final String methodName = methodDefinition.getMethodDescriptor().getFullMethodName().substring(methodDefinition.getMethodDescriptor().getServiceName().length() + 1);
123-
return methodName.equalsIgnoreCase(m.getName());
124-
})
125-
.findFirst()
126-
)
127-
.filter(Optional::isPresent)
128-
.map(Optional::get)
129-
.map(m ->Optional.ofNullable(AnnotationUtils.findAnnotation(m, Secured.class)))
130-
.filter(Optional::isPresent)
131-
.forEach(secured ->
132-
new AuthorizedMethod(serverServiceDefinition.getServiceDescriptor())
133-
.hasAnyAuthority(secured.get().value())
134-
135-
);
119+
for(ServerMethodDefinition<?,?> methodDefinition :serverServiceDefinition.getMethods()){
120+
Stream.of(service.getClass().getMethods()) // get method from methodDefinition
121+
.filter(m -> {
122+
final String methodName = methodDefinition.getMethodDescriptor().getFullMethodName().substring(methodDefinition.getMethodDescriptor().getServiceName().length() + 1);
123+
return methodName.equalsIgnoreCase(m.getName());
124+
})
125+
.findFirst()
126+
.flatMap(m->Optional.ofNullable(AnnotationUtils.findAnnotation(m, Secured.class)))
127+
.ifPresent(secured -> new AuthorizedMethod(methodDefinition.getMethodDescriptor()) .hasAnyAuthority(secured.value()));
128+
129+
}
136130
}
137131
return and();
138132
}

0 commit comments

Comments
 (0)