-
-
Notifications
You must be signed in to change notification settings - Fork 237
Feature/project level compliance check #2415
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
Draft
eshentials
wants to merge
4
commits into
OWASP:main
Choose a base branch
from
eshentials:feature/project-level-compliance-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ae949c3
test(frontend): migrate to Jest v30, bump @types/jest, add VM modules…
6ec7f6d
make test and make check works, 242 tests passed.
9456292
feat: Add project level compliance checking system
d914ea6
fix: address code review comments for project level compliance
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
203 changes: 203 additions & 0 deletions
203
backend/apps/owasp/management/commands/owasp_check_project_level_compliance.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,203 @@ | ||
| """A command to check OWASP project level compliance against the official source of truth.""" | ||
|
|
||
| import logging | ||
| from urllib.request import urlopen | ||
|
|
||
| from django.core.management.base import BaseCommand | ||
|
|
||
| from apps.owasp.models.project import Project | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
| # Official OWASP project levels source | ||
| PROJECT_LEVELS_URL = "https://raw.githubusercontent.com/OWASP/www-projectchapter-example/main/assets/project_levels.json" | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Check OWASP project level compliance against official project_levels.json." | ||
|
|
||
| def add_arguments(self, parser) -> None: | ||
| """Add command-line arguments to the parser. | ||
|
|
||
| Args: | ||
| parser (argparse.ArgumentParser): The argument parser instance. | ||
|
|
||
| """ | ||
| parser.add_argument( | ||
| "--dry-run", | ||
| action="store_true", | ||
| default=False, | ||
| help="Perform a dry run without updating the database.", | ||
| ) | ||
|
|
||
| def fetch_official_project_levels(self) -> dict[str, str]: | ||
| """Fetch and parse the official project levels JSON. | ||
|
|
||
| Returns: | ||
| dict: Mapping of project keys to their official levels. | ||
|
|
||
| """ | ||
| try: | ||
| with urlopen(PROJECT_LEVELS_URL, timeout=30) as response: # noqa: S310 | ||
| import json | ||
|
|
||
| data = json.loads(response.read().decode("utf-8")) | ||
| self.stdout.write( | ||
| self.style.SUCCESS( | ||
| f"✓ Fetched {len(data)} project levels from official source" | ||
| ) | ||
| ) | ||
| return data | ||
| except Exception as e: | ||
| self.stdout.write(self.style.ERROR(f"✗ Failed to fetch project levels: {e}")) | ||
| logger.exception("Failed to fetch project levels from %s", PROJECT_LEVELS_URL) | ||
| return {} | ||
|
|
||
| def normalize_level(self, level: str) -> str: | ||
| """Normalize a level string to match ProjectLevel enum values. | ||
|
|
||
| Args: | ||
| level: The level string to normalize. | ||
|
|
||
| Returns: | ||
| str: Normalized level string. | ||
|
|
||
| """ | ||
| level_mapping = { | ||
| "incubator": "incubator", | ||
| "lab": "lab", | ||
| "production": "production", | ||
| "flagship": "flagship", | ||
| "labs": "lab", # Handle plural form | ||
| "incubators": "incubator", # Handle plural form | ||
| "flagships": "flagship", # Handle plural form | ||
| } | ||
| normalized = level.lower().strip() | ||
| return level_mapping.get(normalized, "other") | ||
|
|
||
| def handle(self, *args, **options) -> None: | ||
| """Handle the command execution.""" | ||
| dry_run = options["dry_run"] | ||
|
|
||
| self.stdout.write(self.style.WARNING("Starting project level compliance check...")) | ||
|
|
||
| if dry_run: | ||
| self.stdout.write(self.style.NOTICE("🔍 DRY RUN MODE - No changes will be saved")) | ||
|
|
||
| # Fetch official project levels | ||
| official_levels = self.fetch_official_project_levels() | ||
| if not official_levels: | ||
| self.stdout.write( | ||
| self.style.ERROR("✗ Could not fetch official project levels. Aborting.") | ||
| ) | ||
| return | ||
|
|
||
| # Get all active projects | ||
| projects = Project.active_projects.all() | ||
| total_projects = projects.count() | ||
|
|
||
| projects_to_update = [] | ||
| compliant_count = 0 | ||
| non_compliant_count = 0 | ||
| not_in_official_list_count = 0 | ||
|
|
||
| self.stdout.write( | ||
| self.style.NOTICE(f"\n📊 Checking {total_projects} active projects...\n") | ||
| ) | ||
|
|
||
| for project in projects: | ||
| # Extract project key (remove 'www-project-' prefix if present) | ||
| project_key = project.key.replace("www-project-", "").lower() | ||
|
|
||
| # Check if project exists in official list | ||
| if project_key not in official_levels: | ||
| # Project not in official list - mark as non-compliant | ||
| if project.is_level_compliant: | ||
| self.stdout.write( | ||
| self.style.WARNING( | ||
| f"⚠ {project.name} ({project.key}): Not found in official list" | ||
| ) | ||
| ) | ||
| project.is_level_compliant = False | ||
| projects_to_update.append(project) | ||
| non_compliant_count += 1 | ||
| else: | ||
| non_compliant_count += 1 | ||
| not_in_official_list_count += 1 | ||
| continue | ||
|
|
||
| # Get official level and normalize it | ||
| official_level = self.normalize_level(official_levels[project_key]) | ||
| local_level = project.level.lower() | ||
|
|
||
| # Compare levels | ||
| if official_level != local_level: | ||
| # Levels don't match - mark as non-compliant | ||
| if project.is_level_compliant: | ||
| self.stdout.write( | ||
| self.style.WARNING( | ||
| f"⚠ {project.name} ({project.key}): " | ||
| f"Level mismatch - Local: {local_level}, Official: {official_level}" | ||
| ) | ||
| ) | ||
| logger.warning( | ||
| "Level mismatch for project %s: local=%s, official=%s", | ||
| project.key, | ||
| local_level, | ||
| official_level, | ||
| ) | ||
| project.is_level_compliant = False | ||
| projects_to_update.append(project) | ||
| non_compliant_count += 1 | ||
| else: | ||
| non_compliant_count += 1 | ||
| else: | ||
| # Levels match - mark as compliant | ||
| if not project.is_level_compliant: | ||
| self.stdout.write( | ||
| self.style.SUCCESS( | ||
| f"✓ {project.name} ({project.key}): Now compliant ({local_level})" | ||
| ) | ||
| ) | ||
| project.is_level_compliant = True | ||
| projects_to_update.append(project) | ||
| compliant_count += 1 | ||
|
|
||
| # Update database | ||
| if projects_to_update and not dry_run: | ||
| Project.bulk_save(projects_to_update, fields=["is_level_compliant"]) | ||
| self.stdout.write( | ||
| self.style.SUCCESS(f"\n✓ Updated {len(projects_to_update)} projects in database") | ||
| ) | ||
| elif projects_to_update and dry_run: | ||
| self.stdout.write( | ||
| self.style.NOTICE( | ||
| f"\n🔍 Would update {len(projects_to_update)} projects (dry run)" | ||
| ) | ||
| ) | ||
|
|
||
| # Print summary | ||
| self.stdout.write("\n" + "=" * 70) | ||
| self.stdout.write(self.style.SUCCESS("\n📈 COMPLIANCE SUMMARY:\n")) | ||
| self.stdout.write(f" Total Projects: {total_projects}") | ||
| self.stdout.write(self.style.SUCCESS(f" ✓ Compliant: {compliant_count}")) | ||
| self.stdout.write( | ||
| self.style.WARNING(f" ⚠ Non-Compliant: {non_compliant_count}") | ||
| ) | ||
| self.stdout.write( | ||
| self.style.ERROR(f" - Not in official list: {not_in_official_list_count}") | ||
| ) | ||
| mismatch_count = non_compliant_count - not_in_official_list_count | ||
| self.stdout.write(self.style.ERROR(f" - Level mismatch: {mismatch_count}")) | ||
| self.stdout.write(f" Changes Applied: {len(projects_to_update)}") | ||
| self.stdout.write("=" * 70 + "\n") | ||
|
|
||
| if non_compliant_count > 0: | ||
| self.stdout.write( | ||
| self.style.WARNING( | ||
| f"\n⚠ {non_compliant_count} non-compliant projects detected. " | ||
| "These projects will receive a score penalty." | ||
| ) | ||
| ) | ||
|
|
||
| self.stdout.write(self.style.SUCCESS("\n✓ Project level compliance check completed.")) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
backend/apps/owasp/migrations/0053_project_is_level_compliant.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| # Generated migration | ||
|
|
||
| from django.db import migrations, models | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
| dependencies = [ | ||
| ("owasp", "0052_remove_entitymember_owasp_entit_member__6e516f_idx_and_more"), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.AddField( | ||
| model_name="project", | ||
| name="is_level_compliant", | ||
| field=models.BooleanField( | ||
| default=True, | ||
| verbose_name="Is level compliant", | ||
| help_text="Indicates if the project level matches the official OWASP project_levels.json", | ||
| ), | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.