-
-
Notifications
You must be signed in to change notification settings - Fork 313
Fix running e2e backend #2710
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
Open
ahmedxgouda
wants to merge
22
commits into
OWASP:feature/e2e-backend
Choose a base branch
from
ahmedxgouda:feature/fix-running-backend
base: feature/e2e-backend
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.
Open
Fix running e2e backend #2710
Changes from 15 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
cafa25f
Add DB environment variables to e2e.yaml and add csrf_decorate function
ahmedxgouda fd0aa86
Skip sonar suggestion
ahmedxgouda 9047b0b
Fix rest api internal error
ahmedxgouda 1556873
Add timeout
ahmedxgouda f506341
Update docs
ahmedxgouda c35e43f
Update code
ahmedxgouda a80f661
Revert csrf update
ahmedxgouda d2d8a58
Add command to dump local data
ahmedxgouda a96185f
Update dump and load data
ahmedxgouda 74bdb6c
Update rest api config and docs
ahmedxgouda 9893384
Apply check-spelling
ahmedxgouda 3b4c9e2
Use .env.e2e.example for frontend e2e tests in gh actions
ahmedxgouda f747481
Apply rabbit's suggestions
ahmedxgouda ddae26b
Migrate dump_data to django command and dump owasp, github, and slack…
ahmedxgouda 95b1391
Apply rabbit's suggestions
ahmedxgouda 4d2e344
Update code
arkid15r fd2219d
Refactor dump_data
ahmedxgouda 3ea8fc7
Use local cache for e2e
ahmedxgouda 5e2c36c
Remove old load_data command
ahmedxgouda d3f9aef
Add tests
ahmedxgouda a1570cf
Skip sonar
ahmedxgouda ddc7e32
Apply rabbit suggestions
ahmedxgouda 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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
ahmedxgouda marked this conversation as resolved.
Show resolved
Hide resolved
|
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,158 @@ | ||
| """Dump masked data from the database into a compressed file.""" | ||
|
|
||
| import os | ||
| from pathlib import Path | ||
| from subprocess import CalledProcessError, run | ||
|
|
||
| from django.conf import settings | ||
| from django.core.management.base import BaseCommand, CommandError | ||
|
|
||
|
|
||
| class Command(BaseCommand): | ||
| help = "Create a dump of selected db tables." | ||
|
|
||
| def add_arguments(self, parser): | ||
| parser.add_argument( | ||
| "--output", | ||
| default=str(Path(settings.BASE_DIR) / "data" / "nest.sql.gz"), | ||
| help="Output dump path (default: data/nest.sql.gz)", | ||
| ) | ||
| parser.add_argument( | ||
| "-t", | ||
| "--table", | ||
| action="append", | ||
| dest="tables", | ||
| default=["public.owasp_*", "public.github_*", "public.slack_*"], | ||
ahmedxgouda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| help=( | ||
| "Table pattern to include. " | ||
| "Defaults: public.owasp_*, public.github_*, public.slack_*" | ||
| ), | ||
| ) | ||
|
|
||
| def handle(self, *args, **options): | ||
| db = settings.DATABASES["default"] | ||
| name = db.get("NAME", "") | ||
| user = db.get("USER", "") | ||
| password = db.get("PASSWORD", "") | ||
| host = db.get("HOST", "localhost") | ||
| port = str(db.get("PORT", "5432")) | ||
| output_path = Path(options["output"]).resolve() | ||
| tables = options["tables"] or [] | ||
| # Ensure output directory exists | ||
| output_path.parent.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| temp_db = f"temp_{name}" | ||
| env = os.environ.copy() | ||
| if password: | ||
| env["PGPASSWORD"] = password | ||
|
|
||
| self.stdout.write(self.style.NOTICE(f"Creating temporary database: {temp_db}")) | ||
| try: | ||
| # 1) Create temp DB from template | ||
| self._psql( | ||
| host, | ||
| port, | ||
| user, | ||
| "postgres", | ||
| f"CREATE DATABASE {temp_db} TEMPLATE {name};", | ||
| env, | ||
| ) | ||
|
|
||
| # 2) Hide emails | ||
| self.stdout.write(self.style.NOTICE("Hiding email fields in temp DB…")) | ||
| self._psql(host, port, user, temp_db, self._hide_emails(), env, via_stdin=True) | ||
|
|
||
| # 3) Dump selected tables | ||
| self.stdout.write(self.style.NOTICE(f"Creating dump at: {output_path}")) | ||
| dump_cmd = [ | ||
| "pg_dump", | ||
| "-h", | ||
| host, | ||
| "-p", | ||
| port, | ||
| "-U", | ||
| user, | ||
| "-d", | ||
| temp_db, | ||
| "--compress=9", | ||
| "--clean", | ||
| ] | ||
| dump_cmd += [f"--table={table}" for table in tables] | ||
| dump_cmd += ["-f", str(output_path)] | ||
|
|
||
| run(dump_cmd, check=True, env=env) | ||
| self.stdout.write(self.style.SUCCESS(f"Dump created: {output_path}")) | ||
| except CalledProcessError as e: | ||
| message = f"Command failed: {e.cmd}" | ||
| raise CommandError(message) from e | ||
| finally: | ||
| # 4) Drop temp DB | ||
| self.stdout.write(self.style.NOTICE(f"Dropping temporary database: {temp_db}")) | ||
| try: | ||
| self._psql( | ||
| host, | ||
| port, | ||
| user, | ||
| "postgres", | ||
| f"DROP DATABASE IF EXISTS {temp_db};", | ||
| env, | ||
| ) | ||
| except CalledProcessError: | ||
| # Best-effort cleanup | ||
| self.stderr.write( | ||
| self.style.WARNING(f"Failed to drop temp DB {temp_db} (ignored).") | ||
| ) | ||
|
|
||
| def _hide_emails(self) -> str: | ||
| # Uses a DO block to UPDATE every column named 'email' in non-system schemas | ||
| return """ | ||
| DO $$ | ||
ahmedxgouda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| DECLARE | ||
| record RECORD; | ||
| statement TEXT; | ||
| BEGIN | ||
| FOR record IN | ||
| SELECT quote_ident(n.nspname) AS schemaname, | ||
| quote_ident(c.relname) AS tablename, | ||
| quote_ident(a.attname) AS colname | ||
| FROM pg_attribute a | ||
| JOIN pg_class c ON c.oid = a.attrelid | ||
| JOIN pg_namespace n ON n.oid = c.relnamespace | ||
| WHERE a.attname = 'email' | ||
| AND a.attnum > 0 | ||
| AND NOT a.attisdropped | ||
| AND n.nspname NOT IN ('pg_catalog','information_schema','pg_toast') | ||
ahmedxgouda marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| LOOP | ||
| statement := format( | ||
| 'UPDATE %s.%s SET %s = %L;', record.schemaname, record.tablename, record.colname, '' | ||
| ); | ||
| EXECUTE statement; | ||
| END LOOP; | ||
| END$$; | ||
| """.strip() | ||
|
|
||
| def _psql( | ||
| self, | ||
| host: str, | ||
| port: str, | ||
| user: str, | ||
| dbname: str, | ||
| sql: str, | ||
| env: dict, | ||
| *, | ||
| via_stdin: bool = False, | ||
| ): | ||
| # Inputs are trusted; safe subprocess usage. | ||
| if via_stdin: | ||
| run( | ||
| ["psql", "-h", host, "-p", port, "-U", user, "-d", dbname], | ||
| input=sql.encode(), | ||
| check=True, | ||
| env=env, | ||
| ) | ||
| return | ||
| run( | ||
| ["psql", "-h", host, "-p", port, "-U", user, "-d", dbname, "-c", sql], | ||
| check=True, | ||
| env=env, | ||
| ) | ||
Binary file not shown.
Binary file not shown.
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
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
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.
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.
Why e2e needs this change?
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.
We didn't setup cache for e2e, so when it tries to access cache it gives 500 internal error. I think there is an option to setup redis cache in CI/CD. Maybe we can do that or keep it simple.
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.
Yes, let's add cache service for e2e instead. The closer to production architecture the better -- for both local and CI/CD cases.
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.
Ok I will add the cache in another PR after this one.
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.
Let's configure the cache backed for e2e via Django settings (locmem for now)