Skip to content

Commit 277331d

Browse files
authored
feat(policy-devel): support providing allowed hostnames (#2339)
Signed-off-by: Miguel Martinez <[email protected]>
1 parent 6f6a4cb commit 277331d

File tree

4 files changed

+45
-34
lines changed

4 files changed

+45
-34
lines changed

app/cli/cmd/policy_develop_eval.go

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,12 @@ import (
2626

2727
func newPolicyDevelopEvalCmd() *cobra.Command {
2828
var (
29-
materialPath string
30-
kind string
31-
annotations []string
32-
policyPath string
33-
inputs []string
29+
materialPath string
30+
kind string
31+
annotations []string
32+
policyPath string
33+
inputs []string
34+
allowedHostnames []string
3435
)
3536

3637
cmd := &cobra.Command{
@@ -44,11 +45,12 @@ evaluates the policy against the provided material or attestation.`,
4445
chainloop policy eval --policy policy.yaml --material sbom.json --kind SBOM_CYCLONEDX_JSON --annotation key1=value1,key2=value2 --input key3=value3`,
4546
RunE: func(_ *cobra.Command, _ []string) error {
4647
opts := &action.PolicyEvalOpts{
47-
MaterialPath: materialPath,
48-
Kind: kind,
49-
Annotations: parseKeyValue(annotations),
50-
PolicyPath: policyPath,
51-
Inputs: parseKeyValue(inputs),
48+
MaterialPath: materialPath,
49+
Kind: kind,
50+
Annotations: parseKeyValue(annotations),
51+
PolicyPath: policyPath,
52+
Inputs: parseKeyValue(inputs),
53+
AllowedHostnames: allowedHostnames,
5254
}
5355

5456
policyEval, err := action.NewPolicyEval(opts, actionOpts)
@@ -71,6 +73,7 @@ evaluates the policy against the provided material or attestation.`,
7173
cmd.Flags().StringSliceVar(&annotations, "annotation", []string{}, "Key-value pairs of material annotations (key=value)")
7274
cmd.Flags().StringVarP(&policyPath, "policy", "p", "policy.yaml", "Path to custom policy file")
7375
cmd.Flags().StringSliceVar(&inputs, "input", []string{}, "Key-value pairs of policy inputs (key=value)")
76+
cmd.Flags().StringSliceVar(&allowedHostnames, "allowed-hostnames", []string{}, "Additional hostnames allowed for http.send requests in policies")
7477

7578
return cmd
7679
}

app/cli/documentation/cli-reference.mdx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2810,12 +2810,13 @@ chainloop policy eval --policy policy.yaml --material sbom.json --kind SBOM_CYCL
28102810
Options
28112811

28122812
```
2813-
--annotation strings Key-value pairs of material annotations (key=value)
2814-
-h, --help help for eval
2815-
--input strings Key-value pairs of policy inputs (key=value)
2816-
--kind string Kind of the material: ["ARTIFACT" "ATTESTATION" "BLACKDUCK_SCA_JSON" "CHAINLOOP_RUNNER_CONTEXT" "CONTAINER_IMAGE" "CSAF_INFORMATIONAL_ADVISORY" "CSAF_SECURITY_ADVISORY" "CSAF_SECURITY_INCIDENT_RESPONSE" "CSAF_VEX" "EVIDENCE" "GHAS_CODE_SCAN" "GHAS_DEPENDENCY_SCAN" "GHAS_SECRET_SCAN" "GITLAB_SECURITY_REPORT" "HELM_CHART" "JACOCO_XML" "JUNIT_XML" "OPENVEX" "SARIF" "SBOM_CYCLONEDX_JSON" "SBOM_SPDX_JSON" "SLSA_PROVENANCE" "STRING" "TWISTCLI_SCAN_JSON" "ZAP_DAST_ZIP"]
2817-
--material string Path to material or attestation file
2818-
-p, --policy string Path to custom policy file (default "policy.yaml")
2813+
--allowed-hostnames strings Additional hostnames allowed for http.send requests in policies
2814+
--annotation strings Key-value pairs of material annotations (key=value)
2815+
-h, --help help for eval
2816+
--input strings Key-value pairs of policy inputs (key=value)
2817+
--kind string Kind of the material: ["ARTIFACT" "ATTESTATION" "BLACKDUCK_SCA_JSON" "CHAINLOOP_RUNNER_CONTEXT" "CONTAINER_IMAGE" "CSAF_INFORMATIONAL_ADVISORY" "CSAF_SECURITY_ADVISORY" "CSAF_SECURITY_INCIDENT_RESPONSE" "CSAF_VEX" "EVIDENCE" "GHAS_CODE_SCAN" "GHAS_DEPENDENCY_SCAN" "GHAS_SECRET_SCAN" "GITLAB_SECURITY_REPORT" "HELM_CHART" "JACOCO_XML" "JUNIT_XML" "OPENVEX" "SARIF" "SBOM_CYCLONEDX_JSON" "SBOM_SPDX_JSON" "SLSA_PROVENANCE" "STRING" "TWISTCLI_SCAN_JSON" "ZAP_DAST_ZIP"]
2818+
--material string Path to material or attestation file
2819+
-p, --policy string Path to custom policy file (default "policy.yaml")
28192820
```
28202821

28212822
Options inherited from parent commands

app/cli/internal/action/policy_develop_eval.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,12 @@ import (
2222
)
2323

2424
type PolicyEvalOpts struct {
25-
MaterialPath string
26-
Kind string
27-
Annotations map[string]string
28-
PolicyPath string
29-
Inputs map[string]string
25+
MaterialPath string
26+
Kind string
27+
Annotations map[string]string
28+
PolicyPath string
29+
Inputs map[string]string
30+
AllowedHostnames []string
3031
}
3132

3233
type PolicyEvalResult struct {
@@ -50,11 +51,12 @@ func NewPolicyEval(opts *PolicyEvalOpts, actionOpts *ActionsOpts) (*PolicyEval,
5051

5152
func (action *PolicyEval) Run() ([]*PolicyEvalResult, error) {
5253
evalOpts := &policydevel.EvalOptions{
53-
PolicyPath: action.opts.PolicyPath,
54-
MaterialKind: action.opts.Kind,
55-
Annotations: action.opts.Annotations,
56-
MaterialPath: action.opts.MaterialPath,
57-
Inputs: action.opts.Inputs,
54+
PolicyPath: action.opts.PolicyPath,
55+
MaterialKind: action.opts.Kind,
56+
Annotations: action.opts.Annotations,
57+
MaterialPath: action.opts.MaterialPath,
58+
Inputs: action.opts.Inputs,
59+
AllowedHostnames: action.opts.AllowedHostnames,
5860
}
5961

6062
// Evaluate policy

app/cli/internal/policydevel/eval.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ import (
2929
)
3030

3131
type EvalOptions struct {
32-
PolicyPath string
33-
MaterialKind string
34-
Annotations map[string]string
35-
MaterialPath string
36-
Inputs map[string]string
32+
PolicyPath string
33+
MaterialKind string
34+
Annotations map[string]string
35+
MaterialPath string
36+
Inputs map[string]string
37+
AllowedHostnames []string
3738
}
3839

3940
type EvalResult struct {
@@ -58,7 +59,7 @@ func Evaluate(opts *EvalOptions, logger zerolog.Logger) ([]*EvalResult, error) {
5859
material.Annotations = opts.Annotations
5960

6061
// 3. Verify material against policy
61-
result, err := verifyMaterial(schema, material, opts.MaterialPath, &logger)
62+
result, err := verifyMaterial(schema, material, opts.MaterialPath, opts.AllowedHostnames, &logger)
6263
if err != nil {
6364
return nil, err
6465
}
@@ -81,8 +82,12 @@ func createCraftingSchema(policyPath string, inputs map[string]string) (*v1.Craf
8182
}, nil
8283
}
8384

84-
func verifyMaterial(schema *v1.CraftingSchema, material *v12.Attestation_Material, materialPath string, logger *zerolog.Logger) ([]*EvalResult, error) {
85-
v := policies.NewPolicyVerifier(schema, nil, logger)
85+
func verifyMaterial(schema *v1.CraftingSchema, material *v12.Attestation_Material, materialPath string, allowedHostnames []string, logger *zerolog.Logger) ([]*EvalResult, error) {
86+
var opts []policies.PolicyVerifierOption
87+
if len(allowedHostnames) > 0 {
88+
opts = append(opts, policies.WithAllowedHostnames(allowedHostnames...))
89+
}
90+
v := policies.NewPolicyVerifier(schema, nil, logger, opts...)
8691
policyEvs, err := v.VerifyMaterial(context.Background(), material, materialPath)
8792
if err != nil {
8893
return nil, err

0 commit comments

Comments
 (0)