Repository navigation
fix(orchestrator): refuse jobs whose run comes from a fork #26
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,118 @@ | ||
| package function | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "os" | ||
| "strings" | ||
| "testing" | ||
| "time" | ||
| ) | ||
|
|
||
| // TestMain points runSource at the job's own repository, so tests that are | ||
| // not about where a run comes from need no GitHub API for it. | ||
| func TestMain(m *testing.M) { | ||
| runSource = func(_ context.Context, event WorkflowJobEvent) (string, error) { | ||
| return event.Repository.FullName, nil | ||
| } | ||
| os.Exit(m.Run()) | ||
| } | ||
|
|
||
| // withRunSourceLookup makes jobs look their run up through GitHub, starting | ||
| // from an empty cache. | ||
| func withRunSourceLookup(t *testing.T) { | ||
| t.Helper() | ||
| original := runSource | ||
| runSource = lookupRunSource | ||
| runSources.sources = map[string]runSourceEntry{} | ||
| t.Cleanup(func() { | ||
| runSource = original | ||
| runSources.sources = map[string]runSourceEntry{} | ||
| }) | ||
| } | ||
|
|
||
| // A pull request from a fork runs the fork's copy of the workflow, which can | ||
| // ask for any gcrunner label, so its job gets a failed check and a cancelled | ||
| // run instead of a VM, even with a config and zones that would provision it. | ||
| // So does a run whose fork has been deleted since. | ||
| func TestARunFromAnotherRepositoryGetsNoVM(t *testing.T) { | ||
| config := "runners:\n deploy:\n family: c3d\n cpu: 4\n" | ||
| gce := engine{zones: []string{"europe-west3-a"}, catalogue: map[string][]*MachineTypeInfo{"europe-west3-a": fourVCPUs}} | ||
| for name, source := range map[string]string{"fork": "mallory/cloud", "deleted fork": ""} { | ||
| withRunSourceLookup(t) | ||
| gh := &github{checkStatus: http.StatusCreated, headRepository: source} | ||
| if code := queuedJob(t, gh, config, gce); code != http.StatusOK { | ||
| t.Errorf("%s: status %d, want 200 so Cloud Tasks does not retry", name, code) | ||
| } | ||
| if len(gh.checks) != 1 || gh.cancels != 1 { | ||
| t.Fatalf("%s: %d check runs and %d cancels, want one of each", name, len(gh.checks), gh.cancels) | ||
| } | ||
| output, _ := gh.checks[0]["output"].(map[string]any) | ||
| if summary, _ := output["summary"].(string); !strings.Contains(summary, "only runs jobs from appwrite-labs/cloud itself") { | ||
| t.Errorf("%s: check run summary %q does not say why", name, summary) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestARunFromTheRepositoryItselfIsAllowed(t *testing.T) { | ||
| withRunSourceLookup(t) | ||
| useGitHub(t, &github{headRepository: "Appwrite-Labs/Cloud"}) | ||
| event := WorkflowJobEvent{WorkflowJob: WorkflowJob{ID: 42, RunID: 7}, Repository: cloudRepository} | ||
| if err := checkRunSource(context.Background(), event); err != nil { | ||
| t.Errorf("run from the repository itself refused: %v", err) | ||
| } | ||
| } | ||
|
|
||
| // Not knowing where a run comes from is no reason to fail it: the task is | ||
| // retried. | ||
| func TestAFailedRunLookupIsRetried(t *testing.T) { | ||
| withRunSourceLookup(t) | ||
| gh := &github{checkStatus: http.StatusCreated, runLookupStatus: http.StatusBadGateway} | ||
| if code := queuedJob(t, gh, "runners: {}\n", engine{}); code != http.StatusInternalServerError { | ||
| t.Errorf("status %d, want 500 so Cloud Tasks retries", code) | ||
| } | ||
| if gh.cancels != 0 || len(gh.checks) != 0 { | ||
| t.Errorf("failed the run (%d checks, %d cancels) without knowing where it comes from", len(gh.checks), gh.cancels) | ||
| } | ||
| } | ||
|
|
||
| // The jobs of one run share a lookup, until it expires. | ||
| func TestJobsOfOneRunLookItUpOnce(t *testing.T) { | ||
| withRunSourceLookup(t) | ||
| gh := &github{headRepository: "appwrite-labs/cloud"} | ||
| useGitHub(t, gh) | ||
| now := jobStart | ||
| runSources.nowFunc = func() time.Time { return now } | ||
| t.Cleanup(func() { runSources.nowFunc = time.Now }) | ||
| for id := int64(1); id <= 3; id++ { | ||
| event := WorkflowJobEvent{WorkflowJob: WorkflowJob{ID: id, RunID: 7}, Repository: cloudRepository} | ||
| if err := checkRunSource(context.Background(), event); err != nil { | ||
| t.Fatalf("job %d refused: %v", id, err) | ||
| } | ||
| } | ||
| if gh.runLookups != 1 { | ||
| t.Errorf("%d lookups for three jobs of one run, want 1", gh.runLookups) | ||
| } | ||
| now = now.Add(runSources.ttl) | ||
| if err := checkRunSource(context.Background(), WorkflowJobEvent{WorkflowJob: WorkflowJob{ID: 4, RunID: 7}, Repository: cloudRepository}); err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if gh.runLookups != 2 { | ||
| t.Errorf("%d lookups, want the expired entry looked up again", gh.runLookups) | ||
| } | ||
| } | ||
|
|
||
| // A failed lookup is not remembered: the retry asks GitHub again. | ||
| func TestAFailedRunLookupIsNotRemembered(t *testing.T) { | ||
| withRunSourceLookup(t) | ||
| gh := &github{headRepository: "appwrite-labs/cloud", runLookupStatus: http.StatusBadGateway} | ||
| useGitHub(t, gh) | ||
| event := WorkflowJobEvent{WorkflowJob: WorkflowJob{ID: 42, RunID: 7}, Repository: cloudRepository} | ||
| if err := checkRunSource(context.Background(), event); err == nil { | ||
| t.Fatal("lookup failure not reported") | ||
| } | ||
| gh.runLookupStatus = 0 | ||
| if err := checkRunSource(context.Background(), event); err != nil { | ||
| t.Errorf("retry after GitHub recovered: %v", err) | ||
| } | ||
| } | ||
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.
Exercise concurrent run-source lookups
These calls are sequential, so they only verify cache hits; a regression that stops coalescing simultaneous jobs would still pass. The comparable config cache has a concurrent-miss test, so a blocked fake lookup with overlapping callers would verify this new guarantee.
Prompt To Fix With AI
🟡 Minor · testing · Reply if this doesn't apply.