Clear CI token in git credential managers
What does this MR do?
This MR adds git credential reject calls at the end of get_sources and at the start of cleanup to clear the CI token from any user credential helper that may have stored it during the job. The erase is best-effort: if it fails, the job still succeeds and the rest of the cleanup still runs.
Only the username=gitlab-ci-token entry is removed for the matching host. Personal credentials stored under a different username are left untouched. The reject only affects credential helpers reachable from git's config chain at the moment the cleanup runs.
Why was this MR needed?
I sometimes run a gitlab-runner shell executor on my personal computer. When FF_GIT_URLS_WITHOUT_TOKENS is false, the gitlab-ci-token credentials get pushed to credential helpers, and then, after a build, when I fetch my own repos, the first fetch always fails because git tries to use the gitlab-ci-token credentials.
There are 3 approaches to prevent CI_JOB_TOKEN from leaking into credential helpers: prevent it from happening in the first place (1), clean up after the build (2), or shadow system and global configs (3).
- Reset the credential helper at the local (repo) level: this is what happens when
FF_GIT_URLS_WITHOUT_TOKENSis true. The job overridescredential.helperin the local git config, sogitnever reads from (or writes to) the user's system credential managers during the job. This was also the approach first tried in !6599 (closed). - Clear the CI token from the credential helpers after the
get_sourcesphase and during cleanup: the approach taken in this MR. Git's credential chain works normally during the job, so a configured credential helper will storeCI_JOB_TOKEN. We explicitlygit credential rejectthegitlab-ci-tokenusername. Only theusername=gitlab-ci-tokenentry is removed, so any personal credential stored under a different username is left untouched. - Set
GIT_CONFIG_NOSYSTEMto 1 andGIT_CONFIG_GLOBALto point to.gitlab-runner.ext.conf(additional effect of !6665 (closed)).
Option 2 preserves the pre_get_sources workarounds: