Skip to content

Commit 4b2ed72

Browse files
authored
fix(resolution): restore hex.Decode/Encode when loading/storing ciphered data from database (#395)
* fix(resolution): restore hex.Decode/Encode when loading/storing ciphered data from database Previously, we were using symmecrypt.DecryptMarshal to load data from the database, and we switched to symmecrypt.Decrypt with bf23fbb. This changed the behaviour because DecryptMarshal was also calling hex.Decode on the ciphered text before doing the Decrypt. We had to restore this behaviour to read old data from our database. Same for EncryptMarshal/Encrypt/hex.Encode Signed-off-by: Romain Beuque <556072+rbeuque74@users.noreply.github.com>
1 parent e08f099 commit 4b2ed72

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

models/resolution/resolution.go

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package resolution
22

33
import (
44
"bytes"
5+
"encoding/hex"
56
"encoding/json"
67
"time"
78

@@ -166,6 +167,9 @@ func Create(dbp zesty.DBProvider, t *task.Task, resolverInputs map[string]interf
166167
if err != nil {
167168
return nil, err
168169
}
170+
171+
dst := make([]byte, hex.EncodedLen(len(encryptedSteps)))
172+
hex.Encode(dst, encryptedSteps)
169173
r.EncryptedSteps = encryptedSteps
170174

171175
err = tt.ValidateResolverInputs(resolverInputs)
@@ -247,7 +251,19 @@ func load(dbp zesty.DBProvider, publicID string, locked bool, lockNoWait bool) (
247251
return nil, err
248252
}
249253

250-
compressedSteps, err := models.EncryptionKey.Decrypt(r.EncryptedSteps, []byte(r.PublicID))
254+
dst := make([]byte, hex.DecodedLen(len(r.EncryptedSteps)))
255+
256+
// if we can't hex Decode, we might be in the case of a Resolution row in database that was
257+
// created between the v1.21.1 and v1.21.3 that was bugged, and failed to hex Encode/Decode the
258+
// ciphered data. We need to keep backward compatibility for those, but this should not happen
259+
// often.
260+
// See https://github.com/ovh/utask/commit/bf23fbb10b62bb487ac4ea01b1e519f85480e58b and migration
261+
// from symmecrypt.Key.DecryptMarshal to symmecrypt.Key.Decrypt
262+
if _, err = hex.Decode(dst, r.EncryptedSteps); err != nil {
263+
dst = r.EncryptedSteps
264+
}
265+
266+
compressedSteps, err := models.EncryptionKey.Decrypt(dst, []byte(r.PublicID))
251267
if err != nil {
252268
return nil, err
253269
}
@@ -377,6 +393,8 @@ func (r *Resolution) Update(dbp zesty.DBProvider) (err error) {
377393
return err
378394
}
379395

396+
dst := make([]byte, hex.EncodedLen(len(compressedSteps)))
397+
hex.Encode(dst, compressedSteps)
380398
encryptedSteps, err := models.EncryptionKey.Encrypt(compressedSteps, []byte(r.PublicID))
381399
if err != nil {
382400
return err

0 commit comments

Comments
 (0)