Skip to content

Commit

Permalink
fix checksum calculation on patch commands
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
  • Loading branch information
matthyx committed Sep 16, 2024
1 parent 3765328 commit 4d4a815
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
15 changes: 10 additions & 5 deletions adapters/incluster/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,6 @@ func (c *Client) callPutOrPatch(ctx context.Context, id domain.KindName, baseObj
c.ShadowObjects[id.String()] = baseObject
}
if oldObject, ok := c.ShadowObjects[id.String()]; ok {
// calculate checksum
checksum, err := utils.CanonicalHash(newObject)
if err != nil {
return fmt.Errorf("calculate checksum: %w", err)
}
// calculate patch
patch, err := jsonpatch.CreateMergePatch(oldObject, newObject)
if err != nil {
Expand All @@ -342,6 +337,16 @@ func (c *Client) callPutOrPatch(ctx context.Context, id domain.KindName, baseObj
if emptyPatch.Match(patch) {
return nil
}
// apply patch to calculate checksum on result
mergeResult, err := jsonpatch.MergePatch(oldObject, patch)
if err != nil {
return fmt.Errorf("verifying patch: %w", err)
}
// calculate checksum
checksum, err := utils.CanonicalHash(mergeResult)
if err != nil {
return fmt.Errorf("calculate checksum: %w", err)
}
err = c.callbacks.PatchObject(ctx, id, checksum, patch)
if err != nil {
return fmt.Errorf("send patch object: %w", err)
Expand Down
30 changes: 20 additions & 10 deletions adapters/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,16 +110,21 @@ func (m *MockAdapter) GetObject(ctx context.Context, id domain.KindName, baseObj
m.shadowObjects[id.String()] = baseObject
}
if oldObject, ok := m.shadowObjects[id.String()]; ok {
// calculate checksum
checksum, err := utils.CanonicalHash(object)
if err != nil {
return fmt.Errorf("calculate checksum: %w", err)
}
// calculate patch
patch, err := jsonpatch.CreateMergePatch(oldObject, object)
if err != nil {
return fmt.Errorf("create merge patch: %w", err)
}
// apply patch to calculate checksum on result
mergeResult, err := jsonpatch.MergePatch(oldObject, patch)
if err != nil {
return fmt.Errorf("verifying patch: %w", err)
}
// calculate checksum
checksum, err := utils.CanonicalHash(mergeResult)
if err != nil {
return fmt.Errorf("calculate checksum: %w", err)
}
return m.callbacks.PatchObject(ctx, id, checksum, patch)
} else {
return m.callbacks.PutObject(ctx, id, object)
Expand Down Expand Up @@ -248,16 +253,21 @@ func (m *MockAdapter) TestCallPutOrPatch(ctx context.Context, id domain.KindName
m.shadowObjects[id.String()] = baseObject
}
if oldObject, ok := m.shadowObjects[id.String()]; ok {
// calculate checksum
checksum, err := utils.CanonicalHash(newObject)
if err != nil {
return fmt.Errorf("calculate checksum: %w", err)
}
// calculate patch
patch, err := jsonpatch.CreateMergePatch(oldObject, newObject)
if err != nil {
return fmt.Errorf("create merge patch: %w", err)
}
// apply patch to calculate checksum on result
mergeResult, err := jsonpatch.MergePatch(oldObject, patch)
if err != nil {
return fmt.Errorf("verifying patch: %w", err)
}
// calculate checksum
checksum, err := utils.CanonicalHash(mergeResult)
if err != nil {
return fmt.Errorf("calculate checksum: %w", err)
}
err = m.callbacks.PatchObject(ctx, id, checksum, patch)
if err != nil {
return fmt.Errorf("send patch object: %w", err)
Expand Down

0 comments on commit 4d4a815

Please sign in to comment.