Files
tasks/internal/nettest/rpc_factory.go
Tim Deeb-SwihartandGitHub 79b9997476 Pull in protojson vuln fixes (#5267)
## What was changed

I bumped the version of api-go and sdk-go and _slightly_ altered our
nettest RPC factory interface to deal with changes in v1.60.0 of go-grpc

## Why?

To fix the protojson DOS vulns recently patched in the upstream
golang/protobuf. See https://github.com/temporalio/api-go/pull/143 for
details

## How did you test it?

I pulled the tests added to the protojson repo into our fork

## Potential risks

N/A

## Is hotfix candidate?

No as it requires all our other proto changes which aren't released
2024-01-08 22:41:00 +00:00

106 lines
3.3 KiB
Go

// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package nettest
import (
"context"
"net"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"go.temporal.io/server/common"
)
// RPCFactory is a common.RPCFactory implementation that uses a PipeListener to create connections. It is useful for
// testing gRPC servers.
type RPCFactory struct {
listener *PipeListener
contextFactory func() context.Context
dialOptions []grpc.DialOption
}
var _ common.RPCFactory = (*RPCFactory)(nil)
// NewRPCFactory creates a new RPCFactory backed by a PipeListener.
func NewRPCFactory(listener *PipeListener, dialOptions ...grpc.DialOption) *RPCFactory {
return &RPCFactory{
listener: listener,
contextFactory: func() context.Context {
return context.Background()
},
dialOptions: dialOptions,
}
}
// SetContextFactory sets the context factory used to create contexts for dialing connections. The default returns a
// background context on each call.
func (f *RPCFactory) SetContextFactory(factory func() context.Context) {
f.contextFactory = factory
}
func (f *RPCFactory) GetFrontendGRPCServerOptions() ([]grpc.ServerOption, error) {
return nil, nil
}
func (f *RPCFactory) GetInternodeGRPCServerOptions() ([]grpc.ServerOption, error) {
return nil, nil
}
func (f *RPCFactory) GetGRPCListener() net.Listener {
return f.listener
}
func (f *RPCFactory) CreateRemoteFrontendGRPCConnection(rpcAddress string) *grpc.ClientConn {
return f.dial(rpcAddress)
}
func (f *RPCFactory) CreateLocalFrontendGRPCConnection() *grpc.ClientConn {
return f.dial(f.listener.Addr().String())
}
func (f *RPCFactory) CreateInternodeGRPCConnection(rpcAddress string) *grpc.ClientConn {
return f.dial(rpcAddress)
}
func (f *RPCFactory) dial(rpcAddress string) *grpc.ClientConn {
dialOptions := append(f.dialOptions,
grpc.WithContextDialer(func(ctx context.Context, s string) (net.Conn, error) {
return f.listener.Connect(ctx.Done())
}),
grpc.WithTransportCredentials(insecure.NewCredentials()),
)
conn, err := grpc.DialContext(
f.contextFactory(),
rpcAddress,
dialOptions...,
)
if err != nil {
panic(err)
}
return conn
}