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

translator to agency #211

Merged
merged 9 commits into from
Apr 8, 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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ API_BRANCH=$(shell ./scripts/branch.sh ../findy-agent-api/)
SRC_ROOT=$(PWD)/../../..
IDL_PATH=../findy-agent-api/idl/v1

check: lint test

protoc: protoc_protocol protoc_agency protoc_agent protoc_authn

protoc_protocol:
Expand Down Expand Up @@ -81,8 +83,6 @@ test_cov_out:
test_cov: test_cov_out
go tool cover -html=coverage.txt

check: check_fmt vet shadow

release:
gh workflow run do-release.yml

3 changes: 3 additions & 0 deletions agency/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func BuildConnBase(tlsPath, fullAddr string, opts []grpc.DialOption) *rpc.Client
return cfg
}

// BuildClientConnBase builds the rpc.ClientCfg from tls path, address, port and
// opts.g. localhost:50051.
func BuildClientConnBase(
tlsPath, addr string,
port int,
Expand All @@ -61,6 +63,7 @@ func BuildClientConnBase(
return cfg
}

// BuildInsecureClientConnBase is helper to create rpc.ClientCfg easily.
func BuildInsecureClientConnBase(
addr string,
port int,
Expand Down
3 changes: 2 additions & 1 deletion client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
)

var (
cert = flag.String("cert", "../cert", "pki cert path")
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")
Expand Down Expand Up @@ -55,7 +56,7 @@ func newClient(user, addr string) (conn *grpc.ClientConn, err error) {
var pki *rpc.PKI
jwtStr := ""
if !*noTLS {
pki = rpc.LoadPKIWithServerName("./cert", addr)
pki = rpc.LoadPKIWithServerName(*cert, addr)
jwtStr = jwt.BuildJWT(user)
}
glog.V(5).Infoln("client with user:", user)
Expand Down
12 changes: 10 additions & 2 deletions jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@

// UserCtxKey is type for key to access user value from context. It's currently
// exported for possible outside use.
// NOTE: Must be own type and cannot be type alias because linter warnings with
// context!
type UserCtxKey string

const userKey = "UserKey"

type customClaims struct {
Username string `json:"un"`
Label string `json:"label,omitempty"`
Expand All @@ -48,7 +52,11 @@

// User is a helper function to get user from the current ctx as a string.
func User(ctx context.Context) string {
return ctx.Value(UserCtxKey("UserKey")).(string)
return ctx.Value(UserCtxKey(userKey)).(string)
}

func NewContextWithUser(ctx context.Context, user string) context.Context {
return context.WithValue(ctx, UserCtxKey(userKey), user)

Check warning on line 59 in jwt/jwt.go

View check run for this annotation

Codecov / codecov/patch

jwt/jwt.go#L58-L59

Added lines #L58 - L59 were not covered by tests
}

// BuildJWT builds a signed JWT token from user string. User string can be user
Expand Down Expand Up @@ -93,7 +101,7 @@
return ctx, false
}
if claims, ok := token.Claims.(*customClaims); ok && token.Valid {
ctx = context.WithValue(ctx, UserCtxKey("UserKey"), claims.Username)
ctx = context.WithValue(ctx, UserCtxKey(userKey), claims.Username)
} else {
glog.Error("no claims in token")
return ctx, false
Expand Down
2 changes: 2 additions & 0 deletions rpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"github.com/findy-network/findy-common-go/jwt"
"github.com/golang/glog"
"github.com/lainio/err2"
"github.com/lainio/err2/assert"
"github.com/lainio/err2/try"
"golang.org/x/oauth2"
"google.golang.org/grpc"
Expand Down Expand Up @@ -72,6 +73,7 @@
glog.V(3).Infoln("insecure gRPC call")
default:
glog.Warning("PKI nor Insecure not set")
assert.NotImplemented()

Check warning on line 76 in rpc/client.go

View check run for this annotation

Codecov / codecov/patch

rpc/client.go#L76

Added line #L76 was not covered by tests
}

if cfg.Opts != nil {
Expand Down
17 changes: 12 additions & 5 deletions rpc/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,18 @@ func Server(cfg *ServerCfg) (s *grpc.Server, err error) {
glog.V(2).Infof("cfg.PKI: %v", cfg.PKI)
opts := make([]grpc.ServerOption, 0, 4)
if cfg.PKI != nil {
creds := try.To1(loadTLSCredentials(cfg.PKI))
creds := try.To1(mTLSCredentials(cfg.PKI))
opts = append(opts, grpc.Creds(creds))
}

errHandler := func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) {
errHandler := func(ctx context.Context,
req interface{},
info *grpc.UnaryServerInfo,
handler grpc.UnaryHandler,
) (
interface{},
error,
) {
glog.V(1).Infoln("-agent gRPC call:", info.FullMethod)
resp, err := handler(ctx, req)
if err != nil {
Expand Down Expand Up @@ -119,7 +126,7 @@ func PrepareServe(cfg *ServerCfg) (s *grpc.Server, lis net.Listener, err error)
return s, lis, nil
}

func loadTLSCredentials(pw *PKI) (creds credentials.TransportCredentials, err error) {
func mTLSCredentials(pw *PKI) (creds credentials.TransportCredentials, err error) {
defer err2.Handle(&err)

caCert := try.To1(os.ReadFile(pw.Client.CertFile))
Expand All @@ -129,13 +136,13 @@ func loadTLSCredentials(pw *PKI) (creds credentials.TransportCredentials, err er
// Load server's certificate and private key
serverCert := try.To1(tls.LoadX509KeyPair(pw.Server.CertFile, pw.Server.KeyFile))

// Create the credentials and return it
// Create the mTLS credentials and return it
config := &tls.Config{
Certificates: []tls.Certificate{serverCert},
ClientAuth: tls.RequireAndVerifyClientCert,
ClientCAs: rootCAs,
}

glog.V(3).Infoln("cert files loaded")
glog.V(1).Infoln("mTLS cert files loaded")
return credentials.NewTLS(config), nil
}
14 changes: 13 additions & 1 deletion server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
)

var (
cert = flag.String("cert", "../cert", "pki cert path")
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")
Expand All @@ -35,7 +36,7 @@ func main() {

var pki *rpc.PKI
if !*noTLS {
pki = rpc.LoadPKI("./cert")
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)
}
Expand All @@ -57,6 +58,17 @@ type devOpsServer struct {
Root string
}

func (d devOpsServer) AuthFuncOverride(
ctx context.Context,
fullMethodName string,
) (
context.Context,
error, //nolint: unparam
) {
glog.V(1).Infoln("======== AuthFuncOverride", fullMethodName)
return jwt.NewContextWithUser(ctx, d.Root), nil
}

func (d devOpsServer) Enter(ctx context.Context, cmd *ops.Cmd) (cr *ops.CmdReturn, err error) {
defer err2.Handle(&err)

Expand Down
Loading