Skip to content

Commit

Permalink
serialize name with caveat and test
Browse files Browse the repository at this point in the history
  • Loading branch information
vroldanbet committed Sep 15, 2022
1 parent 9fd2bac commit a972c8e
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 70 deletions.
9 changes: 6 additions & 3 deletions pkg/caveats/compile.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
impl "github.com/authzed/spicedb/pkg/proto/impl/v1"
)

const anonymousCaveat = ""

// CompiledCaveat is a compiled form of a caveat.
type CompiledCaveat struct {
// env is the environment under which the CEL program was compiled.
Expand All @@ -22,7 +24,7 @@ type CompiledCaveat struct {
name string
}

// Name
// Name represents a user-friendly reference to a caveat
func (cc CompiledCaveat) Name() string {
return cc.name
}
Expand All @@ -43,6 +45,7 @@ func (cc CompiledCaveat) Serialize() ([]byte, error) {
KindOneof: &impl.DecodedCaveat_Cel{
Cel: cexpr,
},
Name: cc.name,
}

return proto.Marshal(caveat)
Expand Down Expand Up @@ -81,7 +84,7 @@ func CompileCaveat(env *Environment, exprString string) (*CompiledCaveat, error)
return nil, CompilationErrors{fmt.Errorf("caveat expression must result in a boolean value: found `%s`", ast.OutputType().String()), nil}
}

return &CompiledCaveat{celEnv, ast, ""}, nil
return &CompiledCaveat{celEnv, ast, anonymousCaveat}, nil
}

// DeserializeCaveat deserializes a byte-serialized caveat back into a CompiledCaveat.
Expand All @@ -98,5 +101,5 @@ func DeserializeCaveat(env *Environment, serialized []byte) (*CompiledCaveat, er
}

ast := cel.CheckedExprToAst(caveat.GetCel())
return &CompiledCaveat{celEnv, ast, ""}, nil
return &CompiledCaveat{celEnv, ast, caveat.Name}, nil
}
17 changes: 17 additions & 0 deletions pkg/caveats/compile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,20 @@ func TestSerialization(t *testing.T) {
})
}
}

func TestSerializeName(t *testing.T) {
env := mustEnvForVariables(map[string]VariableType{
"a": IntType,
"b": IntType,
})
compiled, err := CompileCaveatWithName(env, "a == 1", "hi")
require.NoError(t, err)

serialized, err := compiled.Serialize()
require.NoError(t, err)

deserialized, err := DeserializeCaveat(env, serialized)
require.NoError(t, err)

require.Equal(t, "hi", deserialized.name)
}
2 changes: 1 addition & 1 deletion pkg/caveats/eval.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func (cr CaveatResult) PartialValue() (*CompiledCaveat, error) {
}

expr := interpreter.PruneAst(cr.parentCaveat.ast.Expr(), cr.details.State())
return &CompiledCaveat{cr.parentCaveat.celEnv, cel.ParsedExprToAst(&exprpb.ParsedExpr{Expr: expr}), ""}, nil
return &CompiledCaveat{cr.parentCaveat.celEnv, cel.ParsedExprToAst(&exprpb.ParsedExpr{Expr: expr}), cr.parentCaveat.name}, nil
}

// EvaluateCaveat evaluates the compiled caveat with the specified values, and returns
Expand Down
143 changes: 77 additions & 66 deletions pkg/proto/impl/v1/impl.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions pkg/proto/impl/v1/impl.pb.validate.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions proto/internal/impl/v1/impl.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ option go_package = "github.com/authzed/spicedb/pkg/proto/impl/v1";
import "google/api/expr/v1alpha1/checked.proto";

message DecodedCaveat {
// we do kind_oneof in case we decide to have non-CEL expressions
oneof kind_oneof {
google.api.expr.v1alpha1.CheckedExpr cel = 1;
}
string name = 2;
}

message DecodedZookie {
Expand Down

0 comments on commit a972c8e

Please sign in to comment.