Skip to content

Commit

Permalink
closes #175
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexander Furer committed Oct 27, 2021
1 parent 8f6bca6 commit ded2743
Show file tree
Hide file tree
Showing 35 changed files with 958 additions and 255 deletions.
6 changes: 3 additions & 3 deletions grpc-spring-boot-starter-demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ dependencies {
testCompile 'org.springframework.boot:spring-boot-starter-test'
testCompile 'com.github.stefanbirkner:system-rules:1.18.0'
testCompile('org.springframework.cloud:spring-cloud-starter-consul-discovery')
testCompile 'com.pszymczyk.consul:embedded-consul:2.2.1'
testCompile 'org.awaitility:awaitility:4.0.3'
testCompile "org.springframework.cloud:spring-cloud-config-server"
testCompile "org.springframework.cloud:spring-cloud-config-client"
testCompile "org.springframework.cloud:spring-cloud-starter-bootstrap"

testCompile "com.playtika.testcontainers:embedded-keycloak:2.0.14"
testCompile "com.playtika.testcontainers:embedded-keycloak:2.0.16"
testCompile "com.playtika.testcontainers:embedded-consul:2.0.16"

testImplementation 'org.hamcrest:hamcrest:2.1'
testImplementation 'org.hamcrest:hamcrest:2.2'
testImplementation 'org.mockito:mockito-core:2.23.0'

customSecurityTestCompile sourceSets.test.output
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import org.lognet.springboot.grpc.demo.DemoApp;
import org.lognet.springboot.grpc.security.AuthCallCredentials;
import org.lognet.springboot.grpc.security.AuthHeader;
import org.lognet.springboot.grpc.security.EnableGrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -33,33 +32,27 @@

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {DemoApp.class}, webEnvironment = NONE)
@Import(CustomSecurityTest.TestConfig.class)
@Import(CustomSecurityTest.DemoGrpcSecurityConfig.class)
public class CustomSecurityTest extends GrpcServerTestBase {
private final static String MY_CUSTOM_SCHEME_NAME = "custom";

@TestConfiguration
static class TestConfig {

@EnableGrpcSecurity
public class DemoGrpcSecurityConfig extends GrpcSecurityConfigurerAdapter {


@Override
public void configure(GrpcSecurity builder) throws Exception {
builder.authorizeRequests()
.withSecuredAnnotation()
.authenticationSchemeSelector(scheme ->
Optional.of(scheme.toString())
.filter(s -> s.startsWith(MY_CUSTOM_SCHEME_NAME))
.map(s -> s.substring(MY_CUSTOM_SCHEME_NAME.length() + 1))
.map(token -> {
final String[] chunks = token.split("#");
return new TestingAuthenticationToken(token.split("#")[0], null, "SCOPE_" + chunks[1]);
})
)
.authenticationProvider(new TestingAuthenticationProvider());
}

public static class DemoGrpcSecurityConfig extends GrpcSecurityConfigurerAdapter {

@Override
public void configure(GrpcSecurity builder) throws Exception {
builder.authorizeRequests()
.withSecuredAnnotation()
.authenticationSchemeSelector(scheme ->
Optional.of(scheme.toString())
.filter(s -> s.startsWith(MY_CUSTOM_SCHEME_NAME))
.map(s -> s.substring(MY_CUSTOM_SCHEME_NAME.length() + 1))
.map(token -> {
final String[] chunks = token.split("#");
return new TestingAuthenticationToken(token.split("#")[0], null, "SCOPE_" + chunks[1]);
})
)
.authenticationProvider(new TestingAuthenticationProvider());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
import org.lognet.springboot.grpc.GRpcService;
import org.lognet.springboot.grpc.security.GrpcSecurity;
import org.springframework.security.access.annotation.Secured;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;
Expand Down Expand Up @@ -68,15 +66,6 @@ public void sayAuthHello(Empty request, StreamObserver<GreeterOuterClass.HelloRe
responseObserver.onCompleted();
}

@Override
@PreAuthorize("#person.getAge()<12")
@PostAuthorize("returnObject.getAge()>5")
public void sayPreAuthHello(GreeterOuterClass.Person person, StreamObserver<GreeterOuterClass.Person> responseObserver) {

responseObserver.onNext(person.toBuilder().setNickName("dummy").build());
responseObserver.onCompleted();

}

@Override
@Secured({})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package org.lognet.springboot.grpc.demo;

import io.grpc.examples.tasks.Assignment;
import io.grpc.examples.tasks.Person;
import io.grpc.examples.tasks.TaskServiceGrpc;
import io.grpc.stub.StreamObserver;
import org.lognet.springboot.grpc.GRpcService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;

import java.util.Optional;

@GRpcService
public class GrpcTaskService extends TaskServiceGrpc.TaskServiceImplBase {


private ITaskService service;

@Autowired
public void setService(Optional<ITaskService> service) {
this.service = service.orElse(new ITaskService() {
@Override
public Assignment findAssignment(Person person) {
return null;
}
});
}

@Override
@PreAuthorize("hasAuthority('1') && #person.age<12")
@PostAuthorize("returnObject.description.length()>0")
public void findAssignmentUnary(Person person, StreamObserver<Assignment> responseObserver) {
final Assignment assignment = service.findAssignment(person);
responseObserver.onNext(assignment);
responseObserver.onCompleted();

}

@Override
@PreAuthorize("#p0.age<12")
@PostAuthorize("returnObject.description.length()>0")
public StreamObserver<Person> findAssignmentsBidiStream(StreamObserver<Assignment> responseObserver) {
return new StreamObserver<Person>() {
@Override
public void onNext(Person person) {
final Assignment assignment = service.findAssignment(person);
responseObserver.onNext(assignment);
}

@Override
public void onError(Throwable t) {

}

@Override
public void onCompleted() {
responseObserver.onCompleted();
}
};
}

@Override
@PreAuthorize("#person.age<12")
@PostAuthorize("returnObject.description.length()>0")
public void findAssignmentOutStream(Person person, StreamObserver<Assignment> responseObserver) {
responseObserver.onNext(service.findAssignment(person));
responseObserver.onNext(service.findAssignment(person));
responseObserver.onCompleted();
}

@Override
@PreAuthorize("#p0.getAge()<12")
@PostAuthorize("returnObject.description.length()>0")
public StreamObserver<Person> findAssignmentInStream(StreamObserver<Assignment> responseObserver) {
return new StreamObserver<Person>() {
private final StringBuilder assignment = new StringBuilder();

@Override
public void onNext(Person person) {
if(0!=assignment.length()){
assignment.append(System.lineSeparator());
}
assignment.append(service.findAssignment(person).getDescription());
}

@Override
public void onError(Throwable t) {

}

@Override
public void onCompleted() {
responseObserver.onNext(Assignment.newBuilder()
.setDescription(assignment.toString())
.build());
responseObserver.onCompleted();
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.lognet.springboot.grpc.demo;


import io.grpc.examples.tasks.Assignment;
import io.grpc.examples.tasks.Person;

public interface ITaskService {
Assignment findAssignment(Person person);
}
1 change: 0 additions & 1 deletion grpc-spring-boot-starter-demo/src/main/proto/greeter.proto
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ service Greeter {
rpc SayManyHellos (stream HelloRequest) returns (stream HelloReply) {}
rpc SayAuthHello ( google.protobuf.Empty) returns ( HelloReply) {}
rpc SayAuthOnlyHello ( google.protobuf.Empty) returns ( HelloReply) {}
rpc SayPreAuthHello ( Person) returns ( Person) {}
rpc HelloPersonValidResponse ( Person) returns ( Person) {}
rpc HelloPersonInvalidResponse ( Person) returns ( Person) {}

Expand Down
44 changes: 44 additions & 0 deletions grpc-spring-boot-starter-demo/src/main/proto/tasks.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright 2015 The gRPC Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
syntax = "proto3";

option java_multiple_files = true;
option java_package = "io.grpc.examples.tasks";



package task;

service TaskService {

rpc FindAssignmentUnary(Person) returns ( Assignment) {}

rpc FindAssignmentsBidiStream(stream Person) returns (stream Assignment) {}

rpc FindAssignmentOutStream( Person) returns (stream Assignment) {}

rpc FindAssignmentInStream(stream Person) returns ( Assignment) {}


}


message Person {
string name = 1;
int32 age = 2;
}

message Assignment {
string description = 1;
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.lognet.springboot.grpc.demo.DemoApp;
import org.lognet.springboot.grpc.security.AuthClientInterceptor;
import org.lognet.springboot.grpc.security.AuthHeader;
import org.lognet.springboot.grpc.security.EnableGrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
import org.lognet.springboot.grpc.security.SecurityInterceptor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.lognet.springboot.grpc.demo.DemoApp;
import org.lognet.springboot.grpc.security.EnableGrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
import org.lognet.springboot.grpc.security.jwt.JwtAuthProviderFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.lognet.springboot.grpc.demo.DemoApp;
import org.lognet.springboot.grpc.security.AuthCallCredentials;
import org.lognet.springboot.grpc.security.AuthHeader;
import org.lognet.springboot.grpc.security.EnableGrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
import org.springframework.boot.test.context.SpringBootTest;
Expand Down Expand Up @@ -73,7 +72,6 @@ public void configure(GrpcSecurity builder) throws Exception {

@Test
public void concurrentTest() throws InterruptedException {
System.out.println();

final SecuredGreeterGrpc.SecuredGreeterBlockingStub unsecuredFutureStub = SecuredGreeterGrpc
.newBlockingStub(selectedChanel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import org.junit.Test;
import org.junit.runner.RunWith;
import org.lognet.springboot.grpc.demo.DemoApp;
import org.lognet.springboot.grpc.security.EnableGrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
import org.springframework.boot.test.context.SpringBootTest;
Expand All @@ -16,7 +15,6 @@
import org.springframework.test.context.junit4.SpringRunner;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertNotNull;


@SpringBootTest(classes = DemoApp.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import io.grpc.examples.GreeterGrpc;
import org.junit.runner.RunWith;
import org.lognet.springboot.grpc.demo.DemoApp;
import org.lognet.springboot.grpc.security.EnableGrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
import org.lognet.springboot.grpc.security.jwt.JwtAuthProviderFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,7 @@
import org.lognet.springboot.grpc.demo.DemoApp;
import org.lognet.springboot.grpc.security.AuthCallCredentials;
import org.lognet.springboot.grpc.security.AuthHeader;
import org.lognet.springboot.grpc.security.EnableGrpcSecurity;
import org.lognet.springboot.grpc.security.GrpcSecurityConfigurerAdapter;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;

Expand Down
Loading

0 comments on commit ded2743

Please sign in to comment.