Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(resolution): restore hex.Decode/Encode when loading/storing ciphered data from database #395

Merged
merged 2 commits into from
Nov 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion models/resolution/resolution.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package resolution

import (
"bytes"
"encoding/hex"
"encoding/json"
"time"

Expand Down Expand Up @@ -166,6 +167,9 @@ func Create(dbp zesty.DBProvider, t *task.Task, resolverInputs map[string]interf
if err != nil {
return nil, err
}

dst := make([]byte, hex.EncodedLen(len(encryptedSteps)))
hex.Encode(dst, encryptedSteps)
r.EncryptedSteps = encryptedSteps

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

compressedSteps, err := models.EncryptionKey.Decrypt(r.EncryptedSteps, []byte(r.PublicID))
dst := make([]byte, hex.DecodedLen(len(r.EncryptedSteps)))

// if we can't hex Decode, we might be in the case of a Resolution row in database that was
// created between the v1.21.1 and v1.21.3 that was bugged, and failed to hex Encode/Decode the
// ciphered data. We need to keep backward compatibility for those, but this should not happen
// often.
// See https://github.com/ovh/utask/commit/bf23fbb10b62bb487ac4ea01b1e519f85480e58b and migration
// from symmecrypt.Key.DecryptMarshal to symmecrypt.Key.Decrypt
if _, err = hex.Decode(dst, r.EncryptedSteps); err != nil {
dst = r.EncryptedSteps
}

compressedSteps, err := models.EncryptionKey.Decrypt(dst, []byte(r.PublicID))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -377,6 +393,8 @@ func (r *Resolution) Update(dbp zesty.DBProvider) (err error) {
return err
}

dst := make([]byte, hex.EncodedLen(len(compressedSteps)))
hex.Encode(dst, compressedSteps)
encryptedSteps, err := models.EncryptionKey.Encrypt(compressedSteps, []byte(r.PublicID))
if err != nil {
return err
Expand Down