Skip to content

Git designed for AI agents.

operations: the test agents' requests get the server's defaults; the access check names the agent

Federico Eliad759a83dc363
diff --git a/internal/gitaly/service/operations/land_batch_identity_test.go b/internal/gitaly/service/operations/land_batch_identity_test.go
index 5fdf2ba..8d55ee6 100644
--- a/internal/gitaly/service/operations/land_batch_identity_test.go
+++ b/internal/gitaly/service/operations/land_batch_identity_test.go
@@ -16,6 +16,7 @@ import (
"github.com/feder-cr/git-ai/internal/graft/identity"
"github.com/feder-cr/git-ai/internal/graft/landlane"
"github.com/feder-cr/git-ai/internal/graft/policystore"
+ "github.com/feder-cr/git-ai/internal/graft/requestdefaults"
"github.com/feder-cr/git-ai/internal/log"
"github.com/feder-cr/git-ai/internal/testhelper"
"github.com/feder-cr/git-ai/internal/testhelper/testcfg"
@@ -31,19 +32,27 @@ import (
const testAgentHeader = "x-test-agent"
// agentInterceptors authenticates each request as the agent named in its test header, the way the
-// per-agent token authentication does, so that every request carries its own identity.
-func agentInterceptors(log.Logger, localrepo.Factory) ([]grpc.UnaryServerInterceptor, []grpc.StreamServerInterceptor) {
- return []grpc.UnaryServerInterceptor{
- func(ctx context.Context, req any, _ *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
- if md, ok := metadata.FromIncomingContext(ctx); ok {
- if names := md.Get(testAgentHeader); len(names) > 0 {
- ctx = identity.NewContext(ctx, identity.Identity{Name: names[0], Source: identity.SourceAgent})
+// per-agent token authentication does, so that every request carries its own identity. In
+// standalone mode it then fills the request's defaults from that identity, as the server does right
+// after authentication (requestdefaults): the gl_id and gl_username of an agent's request are the
+// agent's name. The test interceptors run after the server's own, which saw no identity yet.
+func agentInterceptors(standalone bool) testserver.InterceptorsFunc {
+ return func(log.Logger, localrepo.Factory) ([]grpc.UnaryServerInterceptor, []grpc.StreamServerInterceptor) {
+ return []grpc.UnaryServerInterceptor{
+ func(ctx context.Context, req any, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (any, error) {
+ if md, ok := metadata.FromIncomingContext(ctx); ok {
+ if names := md.Get(testAgentHeader); len(names) > 0 {
+ ctx = identity.NewContext(ctx, identity.Identity{Name: names[0], Source: identity.SourceAgent})
+ }
}
- }
- return handler(ctx, req)
- },
- }, nil
+ if standalone {
+ return requestdefaults.UnaryServerInterceptor(ctx, req, info, handler)
+ }
+ return handler(ctx, req)
+ },
+ }, nil
+ }
}
// agentLandClient is the client of a service that setupAgentLandService started. lanes are the land
@@ -80,7 +89,7 @@ func setupAgentLandServiceWith(t *testing.T, ctx context.Context, graft config.G
testcfg.BuildGitalySSH(t, cfg)
testcfg.BuildGitalyHooks(t, cfg)
cfg.SocketPath = runOperationServiceServerWithLanes(t, cfg, lanes, append([]testserver.GitalyServerOpt{
- testserver.WithInterceptors(agentInterceptors),
+ testserver.WithInterceptors(agentInterceptors(graft.Standalone)),
testserver.WithGraftPolicy(policy),
testserver.WithGitLabClient(gitlab.NewStandaloneClientWithPolicy(policy, testhelper.NewLogger(t))),
}, options...)...)
diff --git a/internal/gitaly/service/operations/land_policy_reload_test.go b/internal/gitaly/service/operations/land_policy_reload_test.go
index c45cb7f..19575f8 100644
--- a/internal/gitaly/service/operations/land_policy_reload_test.go
+++ b/internal/gitaly/service/operations/land_policy_reload_test.go
@@ -158,15 +158,73 @@ func testUserLandPolicyReloadedInFlight(t *testing.T, ctx context.Context) {
require.True(t, reloader.fired.Load(), "the policy was reloaded in flight")
testhelper.RequireGrpcCode(t, errs[0], codes.PermissionDenied)
- // Denied alone, by its access check: the detail is the access check's, whose user ID is the
- // request's (pre-receive), not the agent's name as an admission in a batch reports it.
- require.NotNil(t, landErrorDetail(t, errs[0]).GetAccessCheck())
+ require.Equal(t, "bot", landErrorDetail(t, errs[0]).GetAccessCheck().GetUserId())
require.NoError(t, errs[1])
require.NoError(t, errs[2])
require.ElementsMatch(t, []string{"base", "a", "c"}, mainFiles(t, cfg, repoPath))
})
}
+// TestUserLand_accessCheckNamesTheAgent: the denial of an agent's land names the agent as the user
+// of its access check whichever check denied it: the admission of a batch, the pre-receive access
+// check of a land that does not batch, or the pre-receive of a member that lands alone after its
+// batch was refused because the policy changed.
+func TestUserLand_accessCheckNamesTheAgent(t *testing.T) {
+ t.Parallel()
+
+ runLandBatchTest(t, testUserLandAccessCheckNamesTheAgent)
+}
+
+func testUserLandAccessCheckNamesTheAgent(t *testing.T, ctx context.Context) {
+ t.Parallel()
+
+ requireDeniedAs := func(t *testing.T, err error, agent string) {
+ t.Helper()
+ testhelper.RequireGrpcCode(t, err, codes.PermissionDenied)
+ require.Equal(t, agent, landErrorDetail(t, err).GetAccessCheck().GetUserId())
+ }
+
+ t.Run("the admission of a batch", func(t *testing.T) {
+ t.Parallel()
+
+ ctx, cfg, client := setupAgentLandService(t, ctx, agentBatchingGraft(2, denyMainFor("bot")))
+ repo, _, _, change := policyReloadRepo(t, ctx, cfg)
+
+ _, errs := landInOrder(t, ctx, client, repo,
+ agentLanding{agent: "bot", commit: change("b")},
+ agentLanding{agent: "alice", commit: change("a")},
+ )
+ requireDeniedAs(t, errs[0], "bot")
+ require.NoError(t, errs[1])
+ })
+
+ t.Run("the access check of a land that does not batch", func(t *testing.T) {
+ t.Parallel()
+
+ ctx, cfg, client := setupAgentLandService(t, ctx, unbatchedGraft(denyMainFor("bot")))
+ repo, _, _, change := policyReloadRepo(t, ctx, cfg)
+
+ _, errs := landInOrder(t, ctx, client, repo, agentLanding{agent: "bot", commit: change("b")})
+ requireDeniedAs(t, errs[0], "bot")
+ })
+
+ t.Run("the access check of a member that lands alone", func(t *testing.T) {
+ t.Parallel()
+
+ ctx, cfg, client, reloader := setupPolicyReload(t, ctx, agentBatchingGraft(2, nil))
+ repo, _, _, change := policyReloadRepo(t, ctx, cfg)
+
+ reloader.arm(denyMainFor("bot"))
+ _, errs := landInOrder(t, ctx, client, repo,
+ agentLanding{agent: "bot", commit: change("b")},
+ agentLanding{agent: "alice", commit: change("a")},
+ )
+ require.True(t, reloader.fired.Load(), "the policy was reloaded in flight")
+ requireDeniedAs(t, errs[0], "bot")
+ require.NoError(t, errs[1])
+ })
+}
+
// TestUserLand_policyReloadsDuringConcurrentLands lands changes of several agents concurrently
// while the policy alternates between two versions, with invalid reloads in between (PLAN12,
// section 8). agent-0 is allowed by one version and denied by the other. Every land either lands