Skip to content

Commit

Permalink
DEVPROD-12824 Reset ~/.git-credentials between tasks (#8482)
Browse files Browse the repository at this point in the history
  • Loading branch information
malikchaya2 authored Nov 25, 2024
1 parent 62fb931 commit 58fc889
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
12 changes: 12 additions & 0 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,18 @@ func (a *Agent) clearGitConfig(tc *taskContext) {
}

logger.Info("Cleared git config.")

logger.Infof("Clearing git credentials.")
globalGitCredentialsPath := filepath.Join(a.opts.HomeDirectory, ".git-credentials")
if _, err := os.Stat(globalGitCredentialsPath); os.IsNotExist(err) {
logger.Info("Global git credentials file does not exist.")
return
}
if err := os.Remove(globalGitCredentialsPath); err != nil {
logger.Error(errors.Wrap(err, "removing global git credentials file"))
return
}
logger.Info("Cleared git credentials.")
}

func (a *Agent) shouldKill(tc *taskContext, ignoreTaskGroupCheck bool) bool {
Expand Down
12 changes: 10 additions & 2 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2734,28 +2734,36 @@ func (s *AgentSuite) TestClearsGitConfig() {
s.setupRunTask(defaultProjYml)
// create a fake git config file
gitConfigPath := filepath.Join(s.a.opts.HomeDirectory, ".gitconfig")
gitConfigContents := `
gitCredentialsPath := filepath.Join(s.a.opts.HomeDirectory, ".git-credentials")
contents := `
[user]
name = foo bar
email = [email protected]
`
err := os.WriteFile(gitConfigPath, []byte(gitConfigContents), 0600)
err := os.WriteFile(gitConfigPath, []byte(contents), 0600)
s.Require().NoError(err)
s.Require().FileExists(gitConfigPath)

err = os.WriteFile(gitCredentialsPath, []byte(contents), 0600)
s.Require().NoError(err)
s.Require().FileExists(gitCredentialsPath)

s.a.runTeardownGroupCommands(s.ctx, s.tc)
s.NoError(err)

s.NoError(s.tc.logger.Close())
checkMockLogs(s.T(), s.mockCommunicator, s.tc.taskConfig.Task.Id, []string{
"Clearing git config.",
"Cleared git config.",
"Clearing git credentials.",
"Cleared git credentials.",
}, []string{
panicLog,
"Running task commands failed",
})

s.Assert().NoFileExists(gitConfigPath)
s.Assert().NoFileExists(gitCredentialsPath)
}

func (s *AgentSuite) TestShouldRunSetupGroup() {
Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (

// Agent version to control agent rollover. The format is the calendar date
// (YYYY-MM-DD).
AgentVersion = "2024-11-25"
AgentVersion = "2024-11-26"
)

const (
Expand Down
10 changes: 6 additions & 4 deletions docs/Project-Configuration/Task-Runtime-Behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,12 @@ a task group, it will keep the task directory as long as it is running tasks
in the same task group. Once all the task group tasks have finished, it will
clean up the task directory.

### Global Git Config Cleanup
For tasks not in a task group, the global git config will be reset at the end of the
task after all commands have finished running. For a tasks in a task group, the reset
will occur after the all the tasks in the task group tasks have finished.
### Global Git Config and Git Credentials Cleanup
For tasks not in a task group, the global git config and git credentials will be reset
at the end of the task after all commands have finished running. This will be done by
deleting the .git-credentials and .gitconfig files from the home directory.
For a tasks in a task group, the reset will occur after the all the tasks in the task
group tasks have finished.

## Task Timeouts

Expand Down

0 comments on commit 58fc889

Please sign in to comment.