Skip to content

Commit aafbcac

Browse files
committed
fixing encrypt error
1 parent df19d43 commit aafbcac

File tree

2 files changed

+27
-14
lines changed

2 files changed

+27
-14
lines changed

pkg/client/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,9 @@ func (c *Client) getInteractions(callback InteractionCallback) error {
470470

471471
// handle root-tld data if any
472472
for _, data := range response.TLDData {
473+
if len(data) == 0 {
474+
continue
475+
}
473476
interaction := &server.Interaction{}
474477
if err := jsoniter.UnmarshalFromString(data, interaction); err != nil {
475478
gologger.Error().Msgf("Could not unmarshal interaction data interaction: %v\n", err)

pkg/storage/storagedb.go

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,17 @@ func (s *StorageDB) SetIDPublicKey(correlationID, secretKey, publicKey string) e
121121
func (s *StorageDB) SetID(ID string) error {
122122
data := &CorrelationData{}
123123

124-
if s.Options.UseDisk() {
125-
aesKey := make([]byte, 32)
126-
if _, err := rand.Read(aesKey); err != nil {
127-
return errors.Wrap(err, "could not generate AES key")
128-
}
129-
data.AESKey = aesKey
130-
}
131-
132124
s.cache.Put(ID, data)
133125
return nil
134126
}
135127

136128
// AddInteraction adds an interaction data to the correlation ID after encrypting
137129
// it with Public Key for the provided correlation ID.
138130
func (s *StorageDB) AddInteraction(correlationID string, data []byte) error {
131+
if len(data) == 0 {
132+
return nil
133+
}
134+
139135
item, found := s.cache.GetIfPresent(correlationID)
140136
if !found {
141137
return ErrCorrelationIdNotFound
@@ -146,10 +142,15 @@ func (s *StorageDB) AddInteraction(correlationID string, data []byte) error {
146142
}
147143

148144
if s.Options.UseDisk() {
149-
ct, err := AESEncrypt(value.AESKey, data)
150-
if err != nil {
151-
return errors.Wrap(err, "could not encrypt event data")
145+
ct := string(data)
146+
if len(value.AESKey) > 0 {
147+
var err error
148+
ct, err = AESEncrypt(value.AESKey, data)
149+
if err != nil {
150+
return errors.Wrap(err, "could not encrypt event data")
151+
}
152152
}
153+
153154
value.Lock()
154155
existingData, _ := s.db.Get([]byte(correlationID), nil)
155156
_ = s.db.Put([]byte(correlationID), AppendMany("\n", existingData, []byte(ct)), nil)
@@ -165,6 +166,10 @@ func (s *StorageDB) AddInteraction(correlationID string, data []byte) error {
165166

166167
// AddInteractionWithId adds an interaction data to the id bucket
167168
func (s *StorageDB) AddInteractionWithId(id string, data []byte) error {
169+
if len(data) == 0 {
170+
return nil
171+
}
172+
168173
item, ok := s.cache.GetIfPresent(id)
169174
if !ok {
170175
return ErrCorrelationIdNotFound
@@ -175,10 +180,15 @@ func (s *StorageDB) AddInteractionWithId(id string, data []byte) error {
175180
}
176181

177182
if s.Options.UseDisk() {
178-
ct, err := AESEncrypt(value.AESKey, data)
179-
if err != nil {
180-
return errors.Wrap(err, "could not encrypt event data")
183+
ct := string(data)
184+
if len(value.AESKey) > 0 {
185+
var err error
186+
ct, err = AESEncrypt(value.AESKey, data)
187+
if err != nil {
188+
return errors.Wrap(err, "could not encrypt event data")
189+
}
181190
}
191+
182192
value.Lock()
183193
existingData, _ := s.db.Get([]byte(id), nil)
184194
_ = s.db.Put([]byte(id), AppendMany("\n", existingData, []byte(ct)), nil)

0 commit comments

Comments
 (0)