forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
plugins/logs: Fixes unintended mutation of result
When mask rules targeted /result, it was modifying both the result in the decision logs (intended) and the result in the API response (unintended). Added a step to deep copy the result only once, if there is at least one mask rule targeting the result. Fixes open-policy-agent#2752 Signed-off-by: Grant Shively <gshively@godaddy.com>
- Loading branch information
1 parent
5fcb3f0
commit 99a8143
Showing
6 changed files
with
236 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Copyright 2020 The OPA Authors. All rights reserved. | ||
// Use of this source code is governed by an Apache2 | ||
// license that can be found in the LICENSE file. | ||
|
||
package deepcopy | ||
|
||
// DeepCopy performs a recursive deep copy for nested slices/maps and | ||
// returns the copied object. Supports []interface{} | ||
// and map[string]interface{} only | ||
func DeepCopy(val interface{}) interface{} { | ||
switch val := val.(type) { | ||
case []interface{}: | ||
cpy := make([]interface{}, len(val)) | ||
for i := range cpy { | ||
cpy[i] = DeepCopy(val[i]) | ||
} | ||
return cpy | ||
case map[string]interface{}: | ||
cpy := make(map[string]interface{}, len(val)) | ||
for k := range val { | ||
cpy[k] = DeepCopy(val[k]) | ||
} | ||
return cpy | ||
default: | ||
return val | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2020 The OPA Authors. All rights reserved. | ||
// Use of this source code is governed by an Apache2 | ||
// license that can be found in the LICENSE file. | ||
|
||
package deepcopy | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func TestDeepCopyMapRoot(t *testing.T) { | ||
target := map[string]interface{}{ | ||
"a": map[string]interface{}{ | ||
"b": []interface{}{ | ||
"c", | ||
"d", | ||
}, | ||
"e": "f", | ||
}, | ||
"x": "y", | ||
} | ||
result := DeepCopy(target).(map[string]interface{}) | ||
if !reflect.DeepEqual(target, result) { | ||
t.Fatal("Expected result of DeepCopy to be DeepEqual with original.") | ||
} | ||
result["a"] = "mutated" | ||
if target["a"] == "mutated" { | ||
t.Fatal("Expected target to remain unmutated when the DeepCopy result was mutated") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters