Skip to content

Commit b1a71ca

Browse files
committed
make raw result optional for engine
Signed-off-by: Sylwester Piskozub <sylwesterpiskozub@gmail.com>
1 parent be9dc94 commit b1a71ca

3 files changed

Lines changed: 48 additions & 9 deletions

File tree

app/cli/internal/policydevel/eval.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,9 @@ func verifyMaterial(schema *v1.CraftingSchema, material *v12.Attestation_Materia
9898
if len(allowedHostnames) > 0 {
9999
opts = append(opts, policies.WithAllowedHostnames(allowedHostnames...))
100100
}
101+
102+
opts = append(opts, policies.WithIncludeRawData(debug))
103+
101104
v := policies.NewPolicyVerifier(schema, nil, logger, opts...)
102105
policyEvs, err := v.VerifyMaterial(context.Background(), material, materialPath)
103106
if err != nil {

pkg/policies/engine/rego/rego.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ type Engine struct {
3535
// allowedNetworkDomains is a list of network domains that are allowed for the compiler to access
3636
// when using http.send built-in function
3737
allowedNetworkDomains []string
38+
// includeRawData determines whether to collect raw evaluation data
39+
includeRawData bool
3840
}
3941

4042
type EngineOption func(*newEngineOptions)
@@ -51,9 +53,16 @@ func WithAllowedNetworkDomains(domains ...string) EngineOption {
5153
}
5254
}
5355

56+
func WithIncludeRawData(include bool) EngineOption {
57+
return func(e *newEngineOptions) {
58+
e.includeRawData = include
59+
}
60+
}
61+
5462
type newEngineOptions struct {
5563
operatingMode EnvironmentMode
5664
allowedNetworkDomains []string
65+
includeRawData bool
5766
}
5867

5968
// NewEngine creates a new policy engine with the given options
@@ -78,6 +87,7 @@ func NewEngine(opts ...EngineOption) *Engine {
7887
operatingMode: options.operatingMode,
7988
// append base allowed network domains to the user provided ones
8089
allowedNetworkDomains: append(baseAllowedNetworkDomains, options.allowedNetworkDomains...),
90+
includeRawData: options.includeRawData,
8191
}
8292
}
8393

@@ -154,14 +164,17 @@ func (r *Engine) Verify(ctx context.Context, policy *engine.Policy, input []byte
154164
return err
155165
}
156166

157-
// Get raw results first
158-
if err := executeQuery(getRuleName(parsedModule.Package.Path, ""), r.operatingMode == EnvironmentModeRestrictive); err != nil {
159-
return nil, err
160-
}
167+
var rawData *engine.RawData
168+
// Get raw results first if requested
169+
if r.includeRawData {
170+
if err := executeQuery(getRuleName(parsedModule.Package.Path, ""), r.operatingMode == EnvironmentModeRestrictive); err != nil {
171+
return nil, err
172+
}
161173

162-
rawData := &engine.RawData{
163-
Input: decodedInput,
164-
Output: regoResultSetToRawResults(res),
174+
rawData = &engine.RawData{
175+
Input: decodedInput,
176+
Output: regoResultSetToRawResults(res),
177+
}
165178
}
166179

167180
// Try the main rule first

pkg/policies/policies.go

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,14 @@ type PolicyVerifier struct {
6666
logger *zerolog.Logger
6767
client v13.AttestationServiceClient
6868
allowedHostnames []string
69+
includeRawData bool
6970
}
7071

7172
var _ Verifier = (*PolicyVerifier)(nil)
7273

7374
type PolicyVerifierOptions struct {
7475
AllowedHostnames []string
76+
IncludeRawData bool
7577
}
7678

7779
type PolicyVerifierOption func(*PolicyVerifierOptions)
@@ -82,13 +84,25 @@ func WithAllowedHostnames(hostnames ...string) PolicyVerifierOption {
8284
}
8385
}
8486

87+
func WithIncludeRawData(include bool) PolicyVerifierOption {
88+
return func(o *PolicyVerifierOptions) {
89+
o.IncludeRawData = include
90+
}
91+
}
92+
8593
func NewPolicyVerifier(schema *v1.CraftingSchema, client v13.AttestationServiceClient, logger *zerolog.Logger, opts ...PolicyVerifierOption) *PolicyVerifier {
8694
options := &PolicyVerifierOptions{}
8795
for _, opt := range opts {
8896
opt(options)
8997
}
9098

91-
return &PolicyVerifier{schema: schema, client: client, logger: logger, allowedHostnames: options.AllowedHostnames}
99+
return &PolicyVerifier{
100+
schema: schema,
101+
client: client,
102+
logger: logger,
103+
allowedHostnames: options.AllowedHostnames,
104+
includeRawData: options.IncludeRawData,
105+
}
92106
}
93107

94108
// VerifyMaterial applies all required policies to a material
@@ -175,7 +189,9 @@ func (pv *PolicyVerifier) evaluatePolicyAttachment(ctx context.Context, attachme
175189
return nil, NewPolicyError(err)
176190
}
177191

178-
rawResults = append(rawResults, r.RawData)
192+
if r.RawData != nil {
193+
rawResults = append(rawResults, r.RawData)
194+
}
179195

180196
// Skip if the script explicitly instructs us to ignore it, effectively preventing it from being added to the evaluation results
181197
if r.Ignore {
@@ -330,6 +346,10 @@ func (pv *PolicyVerifier) executeScript(ctx context.Context, script *engine.Poli
330346
engineOpts = append(engineOpts, rego.WithAllowedNetworkDomains(pv.allowedHostnames...))
331347
}
332348

349+
if pv.includeRawData {
350+
engineOpts = append(engineOpts, rego.WithIncludeRawData(true))
351+
}
352+
333353
// verify the policy
334354
ng := rego.NewEngine(engineOpts...)
335355
res, err := ng.Verify(ctx, script, material, getInputArguments(args))
@@ -668,6 +688,9 @@ func LogPolicyEvaluations(evaluations []*v12.PolicyEvaluation, logger *zerolog.L
668688
func engineRawResultsToAPIRawResults(rawResults []*engine.RawData) []*structpb.Struct {
669689
res := make([]*structpb.Struct, 0)
670690
for _, r := range rawResults {
691+
if r == nil {
692+
continue
693+
}
671694
// Convert RawData to map
672695
m := map[string]interface{}{
673696
"input": r.Input,

0 commit comments

Comments
 (0)