Skip to content

Commit

Permalink
appease linter
Browse files Browse the repository at this point in the history
  • Loading branch information
bbengfort committed Jul 22, 2024
1 parent a9f5f8f commit 1bb10a6
Show file tree
Hide file tree
Showing 18 changed files with 58 additions and 46 deletions.
10 changes: 4 additions & 6 deletions cmd/trtlsim/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"context"
crand "crypto/rand"
"crypto/tls"
"crypto/x509"
"errors"
Expand All @@ -28,10 +29,6 @@ import (
"google.golang.org/grpc/status"
)

func init() {
rand.Seed(time.Now().UTC().UnixNano())
}

const (
interval = 10 * time.Second // the ticker interval, default to 5 seconds
sigma = 200 * time.Millisecond // the amount of jitter, default to 100 ms
Expand Down Expand Up @@ -173,7 +170,7 @@ func (s *Simulator) connect() (_ pb.TrtlClient, err error) {

// Connect the replica client
var cc *grpc.ClientConn
if cc, err = grpc.Dial(s.Endpoint, opts...); err != nil {
if cc, err = grpc.NewClient(s.Endpoint, opts...); err != nil {
return nil, err
}
log.Printf("connected to trtl server at %s\n", s.Endpoint)
Expand Down Expand Up @@ -637,6 +634,7 @@ func (t *TRISAModel) reissuer(wg *sync.WaitGroup) {
// User updater randomly updates users' profiles
// This writer may generate stomps, but it's unlikely
func (t *TRISAModel) userProfiles(wg *sync.WaitGroup) {
defer wg.Done()
ticker := jitter.New(reissueInterval, reissueSigma)
for {
<-ticker.C
Expand Down Expand Up @@ -735,7 +733,7 @@ func (t *TRISAModel) Iter(namespace string) (err error) {
func (t *TRISAModel) Put(key, namespace string, nbytes int) (err error) {
// Create a random value of the specified length
value := make([]byte, nbytes)
if _, err = rand.Read(value); err != nil {
if _, err = crand.Read(value); err != nil {
return fmt.Errorf("could not create random value: %v", err)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/bff/auth/csrf.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package auth

import (
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"math/rand"
"net/http"
"net/url"
"sync"
Expand Down
2 changes: 1 addition & 1 deletion pkg/bff/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func (c MTLSConfig) Validate() error {
}

// DialOption returns a configured dial option which can be directly used in a
// grpc.Dial or grpc.DialContext call to connect using mTLS.
// grpc.NewClient call to connect using mTLS.
func (c MTLSConfig) DialOption(endpoint string) (opt grpc.DialOption, err error) {
var (
sz *trust.Serializer
Expand Down
3 changes: 1 addition & 2 deletions pkg/bff/mock/gds.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
)

const (
bufSize = 1024 * 1024
RegisterRPC = "Register"
LookupRPC = "Lookup"
SearchRPC = "Search"
Expand All @@ -28,7 +27,7 @@ const (
func NewGDS(conf config.DirectoryConfig) (g *GDS, err error) {
g = &GDS{
srv: grpc.NewServer(),
sock: bufconn.New(bufSize, ""),
sock: bufconn.New(""),
Calls: make(map[string]int),
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/bff/mock/members.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
func NewMembers(conf config.MembersConfig) (m *Members, err error) {
m = &Members{
srv: grpc.NewServer(),
sock: bufconn.New(bufSize, ""),
sock: bufconn.New(""),
Calls: make(map[string]int),
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/bff/mock/trtl.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func NewTrtl() (t *Trtl, err error) {
if t.srv, err = trtl.New(conf); err != nil {
return nil, err
}
t.sock = bufconn.New(1024*1024, "")
t.sock = bufconn.New("")
go t.srv.Run(t.sock.Listener)
return t, nil
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/gds/client/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func (p *DirectoryProfile) Connect() (_ api.TRISADirectoryClient, err error) {

// Connect the directory client
var cc *grpc.ClientConn
if cc, err = grpc.Dial(p.Endpoint, opts...); err != nil {
if cc, err = grpc.NewClient(p.Endpoint, opts...); err != nil {
return nil, err
}
return api.NewTRISADirectoryClient(cc), nil
Expand Down Expand Up @@ -208,7 +208,7 @@ func (p *TrtlProfile) Connect() (conn *grpc.ClientConn, err error) {
}

// Connect the replica client
if conn, err = grpc.Dial(p.Endpoint, opts...); err != nil {
if conn, err = grpc.NewClient(p.Endpoint, opts...); err != nil {
return nil, err
}
return conn, nil
Expand Down Expand Up @@ -271,7 +271,7 @@ func (p *MembersProfile) Connect() (_ members.TRISAMembersClient, err error) {

// Connect the directory client
var cc *grpc.ClientConn
if cc, err = grpc.Dial(p.Endpoint, opts...); err != nil {
if cc, err = grpc.NewClient(p.Endpoint, opts...); err != nil {
return nil, err
}
return members.NewTRISAMembersClient(cc), nil
Expand Down
6 changes: 1 addition & 5 deletions pkg/gds/fixtures/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ import (
"google.golang.org/protobuf/types/known/anypb"
)

const (
bufSize = 1024 * 1024
)

type StoreType uint8

const (
Expand Down Expand Up @@ -232,7 +228,7 @@ func (lib *Library) SetupTrtl() (err error) {
}

// Using a bufconn listener allows us to avoid network requests
lib.trtlListener = bufconn.New(bufSize, "")
lib.trtlListener = bufconn.New("")
go lib.trtl.Run(lib.trtlListener.Listener)

// Connect to the running Trtl server
Expand Down
8 changes: 2 additions & 6 deletions pkg/gds/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,6 @@ import (
pb "github.com/trisacrypto/trisa/pkg/trisa/gds/models/v1beta1"
)

const (
bufSize = 1024 * 1024
)

var (
fixturesPath = filepath.Join("testdata", "fakes.tgz")
dbPath = filepath.Join("testdata", "db")
Expand Down Expand Up @@ -72,7 +68,7 @@ func (s *gdsTestSuite) SetupSuite() {
func (s *gdsTestSuite) SetupGDS() {

// Using a bufconn listener allows us to avoid network requests
s.grpc = bufconn.New(bufSize, "")
s.grpc = bufconn.New("")
go s.svc.GetGDS().Run(s.grpc.Listener)
}

Expand All @@ -81,7 +77,7 @@ func (s *gdsTestSuite) SetupGDS() {
func (s *gdsTestSuite) SetupMembers() {

// Using a bufconn listener allows us to avoid network requests
s.grpc = bufconn.New(bufSize, "")
s.grpc = bufconn.New("")
go s.svc.GetMembers().Run(s.grpc.Listener)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/store/trtl/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func Connect(conf config.StoreConfig) (conn *grpc.ClientConn, err error) {
}

// Connect the replica client
if conn, err = grpc.Dial(dsn.Host, opts...); err != nil {
if conn, err = grpc.NewClient(dsn.Host, opts...); err != nil {
return nil, err
}
return conn, nil
Expand Down
3 changes: 1 addition & 2 deletions pkg/store/trtl/trtl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ const (
metaRegion = "tauceti"
metaOwner = "taurian"
metaPID = 8
bufSize = 1024 * 1024
)

type trtlStoreTestSuite struct {
Expand Down Expand Up @@ -78,7 +77,7 @@ func (s *trtlStoreTestSuite) SetupSuite() {
s.trtl, err = trtl.New(*s.conf)
require.NoError(err)

s.grpc = bufconn.New(bufSize, "")
s.grpc = bufconn.New("")
go s.trtl.Run(s.grpc.Listener)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/trtl/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func setupTrtl(t testing.TB) (bench *trtlBench, err error) {
}

// Create a bufconn listener(s) so that there are no actual network requests
bench.grpc = bufconn.New(benchbuf, "")
bench.grpc = bufconn.New("")

// Run the test server without signals, background routines or maintenance mode checks
go bench.trtl.Run(bench.grpc.Listener)
Expand Down
3 changes: 1 addition & 2 deletions pkg/trtl/mock/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
)

const (
bufSize = 1024 * 1024
GetRPC = "trtl.v1.Trtl/Get"
PutRPC = "trtl.v1.Trtl/Put"
DeleteRPC = "trtl.v1.Trtl/Delete"
Expand All @@ -37,7 +36,7 @@ const (
// ServerOptions with configured TLS.
func New(bufnet *bufconn.GRPCListener, opts ...grpc.ServerOption) *RemoteTrtl {
if bufnet == nil {
bufnet = bufconn.New(bufSize, "")
bufnet = bufconn.New("")
}

if len(opts) == 0 {
Expand Down
25 changes: 24 additions & 1 deletion pkg/trtl/replica/prob.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
package replica

import (
crand "crypto/rand"
"encoding/binary"
"math"
"math/rand"
"sync"
"time"
)

const (
lambda = -0.004
)

var (
random *rand.Rand
mu sync.Mutex
)

func init() {
var b [8]byte
if _, err := crand.Read(b[:]); err != nil {
panic("cannot seed math/rand package with cryptographically secure random number")
}
random = rand.New(rand.NewSource(int64(binary.LittleEndian.Uint64(b[:]))))
}

// TimeProbability returns an exponentially decaying probability between 0 and 1 that
// smoothly decreases to zero over the course of a day (24 hours). E.g. the longer the
// time since the timestamp, the lower the probability that is returned.
Expand All @@ -21,5 +37,12 @@ func TimeProbability(ts time.Time) float64 {
// ReplicateObjectRoulette performs a roulette roll to see if the object should be
// replicated basesd on its TimeProbability.
func ReplicateObjectRoulette(ts time.Time) bool {
return rand.Float64() < TimeProbability(ts)
mu.Lock()
defer mu.Unlock()
return random.Float64() < TimeProbability(ts)
}

// Seed the random number generator for testing purposes
func TestSeed(seed int64) {
random = rand.New(rand.NewSource(seed))
}
4 changes: 2 additions & 2 deletions pkg/trtl/replica/prob_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package replica_test

import (
"math/rand"
"testing"
"time"

Expand Down Expand Up @@ -50,7 +49,8 @@ func TestReplicateObjectRoulette(t *testing.T) {
for i, tc := range cases {
count := 0
ts := time.Now().Add(tc.before)
rand.Seed(24)
replica.TestSeed(24)

for i := 0; i < tc.rolls; i++ {
if replica.ReplicateObjectRoulette(ts) {
count++
Expand Down
7 changes: 3 additions & 4 deletions pkg/trtl/replica/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func (r *Service) AntiEntropySync(peer *peers.Peer, logctx *sentry.Logger) (err

// Dial the remote peer and establish a connection
var cc *grpc.ClientConn
if cc, err = r.connect(ctx, peer); err != nil {
if cc, err = r.connect(peer); err != nil {
return err
}
defer cc.Close()
Expand Down Expand Up @@ -251,10 +251,9 @@ func (r *Service) AntiEntropySync(peer *peers.Peer, logctx *sentry.Logger) (err
// Connect to a remote peer using mTLS credentials or in insecure mode as necessary.
// This method blocks until the connection has been established to prevent any
// anti-entropy work from happening until we know the remote peer is live.
func (r *Service) connect(ctx context.Context, peer *peers.Peer) (cc *grpc.ClientConn, err error) {
func (r *Service) connect(peer *peers.Peer) (cc *grpc.ClientConn, err error) {
// Create the base dial options - ensure blocking
opts := make([]grpc.DialOption, 0, 2)
opts = append(opts, grpc.WithBlock())

// Add mTLS credentials if required
if r.mtls.Insecure {
Expand Down Expand Up @@ -284,7 +283,7 @@ func (r *Service) connect(ctx context.Context, peer *peers.Peer) (cc *grpc.Clien
}

// Dial the remote peer and establish a connection
return grpc.DialContext(ctx, peer.Addr, opts...)
return grpc.NewClient(peer.Addr, opts...)
}

// initiatorPhase1 is the go routine that starts the anti-entropy synchronization
Expand Down
5 changes: 2 additions & 3 deletions pkg/trtl/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
)

const (
bufSize = 1024 * 1024
clientCerts = "testdata/client.pem"
serverCerts = "testdata/server.pem"
clientTarget = "client.trisa.dev"
Expand Down Expand Up @@ -223,7 +222,7 @@ func (s *trtlTestSuite) setupServers() (err error) {
}

// Create a bufconn listener(s) so that there are no actual network requests
s.grpc = bufconn.New(bufSize, "")
s.grpc = bufconn.New("")

// Run the test server without signals, background routines or maintenance mode checks
// TODO: do we need to check if there was an error when starting run?
Expand Down Expand Up @@ -254,7 +253,7 @@ func (s *trtlTestSuite) setupRemoteTrtl() (err error) {
opts = append(opts, grpc.Creds(credentials.NewTLS(tls)))

// Create the remote peer
s.remote = mock.New(bufconn.New(bufSize, serverTarget), opts...)
s.remote = mock.New(bufconn.New(serverTarget), opts...)
return nil
}

Expand Down
12 changes: 8 additions & 4 deletions pkg/utils/bufconn/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import (
"google.golang.org/grpc/test/bufconn"
)

const Endpoint = "passthrough://bufnet"

const bufsize = 1024 * 1024

// GRPCListener handles gRPC connections using a bufconn listener. This is useful for
// testing when it's unnecessary to have a live gRPC server running. The normal
// workflow is to call New() to start the listener, Connect() to start a gRPC
Expand All @@ -20,13 +24,13 @@ type GRPCListener struct {
Conn *grpc.ClientConn
}

func New(bufSize int, target string) *GRPCListener {
func New(target string) *GRPCListener {
if target == "" {
target = "bufnet"
target = Endpoint
}

return &GRPCListener{
Listener: bufconn.Listen(bufSize),
Listener: bufconn.Listen(bufsize),
Target: target,
}
}
Expand All @@ -37,7 +41,7 @@ func (g *GRPCListener) Connect(ctx context.Context, opts ...grpc.DialOption) (er
}

opts = append([]grpc.DialOption{grpc.WithContextDialer(g.Dialer)}, opts...)
if g.Conn, err = grpc.DialContext(ctx, g.Target, opts...); err != nil {
if g.Conn, err = grpc.NewClient(g.Target, opts...); err != nil {
return err
}
return err
Expand Down

0 comments on commit 1bb10a6

Please sign in to comment.