Skip to content

Commit

Permalink
Codacy comments
Browse files Browse the repository at this point in the history
Signed-off-by: Timo Klenk <[email protected]>
  • Loading branch information
kindlich committed Apr 7, 2022
1 parent 7551467 commit 86425bc
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 48 deletions.
16 changes: 8 additions & 8 deletions pkg/ephemeral/io/carrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type Result struct {

// AbstractCarrier is the carriers interface.
type AbstractCarrier interface {
Connect(int32, context.Context, string, string) error
Connect(context.Context, int32, string, string) error
Close() error
Send([]amphora.SecretShare) error
Read(ResponseConverter, bool) (*Result, error)
Expand All @@ -33,7 +33,7 @@ type AbstractCarrier interface {
// Carrier is a TCP client for TCP sockets.
type Carrier struct {
Dialer func(ctx context.Context, addr, port string) (net.Conn, error)
TlsConnector func(conn net.Conn, playerID int32) (net.Conn, error)
TLSConnector func(conn net.Conn, playerID int32) (net.Conn, error)
Conn net.Conn
Packer Packer
connected bool
Expand All @@ -46,7 +46,7 @@ type Config struct {
}

// Connect establishes a TCP connection to a socket on a given host and port.
func (c *Carrier) Connect(playerID int32, ctx context.Context, host string, port string) error {
func (c *Carrier) Connect(ctx context.Context, playerID int32, host string, port string) error {
conn, err := c.Dialer(ctx, host, port)
if err != nil {
return err
Expand All @@ -55,7 +55,7 @@ func (c *Carrier) Connect(playerID int32, ctx context.Context, host string, port
if err != nil {
return err
}
c.Conn, err = c.TlsConnector(conn, playerID)
c.Conn, err = c.TLSConnector(conn, playerID)
if err != nil {
return err
}
Expand Down Expand Up @@ -117,11 +117,11 @@ func (c *Carrier) Send(secret []amphora.SecretShare) error {
// The header consists of the clientId as string:
// - 1 Long (4 Byte) that contains the length of the string in bytes
// - Then come X Bytes for the String
func (c *Carrier) buildHeader(playerId int32) []byte {
playerIdString := []byte(fmt.Sprintf("%d", playerId))
func (c *Carrier) buildHeader(playerID int32) []byte {
playerIDString := []byte(fmt.Sprintf("%d", playerID))
lengthOfString := make([]byte, 4)
binary.LittleEndian.PutUint32(lengthOfString, uint32(len(playerIdString)))
return append(lengthOfString, playerIdString...)
binary.LittleEndian.PutUint32(lengthOfString, uint32(len(playerIDString)))
return append(lengthOfString, playerIDString...)
}

// Read reads the response from the TCP connection and unmarshals it.
Expand Down
50 changes: 25 additions & 25 deletions pkg/ephemeral/io/carrier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

var _ = Describe("Carrier", func() {
var ctx = context.TODO()
var playerId = int32(1) // PlayerID 1, since PlayerID==0 contains another check when connecting
var playerID = int32(1) // PlayerID 1, since PlayerID==0 contains another check when connecting

It("connects to a socket", func() {
var connected bool
Expand All @@ -28,14 +28,14 @@ var _ = Describe("Carrier", func() {
connected = true
return &conn, nil
}
fakeTlsConnector := func(connection net.Conn, playerID int32) (net.Conn, error) {
fakeTLSConnector := func(connection net.Conn, playerID int32) (net.Conn, error) {
return connection, nil
}
carrier := Carrier{
Dialer: fakeDialer,
TlsConnector: fakeTlsConnector,
TLSConnector: fakeTLSConnector,
}
err := carrier.Connect(playerId, context.TODO(), "", "")
err := carrier.Connect(context.TODO(), playerID, "", "")
Expect(connected).To(BeTrue())
Expect(err).NotTo(HaveOccurred())
})
Expand All @@ -44,14 +44,14 @@ var _ = Describe("Carrier", func() {
fakeDialer := func(ctx context.Context, addr, port string) (net.Conn, error) {
return &conn, nil
}
fakeTlsConnector := func(connection net.Conn, playerID int32) (net.Conn, error) {
fakeTLSConnector := func(connection net.Conn, playerID int32) (net.Conn, error) {
return connection, nil
}
carrier := Carrier{
Dialer: fakeDialer,
TlsConnector: fakeTlsConnector,
TLSConnector: fakeTLSConnector,
}
err := carrier.Connect(playerId, context.TODO(), "", "")
err := carrier.Connect(context.TODO(), playerID, "", "")
Expect(err).NotTo(HaveOccurred())
err = carrier.Close()
Expect(err).NotTo(HaveOccurred())
Expand All @@ -61,7 +61,7 @@ var _ = Describe("Carrier", func() {
var (
secret []amphora.SecretShare
output []byte
connectionOutput []byte //Will contain (length 4 byte, playerId 1 byte)
connectionOutput []byte //Will contain (length 4 byte, playerID 1 byte)
client, server net.Conn
dialer func(ctx context.Context, addr, port string) (net.Conn, error)
fakeTlsConnector func(conn net.Conn, playerID int32) (net.Conn, error)
Expand Down Expand Up @@ -89,26 +89,26 @@ var _ = Describe("Carrier", func() {
carrier := Carrier{
Dialer: dialer,
Packer: packer,
TlsConnector: fakeTlsConnector,
TLSConnector: fakeTlsConnector,
}
go server.Read(connectionOutput)
carrier.Connect(playerId, ctx, "", "")
carrier.Connect(ctx, playerID, "", "")
go server.Read(output)
err := carrier.Send(secret)
carrier.Close()
Expect(err).NotTo(HaveOccurred())
Expect(output[0]).To(Equal(byte(1)))
Expect(connectionOutput).To(Equal([]byte{1, 0, 0, 0, fmt.Sprintf("%d", playerId)[0]}))
Expect(connectionOutput).To(Equal([]byte{1, 0, 0, 0, fmt.Sprintf("%d", playerID)[0]}))
})
It("returns an error when it fails to marshal the object", func() {
packer := &FakeBrokenPacker{}
carrier := Carrier{
Dialer: dialer,
Packer: packer,
TlsConnector: fakeTlsConnector,
TLSConnector: fakeTlsConnector,
}
go server.Read(connectionOutput)
carrier.Connect(playerId, ctx, "", "")
carrier.Connect(ctx, playerID, "", "")
go server.Read(output)
err := carrier.Send(secret)
carrier.Close()
Expand All @@ -122,10 +122,10 @@ var _ = Describe("Carrier", func() {
carrier := Carrier{
Dialer: dialer,
Packer: packer,
TlsConnector: fakeTlsConnector,
TLSConnector: fakeTlsConnector,
}
go server.Read(connectionOutput)
carrier.Connect(playerId, ctx, "", "")
carrier.Connect(ctx, playerID, "", "")
// Closing the connection to trigger a failure due to writing into the closed socket.
server.Close()
err := carrier.Send(secret)
Expand All @@ -144,10 +144,10 @@ var _ = Describe("Carrier", func() {
carrier := Carrier{
Dialer: dialer,
Packer: &packer,
TlsConnector: fakeTlsConnector,
TLSConnector: fakeTlsConnector,
}
go server.Read(connectionOutput)
carrier.Connect(playerId, ctx, "", "")
carrier.Connect(ctx, playerID, "", "")
go func() {
server.Write(serverResponse)
server.Close()
Expand All @@ -166,10 +166,10 @@ var _ = Describe("Carrier", func() {
carrier := Carrier{
Dialer: dialer,
Packer: &packer,
TlsConnector: fakeTlsConnector,
TLSConnector: fakeTlsConnector,
}
go server.Read(connectionOutput)
carrier.Connect(playerId, ctx, "", "")
carrier.Connect(ctx, playerID, "", "")
server.Close()
anyConverter := &PlaintextConverter{}
_, err := carrier.Read(anyConverter, false)
Expand All @@ -181,10 +181,10 @@ var _ = Describe("Carrier", func() {
carrier := Carrier{
Dialer: dialer,
Packer: packer,
TlsConnector: fakeTlsConnector,
TLSConnector: fakeTlsConnector,
}
go server.Read(connectionOutput)
carrier.Connect(playerId, ctx, "", "")
carrier.Connect(ctx, playerID, "", "")
go func() {
server.Write(serverResponse)
server.Close()
Expand All @@ -196,7 +196,7 @@ var _ = Describe("Carrier", func() {
})

Context("when connecting as Player0", func() {
playerId := int32(0)
playerID := int32(0)
It("will receive and handle the server's fileHeader", func() {
// Arrange
// ToDo: Better Response for real-life scenario?
Expand All @@ -205,7 +205,7 @@ var _ = Describe("Carrier", func() {
carrier := Carrier{
Dialer: dialer,
Packer: packer,
TlsConnector: fakeTlsConnector,
TLSConnector: fakeTlsConnector,
}
waitGroup := sync.WaitGroup{}
waitGroup.Add(1)
Expand All @@ -214,7 +214,7 @@ var _ = Describe("Carrier", func() {
// Act
var errConnecting error
go func() {
errConnecting = carrier.Connect(playerId, ctx, "", "")
errConnecting = carrier.Connect(ctx, playerID, "", "")
waitGroup.Done()
}()

Expand All @@ -225,7 +225,7 @@ var _ = Describe("Carrier", func() {
waitGroup.Wait()

// Assert
Expect(connectionOutput).To(Equal([]byte{1, 0, 0, 0, fmt.Sprintf("%d", playerId)[0]}))
Expect(connectionOutput).To(Equal([]byte{1, 0, 0, 0, fmt.Sprintf("%d", playerID)[0]}))
Expect(errConnecting).NotTo(HaveOccurred())
Expect(errWrite).NotTo(HaveOccurred())
Expect(numberOfBytesWritten).To(Equal(len(serverResponse)))
Expand Down
4 changes: 2 additions & 2 deletions pkg/ephemeral/io/feeder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func NewAmphoraFeeder(l *zap.SugaredLogger, conf *SPDZEngineTypedConfig) *Amphor
Packer: &SPDZPacker{
MaxBulkSize: conf.MaxBulkSize,
},
TlsConnector: network.NewTlsConnector(),
TLSConnector: network.NewTLSConnector(),
}
return &AmphoraFeeder{
logger: l,
Expand Down Expand Up @@ -119,7 +119,7 @@ func (f *AmphoraFeeder) feedAndRead(params []string, port string, ctx *CtxConfig
default:
return nil, fmt.Errorf("no output config is given, either %s, %s or %s must be defined", PlainText, SecretShare, AmphoraSecret)
}
err := f.carrier.Connect(ctx.Spdz.PlayerID, ctx.Context, "localhost", port)
err := f.carrier.Connect(ctx.Context, ctx.Spdz.PlayerID, "localhost", port)
defer f.carrier.Close()
if err != nil {
return nil, err
Expand Down
6 changes: 3 additions & 3 deletions pkg/ephemeral/io/feeder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ type FakeCarrier struct {
isBulk bool
}

func (f *FakeCarrier) Connect(int32, context.Context, string, string) error {
func (f *FakeCarrier) Connect(context.Context, int32, string, string) error {
return nil
}

Expand All @@ -233,7 +233,7 @@ type BrokenConnectFakeCarrier struct {
isBulk bool
}

func (f *BrokenConnectFakeCarrier) Connect(int32, context.Context, string, string) error {
func (f *BrokenConnectFakeCarrier) Connect(context.Context, int32, string, string) error {
return errors.New("carrier connect error")
}

Expand All @@ -254,7 +254,7 @@ type BrokenSendFakeCarrier struct {
isBulk bool
}

func (f *BrokenSendFakeCarrier) Connect(int32, context.Context, string, string) error {
func (f *BrokenSendFakeCarrier) Connect(context.Context, int32, string, string) error {
return nil
}

Expand Down
18 changes: 13 additions & 5 deletions pkg/ephemeral/network/tls_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,19 @@ import (
"net"
)

func NewTlsConnector() func(conn net.Conn, playerID int32) (net.Conn, error) {
return NewTlsConnectorWithPath("Player-Data")
// NewTLSConnector creates a TLS connector Function in the default Path "Player-Data"
// Simply delegates to NewTLSConnectorWithPath
func NewTLSConnector() func(conn net.Conn, playerID int32) (net.Conn, error) {
return NewTLSConnectorWithPath("Player-Data")
}

func NewTlsConnectorWithPath(folderPath string) func(conn net.Conn, playerID int32) (net.Conn, error) {
// NewTLSConnectorWithPath creates a new TLS connector Function.
// The function will accept the Socket Connection and the PlayerID and upgrade it to a TLS encrypted one.
// Will search for Certificates in the provided folder Path.
// Certificates must be named in the format that MP-SPDZ uses (<Folder>/C<PlayerID>.pem and .key)
func NewTLSConnectorWithPath(folderPath string) func(conn net.Conn, playerID int32) (net.Conn, error) {
return func(conn net.Conn, playerID int32) (net.Conn, error) {
tlsConfig, err := getTlsConfig(playerID, folderPath)
tlsConfig, err := getTLSConfig(playerID, folderPath)
if err != nil {
return nil, err
}
Expand All @@ -27,7 +33,9 @@ func NewTlsConnectorWithPath(folderPath string) func(conn net.Conn, playerID int
}
}

func getTlsConfig(playerID int32, folder string) (*tls.Config, error) {
// getTLSConfig Loads the TLS Config for the provided PlayerId located in the given folder
// Certificates must be named in the format that MP-SPDZ uses (<Folder>/C<PlayerID>.pem and .key)
func getTLSConfig(playerID int32, folder string) (*tls.Config, error) {
certFile := fmt.Sprintf("%s/C%d.pem", folder, playerID)
keyFile := fmt.Sprintf("%s/C%d.key", folder, playerID)
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
Expand Down
10 changes: 5 additions & 5 deletions pkg/ephemeral/network/tls_connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ LO+mQ15hUEpbjrXF3IdY+4MjDqFOETC0KuI72yjUGPZqWe+WAhBcni3VNzs2Ik4=
-----END CERTIFICATE-----`
)

var _ = Describe("TlsConnector", func() {
var _ = Describe("TLSConnector", func() {
var testDataFolder string
var certificateFolder string
var playerID = int32(0)
Expand Down Expand Up @@ -158,7 +158,7 @@ var _ = Describe("TlsConnector", func() {
)

BeforeEach(func() {
tlsConnector = NewTlsConnectorWithPath(certificateFolder)
tlsConnector = NewTLSConnectorWithPath(certificateFolder)
client, server = net.Pipe()
})

Expand All @@ -173,16 +173,16 @@ var _ = Describe("TlsConnector", func() {
serverConfig := &tls.Config{
Certificates: []tls.Certificate{serverCertificate},
}
serverTlsConnection := tls.Server(server, serverConfig)
go serverTlsConnection.Handshake()
serverTLSConnection := tls.Server(server, serverConfig)
go serverTLSConnection.Handshake()

// Act
tlsConnection, err := tlsConnector(client, playerID)
contentToSend := []byte{byte(1)}
go tlsConnection.Write(contentToSend)

contentToReceive := make([]byte, 1)
serverTlsConnection.Read(contentToReceive)
serverTLSConnection.Read(contentToReceive)

// Assert
Expect(err).NotTo(HaveOccurred())
Expand Down

0 comments on commit 86425bc

Please sign in to comment.