Created
May 30, 2019 03:39
-
-
Save xring/6fc0ded4f98215d87e12c7556bcc47b6 to your computer and use it in GitHub Desktop.
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
import com.google.rpc.Status; | |
import io.envoyproxy.envoy.service.auth.v2.AuthorizationGrpc; | |
import io.envoyproxy.envoy.service.auth.v2.CheckRequest; | |
import io.envoyproxy.envoy.service.auth.v2.CheckResponse; | |
import io.envoyproxy.envoy.service.auth.v2.DeniedHttpResponse; | |
import io.envoyproxy.envoy.type.HttpStatus; | |
import io.envoyproxy.envoy.type.StatusCode; | |
import io.grpc.Server; | |
import io.grpc.ServerBuilder; | |
import io.grpc.stub.StreamObserver; | |
import java.io.IOException; | |
import java.util.Map; | |
public class AuthServerGrpc { | |
private Server server; | |
private void start() throws IOException { | |
int port = 3333; | |
server = ServerBuilder.forPort(port) | |
.addService(new AuthService()) | |
.build() | |
.start(); | |
System.out.println("AuthServerGrpc started, listening on " + port); | |
Runtime.getRuntime().addShutdownHook(new Thread() { | |
@Override | |
public void run() { | |
// Use stderr here since the logger may have been reset by its JVM shutdown hook. | |
System.err.println("*** shutting down gRPC server since JVM is shutting down"); | |
AuthServerGrpc.this.stop(); | |
System.err.println("*** server shut down"); | |
} | |
}); | |
} | |
private void stop() { | |
if (server != null) { | |
server.shutdown(); | |
} | |
} | |
private void blockUntilShutdown() throws InterruptedException { | |
if (server != null) { | |
server.awaitTermination(); | |
} | |
} | |
public static void main(String[] args) throws Exception { | |
final AuthServerGrpc server = new AuthServerGrpc(); | |
server.start(); | |
server.blockUntilShutdown(); | |
} | |
static class AuthService extends AuthorizationGrpc.AuthorizationImplBase { | |
@Override | |
public void check(CheckRequest request, StreamObserver<CheckResponse> responseObserver) { | |
Map<String, String> headers = request.getAttributes().getRequest().getHttp().getHeadersMap(); | |
if (headers.containsKey("authorization")) { | |
CheckResponse checkResponse = CheckResponse.newBuilder().build(); | |
responseObserver.onNext(checkResponse); | |
responseObserver.onCompleted(); | |
} else { | |
CheckResponse checkResponse = CheckResponse.newBuilder() | |
.setDeniedResponse(DeniedHttpResponse.newBuilder() | |
.setStatus(HttpStatus.newBuilder().setCode(StatusCode.Unauthorized).build()) | |
.setBody("not allowed") | |
.build()) | |
.setStatus(Status.newBuilder().setCode(StatusCode.Unauthorized_VALUE).build()).build(); | |
responseObserver.onNext(checkResponse); | |
responseObserver.onCompleted(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment