Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions docs/interfaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
23 changes: 23 additions & 0 deletions internal/gitstore/refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions internal/gitstore/refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
46 changes: 45 additions & 1 deletion internal/httpserver/repository_access.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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 := &registrationAccessFailure{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 {
Expand Down
47 changes: 46 additions & 1 deletion internal/httpserver/repository_access_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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) }
20 changes: 19 additions & 1 deletion internal/httpserver/runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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"))
Expand Down Expand Up @@ -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)
Expand Down
90 changes: 90 additions & 0 deletions internal/service/access_check.go
Original file line number Diff line number Diff line change
@@ -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)
}
Loading
Loading