Skip to content
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

Add allowed package sources policy #1212

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

kosciCZ
Copy link

@kosciCZ kosciCZ commented Nov 5, 2024

This adds a new allowed_package_sources policy that can gate content fetched by cachi2. This rule take a lot from the allowed_external_references, but makes it specific to content fetched by cachi2, because it is not uncommon for dependencies fetched by other means to define externalReferences of type distribution. Cachi2 currently produces the mentioned external reference only for packages fetched with the generic fetcher (docs WIP) that always produces a pkg:generic purl, but it is possible it will be extended to other package managers supported by cachi2 as well.

The rule data for this policy are structured as a list of regex patterns for a given purl type. Examples in tests.

Finally, this is my first contribution to EC (and rego) so please excuse any antipatterns I might have invented, feedback very much welcomed.

Resolves ISV-5342

Signed-off-by: Jan Koscielniak <[email protected]>
@kosciCZ
Copy link
Author

kosciCZ commented Nov 5, 2024

@ralphbean could I get your eyes on this as general concept as well as the effective_on date?

@kosciCZ kosciCZ mentioned this pull request Nov 5, 2024
@@ -266,3 +266,5 @@ rule_data_attributes_key := "disallowed_attributes"
rule_data_allowed_external_references_key := "allowed_external_references"

rule_data_disallowed_external_references_key := "disallowed_external_references"

rule_data_allowed_package_sources_key := "allowed_package_sources"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the rule_data_errors rules above. Let's add a new one that verifies the consistency of this new rule data key. This helps us determine if the rule data provided has any problems.

# METADATA
# title: Allowed package sources
# description: >-
# Confirm the CycloneDX SBOM packages fetched by Cachi2 that define externalReferences
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit hard to read. Let's try to make it nicer since it's user-facing, consider:

For each of the components fetched by Cachi2 which define externalReferences of type distribution, verify they are allowed based on the allowed_package_sources rule data key. By default, allowed_package_sources is empty which means no components with such references are allowed.

(We should call them components, not packages, as per the CycloneDX spec.)

# of type `distribution`
properties.name == "cachi2:found_by"
properties.value == "cachi2"
reference.type == "distribution"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably go right after some reference in .... There's no need to iterate over properties if the reference type has an unexpected value.

some reference in component.externalReferences
some properties in component.properties

parsed_purl := ec.purl.parse(component.purl)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's do this after the filtering below to avoid having to parse PURLs unnecessarily.

patterns := _get_purl_allowed_patterns(parsed_purl.type, allowed_data)

distribution_url := object.get(reference, "url", "")
matches := _url_matches_any_pattern(distribution_url, patterns)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove the else from _url_matches_any_pattern and change this line to:

not _url_matches_any_pattern(distribution_url, patterns)

which means we can also remove matches == false

# only progress past this point if no matches were found
matches == false

id := component.purl
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We learned the other day that purl is actually an optional attribute. I think it's ok to do this here because you have already accessed earlier on in the rule, and presumably if there isn't a purl defined, this rule doesn't make sense for that component.

To make things safer(?) for the future, consider purl := component.purl earlier in rule and just use purl throughout.

# collections:
# - redhat
# - policy_data
# effective_on: 2024-11-30T00:00:00Z
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's set this to a month from now.


parsed_purl := ec.purl.parse(component.purl)

# only look at components fetched by cachi2 that define an externalReferences
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do wonder if this rule can be a bit more data driven. For example, what if we allowed the rule data to be something like this:

- type: maven
  properties:
    - name: cachi2:found_by
      value: cachi2
  reference:
    type: distribution
  allowed_references:
    - ".*apache.org.*"
    - ".*example.com.*"

Or some variation of that. WDYT?

@@ -148,6 +148,63 @@ deny contains result if {
result := lib.result_helper_with_term(rego.metadata.chain(), [id, reference.url, reference.type, msg], id)
}

# METADATA
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need corresponding changes in sbom_spdx.rego 😭

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants