Skip to content

Commit 5523658

Browse files
[SNC-387] in policy/decide+eval, perform policy compilation only when context=config. (#983)
1 parent 4bc986f commit 5523658

File tree

3 files changed

+5
-14
lines changed

3 files changed

+5
-14
lines changed

cmd/policy/policy.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -283,9 +283,6 @@ This group of commands allows the management of polices to be verified against b
283283
if policyPath == "" && ownerID == "" {
284284
return fmt.Errorf("either [policy_file_or_dir_path] or --owner-id is required")
285285
}
286-
if !noCompile && ownerID == "" {
287-
return fmt.Errorf("--owner-id is required for compiling config (use --no-compile to evaluate policy against source config only)")
288-
}
289286

290287
metadata, err := readMetadata(meta, metaFile)
291288
if err != nil {
@@ -297,7 +294,7 @@ This group of commands allows the management of polices to be verified against b
297294
return fmt.Errorf("failed to read input file: %w", err)
298295
}
299296

300-
if !noCompile {
297+
if !noCompile && context == "config" {
301298
compiler := config.New(globalConfig)
302299
input, err = mergeCompiledConfig(compiler, config.ProcessConfigOpts{
303300
ConfigPath: inputPath,
@@ -356,6 +353,7 @@ This group of commands allows the management of polices to be verified against b
356353
inputPath string
357354
meta string
358355
metaFile string
356+
context string
359357
ownerID string
360358
query string
361359
noCompile bool
@@ -367,10 +365,6 @@ This group of commands allows the management of polices to be verified against b
367365
RunE: func(cmd *cobra.Command, args []string) error {
368366
policyPath := args[0]
369367

370-
if !noCompile && ownerID == "" {
371-
return fmt.Errorf("--owner-id is required for compiling config (use --no-compile to evaluate policy against source config only)")
372-
}
373-
374368
metadata, err := readMetadata(meta, metaFile)
375369
if err != nil {
376370
return fmt.Errorf("failed to read metadata: %w", err)
@@ -381,7 +375,7 @@ This group of commands allows the management of polices to be verified against b
381375
return fmt.Errorf("failed to read input file: %w", err)
382376
}
383377

384-
if !noCompile {
378+
if !noCompile && context == "config" {
385379
compiler := config.New(globalConfig)
386380
input, err = mergeCompiledConfig(compiler, config.ProcessConfigOpts{
387381
ConfigPath: inputPath,
@@ -410,6 +404,7 @@ This group of commands allows the management of polices to be verified against b
410404

411405
cmd.Flags().StringVar(&ownerID, "owner-id", "", "the id of the policy's owner")
412406
cmd.Flags().StringVar(&inputPath, "input", "", "path to input file")
407+
cmd.Flags().StringVar(&context, "context", "config", "policy context for decision")
413408
cmd.Flags().StringVar(&meta, "meta", "", "decision metadata (json string)")
414409
cmd.Flags().StringVar(&metaFile, "metafile", "", "decision metadata file")
415410
cmd.Flags().StringVar(&query, "query", "data", "policy decision query")

cmd/policy/policy_test.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -826,11 +826,6 @@ test: config
826826
Args: []string{"decide", "./testdata/test0/policy.rego", "--input", "./testdata/test1/test.yml", "--meta", "{}", "--metafile", "somefile", "--no-compile"},
827827
ExpectedErr: "failed to read metadata: use either --meta or --metafile flag, but not both",
828828
},
829-
{
830-
Name: "fails if config compilation is enabled, but owner-id isn't provided",
831-
Args: []string{"decide", "./testdata/test0/policy.rego", "--input", "./testdata/test1/test.yml"},
832-
ExpectedErr: "--owner-id is required for compiling config (use --no-compile to evaluate policy against source config only)",
833-
},
834829
{
835830
Name: "successfully performs decision for policy FILE provided locally",
836831
Args: []string{"decide", "./testdata/test0/policy.rego", "--input", "./testdata/test0/config.yml", "--no-compile"},

cmd/policy/testdata/policy/eval-expected-usage.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ Examples:
55
circleci policy eval ./policies --input ./.circleci/config.yml
66

77
Flags:
8+
--context string policy context for decision (default "config")
89
--input string path to input file
910
--meta string decision metadata (json string)
1011
--metafile string decision metadata file

0 commit comments

Comments
 (0)