From 4cd817a1862710e5af663b308dc004a474095bf4 Mon Sep 17 00:00:00 2001 From: qiansc Date: Sat, 14 Nov 2026 07:49:07 +0000 Subject: [PATCH] feat: check repository access before registration Registering a new repository first lists the remote HEAD with the tokens its fetches will use (personal, site-wide, then anonymous where allowed). Unreadable repositories are not stored; credential failures carry code_provider, settings_url and guidance pointing at the token settings. POST /api/admin/v1/repos/access-check runs the same check alone. The website locks the form while checking, keeps its input on failure and offers to open the token settings in a new window with a retry button. Co-Authored-By: Claude Opus 5.5 --- docs/interfaces.md | 4 + internal/gitstore/refs.go | 23 +++++ internal/gitstore/refs_test.go | 15 ++++ internal/httpserver/repository_access.go | 46 +++++++++- internal/httpserver/repository_access_test.go | 47 +++++++++- internal/httpserver/runtime.go | 20 ++++- internal/service/access_check.go | 90 +++++++++++++++++++ internal/service/access_check_test.go | 64 +++++++++++++ internal/service/code_credentials.go | 3 + internal/service/code_sources_test.go | 4 +- internal/service/git_timeout_test.go | 6 +- internal/service/managers_test.go | 4 +- internal/service/service.go | 20 +++-- internal/sso/handler.go | 5 ++ web/assets/repos-page.js | 28 +++++- web/repos.html | 8 ++ web/settings.html | 2 +- 17 files changed, 367 insertions(+), 22 deletions(-) create mode 100644 internal/service/access_check.go create mode 100644 internal/service/access_check_test.go diff --git a/docs/interfaces.md b/docs/interfaces.md index 48ca179..d315423 100644 --- a/docs/interfaces.md +++ b/docs/interfaces.md @@ -102,6 +102,10 @@ Repository details (`/sourcegraph/v1/repo/availability`, `GET /sourcegraph/api/a Fetching a user-registered repository uses the same credentials as any other: the creator's own code-source token first, then the site-wide token. Turning the setting on therefore lets signed-in users register any repository the site-wide token can read. Fetch failures name the cause (`GIT_REPOSITORY_UNAVAILABLE` for a missing repository or one the token cannot see, `GIT_ACCESS_DENIED`, `GIT_AUTH_FAILED`, `GIT_CREDENTIAL_UNAVAILABLE`, `GIT_FETCH_FAILED`), and the website links to the creator's code-source token settings. +Registering a new repository first checks that it can be fetched: the service lists the remote HEAD with the credentials the registration will use (the caller's personal token, then the site-wide token, then anonymous where the source allows it), outside the registration lock, sharing the remote-lookup concurrency slots and waiting at most 20 seconds. A failed check stores nothing and returns the fetch codes above, or retryable 504 `ACCESS_CHECK_TIMEOUT`. For the four credential failures the error body also carries `code_provider`, `settings_url` and an English `guidance` sentence: a signed-in caller is pointed at User settings → Code source tokens, the service credential (which has no personal token) at the site-wide token in system settings. A name that already exists is not checked and keeps its replay or 409 `REPOSITORY_EXISTS` answer. `POST /api/admin/v1/repos/access-check` with `{name, code_provider}` runs the same check without registering, with the same permission and error fields, and returns `{name, accessible: true}` when the repository is readable. + +The registration form stays locked while the check runs and keeps its input when it fails. Credential failures offer to open the code-source token settings in a new window and a Check again button that resubmits with the same idempotency key; an unreachable host or a timeout offers Check again only. + ### Code sources Code sources, their settings and token order are described in [code sources and tokens](code-sources.md). diff --git a/internal/gitstore/refs.go b/internal/gitstore/refs.go index 26de671..65603e9 100644 --- a/internal/gitstore/refs.go +++ b/internal/gitstore/refs.go @@ -372,3 +372,26 @@ func (s *Store) LocalRefs(ctx context.Context, dir string) (map[string]string, e } return refs, nil } + +// CheckRemoteAccess asks the remote for HEAD only; it succeeds when the +// credential can read the repository, including an empty one. +func (s *Store) CheckRemoteAccess(ctx context.Context, dir, repo string, o FetchOptions) error { + if err := ValidateRepo(repo); err != nil { + return err + } + remote := remoteURL(repo, o.Host) + ctx, cancel := context.WithTimeout(ctx, refTimeout(o)) + defer cancel() + command := s.command(ctx, dir, append(fetchConfiguration(o), "ls-remote", remote, "HEAD")...) + command.Env = append(command.Env, credentialEnv(remote, o)...) + command.Stdout = io.Discard + diagnostic := &prefixTrace{limit: 64 << 10} + command.Stderr = diagnostic + if err := command.Run(); err != nil { + if ctx.Err() != nil { + return ctx.Err() + } + return remoteFailure(diagnostic.String(), "GIT_FETCH_FAILED", "Cannot reach the repository on the code host.") + } + return nil +} diff --git a/internal/gitstore/refs_test.go b/internal/gitstore/refs_test.go index 1dd6d44..7d9f625 100644 --- a/internal/gitstore/refs_test.go +++ b/internal/gitstore/refs_test.go @@ -212,3 +212,18 @@ func TestInvalidateRetentionFailsClosed(t *testing.T) { t.Fatal("inaccessible repository treated as having no marker") } } + +func TestCheckRemoteAccess(t *testing.T) { + s, o, _ := refFixture(t) + ctx := context.Background() + dir := t.TempDir() + if err := s.InitScratch(ctx, dir); err != nil { + t.Fatal(err) + } + if err := s.CheckRemoteAccess(ctx, dir, "org/repo", o); err != nil { + t.Fatalf("readable: %v", err) + } + if err := s.CheckRemoteAccess(ctx, dir, "org/missing", o); err == nil { + t.Fatal("missing repository passed the access check") + } +} diff --git a/internal/httpserver/repository_access.go b/internal/httpserver/repository_access.go index 77044c0..5173fcc 100644 --- a/internal/httpserver/repository_access.go +++ b/internal/httpserver/repository_access.go @@ -2,8 +2,11 @@ package httpserver import ( "context" + "errors" + "fmt" "net/http" + "github.com/context4ai/sourcegraph/internal/codesources" "github.com/context4ai/sourcegraph/internal/contract" "github.com/context4ai/sourcegraph/internal/control" "github.com/context4ai/sourcegraph/internal/identity" @@ -26,8 +29,9 @@ func repositoryManagementRoute(path string) bool { return false } +// registrationRoute covers registering and its preceding access check. func registrationRoute(r *http.Request) bool { - return r.Method == "POST" && r.URL.Path == Prefix+"/api/admin/v1/repos" + return r.Method == "POST" && (r.URL.Path == Prefix+"/api/admin/v1/repos" || r.URL.Path == Prefix+"/api/admin/v1/repos/access-check") } // delegatedRoute is opened to signed-in users at the gate; the handler then @@ -69,6 +73,46 @@ type registrationConflict struct { Creator *sso.Person `json:"creator"` } +// registrationAccessFailure tells the caller where to bind a code-source token +// when the repository cannot be fetched with the tokens a registration would use. +type registrationAccessFailure struct { + contract.ErrorResponse + CodeProvider string `json:"code_provider"` + SettingsURL string `json:"settings_url"` + Guidance string `json:"guidance"` +} + +// accessFailure returns guidance for credential failures of the access check, or nil. +func accessFailure(r *http.Request, browserAuth *sso.Handler, err error, name, provider string) *registrationAccessFailure { + var ce *contract.Error + if !errors.As(err, &ce) { + return nil + } + switch ce.Code { + case "GIT_CREDENTIAL_UNAVAILABLE", "GIT_AUTH_FAILED", "GIT_ACCESS_DENIED", "GIT_REPOSITORY_UNAVAILABLE": + default: + return nil + } + if provider == "" { + provider = service.DefaultCodeProvider + } + source, _ := codesources.Lookup(provider) + site := "" + if browserAuth != nil { + site = browserAuth.SiteURL(r) + } + out := ®istrationAccessFailure{CodeProvider: provider} + // Without a personal identity (the service credential) only the global token applies. + if identity.UserID(r.Context()) == "" { + out.SettingsURL = site + Prefix + "/settings#code-source-tokens" + out.Guidance = fmt.Sprintf("%s cannot be fetched with the global %s token. A site administrator should set a global access token that can read it at %s, then register again. Nothing was registered.", name, source.Name, out.SettingsURL) + } else { + out.SettingsURL = site + Prefix + "/user-settings#code-source-tokens" + out.Guidance = fmt.Sprintf("%s cannot be fetched with your %s token or the global one. Bind a personal access token that can read it at %s, then register again. Nothing was registered.", name, source.Name, out.SettingsURL) + } + return out +} + // repositoryView adds who created and manages a repository, and whether the // caller may manage it. People are shown to signed-in callers only. type repositoryView struct { diff --git a/internal/httpserver/repository_access_test.go b/internal/httpserver/repository_access_test.go index 2775365..5642e91 100644 --- a/internal/httpserver/repository_access_test.go +++ b/internal/httpserver/repository_access_test.go @@ -136,6 +136,8 @@ type site struct { settings *sitesettings.Service db *repositories people *people + // access is what the registration access check answers; nil means reachable. + access error } // fakeIssuer serves just enough OIDC discovery for sso.New; no login runs. @@ -177,7 +179,9 @@ func newSite(t *testing.T, users map[string]string, admins ...string) *site { t.Fatal(err) } t.Cleanup(s.Close) - return &site{t: t, handler: RuntimeWithSettings(config.Defaults(), slog.New(slog.NewTextHandler(io.Discard, nil)), s, strings.Repeat("t", 32), settings, auth), settings: settings, db: db, people: store} + st := &site{t: t, handler: RuntimeWithSettings(config.Defaults(), slog.New(slog.NewTextHandler(io.Discard, nil)), s, strings.Repeat("t", 32), settings, auth), settings: settings, db: db, people: store} + s.AccessCheck = func(context.Context, string, string) error { return st.access } + return st } // call sends a browser request as user (empty for anonymous) and decodes the JSON body. @@ -230,6 +234,13 @@ func TestOrdinaryUsersRegisterAndManageTheirRepositories(t *testing.T) { if _, policy := s.call("", "GET", "/sourcegraph/access-policy", ""); policy["user_repositories"] != false { t.Fatalf("access policy while off: %v", policy) } + // The pre-registration access check follows the same permission. + if code, body := s.call(alice, "POST", "/sourcegraph/api/admin/v1/repos/access-check", `{"name":"context4ai/agent-graph","code_provider":"github"}`); code != 403 || errorCode(body) != "REGISTRATION_DISABLED" { + t.Fatalf("access check while off: %d %v", code, body) + } + if code, body := s.call(admin, "POST", "/sourcegraph/api/admin/v1/repos/access-check", `{"name":"not a repository","code_provider":"github"}`); code != 400 { + t.Fatalf("access check validates the name: %d %v", code, body) + } s.allowUsers(true) if _, policy := s.call("", "GET", "/sourcegraph/access-policy", ""); policy["user_repositories"] != true { @@ -361,4 +372,38 @@ func TestServiceCredentialRegistersWhileUserRegistrationIsOff(t *testing.T) { } } +func TestRegistrationRejectsUnfetchableRepositoriesWithSettingsLink(t *testing.T) { + admin, alice := hexID("admin"), hexID("alice") + s := newSite(t, map[string]string{"admin": admin, "alice": alice}, admin) + s.allowUsers(true) + s.access = contract.Fail("GIT_REPOSITORY_UNAVAILABLE", "Repository not found, or the code-service credential used cannot see it.", 404) + for path, request := range map[string]string{"/sourcegraph/api/admin/v1/repos": register, "/sourcegraph/api/admin/v1/repos/access-check": `{"name":"context4ai/agent-graph","code_provider":"github"}`} { + code, body := s.call(alice, "POST", path, request, "Idempotency-Key", "register-denied") + url, _ := body["settings_url"].(string) + guidance, _ := body["guidance"].(string) + if code != 404 || errorCode(body) != "GIT_REPOSITORY_UNAVAILABLE" || !strings.HasSuffix(url, "/sourcegraph/user-settings#code-source-tokens") || !strings.Contains(guidance, url) || body["code_provider"] != "github" { + t.Fatalf("%s: %d %v", path, code, body) + } + } + if _, err := s.db.Get(context.Background(), "context4ai/agent-graph"); err == nil { + t.Fatal("unfetchable repository was registered") + } + // The service credential has no personal token; it is pointed at the global one. + r := httptest.NewRequest("POST", "/sourcegraph/api/admin/v1/repos", strings.NewReader(register)) + r.Header.Set("Authorization", "Bearer "+strings.Repeat("t", 32)) + r.Header.Set("Idempotency-Key", "register-svc") + w := httptest.NewRecorder() + s.handler.ServeHTTP(w, r) + var body map[string]any + _ = json.Unmarshal(w.Body.Bytes(), &body) + if url, _ := body["settings_url"].(string); w.Code != 404 || !strings.HasSuffix(url, "/sourcegraph/settings#code-source-tokens") { + t.Fatalf("service credential: %d %s", w.Code, w.Body.String()) + } + // Other failures carry no settings link. + s.access = contract.Fail("GIT_FETCH_FAILED", "Cannot reach the repository on the code host.", 503) + if code, body := s.call(alice, "POST", "/sourcegraph/api/admin/v1/repos", register, "Idempotency-Key", "register-down"); code != 503 || body["settings_url"] != nil { + t.Fatalf("unreachable host: %d %v", code, body) + } +} + func itoa(n int64) string { b, _ := json.Marshal(n); return string(b) } diff --git a/internal/httpserver/runtime.go b/internal/httpserver/runtime.go index b3ad4b6..1057288 100644 --- a/internal/httpserver/runtime.go +++ b/internal/httpserver/runtime.go @@ -272,6 +272,7 @@ func runtime(c config.Config, log *slog.Logger, s *service.Service, token string var result any var err error var existsCreator *sso.Person + var accessHelp *registrationAccessFailure decode := func(v any) bool { b, e := io.ReadAll(http.MaxBytesReader(w, r.Body, c.MaxBodyBytes)) if e == nil { @@ -336,6 +337,7 @@ func runtime(c config.Config, log *slog.Logger, s *service.Service, token string } var row control.Repository row, err = s.RegisterWithGitTimeout(r.Context(), q.Name, policy, interval, q.IndexMode, q.PathGroups, q.PublicRead, q.CodeProvider, q.GitTimeoutMinutes, r.Header.Get("Idempotency-Key")) + accessHelp = accessFailure(r, browserAuth, err, q.Name, q.CodeProvider) result = row // A taken name names its registrant, so the caller can ask to become a manager. var ce *contract.Error @@ -349,6 +351,19 @@ func runtime(c config.Config, log *slog.Logger, s *service.Service, token string } else { err = contract.Fail("METHOD_NOT_ALLOWED", "Use GET.", 405) } + case Prefix + "/api/admin/v1/repos/access-check": + if method("POST") { + var q struct { + Name string `json:"name"` + CodeProvider string `json:"code_provider"` + } + if decode(&q) { + if err = s.CheckAccess(r.Context(), q.Name, q.CodeProvider); err == nil { + result = map[string]any{"name": q.Name, "accessible": true} + } + accessHelp = accessFailure(r, browserAuth, err, q.Name, q.CodeProvider) + } + } case Prefix + "/v1/repo/plugins": if r.Method == "GET" { result, err = s.DiscoverPlugins(r.Context(), repo, r.URL.Query().Get("revision")) @@ -517,7 +532,10 @@ func runtime(c config.Config, log *slog.Logger, s *service.Service, token string if ce.RetryAfterSeconds > 0 { w.Header().Set("Retry-After", strconv.Itoa(ce.RetryAfterSeconds)) } - if existsCreator != nil { + if accessHelp != nil { + accessHelp.ErrorResponse = contract.ErrorResponse{Error: ce.Message, Meta: contract.Meta{Protocol: "zoekt-single-repo-v1", RequestID: id, Status: "error", Code: ce.Code}} + writeJSON(w, status, accessHelp) + } else if existsCreator != nil { writeJSON(w, status, registrationConflict{ErrorResponse: contract.ErrorResponse{Error: ce.Message, Meta: contract.Meta{Protocol: "zoekt-single-repo-v1", RequestID: id, Status: "error", Code: ce.Code}}, Creator: existsCreator}) } else { writeError(w, id, status, ce.Code, ce.Message) diff --git a/internal/service/access_check.go b/internal/service/access_check.go new file mode 100644 index 0000000..f58a713 --- /dev/null +++ b/internal/service/access_check.go @@ -0,0 +1,90 @@ +package service + +import ( + "context" + "errors" + "time" + + "github.com/context4ai/sourcegraph/internal/codesources" + "github.com/context4ai/sourcegraph/internal/contract" + "github.com/context4ai/sourcegraph/internal/control" + "github.com/context4ai/sourcegraph/internal/gitstore" + "github.com/context4ai/sourcegraph/internal/identity" +) + +// accessCheckLimit bounds one registration access check within the request deadline. +const accessCheckLimit = 20 * time.Second + +var errAccessCheckTimeout = contract.FailRetryAfter("ACCESS_CHECK_TIMEOUT", "The code host did not answer the access check in time; retry shortly.", 504, 10) + +// CheckAccess lists the remote HEAD with the credentials a registration by the +// caller would use (personal, then global, then anonymous), so a repository +// that cannot be fetched is found before it is registered. Nothing is stored. +func (s *Service) CheckAccess(ctx context.Context, name, provider string) error { + if provider == "" { + provider = defaultCodeProvider + } + source, known := codesources.Lookup(provider) + if !known { + return contract.Fail("INVALID_CODE_PROVIDER", "Choose one of the configured code sources.", 400) + } + if source.Host == "" { + return contract.Fail("CODE_SOURCE_NOT_CONFIGURED", "Configure this code source's host before registering repositories.", 409) + } + if err := gitstore.ValidateRepo(name); err != nil { + return err + } + if s.AccessCheck != nil { + return s.AccessCheck(ctx, name, provider) + } + wait := min(requestWait(ctx), accessCheckLimit) + if wait == 0 { + return errAccessCheckTimeout + } + ctx, cancel := context.WithTimeout(ctx, wait) + defer cancel() + m := &s.refState + m.mu.Lock() + m.init() + m.mu.Unlock() + // Shares the remote-lookup slots; no per-name state is kept for unregistered names. + select { + case m.lookups8 <- struct{}{}: + defer func() { <-m.lookups8 }() + case <-ctx.Done(): + return errAccessCheckTimeout + } + dir, cleanup, err := s.refScratch(ctx) + if err != nil { + return err + } + defer cleanup() + r := control.Repository{Name: name, CodeProvider: provider, CodeHost: source.Host, CreatorID: identity.UserID(ctx)} + err = s.withGitCredential(ctx, r, func(token string) error { + o := s.fetchOptions(r, token) + o.CheckGrowth = nil + return s.Git.CheckRemoteAccess(ctx, dir, name, o) + }) + if errors.Is(err, context.DeadlineExceeded) || (err != nil && ctx.Err() != nil) { + return errAccessCheckTimeout + } + return err +} + +// CheckRegistrationAccess runs CheckAccess before a new registration, outside +// the registration lock. An existing name is left to Register, which replays +// or reports the conflict without contacting the code host. +func (s *Service) CheckRegistrationAccess(ctx context.Context, name, provider string) error { + if err := gitstore.ValidateRepo(name); err != nil { + return err + } + _, err := s.DB.Get(ctx, name) + if err == nil { + return nil + } + var ce *contract.Error + if !errors.As(err, &ce) || ce.Code != "REPOSITORY_NOT_FOUND" { + return err + } + return s.CheckAccess(ctx, name, provider) +} diff --git a/internal/service/access_check_test.go b/internal/service/access_check_test.go new file mode 100644 index 0000000..514744e --- /dev/null +++ b/internal/service/access_check_test.go @@ -0,0 +1,64 @@ +package service + +import ( + "context" + "os" + "testing" + + "github.com/context4ai/sourcegraph/internal/codesources" + "github.com/context4ai/sourcegraph/internal/contract" + "github.com/context4ai/sourcegraph/internal/control" +) + +func TestCheckAccessUsesRegistrationSourceAndLeavesNoState(t *testing.T) { + s, _, _ := refService(t) + if err := codesources.Configure(map[string]codesources.Override{defaultCodeProvider: {Host: "https://fixture.example"}}); err != nil { + t.Fatal(err) + } + t.Cleanup(func() { _ = codesources.Configure(nil) }) + ctx := context.Background() + if err := s.CheckAccess(ctx, "org/repo", ""); err != nil { + t.Fatalf("readable repository: %v", err) + } + if err := s.CheckAccess(ctx, "org/missing", ""); err == nil { + t.Fatal("missing repository passed") + } + if got := code(s.CheckAccess(ctx, "org/repo", "unknown")); got != "INVALID_CODE_PROVIDER" { + t.Fatalf("unknown provider: %s", got) + } + if got := code(s.CheckAccess(ctx, "not a repository", "")); got == "" { + t.Fatal("invalid name passed") + } + entries, err := os.ReadDir(refsRoot(s.Root)) + if err != nil || len(entries) != 0 { + t.Fatalf("scratch directories left: %v %v", entries, err) + } + if len(s.refState.repoSlots) != 0 { + t.Fatalf("per-name state kept: %v", s.refState.repoSlots) + } +} + +// reachable stands in for the remote probe in registration tests. +func reachable(context.Context, string, string) error { return nil } + +func TestRegisterChecksAccessOnlyForNewNames(t *testing.T) { + db := &sourceStore{repos: map[string]control.Repository{}} + calls := 0 + var answer error + s := &Service{DB: db, Root: t.TempDir(), AccessCheck: func(context.Context, string, string) error { calls++; return answer }} + policy := control.Policy{Branches: []string{"main"}, Count: 10} + answer = contract.Fail("GIT_ACCESS_DENIED", "denied", 403) + if _, err := s.RegisterWithProvider(context.Background(), "context4ai/agent-graph", policy, 60, "full", nil, nil, "github", "register-k1"); code(err) != "GIT_ACCESS_DENIED" || len(db.repos) != 0 { + t.Fatalf("unfetchable registration: %v %v", err, db.repos) + } + answer = nil + if _, err := s.RegisterWithProvider(context.Background(), "context4ai/agent-graph", policy, 60, "full", nil, nil, "github", "register-k1"); err != nil || len(db.repos) != 1 { + t.Fatalf("retry after binding a token: %v", err) + } + // A replay or a taken name is answered without contacting the code host. + before := calls + answer = contract.Fail("GIT_ACCESS_DENIED", "denied", 403) + if _, err := s.RegisterWithProvider(context.Background(), "context4ai/agent-graph", policy, 60, "full", nil, nil, "github", "register-k1"); err != nil || calls != before { + t.Fatalf("replay: %v calls=%d", err, calls-before) + } +} diff --git a/internal/service/code_credentials.go b/internal/service/code_credentials.go index c89c6ed..4af92cb 100644 --- a/internal/service/code_credentials.go +++ b/internal/service/code_credentials.go @@ -43,6 +43,9 @@ func (s *Service) withGitCredential(ctx context.Context, r control.Repository, o // defaultCodeProvider is the provider of repositories registered before providers were recorded. const defaultCodeProvider = "github" +// DefaultCodeProvider is the code source of registrations that omit code_provider. +const DefaultCodeProvider = defaultCodeProvider + // fetchOptions uses the host pinned at registration; the source still supplies username and anonymous access. func (s *Service) fetchOptions(r control.Repository, token string) gitstore.FetchOptions { provider := r.CodeProvider diff --git a/internal/service/code_sources_test.go b/internal/service/code_sources_test.go index 1e6ab6b..a4482ab 100644 --- a/internal/service/code_sources_test.go +++ b/internal/service/code_sources_test.go @@ -45,7 +45,7 @@ func (db *sourceStore) List(_ context.Context, after string, limit int) ([]contr func TestRegistrationPinsTheSourceHost(t *testing.T) { db := &sourceStore{repos: map[string]control.Repository{}} - s := &Service{DB: db, Root: t.TempDir()} + s := &Service{DB: db, Root: t.TempDir(), AccessCheck: reachable} policy := control.Policy{Branches: []string{"main"}, Count: 10} r, err := s.RegisterWithProvider(context.Background(), "context4ai/agent-graph", policy, 60, "full", nil, nil, "github", "register-github") if err != nil || r.CodeHost != "https://github.com" || db.repos["context4ai/agent-graph"].CodeHost != "https://github.com" { @@ -65,7 +65,7 @@ func TestPinCodeHostsKeepsExistingRepositoriesOnTheirHost(t *testing.T) { "group/old": {ID: "1", Name: "group/old", Revision: 1}, "group/moved": {ID: "2", Name: "group/moved", CodeProvider: "git", CodeHost: "https://old.example.com", Revision: 1}, }} - s := &Service{DB: db, Root: t.TempDir()} + s := &Service{DB: db, Root: t.TempDir(), AccessCheck: reachable} s.PinCodeHosts(context.Background(), nil) if db.repos["group/old"].CodeHost != "https://github.com" { t.Fatalf("unpinned repository = %#v", db.repos["group/old"]) diff --git a/internal/service/git_timeout_test.go b/internal/service/git_timeout_test.go index 0be041c..fc24cbf 100644 --- a/internal/service/git_timeout_test.go +++ b/internal/service/git_timeout_test.go @@ -41,7 +41,7 @@ func TestGitTimeoutInheritanceAndTaskSnapshot(t *testing.T) { func TestGitTimeoutUpdatePreservesWorkAndVersions(t *testing.T) { policy := control.Policy{Branches: []string{"main"}, Count: 10} db := &managementStore{repo: control.Repository{Name: "org/repo", Enabled: true, Revision: 4, PolicyRevision: 2, Policy: policy, Versions: []control.Version{{Commit: "1111111111111111111111111111111111111111", Generation: 3}}, Jobs: []control.Job{{State: "running", Kind: "sync"}}}} - s := &Service{DB: db, Root: t.TempDir()} + s := &Service{DB: db, Root: t.TempDir(), AccessCheck: reachable} for _, n := range []int{30, 60, 0} { u := Update{Policy: policy, Enabled: true, GitTimeoutMinutes: &n} u.Preview = preview(db.repo, u) @@ -90,7 +90,7 @@ func TestWorkerUsesConfiguredGitBudget(t *testing.T) { if !ok { t.Fatal("claim") } - s := &Service{DB: db, Root: t.TempDir()} + s := &Service{DB: db, Root: t.TempDir(), AccessCheck: reachable} global := n s.UseGitTimeoutDefault(func() int { return global }) called := false @@ -116,7 +116,7 @@ func TestWorkerUsesConfiguredGitBudget(t *testing.T) { func TestGitTimeoutRegistrationIdempotency(t *testing.T) { db := &sourceStore{repos: map[string]control.Repository{}} - s := &Service{DB: db, Root: t.TempDir()} + s := &Service{DB: db, Root: t.TempDir(), AccessCheck: reachable} policy := control.Policy{Branches: []string{"main"}, Count: 10} register := func(n int) (control.Repository, error) { return s.RegisterWithGitTimeout(context.Background(), "org/repo", policy, 5, "full", nil, nil, "github", n, "timeout-registration") diff --git a/internal/service/managers_test.go b/internal/service/managers_test.go index 3127df9..c56eb96 100644 --- a/internal/service/managers_test.go +++ b/internal/service/managers_test.go @@ -20,7 +20,7 @@ func managerCode(err error) string { func TestUpdateManagers(t *testing.T) { db := &sourceStore{repos: map[string]control.Repository{"org/repo": {Name: "org/repo", CreatorID: "creator", Revision: 3, Enabled: true}}} - s := &Service{DB: db, Root: t.TempDir()} + s := &Service{DB: db, Root: t.TempDir(), AccessCheck: reachable} ctx := identity.WithActor(context.Background(), "sso:creator") if _, err := s.UpdateManagers(ctx, "org/repo", ManagerChange{Add: "a", Remove: "b"}, 3); managerCode(err) != "INVALID_MANAGER_CHANGE" { @@ -62,7 +62,7 @@ func TestUpdateManagers(t *testing.T) { func TestRegistrationRecordsTheCreatorAsManager(t *testing.T) { db := &sourceStore{repos: map[string]control.Repository{}} - s := &Service{DB: db, Root: t.TempDir()} + s := &Service{DB: db, Root: t.TempDir(), AccessCheck: reachable} ctx := identity.WithActor(context.Background(), "sso:creator") r, err := s.RegisterWithProvider(ctx, "context4ai/agent-graph", control.Policy{Branches: []string{"main"}, Count: 10}, 60, "full", nil, nil, "github", "register") if err != nil || r.CreatorID != "creator" || !r.IsManager("creator") { diff --git a/internal/service/service.go b/internal/service/service.go index 1fc7ce6..b50ac2a 100644 --- a/internal/service/service.go +++ b/internal/service/service.go @@ -33,13 +33,15 @@ type Store interface { NextGeneration(context.Context) (uint32, error) } type Service struct { - gitTimeoutDefault func() int - linkManifests linkManifestCache - Plugins *wasmplugin.Host - pluginSnapshots sync.RWMutex - pluginCache pluginCacheState - QueryLogs *querylog.Log - Credentials *codecredentials.Manager + gitTimeoutDefault func() int + linkManifests linkManifestCache + Plugins *wasmplugin.Host + pluginSnapshots sync.RWMutex + pluginCache pluginCacheState + QueryLogs *querylog.Log + Credentials *codecredentials.Manager + // AccessCheck replaces the remote probe of CheckAccess in tests. + AccessCheck func(ctx context.Context, name, provider string) error diskWake chan struct{} diskWakeOnce sync.Once storage storageSlot @@ -221,6 +223,10 @@ func (s *Service) RegisterWithGitTimeout(ctx context.Context, name string, p con if e != nil { return control.Repository{}, e } + // A new name must be fetchable with the tokens its registration will use. + if e := s.CheckRegistrationAccess(ctx, name, provider); e != nil { + return control.Repository{}, e + } s.mu.Lock() defer s.mu.Unlock() if existing, e := s.DB.Get(ctx, name); e == nil { diff --git a/internal/sso/handler.go b/internal/sso/handler.go index dcc5f8e..4c058df 100644 --- a/internal/sso/handler.go +++ b/internal/sso/handler.go @@ -226,6 +226,11 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { // is host-only and the provider always returns to the configured address, so a // login started elsewhere cannot complete. The site's reverse proxy forwards the // original host; only the configured origin is ever used as a redirect target. +// SiteURL is the configured site origin, for links in API responses. +func (h *Handler) SiteURL(*http.Request) string { + return strings.TrimSuffix(h.config.Origin, "/") +} + func (h *Handler) siteLogin(r *http.Request) string { site, err := url.Parse(h.config.Origin) if err != nil || site.Host == "" { diff --git a/web/assets/repos-page.js b/web/assets/repos-page.js index 520beec..e07757c 100644 --- a/web/assets/repos-page.js +++ b/web/assets/repos-page.js @@ -127,18 +127,38 @@ $('#f-sync-interval').addEventListener('change',()=>{$('#form-error').textContent='';}); // A name registered by someone else may be hidden; point to its registrant instead. function taken(c){const who=c&&c.name?c.name+(c.email?' ('+c.email+')':''):'';return who?t('该仓库已由 '+who+' 登记。如需管理,请联系对方将你加入仓库管理员','This repository is already registered by '+who+'. To manage it, ask them to add you as a repository manager'):t('该仓库已登记。如需管理,请联系站点管理员将你加入仓库管理员','This repository is already registered. To manage it, ask a site administrator to add you as a repository manager');} + // Registration checks that the repository can be fetched with the caller's tokens before + // storing anything; the form stays locked meanwhile and keeps its content when it fails. + const ACCESS={ + GIT_CREDENTIAL_UNAVAILABLE:['这个代码源还没有可用的访问令牌,无法拉取该仓库。请在用户设置里绑定你的个人访问令牌。','This code source has no usable access token, so the repository cannot be fetched. Bind your personal access token in user settings.'], + GIT_AUTH_FAILED:['代码服务拒绝了当前令牌,令牌可能已过期或被撤销。请在用户设置里更新你的个人访问令牌。','The code host rejected the token; it may have expired or been revoked. Update your personal access token in user settings.'], + GIT_ACCESS_DENIED:['当前可用的令牌没有读取该仓库的权限。请在用户设置里绑定能读取它的个人访问令牌。','The available tokens cannot read this repository. Bind a personal access token that can read it in user settings.'], + GIT_REPOSITORY_UNAVAILABLE:['找不到该仓库,或当前令牌看不到它。请先核对仓库名;仓库存在时,在用户设置里绑定能读取它的个人访问令牌。','Repository not found, or the token cannot see it. Check the name first; if it exists, bind a personal access token that can read it in user settings.'], + GIT_FETCH_FAILED:['暂时连不上代码服务,请稍后重新检查。','The code host cannot be reached right now; check again shortly.'], + ACCESS_CHECK_TIMEOUT:['代码服务没有及时响应,请稍后重新检查。','The code host did not answer in time; check again shortly.']}; + let accessCode=''; + function formStatus(text){$('#form-status').textContent=text;} + function lock(on,label){submitting=on;$('#form').inert=on;$('#form').setAttribute('aria-busy',String(on));document.querySelectorAll('#drawer [data-close]').forEach(b=>b.disabled=on);$('#submit').disabled=on;$('#submit').textContent=on?label:t('登记并准备','Register and prepare');} + function showAccess(code){ + accessCode=code;$('#access-help').classList.toggle('hidden',!code);if(!code)return; + const token=!['GIT_FETCH_FAILED','ACCESS_CHECK_TIMEOUT'].includes(code); + $('#access-help-text').textContent=t(ACCESS[code][0],ACCESS[code][1])+(token?t('表单内容已保留,绑定后回到这里点「重新检查」。',' The form is kept; come back and choose Check again after binding.'):''); + $('#access-settings').classList.toggle('hidden',!token); + } + $('#access-settings').onclick=()=>{window.open('/sourcegraph/user-settings#code-source-tokens','_blank','noopener');$('#access-retry').focus();}; + $('#access-retry').onclick=()=>submit(); async function submit(e){if(e)e.preventDefault();if(submitting||!M.manage(canRegister())||!$('#form').reportValidity())return;let body; try {const name=M.repositoryName($('#f-name').value);if(rows.some(r=>r.name===name))throw new Error(t('该仓库已登记,请到详情页修改','This repository is already registered. Edit it from its details page'));body={name,git_timeout_minutes:M.gitTimeout($('#f-git-timeout').value),code_provider:$('#f-code-provider').value,index_mode:$('#f-index-mode').value,public_read:$('#f-ack').checked,policy:{branches:branchInput.value(),...(branchInput.usesDefault()?{default_branch:true}:{})},sync_interval_minutes:M.syncInterval($('#f-sync-interval').value)};if(body.index_mode==='monorepo')body.path_groups=pathInput.value();} catch(e){$('#form-error').textContent=e.message;return;} - submitting=true;$('#submit').disabled=true;$('#submit').textContent=t('提交中','Submitting');$('#form-error').textContent=''; + $('#form-error').textContent='';showAccess('');lock(true,t('检查中','Checking'));formStatus(t('正在检查能否拉取该仓库,通过后登记…','Checking that the repository can be fetched, then registering…')); try {const r=await API.request('/api/admin/v1/repos',{method:'POST',body,headers:{'Idempotency-Key':keys.get(body)}});keys.done(body);A.toast(t('已登记,首轮同步已排队','Registered, first sync queued'));location.assign('/sourcegraph/repo?repo='+encodeURIComponent(r.name)+'&tab=jobs');} - catch(e){$('#form-error').textContent=e.code==='REPOSITORY_EXISTS'?taken(e.creator):M.error(e);} - finally{submitting=false;$('#submit').disabled=false;$('#submit').textContent=t('登记并准备','Register and prepare');} + catch(e){formStatus('');if(ACCESS[e.code])showAccess(e.code);else $('#form-error').textContent=e.code==='REPOSITORY_EXISTS'?taken(e.creator):M.error(e);} + finally{lock(false);} } $('#submit').onclick=submit;$('#form').onsubmit=submit; function filter(){managerChoice=$('#manager-filter-wrap').classList.contains('hidden')?managerChoice:$('#manager-filter').value;sortChoice=$('#sort').value;const p=new URLSearchParams();if($('#filter').value)p.set('q',$('#filter').value);if(managerChoice)p.set('manager',managerChoice);if(sortChoice)p.set('sort',sortChoice);if($('#show-deleted').checked)p.set('deleted','1');history.replaceState(null,'',location.pathname+(p.size?'?'+p:'')+location.hash);render();} $('#filter').oninput=filter;$('#show-deleted').onchange=filter;$('#manager-filter').onchange=filter;$('#sort').onchange=filter; - document.addEventListener('langchange',()=>{render();renderSyncInterval();branchInput.refresh();pathInput.refresh();}); + document.addEventListener('langchange',()=>{render();if(accessCode)showAccess(accessCode);if(!submitting)$('#submit').textContent=t('登记并准备','Register and prepare');renderSyncInterval();branchInput.refresh();pathInput.refresh();}); document.addEventListener('app:auth',render); const poller=M.poll(load,10000,60); App.ready.then(()=>{render();poller.refresh();if(initial.get('new')==='1'||location.hash==='#new')open();}); diff --git a/web/repos.html b/web/repos.html index ce1f88b..769e6b7 100644 --- a/web/repos.html +++ b/web/repos.html @@ -76,6 +76,14 @@

公开仓库:所有人可搜索和查看;取消后仅创建者、仓库管理员和站点管理员可见 + +

diff --git a/web/settings.html b/web/settings.html index 4fb978d..587e6ba 100644 --- a/web/settings.html +++ b/web/settings.html @@ -76,7 +76,7 @@ -
+

代码源

登记的仓库从这里的代码源拉取代码。每个代码源配置一个全局令牌;用户可在个人设置里配置自己的令牌,优先使用。