Added payload reset on PayloadManager level

This commit is contained in:
Samuel Hawksby-Robinson 2022-07-05 06:40:43 +01:00
parent 48abeedc53
commit 5eb8e5a34f
6 changed files with 38 additions and 21 deletions

View File

@ -47,7 +47,7 @@ type CreateCommunity struct {
Tags []string `json:"tags,omitempty"` Tags []string `json:"tags,omitempty"`
} }
func adaptIdentityImageToProtobuf(img *userimages.IdentityImage) *protobuf.IdentityImage { func adaptIdentityImageToProtobuf(img userimages.IdentityImage) *protobuf.IdentityImage {
return &protobuf.IdentityImage{ return &protobuf.IdentityImage{
Payload: img.Payload, Payload: img.Payload,
SourceType: protobuf.IdentityImage_RAW_PAYLOAD, SourceType: protobuf.IdentityImage_RAW_PAYLOAD,
@ -103,8 +103,8 @@ func (c *CreateCommunity) ToCommunityDescription() (*protobuf.CommunityDescripti
if err != nil { if err != nil {
return nil, err return nil, err
} }
for _, img := range imgs { for i := range imgs {
ciis[img.Name] = adaptIdentityImageToProtobuf(&img) ciis[imgs[i].Name] = adaptIdentityImageToProtobuf(imgs[i])
} }
} }
if c.Banner.ImagePath != "" { if c.Banner.ImagePath != "" {
@ -113,7 +113,7 @@ func (c *CreateCommunity) ToCommunityDescription() (*protobuf.CommunityDescripti
if err != nil { if err != nil {
return nil, err return nil, err
} }
ciis[img.Name] = adaptIdentityImageToProtobuf(img) ciis[img.Name] = adaptIdentityImageToProtobuf(*img)
} }
ci.Images = ciis ci.Images = ciis
log.Info("set images", "images", ci) log.Info("set images", "images", ci)

View File

@ -13,12 +13,12 @@ import (
type PairingClient struct { type PairingClient struct {
*http.Client *http.Client
PayloadManager
baseAddress *url.URL baseAddress *url.URL
certPEM []byte certPEM []byte
privateKey *ecdsa.PrivateKey privateKey *ecdsa.PrivateKey
serverMode Mode serverMode Mode
PayloadManager PayloadManager
} }
func NewPairingClient(c *ConnectionParams, config *PairingPayloadManagerConfig) (*PairingClient, error) { func NewPairingClient(c *ConnectionParams, config *PairingPayloadManagerConfig) (*PairingClient, error) {

View File

@ -145,3 +145,7 @@ func (m *MockEncryptOnlyPayloadManager) ToSend() []byte {
func (m *MockEncryptOnlyPayloadManager) Received() []byte { func (m *MockEncryptOnlyPayloadManager) Received() []byte {
return m.pem.Received() return m.pem.Received()
} }
func (m *MockEncryptOnlyPayloadManager) ResetPayload() {
m.pem.ResetPayload()
}

View File

@ -25,6 +25,7 @@ type PayloadManager interface {
Receive(data []byte) error Receive(data []byte) error
ToSend() []byte ToSend() []byte
Received() []byte Received() []byte
ResetPayload()
} }
// PairingPayloadManagerConfig represents the initialisation parameters required for a PairingPayloadManager // PairingPayloadManagerConfig represents the initialisation parameters required for a PairingPayloadManager
@ -35,6 +36,7 @@ type PairingPayloadManagerConfig struct {
// PairingPayloadManager is responsible for the whole lifecycle of a PairingPayload // PairingPayloadManager is responsible for the whole lifecycle of a PairingPayload
type PairingPayloadManager struct { type PairingPayloadManager struct {
pp *PairingPayload
pem *PayloadEncryptionManager pem *PayloadEncryptionManager
ppm *PairingPayloadMarshaller ppm *PairingPayloadMarshaller
ppr PayloadRepository ppr PayloadRepository
@ -51,6 +53,7 @@ func NewPairingPayloadManager(pk *ecdsa.PrivateKey, config *PairingPayloadManage
p := new(PairingPayload) p := new(PairingPayload)
return &PairingPayloadManager{ return &PairingPayloadManager{
pp: p,
pem: pem, pem: pem,
ppm: NewPairingPayloadMarshaller(p), ppm: NewPairingPayloadMarshaller(p),
ppr: NewPairingPayloadRepository(p, config), ppr: NewPairingPayloadRepository(p, config),
@ -97,6 +100,12 @@ func (ppm *PairingPayloadManager) Received() []byte {
return ppm.pem.Received() return ppm.pem.Received()
} }
// ResetPayload resets all payload state managed by the PairingPayloadManager
func (ppm *PairingPayloadManager) ResetPayload() {
ppm.pp.ResetPayload()
ppm.pem.ResetPayload()
}
// EncryptionPayload represents the plain text and encrypted text of payload data // EncryptionPayload represents the plain text and encrypted text of payload data
type EncryptionPayload struct { type EncryptionPayload struct {
plain []byte plain []byte
@ -161,6 +170,10 @@ type PairingPayload struct {
password string password string
} }
func (pp *PairingPayload) ResetPayload() {
*pp = PairingPayload{}
}
// PairingPayloadMarshaller is responsible for marshalling and unmarshalling PairingServer payload data // PairingPayloadMarshaller is responsible for marshalling and unmarshalling PairingServer payload data
type PairingPayloadMarshaller struct { type PairingPayloadMarshaller struct {
*PairingPayload *PairingPayload

View File

@ -9,10 +9,10 @@ import (
type PairingServer struct { type PairingServer struct {
Server Server
PayloadManager
pk *ecdsa.PrivateKey pk *ecdsa.PrivateKey
mode Mode mode Mode
PayloadManager PayloadManager
} }
type Config struct { type Config struct {

View File

@ -35,7 +35,7 @@ func (s *PairingServerSuite) TestPairingServer_StartPairing() {
s.PS.mode = m s.PS.mode = m
if m == Sending { if m == Sending {
err := s.PS.PayloadManager.Mount() err := s.PS.Mount()
s.Require().NoError(err) s.Require().NoError(err)
} }
@ -64,7 +64,7 @@ func (s *PairingServerSuite) TestPairingServer_StartPairing() {
s.Require().NoError(err) s.Require().NoError(err)
if m == Receiving { if m == Receiving {
err := c.PayloadManager.Mount() err := c.Mount()
s.Require().NoError(err) s.Require().NoError(err)
} }
@ -73,18 +73,18 @@ func (s *PairingServerSuite) TestPairingServer_StartPairing() {
switch m { switch m {
case Receiving: case Receiving:
s.Require().Equal(c.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.toSend.plain, s.PS.PayloadManager.Received()) s.Require().Equal(c.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.toSend.plain, s.PS.Received())
s.Require().Equal(s.PS.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.received.encrypted, c.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.toSend.encrypted) s.Require().Equal(s.PS.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.received.encrypted, c.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.toSend.encrypted)
s.Require().Nil(s.PS.PayloadManager.ToSend()) s.Require().Nil(s.PS.ToSend())
s.Require().Nil(c.PayloadManager.Received()) s.Require().Nil(c.Received())
case Sending: case Sending:
s.Require().Equal(c.PayloadManager.Received(), s.PS.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.toSend.plain) s.Require().Equal(c.Received(), s.PS.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.toSend.plain)
s.Require().Equal(c.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.received.encrypted, s.PS.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.toSend.encrypted) s.Require().Equal(c.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.received.encrypted, s.PS.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.toSend.encrypted)
s.Require().Nil(c.PayloadManager.ToSend()) s.Require().Nil(c.ToSend())
s.Require().Nil(s.PS.PayloadManager.Received()) s.Require().Nil(s.PS.Received())
} }
// Reset the server's PayloadEncryptionManager // Reset the server's PayloadEncryptionManager
s.PS.PayloadManager.(*MockEncryptOnlyPayloadManager).pem.ResetPayload() s.PS.PayloadManager.(*MockEncryptOnlyPayloadManager).ResetPayload()
} }
} }