Skip to content

Commit

Permalink
feat: JSON schema for license policy exports and renaming to ignore_p…
Browse files Browse the repository at this point in the history
…url_types
  • Loading branch information
StefanFl committed Jan 16, 2025
1 parent 9903304 commit 3dbf1dc
Show file tree
Hide file tree
Showing 19 changed files with 225 additions and 66 deletions.
8 changes: 4 additions & 4 deletions backend/application/licenses/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -395,10 +395,10 @@ class Meta:
model = License_Policy
exclude = ["users", "authorization_groups"]

def validate_ignore_component_types(self, value: str) -> str:
ignore_component_types = get_ignore_component_type_list(value)
for component_type in ignore_component_types:
for component_type in ignore_component_types:
def validate_ignore_purl_types(self, value: str) -> str:
ignore_purl_types = get_ignore_component_type_list(value)
for component_type in ignore_purl_types:
for component_type in ignore_purl_types:
if not PURL_Type.PURL_TYPE_CHOICES.get(component_type):
raise ValidationError(f"Invalid component type {component_type}")

Expand Down
2 changes: 1 addition & 1 deletion backend/application/licenses/fixtures/initial_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -7770,7 +7770,7 @@
"fields": {
"name": "Standard",
"description": "",
"ignore_component_types": "",
"ignore_purl_types": "",
"is_public": true
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.5 on 2025-01-16 19:55

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("licenses", "0015_rename_cpe_license_component_component_cpe_and_more"),
]

