Commit 7779f143 authored by ElyKar's avatar ElyKar

Add TTP connection to Mongo and start gRPC server

parent e98df816
Pipeline #362 passed with stage
all:
protoc --go_out=plugins=grpc:. resolution.proto
// Code generated by protoc-gen-go.
// source: gRPCServices.proto
// DO NOT EDIT!
/*
Package api is a generated protocol buffer package.
It is generated from these files:
gRPCServices.proto
It has these top-level messages:
AlertRequest
RecoverRequest
TTPResponse
*/
package api
import proto "github.com/golang/protobuf/proto"
import fmt "fmt"
import math "math"
import (
context "golang.org/x/net/context"
grpc "google.golang.org/grpc"
)
// Reference imports to suppress errors if they are not otherwise used.
var _ = proto.Marshal
var _ = fmt.Errorf
var _ = math.Inf
// This is a compile-time assertion to ensure that this generated file
// is compatible with the proto package it is being compiled against.
const _ = proto.ProtoPackageIsVersion1
// Alert message sent by a signer
type AlertRequest struct {
}
func (m *AlertRequest) Reset() { *m = AlertRequest{} }
func (m *AlertRequest) String() string { return proto.CompactTextString(m) }
func (*AlertRequest) ProtoMessage() {}
func (*AlertRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{0} }
// RecoverRequest message to get a signed contract
type RecoverRequest struct {
}
func (m *RecoverRequest) Reset() { *m = RecoverRequest{} }
func (m *RecoverRequest) String() string { return proto.CompactTextString(m) }
func (*RecoverRequest) ProtoMessage() {}
func (*RecoverRequest) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{1} }
// TTPResponse message to answer an Alert or Recover message
type TTPResponse struct {
// Yes for abort token, No for signed contract
Abort bool `protobuf:"varint,1,opt,name=abort" json:"abort,omitempty"`
// Nil for abort token, non-empty otherwise
Contract []byte `protobuf:"bytes,2,opt,name=contract,proto3" json:"contract,omitempty"`
}
func (m *TTPResponse) Reset() { *m = TTPResponse{} }
func (m *TTPResponse) String() string { return proto.CompactTextString(m) }
func (*TTPResponse) ProtoMessage() {}
func (*TTPResponse) Descriptor() ([]byte, []int) { return fileDescriptor0, []int{2} }
func init() {
proto.RegisterType((*AlertRequest)(nil), "api.AlertRequest")
proto.RegisterType((*RecoverRequest)(nil), "api.RecoverRequest")
proto.RegisterType((*TTPResponse)(nil), "api.TTPResponse")
}
// Reference imports to suppress errors if they are not otherwise used.
var _ context.Context
var _ grpc.ClientConn
// Client API for TTP service
type TTPClient interface {
Alert(ctx context.Context, in *AlertRequest, opts ...grpc.CallOption) (*TTPResponse, error)
Recover(ctx context.Context, in *RecoverRequest, opts ...grpc.CallOption) (*TTPResponse, error)
}
type tTPClient struct {
cc *grpc.ClientConn
}
func NewTTPClient(cc *grpc.ClientConn) TTPClient {
return &tTPClient{cc}
}
func (c *tTPClient) Alert(ctx context.Context, in *AlertRequest, opts ...grpc.CallOption) (*TTPResponse, error) {
out := new(TTPResponse)
err := grpc.Invoke(ctx, "/api.TTP/Alert", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
func (c *tTPClient) Recover(ctx context.Context, in *RecoverRequest, opts ...grpc.CallOption) (*TTPResponse, error) {
out := new(TTPResponse)
err := grpc.Invoke(ctx, "/api.TTP/Recover", in, out, c.cc, opts...)
if err != nil {
return nil, err
}
return out, nil
}
// Server API for TTP service
type TTPServer interface {
Alert(context.Context, *AlertRequest) (*TTPResponse, error)
Recover(context.Context, *RecoverRequest) (*TTPResponse, error)
}
func RegisterTTPServer(s *grpc.Server, srv TTPServer) {
s.RegisterService(&_TTP_serviceDesc, srv)
}
func _TTP_Alert_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) {
in := new(AlertRequest)
if err := dec(in); err != nil {
return nil, err
}
out, err := srv.(TTPServer).Alert(ctx, in)
if err != nil {
return nil, err
}
return out, nil
}
func _TTP_Recover_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error) (interface{}, error) {
in := new(RecoverRequest)
if err := dec(in); err != nil {
return nil, err
}
out, err := srv.(TTPServer).Recover(ctx, in)
if err != nil {
return nil, err
}
return out, nil
}
var _TTP_serviceDesc = grpc.ServiceDesc{
ServiceName: "api.TTP",
HandlerType: (*TTPServer)(nil),
Methods: []grpc.MethodDesc{
{
MethodName: "Alert",
Handler: _TTP_Alert_Handler,
},
{
MethodName: "Recover",
Handler: _TTP_Recover_Handler,
},
},
Streams: []grpc.StreamDesc{},
}
var fileDescriptor0 = []byte{
// 178 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x09, 0x6e, 0x88, 0x02, 0xff, 0xe2, 0x12, 0x4a, 0x0f, 0x0a, 0x70,
0x0e, 0x4e, 0x2d, 0x2a, 0xcb, 0x4c, 0x4e, 0x2d, 0xd6, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0x62,
0x4e, 0x2c, 0xc8, 0x54, 0xe2, 0xe3, 0xe2, 0x71, 0xcc, 0x49, 0x2d, 0x2a, 0x09, 0x4a, 0x2d, 0x2c,
0x4d, 0x2d, 0x2e, 0x51, 0x12, 0xe0, 0xe2, 0x0b, 0x4a, 0x4d, 0xce, 0x2f, 0x4b, 0x2d, 0x82, 0x89,
0xd8, 0x73, 0x71, 0x87, 0x84, 0x04, 0x04, 0xa5, 0x16, 0x17, 0xe4, 0xe7, 0x15, 0xa7, 0x0a, 0x89,
0x70, 0xb1, 0x26, 0x26, 0xe5, 0x17, 0x95, 0x48, 0x30, 0x2a, 0x30, 0x6a, 0x70, 0x04, 0x41, 0x38,
0x42, 0x52, 0x5c, 0x1c, 0xc9, 0xf9, 0x79, 0x25, 0x45, 0x89, 0xc9, 0x25, 0x12, 0x4c, 0x40, 0x09,
0x9e, 0x20, 0x38, 0xdf, 0x28, 0x93, 0x8b, 0x19, 0x68, 0x80, 0x90, 0x1e, 0x17, 0x2b, 0xd8, 0x26,
0x21, 0x41, 0x3d, 0xa0, 0xc5, 0x7a, 0xc8, 0xb6, 0x4a, 0x09, 0x80, 0x85, 0x90, 0xac, 0x51, 0x62,
0x10, 0x32, 0xe2, 0x62, 0x87, 0xba, 0x44, 0x48, 0x18, 0x2c, 0x8d, 0xea, 0x2e, 0x6c, 0x7a, 0x92,
0xd8, 0xc0, 0x3e, 0x33, 0x06, 0x04, 0x00, 0x00, 0xff, 0xff, 0xb5, 0x08, 0x99, 0x18, 0xef, 0x00,
0x00, 0x00,
}
syntax = "proto3";
package api;
service TTP {
rpc Alert(AlertRequest) returns (TTPResponse) {}
rpc Recover(RecoverRequest) returns (TTPResponse) {}
}
// Alert message sent by a signer
message AlertRequest {}
// RecoverRequest message to get a signed contract
message RecoverRequest {}
// TTPResponse message to answer an Alert or Recover message
message TTPResponse {
// Yes for abort token, No for signed contract
bool abort = 1;
// Nil for abort token, non-empty otherwise
bytes contract = 2;
}
package server
import (
"crypto/rsa"
"crypto/x509"
"dfss/auth"
"dfss/dfsst/api"
"dfss/mgdb"
"dfss/net"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
const (
// PkeyFileName is the private key file default name
PkeyFileName = "dfsst_pkey.pem"
// CertFileName is the certificate file default name
CertFileName = "dfsst_cert.pem"
// RootCAFileName is the root certificate file default name
RootCAFileName = "dfsst_rootca.pem"
)
type ttpServer struct {
PKey *rsa.PrivateKey
Cert *x509.Certificate
RootCA *x509.Certificate
DB *mgdb.MongoManager
CertDuration int
Verbose bool
}
// Alert route for the TTP
func (server *ttpServer) Alert(ctx context.Context, in *api.AlertRequest) (*api.TTPResponse, error) {
return nil, nil
}
// Recover route for the TTP
func (server *ttpServer) Recover(ctx context.Context, in *api.RecoverRequest) (*api.TTPResponse, error) {
return nil, nil
}
// GetServer returns the gRPC server
func GetServer(keyPath, db string, certValidity int, verbose bool) *grpc.Server {
server, err := loadCredentials(keyPath)
if err != nil {
fmt.Fprintln(os.Stderr, "An error occured during the private key and certificates check:", err)
os.Exit(1)
}
dbManager, err := mgdb.NewManager(db)
if err != nil {
fmt.Fprintln(os.Stderr, "An error occured during the connection to MongoDB:", err)
os.Exit(1)
}
server.CertDuration = certValidity
server.Verbose = verbose
server.DB = dbManager
netServer := net.NewServer(server.Cert, server.PKey, server.RootCA)
api.RegisterTTPServer(netServer, server)
return netServer
}
// Assert the private key and certificates are valid
func loadCredentials(path string) (*ttpServer, error) {
keyPath := filepath.Join(path, PkeyFileName)
certPath := filepath.Join(path, CertFileName)
rootCAPath := filepath.Join(path, RootCAFileName)
// Recovering the private rsa key from file.
keyBytes, err := ioutil.ReadFile(keyPath)
if err != nil {
return nil, err
}
key, err := auth.PEMToPrivateKey(keyBytes)
if err != nil {
return nil, err
}
// Recovering the certificate from file.
certBytes, err := ioutil.ReadFile(certPath)
if err != nil {
return nil, err
}
cert, err := auth.PEMToCertificate(certBytes)
if err != nil {
return nil, err
}
// Recovering the root certificate from file.
caBytes, err := ioutil.ReadFile(rootCAPath)
if err != nil {
return nil, err
}
ca, err := auth.PEMToCertificate(caBytes)
if err != nil {
return nil, err
}
res := &ttpServer{
PKey: key,
Cert: cert,
RootCA: ca,
}
return res, nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment