/
cel.go
61 lines (45 loc) · 1.47 KB
/
cel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Copyright 2021-2024 Zenauth Ltd.
// SPDX-License-Identifier: Apache-2.0
package internal
import (
"fmt"
"github.com/google/cel-go/checker/decls"
"github.com/google/cel-go/common/types"
exprpb "google.golang.org/genproto/googleapis/api/expr/v1alpha1"
enginev1 "github.com/cerbos/cerbos/api/genpb/cerbos/engine/v1"
"github.com/cerbos/cerbos/internal/conditions"
)
const lastResultVar = "_"
var (
qualifiedPrincipal = conditions.Fqn(conditions.CELPrincipalField)
qualifiedResource = conditions.Fqn(conditions.CELResourceField)
specialVars = buildSpecialVarsSet()
)
func buildSpecialVarsSet() map[string]struct{} {
m := make(map[string]struct{}, len(conditions.StdEnvDecls)+1)
m[lastResultVar] = struct{}{}
for _, d := range conditions.StdEnvDecls {
m[d.GetName()] = struct{}{}
}
return m
}
func resetVarsAndDecls() (variables, map[string]*exprpb.Decl) {
vars := variables{}
for v := range specialVars {
vars[v] = types.NullValue
}
// request and variable decls are already defined in StdEnv
decls := map[string]*exprpb.Decl{lastResultVar: decls.NewVar(lastResultVar, decls.Dyn)}
return vars, decls
}
func getCheckInput(vars variables) (*enginev1.CheckInput, error) {
v, ok := vars[conditions.CELRequestIdent]
if !ok || v == nil || v == types.NullValue {
return &enginev1.CheckInput{}, nil
}
vv, ok := v.Value().(*enginev1.CheckInput)
if !ok {
return nil, fmt.Errorf("unexpected type %T for %q", v.Value(), conditions.CELRequestIdent)
}
return vv, nil
}