Commit 7f0be09a authored by Loïck Bonniot's avatar Loïck Bonniot
Browse files

Merge branch '113_grpc_lib' into 'master'

113 grpc lib

Implement gRPC primitives

See merge request !8
parents 0fe4adce 257fe509
Pipeline #148 passed with stage
......@@ -15,7 +15,7 @@ Unit tests:
- "coverage.html"
script:
- "ln -s $(pwd) $GOPATH/src/dfss"
- "go get gopkg.in/mgo.v2"
- "./build/deps.sh"
- "go test -coverprofile auth.part -v ./auth"
- "go test -coverprofile mgdb.part -v ./mgdb"
- "go test -coverprofile mails.part -v ./mails"
......@@ -28,7 +28,7 @@ ARM tests:
tags:
- arm
script:
- "go get gopkg.in/mgo.v2"
- "./build/deps.sh"
- "go test -cover -short -v ./auth"
- "go test -cover -short -v ./mgdb"
......@@ -41,7 +41,7 @@ Code lint:
script:
- "ln -s $(pwd) $GOPATH/src/dfss"
- "go get github.com/alecthomas/gometalinter"
- "go get gopkg.in/mgo.v2"
- "./build/deps.sh"
- "go install ./..."
- "gometalinter --install"
- "gometalinter -t --deadline=100s -j1 ./..."
#!/bin/sh
# mongo
go get gopkg.in/mgo.v2
# grpc
go get google.golang.org/grpc
package net
import (
"crypto/tls"
"crypto/x509"
"log"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
)
// Connect to a peer.
//
// Given parameters cert/key/ca are PEM-encoded array of bytes.
// Closing must be defered after call.
func Connect(addrPort string, cert, key, ca []byte) *grpc.ClientConn {
// load peer cert/key, ca as PEM buffers
peerCert, err := tls.X509KeyPair(cert, key)
if err != nil {
log.Fatalf("Load peer cert/key error: %v", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(ca)
// configure transport authentificator
ta := credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{peerCert},
RootCAs: caCertPool,
})
// let's do the dialing !
con, err := grpc.Dial(addrPort, grpc.WithTransportCredentials(ta))
if err != nil {
grpclog.Fatalf("Fail to dial: %v", err)
}
return con
}
package net
import (
"crypto/tls"
"crypto/x509"
"log"
"net"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/grpclog"
)
// NewServer creates a new grpc server with given tls credentials.
//
// cert/key/ca are PEM-encoded array of bytes.
//
// The returned grpcServer must be used in association with server{} to
// register APIs before calling Listen().
func NewServer(cert, key, ca []byte) *grpc.Server {
// configure gRPC
var opts []grpc.ServerOption
serverCert, err := tls.X509KeyPair(cert, key)
if err != nil {
log.Fatalf("Load peer cert/key error: %v", err)
}
caCertPool := x509.NewCertPool()
caCertPool.AppendCertsFromPEM(ca)
// configure transport authentificator
ta := credentials.NewTLS(&tls.Config{
Certificates: []tls.Certificate{serverCert},
RootCAs: caCertPool,
ClientCAs: caCertPool,
ClientAuth: tls.RequireAndVerifyClientCert,
})
opts = []grpc.ServerOption{grpc.Creds(ta)}
return grpc.NewServer(opts...)
}
// Listen with specified server on addr:port.
//
// addrPort is formated as 127.0.0.1:8001.
func Listen(addrPort string, grpcServer *grpc.Server) {
// open tcp socket
lis, err := net.Listen("tcp", addrPort)
if err != nil {
grpclog.Fatalf("Failed to open tcp socket: %v", err)
}
err = grpcServer.Serve(lis)
if err != nil {
grpclog.Fatalf("Failed to bind gRPC server: %v", err)
}
}
Supports Markdown
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