-
Notifications
You must be signed in to change notification settings - Fork 144
feat(workloads): Add generic Docker workload execution #580
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
JVRLC
wants to merge
4
commits into
frappe:develop
Choose a base branch
from
JVRLC:feat/generic-workloads
base: develop
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
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d2c5b08
feat(workloads): Add generic Docker workload execution
JVRLC 0940b59
fix(workloads): Isolate concurrent deployment secrets
JVRLC cd07ade
fix(workloads): Encrypt queued deployment environments
JVRLC 6c77acf
fix(workloads): Remove cancelled deployment envelopes
JVRLC 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,123 @@ | ||
| import json | ||
| import os | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
|
|
||
| from agent.workload import ENVIRONMENT_FILE_MAX_AGE, InvalidWorkload, Workload, WorkloadConfig | ||
|
|
||
|
|
||
| class TestWorkloadConfig(unittest.TestCase): | ||
| def valid_config(self): | ||
| return { | ||
| "name": "xassida-search", | ||
| "image": "ghcr.io/jvrlc/xassida-search:abc123", | ||
| "container_port": 3000, | ||
| "host_port": 13000, | ||
| "health_path": "/api/health", | ||
| } | ||
|
|
||
| def test_valid_config_builds_loopback_only_docker_command(self): | ||
| config = WorkloadConfig(self.valid_config(), {"SUPABASE_URL": "safe"}) | ||
| config.validate() | ||
|
|
||
| command = config.run_command("xassida-search-candidate", Path("/secure/environment")) | ||
|
|
||
| self.assertIn("127.0.0.1:13000:3000", command) | ||
| self.assertNotIn("safe", command) | ||
|
|
||
|
Comment on lines
+10
to
+28
|
||
| def test_public_config_contains_secret_names_but_not_values(self): | ||
| config = WorkloadConfig(self.valid_config(), {"SUPABASE_SERVICE_ROLE_KEY": "secret"}) | ||
| config.validate() | ||
|
|
||
| public = config.public_config() | ||
|
|
||
| self.assertEqual(public["environment_keys"], ["SUPABASE_SERVICE_ROLE_KEY"]) | ||
| self.assertNotIn("secret", str(public)) | ||
|
|
||
| def test_queued_config_does_not_contain_environment_values(self): | ||
| config = WorkloadConfig(self.valid_config(), {"SUPABASE_SERVICE_ROLE_KEY": "secret"}) | ||
| config.validate() | ||
|
|
||
| queued = config.public_config() | ||
|
|
||
| self.assertNotIn("secret", str(queued)) | ||
|
|
||
| def test_rejects_shell_metacharacters_in_workload_name(self): | ||
| config = self.valid_config() | ||
| config["name"] = "xassida;rm" | ||
|
|
||
| with self.assertRaisesRegex(InvalidWorkload, "Workload name"): | ||
| WorkloadConfig(config, {}).validate() | ||
|
|
||
| def test_rejects_environment_values_with_newlines(self): | ||
| with self.assertRaisesRegex(InvalidWorkload, "SUPABASE_URL"): | ||
| WorkloadConfig(self.valid_config(), {"SUPABASE_URL": "safe\nINJECTED=yes"}).validate() | ||
|
|
||
| def test_rejects_public_privileged_port(self): | ||
| config = self.valid_config() | ||
| config["host_port"] = 443 | ||
|
|
||
| with self.assertRaisesRegex(InvalidWorkload, "host_port"): | ||
| WorkloadConfig(config, {}).validate() | ||
|
|
||
|
|
||
| class TestWorkloadEnvironmentFiles(unittest.TestCase): | ||
| def test_queued_environment_files_are_distinct_and_encrypted(self): | ||
| workload = object.__new__(Workload) | ||
| with tempfile.TemporaryDirectory() as temporary_directory: | ||
| directory = Path(temporary_directory) | ||
| workload.directory = temporary_directory | ||
|
|
||
| first = workload._write_environment(directory, {"TOKEN": "first"}) | ||
| second = workload._write_environment(directory, {"TOKEN": "second"}) | ||
|
|
||
| self.assertNotEqual(first, second) | ||
| self.assertNotIn(b"first", first.read_bytes()) | ||
| self.assertNotIn(b"second", second.read_bytes()) | ||
| self.assertEqual(first.stat().st_mode & 0o777, 0o600) | ||
| self.assertEqual(second.stat().st_mode & 0o777, 0o600) | ||
|
|
||
| def test_plaintext_exists_only_while_worker_uses_it(self): | ||
| workload = object.__new__(Workload) | ||
| with tempfile.TemporaryDirectory() as temporary_directory: | ||
| directory = Path(temporary_directory) | ||
| workload.directory = temporary_directory | ||
| queued = workload._write_environment(directory, {"TOKEN": "secret"}) | ||
|
|
||
| with workload._decrypted_environment(queued) as runtime: | ||
| self.assertEqual(runtime.read_text(), "TOKEN=secret\n") | ||
| self.assertEqual(runtime.stat().st_mode & 0o777, 0o600) | ||
|
|
||
| self.assertFalse(runtime.exists()) | ||
|
|
||
| def test_expired_encrypted_file_from_cancelled_job_is_removed(self): | ||
| workload = object.__new__(Workload) | ||
| with tempfile.TemporaryDirectory() as temporary_directory: | ||
| directory = Path(temporary_directory) | ||
| workload.directory = temporary_directory | ||
| expired = workload._write_environment(directory, {"TOKEN": "expired"}) | ||
| fresh = workload._write_environment(directory, {"TOKEN": "fresh"}) | ||
| expired_time = expired.stat().st_mtime - ENVIRONMENT_FILE_MAX_AGE - 1 | ||
| os.utime(expired, (expired_time, expired_time)) | ||
|
|
||
| workload._remove_expired_environments(directory) | ||
|
|
||
| self.assertFalse(expired.exists()) | ||
| self.assertTrue(fresh.exists()) | ||
|
|
||
| def test_cancelled_job_removes_its_encrypted_environment(self): | ||
| workload = object.__new__(Workload) | ||
| with tempfile.TemporaryDirectory() as temporary_directory: | ||
| directory = Path(temporary_directory) | ||
| workload.directory = temporary_directory | ||
| queued = workload._write_environment(directory, {"TOKEN": "secret"}) | ||
| job_data = json.dumps({"function": "deploy", "args": [{}, str(queued)], "kwargs": {}}) | ||
|
|
||
| workload.cleanup_cancelled_environment(job_data) | ||
|
|
||
| self.assertFalse(queued.exists()) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() | ||
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.
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.
If a worker starts the deployment after
cancel_or_stop()checksis_started,cancel()does not stop that running job but still deletes its queued environment file, causing the worker to fail withFileNotFoundErrorwhen decrypting it.Context Used: Guidelines for reviewing Frappe Framework applicat... (source)
Knowledge Base Used: Job Execution: RQ Workers, Steps, and Callbacks