operations = [
migrations.RenameField(
model_name="license_policy",
old_name="ignore_component_types",
new_name="ignore_purl_types",
),
]
2 changes: 1 addition & 1 deletion backend/application/licenses/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ class License_Policy(Model):
name = CharField(max_length=255, unique=True)
description = TextField(max_length=2048, blank=True)
is_public = BooleanField(default=False)
ignore_component_types = CharField(max_length=255, blank=True)
ignore_purl_types = CharField(max_length=255, blank=True)
users: ManyToManyField = ManyToManyField(
User,
through="License_Policy_Member",
Expand Down
10 changes: 5 additions & 5 deletions backend/application/licenses/services/export_license_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ class License_Policy_Export_Item:


@dataclass
class License_Policy_Export_Ignore_Component_Type:
component_type: str
class License_Policy_Export_Ignore_PURL_Type:
purl_type: str


@dataclass
class License_Policy_Export:
name: str
description: str
items: list[License_Policy_Export_Item]
ignore_component_types: list[License_Policy_Export_Ignore_Component_Type]
ignore_purl_types: list[License_Policy_Export_Ignore_PURL_Type]
parent: Optional[str] = None


Expand All @@ -53,8 +53,8 @@ def _create_license_policy_export(
name=license_policy.name,
description=license_policy.description,
items=[],
ignore_component_types=get_ignore_component_type_list(
license_policy.ignore_component_types
ignore_purl_types=get_ignore_component_type_list(
license_policy.ignore_purl_types
),
)
if license_policy.parent:
Expand Down
8 changes: 4 additions & 4 deletions backend/application/licenses/services/license_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def process_license_components(
components_updated = 0

license_policy = vulnerability_check.product.license_policy
ignore_component_types = (
get_ignore_component_type_list(license_policy.ignore_component_types)
ignore_purl_types = (
get_ignore_component_type_list(license_policy.ignore_purl_types)
if license_policy
else []
)
Expand Down Expand Up @@ -94,7 +94,7 @@ def process_license_components(
apply_license_policy_to_component(
existing_component,
license_evaluation_results,
ignore_component_types,
ignore_purl_types,
)
existing_component.import_last_seen = timezone.now()
if (
Expand All @@ -118,7 +118,7 @@ def process_license_components(
apply_license_policy_to_component(
unsaved_component,
license_evaluation_results,
ignore_component_types,
ignore_purl_types,
)

unsaved_component.import_last_seen = timezone.now()
Expand Down
18 changes: 8 additions & 10 deletions backend/application/licenses/services/license_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def copy_license_policy(
name=name,
description=source_license_policy.description,
is_public=source_license_policy.is_public,
ignore_component_types=source_license_policy.ignore_component_types,
ignore_purl_types=source_license_policy.ignore_purl_types,
)

items = License_Policy_Item.objects.filter(license_policy=source_license_policy)
Expand Down Expand Up @@ -163,7 +163,7 @@ def apply_license_policy_product(product: Product) -> None:
apply_license_policy_to_component(
component,
license_evaluation_results,
get_ignore_component_type_list(license_policy.ignore_component_types),
get_ignore_component_type_list(license_policy.ignore_purl_types),
)
else:
component.evaluation_result = (
Expand All @@ -183,10 +183,10 @@ def apply_license_policy_product(product: Product) -> None:
def apply_license_policy_to_component(
component: License_Component,
evaluation_results: dict[str, LicensePolicyEvaluationResult],
ignore_component_types: list,
ignore_purl_types: list,
) -> None:
evaluation_result = None
if component.component_purl_type in ignore_component_types:
if component.component_purl_type in ignore_purl_types:
evaluation_result = License_Policy_Evaluation_Result.RESULT_IGNORED
elif component.license:
evaluation_result = _get_license_evaluation_result(
Expand All @@ -206,12 +206,10 @@ def apply_license_policy_to_component(
component.evaluation_result = evaluation_result


def get_ignore_component_type_list(ignore_component_types: str) -> list:
ignore_component_types_list = (
ignore_component_types.split(",") if ignore_component_types else []
)
ignore_component_types_list = [x.strip() for x in ignore_component_types_list]
return ignore_component_types_list
def get_ignore_component_type_list(ignore_purl_types: str) -> list:
ignore_purl_types_list = ignore_purl_types.split(",") if ignore_purl_types else []
ignore_purl_types_list = [x.strip() for x in ignore_purl_types_list]
return ignore_purl_types_list


def _get_license_policy(product: Product) -> Optional[License_Policy]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ class TestAuthorizationLicensePolicies(TestAuthorizationBase):
def test_authorization_license_policies(self):
License_Policy.objects.filter(pk__lt=1000).delete()

expected_data = "{'count': 5, 'next': None, 'previous': None, 'results': [{'id': 1000, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': False, 'name': 'public', 'description': '', 'is_public': True, 'ignore_component_types': '', 'parent': None}, {'id': 1001, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': True, 'has_authorization_groups': False, 'name': 'internal_read_not_manager', 'description': '', 'is_public': False, 'ignore_component_types': '', 'parent': None}, {'id': 1002, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': True, 'has_authorization_groups': False, 'name': 'internal_write_manager', 'description': '', 'is_public': False, 'ignore_component_types': '', 'parent': None}, {'id': 1003, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': True, 'name': 'authorization_group_not_manager', 'description': '', 'is_public': False, 'ignore_component_types': '', 'parent': None}, {'id': 1004, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': True, 'name': 'authorization_group_manager', 'description': '', 'is_public': False, 'ignore_component_types': '', 'parent': None}]}"
expected_data = "{'count': 5, 'next': None, 'previous': None, 'results': [{'id': 1000, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': False, 'name': 'public', 'description': '', 'is_public': True, 'ignore_purl_types': '', 'parent': None}, {'id': 1001, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': True, 'has_authorization_groups': False, 'name': 'internal_read_not_manager', 'description': '', 'is_public': False, 'ignore_purl_types': '', 'parent': None}, {'id': 1002, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': True, 'has_authorization_groups': False, 'name': 'internal_write_manager', 'description': '', 'is_public': False, 'ignore_purl_types': '', 'parent': None}, {'id': 1003, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': True, 'name': 'authorization_group_not_manager', 'description': '', 'is_public': False, 'ignore_purl_types': '', 'parent': None}, {'id': 1004, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': True, 'name': 'authorization_group_manager', 'description': '', 'is_public': False, 'ignore_purl_types': '', 'parent': None}]}"
self._test_api(
APITest(
"db_admin", "get", "/api/license_policies/", None, 200, expected_data
)
)

expected_data = "{'count': 3, 'next': None, 'previous': None, 'results': [{'id': 1000, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': False, 'name': 'public', 'description': '', 'is_public': True, 'ignore_component_types': '', 'parent': None}, {'id': 1002, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': True, 'has_authorization_groups': False, 'name': 'internal_write_manager', 'description': '', 'is_public': False, 'ignore_component_types': '', 'parent': None}, {'id': 1003, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': True, 'name': 'authorization_group_not_manager', 'description': '', 'is_public': False, 'ignore_component_types': '', 'parent': None}]}"
expected_data = "{'count': 3, 'next': None, 'previous': None, 'results': [{'id': 1000, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': False, 'name': 'public', 'description': '', 'is_public': True, 'ignore_purl_types': '', 'parent': None}, {'id': 1002, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': True, 'has_authorization_groups': False, 'name': 'internal_write_manager', 'description': '', 'is_public': False, 'ignore_purl_types': '', 'parent': None}, {'id': 1003, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': True, 'name': 'authorization_group_not_manager', 'description': '', 'is_public': False, 'ignore_purl_types': '', 'parent': None}]}"
self._test_api(
APITest(
"db_internal_write",
Expand All @@ -30,7 +30,7 @@ def test_authorization_license_policies(self):
)
)

expected_data = "{'count': 3, 'next': None, 'previous': None, 'results': [{'id': 1000, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': False, 'name': 'public', 'description': '', 'is_public': True, 'ignore_component_types': '', 'parent': None}, {'id': 1003, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': True, 'name': 'authorization_group_not_manager', 'description': '', 'is_public': False, 'ignore_component_types': '', 'parent': None}, {'id': 1004, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': True, 'name': 'authorization_group_manager', 'description': '', 'is_public': False, 'ignore_component_types': '', 'parent': None}]}"
expected_data = "{'count': 3, 'next': None, 'previous': None, 'results': [{'id': 1000, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': False, 'name': 'public', 'description': '', 'is_public': True, 'ignore_purl_types': '', 'parent': None}, {'id': 1003, 'parent_name': '', 'is_parent': False, 'is_manager': False, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': True, 'name': 'authorization_group_not_manager', 'description': '', 'is_public': False, 'ignore_purl_types': '', 'parent': None}, {'id': 1004, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': True, 'name': 'authorization_group_manager', 'description': '', 'is_public': False, 'ignore_purl_types': '', 'parent': None}]}"
self._test_api(
APITest(
"db_product_group_user",
Expand All @@ -43,7 +43,7 @@ def test_authorization_license_policies(self):
)
)

expected_data = "{'id': 1002, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': True, 'has_authorization_groups': False, 'name': 'internal_write_manager', 'description': '', 'is_public': False, 'ignore_component_types': '', 'parent': None}"
expected_data = "{'id': 1002, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': True, 'has_authorization_groups': False, 'name': 'internal_write_manager', 'description': '', 'is_public': False, 'ignore_purl_types': '', 'parent': None}"
self._test_api(
APITest(
"db_internal_write",
Expand Down Expand Up @@ -151,7 +151,7 @@ def test_authorization_license_policies(self):
)

post_data = {"name": "new_license_policy"}
expected_data = "{'id': 1005, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': False, 'has_users': True, 'has_authorization_groups': False, 'name': 'new_license_policy', 'description': '', 'is_public': False, 'ignore_component_types': '', 'parent': None}"
expected_data = "{'id': 1005, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': False, 'has_users': True, 'has_authorization_groups': False, 'name': 'new_license_policy', 'description': '', 'is_public': False, 'ignore_purl_types': '', 'parent': None}"
self._test_api(
APITest(
"db_internal_write",
Expand Down Expand Up @@ -180,7 +180,7 @@ def test_authorization_license_policies(self):
)
)

expected_data = "{'id': 1002, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': True, 'has_authorization_groups': False, 'name': 'internal_write_manager', 'description': 'changed', 'is_public': False, 'ignore_component_types': '', 'parent': None}"
expected_data = "{'id': 1002, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': True, 'has_authorization_groups': False, 'name': 'internal_write_manager', 'description': 'changed', 'is_public': False, 'ignore_purl_types': '', 'parent': None}"
self._test_api(
APITest(
"db_internal_write",
Expand Down Expand Up @@ -208,7 +208,7 @@ def test_authorization_license_policies(self):
)
)

expected_data = "{'id': 1004, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': True, 'name': 'authorization_group_manager', 'description': 'changed', 'is_public': False, 'ignore_component_types': '', 'parent': None}"
expected_data = "{'id': 1004, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': False, 'has_authorization_groups': True, 'name': 'authorization_group_manager', 'description': 'changed', 'is_public': False, 'ignore_purl_types': '', 'parent': None}"
self._test_api(
APITest(
"db_product_group_user",
Expand Down Expand Up @@ -249,7 +249,7 @@ def test_authorization_license_policies(self):
)

post_data = {"name": "copied_license_policy"}
expected_data = "{'id': 1006, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': True, 'has_authorization_groups': False, 'name': 'copied_license_policy', 'description': 'changed', 'is_public': False, 'ignore_component_types': '', 'parent': None}"
expected_data = "{'id': 1006, 'parent_name': '', 'is_parent': False, 'is_manager': True, 'has_products': False, 'has_product_groups': False, 'has_items': True, 'has_users': True, 'has_authorization_groups': False, 'name': 'copied_license_policy', 'description': 'changed', 'is_public': False, 'ignore_purl_types': '', 'parent': None}"
self._test_api(
APITest(
"db_internal_write",
Expand Down
Loading

0 comments on commit 3dbf1dc

Please sign in to comment.