-
Notifications
You must be signed in to change notification settings - Fork 39
feat(policy-devel): allow external policy references #2524
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,7 @@ import ( | |
| "fmt" | ||
| "os" | ||
|
|
||
| controlplanev1 "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" | ||
| v1 "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1" | ||
| "github.com/chainloop-dev/chainloop/pkg/casclient" | ||
| "github.com/chainloop-dev/chainloop/pkg/policies" | ||
|
|
@@ -34,13 +35,14 @@ const ( | |
| ) | ||
|
|
||
| type EvalOptions struct { | ||
| PolicyPath string | ||
| MaterialKind string | ||
| Annotations map[string]string | ||
| MaterialPath string | ||
| Inputs map[string]string | ||
| AllowedHostnames []string | ||
| Debug bool | ||
| PolicyPath string | ||
| MaterialKind string | ||
| Annotations map[string]string | ||
| MaterialPath string | ||
| Inputs map[string]string | ||
| AllowedHostnames []string | ||
| Debug bool | ||
| AttestationClient controlplanev1.AttestationServiceClient | ||
| } | ||
|
|
||
| type EvalResult struct { | ||
|
|
@@ -74,7 +76,7 @@ func Evaluate(opts *EvalOptions, logger zerolog.Logger) (*EvalSummary, error) { | |
| material.Annotations = opts.Annotations | ||
|
|
||
| // 3. Verify material against policy | ||
| summary, err := verifyMaterial(policies, material, opts.MaterialPath, opts.Debug, opts.AllowedHostnames, &logger) | ||
| summary, err := verifyMaterial(policies, material, opts.MaterialPath, opts.Debug, opts.AllowedHostnames, opts.AttestationClient, &logger) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
@@ -83,18 +85,26 @@ func Evaluate(opts *EvalOptions, logger zerolog.Logger) (*EvalSummary, error) { | |
| } | ||
|
|
||
| func createPolicies(policyPath string, inputs map[string]string) (*v1.Policies, error) { | ||
| // Check if the policy path already has a scheme (chainloop://, http://, https://, file://) | ||
| ref := policyPath | ||
| scheme, _ := policies.RefParts(policyPath) | ||
| if scheme == "" { | ||
| // Default to file:// | ||
| ref = fmt.Sprintf("file://%s", policyPath) | ||
| } | ||
|
Comment on lines
+90
to
+94
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
It works out of the box, file:// is optional, backwards compatibility is held by this fragment and that's the only reason we have to check for scheme.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. awesome, that makes sense. |
||
|
|
||
| return &v1.Policies{ | ||
| Materials: []*v1.PolicyAttachment{ | ||
| { | ||
| Policy: &v1.PolicyAttachment_Ref{Ref: fmt.Sprintf("file://%s", policyPath)}, | ||
| Policy: &v1.PolicyAttachment_Ref{Ref: ref}, | ||
| With: inputs, | ||
| }, | ||
| }, | ||
| Attestation: nil, | ||
| }, nil | ||
| } | ||
|
|
||
| func verifyMaterial(pol *v1.Policies, material *v12.Attestation_Material, materialPath string, debug bool, allowedHostnames []string, logger *zerolog.Logger) (*EvalSummary, error) { | ||
| func verifyMaterial(pol *v1.Policies, material *v12.Attestation_Material, materialPath string, debug bool, allowedHostnames []string, attestationClient controlplanev1.AttestationServiceClient, logger *zerolog.Logger) (*EvalSummary, error) { | ||
| var opts []policies.PolicyVerifierOption | ||
| if len(allowedHostnames) > 0 { | ||
| opts = append(opts, policies.WithAllowedHostnames(allowedHostnames...)) | ||
|
|
@@ -103,7 +113,7 @@ func verifyMaterial(pol *v1.Policies, material *v12.Attestation_Material, materi | |
| opts = append(opts, policies.WithIncludeRawData(debug)) | ||
| opts = append(opts, policies.WithEnablePrint(enablePrint)) | ||
|
|
||
| v := policies.NewPolicyVerifier(pol, nil, logger, opts...) | ||
| v := policies.NewPolicyVerifier(pol, attestationClient, logger, opts...) | ||
| policyEvs, err := v.VerifyMaterial(context.Background(), material, materialPath) | ||
| if err != nil { | ||
| return nil, err | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,8 @@ | |
| package action | ||
|
|
||
| import ( | ||
| pb "github.com/chainloop-dev/chainloop/app/controlplane/api/controlplane/v1" | ||
|
|
||
| "github.com/chainloop-dev/chainloop/app/cli/internal/policydevel" | ||
| ) | ||
|
|
||
|
|
@@ -42,14 +44,20 @@ func NewPolicyEval(opts *PolicyEvalOpts, actionOpts *ActionsOpts) (*PolicyEval, | |
| } | ||
|
|
||
| func (action *PolicyEval) Run() (*policydevel.EvalSummary, error) { | ||
| var attClient pb.AttestationServiceClient | ||
| if action.CPConnection != nil { | ||
| attClient = pb.NewAttestationServiceClient(action.CPConnection) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we might want to make sure this is not mandatory, I mean, if you do not provide a connection, the command should still work with file:// or http://
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's not mandatory, we use it if it's available otherwise it works the same way as before and it will return jwt related error to the user for |
||
| } | ||
|
|
||
| evalOpts := &policydevel.EvalOptions{ | ||
| PolicyPath: action.opts.PolicyPath, | ||
| MaterialKind: action.opts.Kind, | ||
| Annotations: action.opts.Annotations, | ||
| MaterialPath: action.opts.MaterialPath, | ||
| Inputs: action.opts.Inputs, | ||
| AllowedHostnames: action.opts.AllowedHostnames, | ||
| Debug: action.opts.Debug, | ||
| PolicyPath: action.opts.PolicyPath, | ||
| MaterialKind: action.opts.Kind, | ||
| Annotations: action.opts.Annotations, | ||
| MaterialPath: action.opts.MaterialPath, | ||
| Inputs: action.opts.Inputs, | ||
| AllowedHostnames: action.opts.AllowedHostnames, | ||
| Debug: action.opts.Debug, | ||
| AttestationClient: attClient, | ||
| } | ||
|
|
||
| // Evaluate policy | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
check if there is smth already on the way we parse the policies in the contracts