Git designed for AI agents.
operations, testserver: tests of a policy reloaded while a land is in flight
Federico Elia897994bc99a0
diff --git a/internal/gitaly/service/operations/land_batch_identity_test.go b/internal/gitaly/service/operations/land_batch_identity_test.goindex c2a7b80..5fdf2ba 100644--- a/internal/gitaly/service/operations/land_batch_identity_test.go+++ b/internal/gitaly/service/operations/land_batch_identity_test.go@@ -15,6 +15,7 @@ import ("github.com/feder-cr/git-ai/internal/graft/changelog""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/log""github.com/feder-cr/git-ai/internal/testhelper""github.com/feder-cr/git-ai/internal/testhelper/testcfg"@@ -58,6 +59,15 @@ type agentLandClient struct {func setupAgentLandService(t *testing.T, ctx context.Context, graft config.Graft) (context.Context, config.Cfg, agentLandClient) {t.Helper()+ return setupAgentLandServiceWith(t, ctx, graft, policystore.New(graft.Policy))+}++// setupAgentLandServiceWith is setupAgentLandService with the store of the policy, which every+// reader of the policy reads (the access check of pre-receive, the hook and the service), and more+// options of the server.+func setupAgentLandServiceWith(t *testing.T, ctx context.Context, graft config.Graft, policy *policystore.Store, options ...testserver.GitalyServerOpt) (context.Context, config.Cfg, agentLandClient) {+ t.Helper()+cfg := testcfg.Build(t, testcfg.WithBase(config.Cfg{Graft: graft}))var lanes *landlane.Sharedif graft.Standalone && !graft.Land.Batch.Disabled {@@ -69,10 +79,11 @@ func setupAgentLandService(t *testing.T, ctx context.Context, graft config.Grafttestcfg.BuildGitalySSH(t, cfg)testcfg.BuildGitalyHooks(t, cfg)- cfg.SocketPath = runOperationServiceServerWithLanes(t, cfg, lanes,+ cfg.SocketPath = runOperationServiceServerWithLanes(t, cfg, lanes, append([]testserver.GitalyServerOpt{testserver.WithInterceptors(agentInterceptors),- testserver.WithGitLabClient(gitlab.NewStandaloneClient(graft, testhelper.NewLogger(t))),- )+ testserver.WithGraftPolicy(policy),+ testserver.WithGitLabClient(gitlab.NewStandaloneClientWithPolicy(policy, testhelper.NewLogger(t))),+ }, options...)...)client, conn := newOperationClient(t, cfg.SocketPath)t.Cleanup(func() { conn.Close() })ctx = testhelper.MergeOutgoingMetadata(ctx, testcfg.GitalyServersMetadataFromCfg(t, cfg))diff --git a/internal/gitaly/service/operations/land_policy_reload_test.go b/internal/gitaly/service/operations/land_policy_reload_test.gonew file mode 100644index 0000000..ed3a2f5--- /dev/null+++ b/internal/gitaly/service/operations/land_policy_reload_test.go@@ -0,0 +1,162 @@+package operations++import (+ "context"+ "io"+ "strings"+ "sync/atomic"+ "testing"++ "github.com/feder-cr/git-ai/internal/git"+ "github.com/feder-cr/git-ai/internal/git/gittest"+ "github.com/feder-cr/git-ai/internal/gitaly/config"+ "github.com/feder-cr/git-ai/internal/gitaly/hook"+ "github.com/feder-cr/git-ai/internal/graft/policystore"+ "github.com/feder-cr/git-ai/internal/helper/text"+ "github.com/feder-cr/git-ai/internal/testhelper"+ "github.com/feder-cr/git-ai/internal/testhelper/testserver"+ "github.com/feder-cr/git-ai/proto/go/gitalypb"+ "github.com/stretchr/testify/require"+ "google.golang.org/grpc/codes"+)++// policyReloader reloads the policy of a running service at a point of a land: once armed, right+// before the next reference-transaction hook reaches the hook manager. That is after the land's+// policy check and its pre-receive, which decided with the policy before, and before the hook+// decides in the prepared phase.+type policyReloader struct {+ hook.Manager+ t *testing.T+ store *policystore.Store+ running config.Graft+ armed atomic.Pointer[[]config.GraftPolicyRule]+ fired atomic.Bool+}++// arm makes the next reference-transaction hook reload the policy to rules.+func (r *policyReloader) arm(rules []config.GraftPolicyRule) {+ r.armed.Store(&rules)+}++// ReferenceTransactionHook reloads the policy when armed, then runs the hook.+func (r *policyReloader) ReferenceTransactionHook(ctx context.Context, state hook.ReferenceTransactionState, env []string, stdin io.Reader) error {+ if rules := r.armed.Swap(nil); rules != nil {+ next := r.running+ next.Policy = *rules+ outcome, err := r.store.Reload(next, r.running)+ require.NoError(r.t, err)+ require.Equal(r.t, policystore.Applied, outcome.Result)+ r.fired.Store(true)+ }+ return r.Manager.ReferenceTransactionHook(ctx, state, env, stdin)+}++// setupPolicyReload starts the agents' land service of graft with a policyReloader around its hook+// manager.+func setupPolicyReload(t *testing.T, ctx context.Context, graft config.Graft) (context.Context, config.Cfg, agentLandClient, *policyReloader) {+ reloader := &policyReloader{t: t, store: policystore.New(graft.Policy), running: graft}+ ctx, cfg, client := setupAgentLandServiceWith(t, ctx, graft, reloader.store, testserver.WithHookManagerWrapper(func(manager hook.Manager) hook.Manager {+ reloader.Manager = manager+ return reloader+ }))+ return ctx, cfg, client, reloader+}++// policyReloadRepo is a repository whose main is at base, and changes of it, each adding a file.+func policyReloadRepo(t *testing.T, ctx context.Context, cfg config.Cfg) (*gitalypb.Repository, string, git.ObjectID, func(string) git.ObjectID) {+ repoProto, repoPath := gittest.CreateRepository(t, ctx, cfg)+ baseEntry := gittest.TreeEntry{Mode: "100644", Path: "base", Content: "base"}+ base := gittest.WriteCommit(t, cfg, repoPath, gittest.WithBranch(git.DefaultBranch), gittest.WithTreeEntries(baseEntry))+ change := func(name string) git.ObjectID {+ return gittest.WriteCommit(t, cfg, repoPath, gittest.WithParents(base),+ gittest.WithTreeEntries(baseEntry, gittest.TreeEntry{Mode: "100644", Path: name, Content: name}))+ }+ return repoProto, repoPath, base, change+}++// unbatchedGraft is a standalone configuration with policy whose lands do not batch, so that the+// reference-transaction hook decides each agent update.+func unbatchedGraft(policy []config.GraftPolicyRule) config.Graft {+ return config.Graft{+ Standalone: true,+ Policy: policy,+ Land: config.GraftLand{Batch: config.GraftLandBatch{Disabled: true}},+ }+}++func mainFiles(t *testing.T, cfg config.Cfg, repoPath string) []string {+ return strings.Fields(string(gittest.Exec(t, cfg, "-C", repoPath, "ls-tree", "--name-only", "refs/heads/"+git.DefaultBranch)))+}++// TestUserLand_policyReloadedInFlight reloads the policy while a land is in flight: a tightening+// applies to an update that reaches the reference-transaction hook after it, whatever the earlier+// checks allowed (PLAN12, section 5).+func TestUserLand_policyReloadedInFlight(t *testing.T) {+ t.Parallel()++ runLandBatchTest(t, testUserLandPolicyReloadedInFlight)+}++func testUserLandPolicyReloadedInFlight(t *testing.T, ctx context.Context) {+ t.Parallel()++ t.Run("a tightening refuses an update its earlier checks allowed", func(t *testing.T) {+ t.Parallel()++ ctx, cfg, client, reloader := setupPolicyReload(t, ctx, unbatchedGraft(nil))+ repo, repoPath, base, change := policyReloadRepo(t, ctx, cfg)++ reloader.arm(denyMainFor("bot"))+ _, errs := landInOrder(t, ctx, client, repo, agentLanding{agent: "bot", commit: change("b")})++ require.True(t, reloader.fired.Load(), "the policy was reloaded in flight")+ testhelper.RequireGrpcCode(t, errs[0], codes.PermissionDenied)+ require.Equal(t, base.String(), text.ChompBytes(gittest.Exec(t, cfg, "-C", repoPath, "rev-parse", "refs/heads/"+git.DefaultBranch)))+ })++ t.Run("a loosening lets the refused land again", func(t *testing.T) {+ t.Parallel()++ ctx, cfg, client, reloader := setupPolicyReload(t, ctx, unbatchedGraft(denyMainFor("bot")))+ repo, repoPath, _, change := policyReloadRepo(t, ctx, cfg)+ commit := change("b")++ _, errs := landInOrder(t, ctx, client, repo, agentLanding{agent: "bot", commit: commit})+ testhelper.RequireGrpcCode(t, errs[0], codes.PermissionDenied)++ next := reloader.running+ next.Policy = nil+ outcome, err := reloader.store.Reload(next, reloader.running)+ require.NoError(t, err)+ require.Equal(t, policystore.Applied, outcome.Result)++ _, errs = landInOrder(t, ctx, client, repo, agentLanding{agent: "bot", commit: commit})+ require.NoError(t, errs[0])+ require.ElementsMatch(t, []string{"base", "b"}, mainFiles(t, cfg, repoPath))+ })++ t.Run("a batch admitted under another policy is admitted again, and the newly denied member fails alone", func(t *testing.T) {+ t.Parallel()++ ctx, cfg, client, reloader := setupPolicyReload(t, ctx, agentBatchingGraft(3, nil))+ repo, repoPath, _, change := policyReloadRepo(t, ctx, cfg)++ // The first reference-transaction hook is the batch's combined update, admitted with the+ // policy before the reload.+ reloader.arm(denyMainFor("bot"))+ _, errs := landInOrder(t, ctx, client, repo,+ agentLanding{agent: "bot", commit: change("b")},+ agentLanding{agent: "alice", commit: change("a")},+ agentLanding{agent: "carol", commit: change("c")},+ )++ 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.NoError(t, errs[1])+ require.NoError(t, errs[2])+ require.ElementsMatch(t, []string{"base", "a", "c"}, mainFiles(t, cfg, repoPath))+ })+}diff --git a/internal/testhelper/testserver/gitaly.go b/internal/testhelper/testserver/gitaly.goindex 816a9ed..017fcaf 100644--- a/internal/testhelper/testserver/gitaly.go+++ b/internal/testhelper/testserver/gitaly.go@@ -35,6 +35,7 @@ import (corestoresyncer "github.com/feder-cr/git-ai/internal/graft/corestore/syncer""github.com/feder-cr/git-ai/internal/graft/corestore/voter""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/grpc/backchannel""github.com/feder-cr/git-ai/internal/grpc/client""github.com/feder-cr/git-ai/internal/grpc/middleware/limithandler"@@ -321,6 +322,8 @@ type gitalyServerDeps struct {graftStorage *corestore.CoregraftStorageSyncer *corestoresyncer.SyncergraftStorageGate *gate.Gate+ graftPolicy *policystore.Store+ hookMgrWrapper func(hook.Manager) hook.Manager}func (gsd *gitalyServerDeps) createDependencies(tb testing.TB, ctx context.Context, cfg config.Cfg) *service.Dependencies {@@ -384,7 +387,12 @@ func (gsd *gitalyServerDeps) createDependencies(tb testing.TB, ctx context.Conte// The Graft storage core is wired as in serve.go: the voter in the hook manager, the syncer in// the dependencies and, in authoritative mode, the gate interceptors (see runGitaly).- var hookManagerOpts []hook.ManagerOption+ // One store of the standalone access policy serves the hook manager and the services, as in+ // serve.go, so that a test that reloads it reaches every reader.+ if gsd.graftPolicy == nil {+ gsd.graftPolicy = policystore.New(cfg.Graft.Policy)+ }+ hookManagerOpts := []hook.ManagerOption{hook.WithPolicyStore(gsd.graftPolicy)}if gsd.graftStorage.Enabled() {if gsd.catfileCache == nil {cache := catfile.NewCache(cfg)@@ -412,6 +420,9 @@ func (gsd *gitalyServerDeps) createDependencies(tb testing.TB, ctx context.ContehookManagerOpts...,)}+ if gsd.hookMgrWrapper != nil {+ gsd.hookMgr = gsd.hookMgrWrapper(gsd.hookMgr)+ }if gsd.catfileCache == nil {cache := catfile.NewCache(cfg)@@ -511,6 +522,7 @@ func (gsd *gitalyServerDeps) createDependencies(tb testing.TB, ctx context.ContePoolMetadataStore: gsd.poolMetadataStore,ObjectPoolStateManager: gsd.objectPoolStateManager,GraftLandLanes: gsd.graftLandLanes,+ GraftPolicy: gsd.graftPolicy,}}@@ -558,6 +570,26 @@ func WithGitLabClient(gitlabClient gitlab.Client) GitalyServerOpt {}}+// WithGraftPolicy sets the store of the standalone access policy that the hook manager and the+// services read, so that a test can reload the policy while the server runs. Without it, the server+// has a store of the configuration's policy.+func WithGraftPolicy(store *policystore.Store) GitalyServerOpt {+ return func(deps gitalyServerDeps) gitalyServerDeps {+ deps.graftPolicy = store+ return deps+ }+}++// WithHookManagerWrapper wraps the hook manager of the server, the one WithHookManager sets or the+// server's own, with wrap: the services and the updater with hooks call the wrapper, which a test+// uses to act at a point of the hooks.+func WithHookManagerWrapper(wrap func(hook.Manager) hook.Manager) GitalyServerOpt {+ return func(deps gitalyServerDeps) gitalyServerDeps {+ deps.hookMgrWrapper = wrap+ return deps+ }+}+// WithHookManager sets hook.Manager instance that will be used for gitaly services initialisation.func WithHookManager(hookMgr hook.Manager) GitalyServerOpt {return func(deps gitalyServerDeps) gitalyServerDeps {