File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed
Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -125,7 +125,15 @@ func (f *Format) update(old *Format, force bool) error {
125125 args = []interface {}{"meta version" , old .MetaVersion , f .MetaVersion }
126126 }
127127 if args == nil {
128- f .UUID = old .UUID
128+ if f .UUID != old .UUID {
129+ if err := f .Decrypt (); err != nil {
130+ return fmt .Errorf ("decrypt format: %s" , err )
131+ }
132+ f .UUID = old .UUID // UUID cannot be changed alone
133+ if err := f .Encrypt (); err != nil {
134+ return fmt .Errorf ("encrypt format: %s" , err )
135+ }
136+ }
129137 } else {
130138 return fmt .Errorf ("cannot update volume %s from %v to %v" , args ... )
131139 }
Original file line number Diff line number Diff line change @@ -21,6 +21,7 @@ import (
2121 "testing"
2222
2323 "github.com/juicedata/juicefs/pkg/object"
24+ "github.com/stretchr/testify/assert"
2425)
2526
2627func TestRemoveSecret (t * testing.T ) {
@@ -66,3 +67,26 @@ func TestEncrypt(t *testing.T) {
6667 })
6768 }
6869}
70+
71+ func TestFormat_Update_KeyConflict (t * testing.T ) {
72+ oldFormat := Format {Name : "test" , UUID : "UUID-A" }
73+
74+ newFormat := Format {Name : "test" , UUID : "UUID-B" , SecretKey : "secret" }
75+ if err := newFormat .Encrypt (); err != nil {
76+ t .Fatal (err )
77+ }
78+ assert .True (t , newFormat .KeyEncrypted )
79+
80+ if err := newFormat .update (& oldFormat , false ); err != nil {
81+ t .Fatal (err )
82+ }
83+
84+ assert .Equal (t , "UUID-A" , newFormat .UUID )
85+ assert .True (t , newFormat .KeyEncrypted )
86+
87+ if err := newFormat .Decrypt (); err != nil {
88+ t .Fatalf ("failed to decrypt with new UUID (which is old UUID A): %s" , err )
89+ }
90+
91+ assert .Equal (t , "secret" , newFormat .SecretKey )
92+ }
You can’t perform that action at this time.
0 commit comments