2019-04-15 03:56:55 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-05-06 00:56:27 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2019-04-15 03:56:55 +00:00
|
|
|
"flag"
|
|
|
|
"io"
|
2019-08-08 04:46:51 +00:00
|
|
|
"io/ioutil"
|
2019-05-06 00:56:27 +00:00
|
|
|
stdlog "log"
|
2019-04-15 03:56:55 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2019-05-22 15:22:40 +00:00
|
|
|
"syscall"
|
2019-04-15 03:56:55 +00:00
|
|
|
|
|
|
|
"github.com/go-kit/kit/log"
|
2019-05-22 15:22:40 +00:00
|
|
|
"google.golang.org/grpc"
|
|
|
|
"google.golang.org/grpc/credentials"
|
|
|
|
|
2019-07-12 01:02:46 +00:00
|
|
|
protobuf "github.com/mitchell/selfpass/protobuf/go"
|
2019-07-11 02:33:22 +00:00
|
|
|
"github.com/mitchell/selfpass/services/credentials/middleware"
|
|
|
|
"github.com/mitchell/selfpass/services/credentials/repositories"
|
|
|
|
"github.com/mitchell/selfpass/services/credentials/service"
|
|
|
|
"github.com/mitchell/selfpass/services/credentials/transport"
|
|
|
|
"github.com/mitchell/selfpass/services/credentials/types"
|
2019-04-15 03:56:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var logger log.Logger
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
var (
|
2019-05-28 01:16:50 +00:00
|
|
|
stop = make(chan os.Signal, 1)
|
|
|
|
jsonLogs = flag.Bool("json-logs", false, "enables json logging")
|
|
|
|
port = flag.String("port", "8080", "specify the port to listen on")
|
|
|
|
verbose = flag.Bool("v", false, "be more verbose")
|
2019-08-08 04:46:51 +00:00
|
|
|
caFile = flag.String("ca", "/run/secrets/ca", "specify an alternate ca file")
|
|
|
|
certFile = flag.String("cert", "/run/secrets/cert", "specify an alternate cert file")
|
|
|
|
keyFile = flag.String("key", "/run/secrets/key", "specify an alternate key file")
|
2019-04-15 03:56:55 +00:00
|
|
|
)
|
|
|
|
flag.Parse()
|
|
|
|
|
2019-05-22 15:22:40 +00:00
|
|
|
signal.Notify(stop, syscall.SIGINT)
|
|
|
|
signal.Notify(stop, syscall.SIGKILL)
|
|
|
|
signal.Notify(stop, syscall.SIGTERM)
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
logger = newLogger(os.Stdout, *jsonLogs)
|
2019-04-15 03:56:55 +00:00
|
|
|
|
2019-08-08 04:46:51 +00:00
|
|
|
ca, err := ioutil.ReadFile(*caFile)
|
|
|
|
check(err)
|
|
|
|
cert, err := ioutil.ReadFile(*certFile)
|
|
|
|
check(err)
|
|
|
|
key, err := ioutil.ReadFile(*keyFile)
|
|
|
|
check(err)
|
|
|
|
|
|
|
|
keypair, err := tls.X509KeyPair(cert, key)
|
2019-05-06 00:56:27 +00:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
caPool := x509.NewCertPool()
|
2019-08-08 04:46:51 +00:00
|
|
|
caPool.AppendCertsFromPEM(ca)
|
2019-05-06 00:56:27 +00:00
|
|
|
|
|
|
|
creds := credentials.NewTLS(&tls.Config{
|
2019-06-07 09:03:15 +00:00
|
|
|
Certificates: []tls.Certificate{keypair},
|
|
|
|
ClientCAs: caPool,
|
|
|
|
ClientAuth: tls.RequireAndVerifyClientCert,
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
PreferServerCipherSuites: true,
|
|
|
|
CurvePreferences: []tls.CurveID{
|
|
|
|
tls.CurveP256,
|
|
|
|
},
|
2019-05-06 00:56:27 +00:00
|
|
|
})
|
|
|
|
|
2019-07-23 00:24:00 +00:00
|
|
|
db, err := repositories.OpenBoltDB("/home/selfpass/data/bolt.db", 0600, nil)
|
2019-05-06 00:56:27 +00:00
|
|
|
check(err)
|
|
|
|
|
|
|
|
var svc types.Service
|
|
|
|
svc = service.NewCredentials(db)
|
|
|
|
if *verbose {
|
|
|
|
svc = middleware.NewServiceLogger(logger, svc)
|
|
|
|
}
|
|
|
|
|
|
|
|
gsrv := transport.NewGRPCServer(svc, logger)
|
|
|
|
srv := grpc.NewServer(grpc.Creds(creds))
|
2019-07-12 01:02:46 +00:00
|
|
|
protobuf.RegisterCredentialsServer(srv, gsrv)
|
2019-04-15 03:56:55 +00:00
|
|
|
|
2019-05-06 00:56:27 +00:00
|
|
|
addr := ":" + *port
|
2019-04-15 03:56:55 +00:00
|
|
|
lis, err := net.Listen("tcp", addr)
|
|
|
|
check(err)
|
|
|
|
|
2019-05-06 00:56:27 +00:00
|
|
|
_ = logger.Log(
|
|
|
|
"message", "serving",
|
|
|
|
"address", addr,
|
|
|
|
)
|
|
|
|
|
|
|
|
go func() { check(srv.Serve(lis)) }()
|
2019-04-15 03:56:55 +00:00
|
|
|
|
|
|
|
<-stop
|
2019-05-06 00:56:27 +00:00
|
|
|
_ = logger.Log("message", "gracefully stopping")
|
2019-04-15 03:56:55 +00:00
|
|
|
srv.GracefulStop()
|
|
|
|
}
|
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
func newLogger(writer io.Writer, jsonLogs bool) log.Logger {
|
2019-04-15 03:56:55 +00:00
|
|
|
writer = log.NewSyncWriter(writer)
|
2019-05-28 01:16:50 +00:00
|
|
|
l := log.NewLogfmtLogger(writer)
|
2019-04-15 03:56:55 +00:00
|
|
|
|
2019-05-28 01:16:50 +00:00
|
|
|
if jsonLogs {
|
2019-04-15 03:56:55 +00:00
|
|
|
l = log.NewJSONLogger(writer)
|
|
|
|
}
|
2019-05-06 00:56:27 +00:00
|
|
|
l = log.WithPrefix(l, "caller", log.Caller(5), "timestamp", log.DefaultTimestamp)
|
2019-04-15 03:56:55 +00:00
|
|
|
|
|
|
|
lfunc := log.LoggerFunc(func(in ...interface{}) error {
|
|
|
|
if err := l.Log(in...); err != nil {
|
2019-05-06 00:56:27 +00:00
|
|
|
stdlog.Println(err)
|
2019-04-15 03:56:55 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
return lfunc
|
|
|
|
}
|
|
|
|
|
|
|
|
func check(err error) {
|
|
|
|
if err != nil {
|
2019-05-06 00:56:27 +00:00
|
|
|
_ = logger.Log("error", err)
|
2019-04-15 03:56:55 +00:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
}
|