From 47d502ed80d427575b8b4f55f7aaeb7572cfe5db Mon Sep 17 00:00:00 2001 From: Thomas Boop <52323235+thboop@users.noreply.github.com> Date: Thu, 14 Apr 2022 12:12:00 -0400 Subject: Safe Directory v2 update (#764) * set safe directory when running checkout --- src/git-auth-helper.ts | 50 +++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 39 insertions(+), 11 deletions(-) (limited to 'src/git-auth-helper.ts') diff --git a/src/git-auth-helper.ts b/src/git-auth-helper.ts index 233b3e6..385142a 100644 --- a/src/git-auth-helper.ts +++ b/src/git-auth-helper.ts @@ -19,8 +19,9 @@ export interface IGitAuthHelper { configureAuth(): Promise configureGlobalAuth(): Promise configureSubmoduleAuth(): Promise + configureTempGlobalConfig(repositoryPath?: string): Promise removeAuth(): Promise - removeGlobalAuth(): Promise + removeGlobalConfig(): Promise } export function createAuthHelper( @@ -80,7 +81,11 @@ class GitAuthHelper { await this.configureToken() } - async configureGlobalAuth(): Promise { + async configureTempGlobalConfig(repositoryPath?: string): Promise { + // Already setup global config + if (this.temporaryHomePath?.length > 0) { + return path.join(this.temporaryHomePath, '.gitconfig') + } // Create a temp home directory const runnerTemp = process.env['RUNNER_TEMP'] || '' assert.ok(runnerTemp, 'RUNNER_TEMP is not defined') @@ -110,13 +115,34 @@ class GitAuthHelper { await fs.promises.writeFile(newGitConfigPath, '') } - try { - // Override HOME - core.info( - `Temporarily overriding HOME='${this.temporaryHomePath}' before making global git config changes` + // Override HOME + core.info( + `Temporarily overriding HOME='${this.temporaryHomePath}' before making global git config changes` + ) + this.git.setEnvironmentVariable('HOME', this.temporaryHomePath) + + // Setup the workspace as a safe directory, so if we pass this into a container job with a different user it doesn't fail + // Otherwise all git commands we run in a container fail + core.info( + `Adding working directory to the temporary git global config as a safe directory` + ) + await this.git + .config( + 'safe.directory', + repositoryPath ?? this.settings.repositoryPath, + true, + true ) - this.git.setEnvironmentVariable('HOME', this.temporaryHomePath) + .catch(error => { + core.info(`Failed to initialize safe directory with error: ${error}`) + }) + return newGitConfigPath + } + async configureGlobalAuth(): Promise { + // 'configureTempGlobalConfig' noops if already set, just returns the path + const newGitConfigPath = await this.configureTempGlobalConfig() + try { // Configure the token await this.configureToken(newGitConfigPath, true) @@ -181,10 +207,12 @@ class GitAuthHelper { await this.removeToken() } - async removeGlobalAuth(): Promise { - core.debug(`Unsetting HOME override`) - this.git.removeEnvironmentVariable('HOME') - await io.rmRF(this.temporaryHomePath) + async removeGlobalConfig(): Promise { + if (this.temporaryHomePath?.length > 0) { + core.debug(`Unsetting HOME override`) + this.git.removeEnvironmentVariable('HOME') + await io.rmRF(this.temporaryHomePath) + } } private async configureSsh(): Promise { -- cgit v1.2.3