Skip to content

Commit 9f3b718

Browse files
committed
Have caveat diffs properly check if an expression has changed
By making the serialized caveat expressions stable, the diff should only return a change now when the caveat expr's has actually changed, rather than pretty much always, thus reducing the storage for "changed" caveats Fixes authzed#1742 We should go back to MarshalVT once planetscale/vtprotobuf#133 has merged
1 parent 959cb16 commit 9f3b718

File tree

3 files changed

+65
-8
lines changed

3 files changed

+65
-8
lines changed

pkg/caveats/compile.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66

77
"github.com/authzed/cel-go/cel"
88
"github.com/authzed/cel-go/common"
9+
"google.golang.org/protobuf/proto"
910

1011
"github.com/authzed/spicedb/pkg/caveats/types"
1112
"github.com/authzed/spicedb/pkg/genutil/mapz"
@@ -52,7 +53,9 @@ func (cc CompiledCaveat) Serialize() ([]byte, error) {
5253
Name: cc.name,
5354
}
5455

55-
return caveat.MarshalVT()
56+
// TODO(jschorr): change back to MarshalVT once stable is supported.
57+
// See: https://github.com/planetscale/vtprotobuf/pull/133
58+
return proto.MarshalOptions{Deterministic: true}.Marshal(caveat)
5659
}
5760

5861
// ReferencedParameters returns the names of the parameters referenced in the expression.

pkg/diff/caveats/diff.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,8 @@ const (
3434
// ParameterTypeChanged indicates that the type of the parameter was changed.
3535
ParameterTypeChanged DeltaType = "parameter-type-changed"
3636

37-
// CaveatExpressionMayHaveChanged indicates that the expression of the caveat *may* have changed.
38-
// This uses a direct byte comparison which can return that a change occurred, even when it has
39-
// not.
40-
CaveatExpressionMayHaveChanged DeltaType = "expression-may-have-changed"
37+
// CaveatExpressionChanged indicates that the expression of the caveat has changed.
38+
CaveatExpressionChanged DeltaType = "expression-has-changed"
4139
)
4240

4341
// Diff holds the diff between two caveats.
@@ -154,7 +152,7 @@ func DiffCaveats(existing *core.CaveatDefinition, updated *core.CaveatDefinition
154152

155153
if !bytes.Equal(existing.SerializedExpression, updated.SerializedExpression) {
156154
deltas = append(deltas, Delta{
157-
Type: CaveatExpressionMayHaveChanged,
155+
Type: CaveatExpressionChanged,
158156
})
159157
}
160158

pkg/diff/caveats/diff_test.go

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ func TestCaveatDiff(t *testing.T) {
187187
"false",
188188
),
189189
[]Delta{
190-
{Type: CaveatExpressionMayHaveChanged},
190+
{Type: CaveatExpressionChanged},
191191
},
192192
},
193193
{
@@ -207,9 +207,65 @@ func TestCaveatDiff(t *testing.T) {
207207
"someparam <= 1",
208208
),
209209
[]Delta{
210-
{Type: CaveatExpressionMayHaveChanged},
210+
{Type: CaveatExpressionChanged},
211211
},
212212
},
213+
{
214+
"third changed expression",
215+
ns.MustCaveatDefinition(
216+
caveats.MustEnvForVariables(map[string]types.VariableType{
217+
"someparam": types.IntType,
218+
}),
219+
"somecaveat",
220+
"someparam == 1",
221+
),
222+
ns.MustCaveatDefinition(
223+
caveats.MustEnvForVariables(map[string]types.VariableType{
224+
"someparam": types.IntType,
225+
}),
226+
"somecaveat",
227+
"someparam == 2",
228+
),
229+
[]Delta{
230+
{Type: CaveatExpressionChanged},
231+
},
232+
},
233+
{
234+
"unchanged expression",
235+
ns.MustCaveatDefinition(
236+
caveats.MustEnvForVariables(map[string]types.VariableType{
237+
"someparam": types.IntType,
238+
}),
239+
"somecaveat",
240+
"someparam == 1",
241+
),
242+
ns.MustCaveatDefinition(
243+
caveats.MustEnvForVariables(map[string]types.VariableType{
244+
"someparam": types.IntType,
245+
}),
246+
"somecaveat",
247+
"someparam == 1",
248+
),
249+
[]Delta{},
250+
},
251+
{
252+
"unchanged slightly more complex expression",
253+
ns.MustCaveatDefinition(
254+
caveats.MustEnvForVariables(map[string]types.VariableType{
255+
"someparam": types.IntType,
256+
}),
257+
"somecaveat",
258+
"someparam == {\"a\": 42}.a",
259+
),
260+
ns.MustCaveatDefinition(
261+
caveats.MustEnvForVariables(map[string]types.VariableType{
262+
"someparam": types.IntType,
263+
}),
264+
"somecaveat",
265+
"someparam == {\"a\": 42}.a",
266+
),
267+
[]Delta{},
268+
},
213269
}
214270

215271
for _, tc := range testCases {

0 commit comments

Comments
 (0)