gitserver: make list of ignored git commands for recording configurable (#55100)

Closes #55059

This PR makes the list of git commands that should be ignored for recording configurable via the site config setting.

## Test plan

<!-- All pull requests REQUIRE a test plan: https://docs.sourcegraph.com/dev/background-information/testing_principles -->
* Add an entry to the `gitRecorder.ignoredGitCommand` array.
* Perform an operation that will execute that command inside of `gitServer`, check redis for the value associated with key `recording-cmd` and it shouldn't be part of the result.
This commit is contained in:
Bolaji Olajide 2023-07-19 15:16:21 +01:00 committed by GitHub
parent d9c9fa8120
commit befd806646
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 12 deletions

View File

@ -18,6 +18,7 @@ go_library(
"//internal/api",
"//internal/authz",
"//internal/codeintel/dependencies",
"//internal/collections",
"//internal/conf",
"//internal/conf/conftypes",
"//internal/database",

View File

@ -28,6 +28,7 @@ import (
"github.com/sourcegraph/sourcegraph/internal/api"
"github.com/sourcegraph/sourcegraph/internal/authz"
"github.com/sourcegraph/sourcegraph/internal/codeintel/dependencies"
"github.com/sourcegraph/sourcegraph/internal/collections"
"github.com/sourcegraph/sourcegraph/internal/conf"
"github.com/sourcegraph/sourcegraph/internal/conf/conftypes"
"github.com/sourcegraph/sourcegraph/internal/database"
@ -146,7 +147,7 @@ func Main(ctx context.Context, observationCtx *observation.Context, ready servic
recordingCommandFactory.Disable()
return
}
recordingCommandFactory.Update(recordCommandsOnRepos(recordingConf.Repos), recordingConf.Size)
recordingCommandFactory.Update(recordCommandsOnRepos(recordingConf.Repos, recordingConf.IgnoredGitCommands), recordingConf.Size)
})
gitserver := server.Server{
@ -615,9 +616,17 @@ func methodSpecificUnaryInterceptor(method string, next grpc.UnaryServerIntercep
}
}
var defaultIgnoredGitCommands = []string{
"show",
"rev-parse",
"log",
"diff",
"ls-tree",
}
// recordCommandsOnRepos returns a ShouldRecordFunc which determines whether the given command should be recorded
// for a particular repository.
func recordCommandsOnRepos(repos []string) wrexec.ShouldRecordFunc {
func recordCommandsOnRepos(repos []string, ignoredGitCommands []string) wrexec.ShouldRecordFunc {
// empty repos, means we should never record since there is nothing to match on
if len(repos) == 0 {
return func(ctx context.Context, c *exec.Cmd) bool {
@ -625,14 +634,13 @@ func recordCommandsOnRepos(repos []string) wrexec.ShouldRecordFunc {
}
}
// we won't record any git commands with these commands since they are considered to be not destructive
ignoredGitCommands := map[string]struct{}{
"show": {},
"rev-parse": {},
"log": {},
"diff": {},
"ls-tree": {},
if len(ignoredGitCommands) == 0 {
ignoredGitCommands = append(ignoredGitCommands, defaultIgnoredGitCommands...)
}
// we won't record any git commands with these commands since they are considered to be not destructive
var ignoredGitCommandsMap = collections.NewSet(ignoredGitCommands...)
return func(ctx context.Context, cmd *exec.Cmd) bool {
base := filepath.Base(cmd.Path)
if base != "git" {
@ -658,7 +666,7 @@ func recordCommandsOnRepos(repos []string) wrexec.ShouldRecordFunc {
// we have to scan the Args, since it isn't guaranteed that the Arg at index 1 is the git command:
// git -c "protocol.version=2" remote show
for _, arg := range cmd.Args {
if _, ok := ignoredGitCommands[arg]; ok {
if ok := ignoredGitCommandsMap.Has(arg); ok {
return false
}
}

View File

@ -1311,6 +1311,8 @@ type GitLabWebhook struct {
// GitRecorder description: Record git operations that are executed on configured repositories. The following commands are not recorded: show, log, rev-parse and diff.
type GitRecorder struct {
// IgnoredGitCommands description: List of git commands that should be ignored and not recorded.
IgnoredGitCommands []string `json:"ignoredGitCommands,omitempty"`
// Repos description: List of repositories whose git operations should be recorded.
Repos []string `json:"repos,omitempty"`
// Size description: Defines how many recordings to keep. Once this size is reached, the oldest entry will be removed.

View File

@ -1905,7 +1905,9 @@
"size": {
"description": "Defines how many recordings to keep. Once this size is reached, the oldest entry will be removed.",
"type": "integer",
"default": 100000
"default": 10000,
"minimum": 1,
"maximum": 10000
},
"repos": {
"description": "List of repositories whose git operations should be recorded.",
@ -1913,12 +1915,21 @@
"items": {
"type": "string"
}
},
"ignoredGitCommands": {
"description": "List of git commands that should be ignored and not recorded.",
"type": "array",
"items": {
"type": "string"
},
"default": ["show", "rev-parse", "log", "diff", "ls-tree"]
}
},
"examples": [
{
"size": 1000,
"repos": ["github.com/sourcegraph/sourcegraph", "github.com/gorilla/mux"]
"repos": ["github.com/sourcegraph/sourcegraph", "github.com/gorilla/mux"],
"ignoredGitCommands": ["show", "rev-parse", "log", "diff", "ls-tree"]
}
]
},