Skip to content
This repository has been archived by the owner on May 28, 2024. It is now read-only.

refactoring & feature fixes #184

Merged
merged 8 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/findy-network/findy-common-go/rpc"
"github.com/golang/glog"
"github.com/lainio/err2"
_ "github.com/lainio/err2/assert" // we want an --asserter flag
"github.com/lainio/err2/try"
"google.golang.org/grpc"
)
Expand All @@ -20,19 +21,19 @@ var (
user = flag.String("user", "", "test user name")
serverAddr = flag.String("addr", "localhost", "agency host gRPC address")
port = flag.Int("port", 50051, "agency host gRPC port")
tls = flag.Bool("tls", true, "use TLS and cert files")
noTLS = flag.Bool("no-tls", false, "do NOT use TLS and cert files (hard coded)")
)

func main() {
err2.SetTracers(os.Stderr)
os.Args = append(os.Args,
"-logtostderr",
)
glog.CopyStandardLogTo("ERROR") // for err2 binging

defer err2.Catch(err2.Err(func(err error) {
glog.Error(err)
}))
flag.Parse()
//defer err2.Catch(err2.ToStderr) // TODO: nex err2 version will have
defer err2.Catch()

// we want this for glog, this is just a tester, not a real world service
try.To(flag.Set("logtostderr", "true"))
flag.Parse()

conn := try.To1(newClient(*user, fmt.Sprintf("%s:%d", *serverAddr, *port)))
defer conn.Close()
Expand All @@ -54,7 +55,7 @@ func newClient(user, addr string) (conn *grpc.ClientConn, err error) {

var pki *rpc.PKI
jwtStr := ""
if *tls {
if !*noTLS {
pki = rpc.LoadPKIWithServerName("./cert", addr)
jwtStr = jwt.BuildJWT(user)
}
Expand All @@ -64,7 +65,7 @@ func newClient(user, addr string) (conn *grpc.ClientConn, err error) {
JWT: jwtStr,
Addr: addr,

Insecure: !*tls,
Insecure: *noTLS,
}))
return
}
6 changes: 4 additions & 2 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package http

import (
"context"
"fmt"
nethttp "net/http"
"os"
"os/signal"
Expand Down Expand Up @@ -33,9 +34,11 @@ func Run(s *nethttp.Server, a ...string) <-chan os.Signal {
}
startHTTPS := len(a) == 2
glog.V(3).Infof("startHTTPS: %v, length a: %v", startHTTPS, len(a))
shutdownCh := make(chan os.Signal, 1)
go func() {
defer err2.Catch(err2.Err(func(err error) {
glog.Error(err)
fmt.Fprintln(os.Stderr, err)
shutdownCh <- syscall.SIGALRM
}))

if startHTTPS {
Expand All @@ -51,7 +54,6 @@ func Run(s *nethttp.Server, a ...string) <-chan os.Signal {
}
}()

shutdownCh := make(chan os.Signal, 1)
signal.Notify(shutdownCh, os.Interrupt, syscall.SIGTERM)

return shutdownCh
Expand Down
7 changes: 3 additions & 4 deletions integration/grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,10 @@ func setUp() {
}

func tearDown() {
err := conn.Close()
try.To(err) // just dump information out, we are inside a test
try.To(conn.Close())
server.GracefulStop()

err = insecureConn.Close()
try.To(err) // just dump information out, we are inside a test
try.To(insecureConn.Close())
insecureServer.GracefulStop()
}

Expand Down Expand Up @@ -134,6 +132,7 @@ func newClient(user, addr string) (conn *grpc.ClientConn, err error) {
return
}

// runServer starts a gRPC server in own goroutine.
func runServer() {
pki := rpc.LoadPKI("../cert")
glog.V(1).Infof("starting gRPC server with\ncrt:\t%s\nkey:\t%s\nclient:\t%s",
Expand Down
6 changes: 5 additions & 1 deletion rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Server(cfg *ServerCfg) (s *grpc.Server, err error) {
}

if cfg.NoAuthorization {
glog.V(1).Infoln("no jwt authorization")
glog.V(1).Infoln("no authorization")
opts = append(opts,
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_recovery.UnaryServerInterceptor(),
Expand All @@ -68,6 +68,7 @@ func Server(cfg *ServerCfg) (s *grpc.Server, err error) {
)),
)
} else {
glog.V(1).Infoln("jwt token validity checked")
opts = append(opts,
grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
grpc_auth.UnaryServerInterceptor(jwt.CheckTokenValidity),
Expand Down Expand Up @@ -99,6 +100,9 @@ func Serve(cfg *ServerCfg) {
try.To(s.Serve(lis))
}

// PrepareServe builds gRPC server that allows graceful shutdown. You must start
// it by yourself with Serve() that blocks which typically means that you need a
// goroutine for that.
func PrepareServe(cfg *ServerCfg) (s *grpc.Server, lis net.Listener, err error) {
defer err2.Handle(&err)

Expand Down
20 changes: 12 additions & 8 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,43 @@ import (
"errors"
"flag"
"fmt"
"os"

ops "github.com/findy-network/findy-common-go/grpc/ops/v1"
"github.com/findy-network/findy-common-go/jwt"
"github.com/findy-network/findy-common-go/rpc"
"github.com/golang/glog"
"github.com/lainio/err2"
"github.com/lainio/err2/try"
_ "github.com/lainio/err2/assert" // we want an --asserter flag
"google.golang.org/grpc"
)

var (
user = flag.String("user", "findy-root", "test user name")
serverAddr = flag.String("addr", "localhost", "agency host gRPC address")
port = flag.Int("port", 50051, "agency host gRPC port")
tls = flag.Bool("tls", true, "use TLS and cert files")
noTLS = flag.Bool("no-tls", false, "do NOT use TLS and cert files (hard coded)")
)

func main() {
os.Args = append(os.Args,
"-logtostderr",
)
glog.CopyStandardLogTo("ERROR") // for err2 binging

//defer err2.Catch(err2.ToStderr) // TODO: nex err2 version will have
defer err2.Catch()

flag.Parse()

// whe want this for glog, this is just a tester, not a real world service
try.To(flag.Set("logtostderr", "true"))

var pki *rpc.PKI
if *tls {
if !*noTLS {
pki = rpc.LoadPKI("./cert")
glog.V(3).Infof("starting gRPC server with\ncrt:\t%s\nkey:\t%s\nclient:\t%s",
pki.Server.CertFile, pki.Server.KeyFile, pki.Client.CertFile)
}
rpc.Serve(&rpc.ServerCfg{
NoAuthorization: !*tls,
NoAuthorization: *noTLS,

Port: *port,
PKI: pki,
Expand All @@ -58,7 +62,7 @@ func (d devOpsServer) Enter(ctx context.Context, cmd *ops.Cmd) (cr *ops.CmdRetur
defer err2.Handle(&err)

glog.V(1).Info("enter Enter()")
if *tls {
if !*noTLS {
user := jwt.User(ctx)

if user != d.Root {
Expand Down
7 changes: 7 additions & 0 deletions utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import (
)

func ParseLoggingArgs(s string) {
if s == "" {
return
}

os.Args = append(os.Args,
"-logtostderr", // todo: should be the first if we want to change this
)
args := make([]string, 1, 12)
args[0] = os.Args[0]
args = append(args, strings.Split(s, " ")...)
Expand Down