2019-05-18 13:05:02 -07:00
|
|
|
/*
|
|
|
|
|
* JobServServer
|
|
|
|
|
*
|
|
|
|
|
* v1.0
|
|
|
|
|
*
|
|
|
|
|
* May 18, 2019
|
|
|
|
|
*/
|
|
|
|
|
|
2019-05-16 14:49:47 -07:00
|
|
|
package JobServ;
|
|
|
|
|
|
|
|
|
|
import io.grpc.Server;
|
|
|
|
|
import io.grpc.ServerBuilder;
|
2019-05-18 15:28:36 -07:00
|
|
|
import io.grpc.netty.GrpcSslContexts;
|
|
|
|
|
import io.grpc.netty.NettyServerBuilder;
|
|
|
|
|
import io.netty.handler.ssl.ClientAuth;
|
|
|
|
|
import io.netty.handler.ssl.SslContextBuilder;
|
|
|
|
|
import io.netty.handler.ssl.SslProvider;
|
2019-05-16 14:49:47 -07:00
|
|
|
|
2019-05-18 16:40:58 -07:00
|
|
|
import java.io.File;
|
2019-05-16 14:49:47 -07:00
|
|
|
import java.io.IOException;
|
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
|
|
2019-05-18 13:05:02 -07:00
|
|
|
/*
|
|
|
|
|
* The JobServServer class implements the JobServ protobuf API
|
|
|
|
|
* It does this by extending the gRPC stub code.
|
|
|
|
|
* Additionally, JobServServer starts and manages a daemon
|
|
|
|
|
* Which accepts incoming connections from client.
|
|
|
|
|
*/
|
2019-05-16 14:49:47 -07:00
|
|
|
public class JobServServer {
|
|
|
|
|
private static final Logger logger = Logger.getLogger(JobServServer.class.getName());
|
|
|
|
|
|
|
|
|
|
private Server server;
|
2019-05-18 15:28:36 -07:00
|
|
|
private final int port;
|
|
|
|
|
private final String certChainFilePath;
|
|
|
|
|
private final String privateKeyFilePath;
|
|
|
|
|
private final String trustCertCollectionFilePath;
|
|
|
|
|
|
|
|
|
|
public JobServServer(int port,
|
|
|
|
|
String certChainFilePath,
|
|
|
|
|
String privateKeyFilePath,
|
|
|
|
|
String trustCertCollectionFilePath) {
|
|
|
|
|
this.port = port;
|
|
|
|
|
this.certChainFilePath = certChainFilePath;
|
|
|
|
|
this.privateKeyFilePath = privateKeyFilePath;
|
|
|
|
|
this.trustCertCollectionFilePath = trustCertCollectionFilePath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private SslContextBuilder getSslContextBuilder() {
|
|
|
|
|
SslContextBuilder sslClientContextBuilder = SslContextBuilder.forServer(new File(certChainFilePath),
|
|
|
|
|
new File(privateKeyFilePath));
|
|
|
|
|
|
|
|
|
|
sslClientContextBuilder.trustManager(new File(trustCertCollectionFilePath));
|
|
|
|
|
sslClientContextBuilder.clientAuth(ClientAuth.REQUIRE);
|
|
|
|
|
|
|
|
|
|
return GrpcSslContexts.configure(sslClientContextBuilder);
|
2019-05-18 16:40:58 -07:00
|
|
|
}
|
2019-05-16 14:49:47 -07:00
|
|
|
|
|
|
|
|
// starts the GRPC API Server
|
|
|
|
|
private void start() throws IOException {
|
2019-05-17 01:28:26 -07:00
|
|
|
// TODO: this should be passed in from a configuration manager
|
2019-05-18 16:40:58 -07:00
|
|
|
server = NettyServerBuilder.forPort(port)
|
|
|
|
|
.addService(new ShellServerService())
|
2019-05-18 15:28:36 -07:00
|
|
|
.sslContext(getSslContextBuilder().build())
|
2019-05-17 01:28:26 -07:00
|
|
|
.build()
|
|
|
|
|
.start();
|
2019-05-18 15:28:36 -07:00
|
|
|
logger.info("Server initialized!");
|
|
|
|
|
|
2019-05-17 01:35:55 -07:00
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread() {
|
2019-05-17 01:28:26 -07:00
|
|
|
@Override
|
|
|
|
|
public void run() {
|
|
|
|
|
// JVM shutdown might break logger functionality
|
|
|
|
|
// so investigate this....
|
|
|
|
|
logger.info("Shutting down server");
|
|
|
|
|
JobServServer.this.stop();
|
|
|
|
|
}
|
|
|
|
|
});
|
2019-05-16 14:49:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void stop() {
|
2019-05-17 01:28:26 -07:00
|
|
|
if (server != null) {
|
|
|
|
|
server.shutdown();
|
|
|
|
|
}
|
2019-05-16 14:49:47 -07:00
|
|
|
}
|
2019-05-18 12:20:12 -07:00
|
|
|
|
2019-05-16 14:49:47 -07:00
|
|
|
// spinlock for main()
|
|
|
|
|
private void blockUntilShutdown() throws InterruptedException {
|
2019-05-17 01:28:26 -07:00
|
|
|
if (server != null) {
|
|
|
|
|
server.awaitTermination();
|
|
|
|
|
}
|
2019-05-16 14:49:47 -07:00
|
|
|
}
|
|
|
|
|
|
2019-05-17 01:28:26 -07:00
|
|
|
// Main function. starts GRPC server and spins until server is shutdown
|
2019-05-16 14:49:47 -07:00
|
|
|
public static void main(String[] args) throws IOException, InterruptedException {
|
2019-05-18 16:40:58 -07:00
|
|
|
int port = 8448; // TODO: port and key/cert files should be handled by a config manager
|
2019-05-18 15:28:36 -07:00
|
|
|
|
|
|
|
|
if(args.length < 3) {
|
|
|
|
|
System.out.println("Usage: ./jobserv-server certchain privatekey truststore");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final JobServServer server = new JobServServer(8448, args[0], args[1], args[2]);
|
2019-05-17 01:28:26 -07:00
|
|
|
server.start();
|
|
|
|
|
server.blockUntilShutdown();
|
2019-05-16 14:49:47 -07:00
|
|
|
}
|
|
|
|
|
}
|