git-auth-helper.test.ts 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  1. import * as core from '@actions/core'
  2. import * as fs from 'fs'
  3. import * as gitAuthHelper from '../lib/git-auth-helper'
  4. import * as io from '@actions/io'
  5. import * as os from 'os'
  6. import * as path from 'path'
  7. import * as stateHelper from '../lib/state-helper'
  8. import {IGitCommandManager} from '../lib/git-command-manager'
  9. import {IGitSourceSettings} from '../lib/git-source-settings'
  10. const isWindows = process.platform === 'win32'
  11. const testWorkspace = path.join(__dirname, '_temp', 'git-auth-helper')
  12. const originalRunnerTemp = process.env['RUNNER_TEMP']
  13. const originalHome = process.env['HOME']
  14. let workspace: string
  15. let localGitConfigPath: string
  16. let globalGitConfigPath: string
  17. let runnerTemp: string
  18. let tempHomedir: string
  19. let git: IGitCommandManager & {env: {[key: string]: string}}
  20. let settings: IGitSourceSettings
  21. let sshPath: string
  22. let githubServerUrl: string
  23. describe('git-auth-helper tests', () => {
  24. beforeAll(async () => {
  25. // SSH
  26. sshPath = await io.which('ssh')
  27. // Clear test workspace
  28. await io.rmRF(testWorkspace)
  29. })
  30. beforeEach(() => {
  31. // Mock setSecret
  32. jest.spyOn(core, 'setSecret').mockImplementation((secret: string) => {})
  33. // Mock error/warning/info/debug
  34. jest.spyOn(core, 'error').mockImplementation(jest.fn())
  35. jest.spyOn(core, 'warning').mockImplementation(jest.fn())
  36. jest.spyOn(core, 'info').mockImplementation(jest.fn())
  37. jest.spyOn(core, 'debug').mockImplementation(jest.fn())
  38. // Mock state helper
  39. jest.spyOn(stateHelper, 'setSshKeyPath').mockImplementation(jest.fn())
  40. jest
  41. .spyOn(stateHelper, 'setSshKnownHostsPath')
  42. .mockImplementation(jest.fn())
  43. })
  44. afterEach(() => {
  45. // Unregister mocks
  46. jest.restoreAllMocks()
  47. // Restore HOME
  48. if (originalHome) {
  49. process.env['HOME'] = originalHome
  50. } else {
  51. delete process.env['HOME']
  52. }
  53. })
  54. afterAll(() => {
  55. // Restore RUNNER_TEMP
  56. delete process.env['RUNNER_TEMP']
  57. if (originalRunnerTemp) {
  58. process.env['RUNNER_TEMP'] = originalRunnerTemp
  59. }
  60. })
  61. async function testAuthHeader(
  62. testName: string,
  63. serverUrl: string | undefined = undefined
  64. ) {
  65. // Arrange
  66. let expectedServerUrl = 'https://github.com'
  67. if (serverUrl) {
  68. githubServerUrl = serverUrl
  69. expectedServerUrl = githubServerUrl
  70. }
  71. await setup(testName)
  72. expect(settings.authToken).toBeTruthy() // sanity check
  73. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  74. // Act
  75. await authHelper.configureAuth()
  76. // Assert config
  77. const configContent = (
  78. await fs.promises.readFile(localGitConfigPath)
  79. ).toString()
  80. const basicCredential = Buffer.from(
  81. `x-access-token:${settings.authToken}`,
  82. 'utf8'
  83. ).toString('base64')
  84. expect(
  85. configContent.indexOf(
  86. `http.${expectedServerUrl}/.extraheader AUTHORIZATION: basic ${basicCredential}`
  87. )
  88. ).toBeGreaterThanOrEqual(0)
  89. }
  90. const configureAuth_configuresAuthHeader =
  91. 'configureAuth configures auth header'
  92. it(configureAuth_configuresAuthHeader, async () => {
  93. await testAuthHeader(configureAuth_configuresAuthHeader)
  94. })
  95. const configureAuth_AcceptsGitHubServerUrl =
  96. 'inject https://my-ghes-server.com as github server url'
  97. it(configureAuth_AcceptsGitHubServerUrl, async () => {
  98. await testAuthHeader(
  99. configureAuth_AcceptsGitHubServerUrl,
  100. 'https://my-ghes-server.com'
  101. )
  102. })
  103. const configureAuth_AcceptsGitHubServerUrlSetToGHEC =
  104. 'inject https://github.com as github server url'
  105. it(configureAuth_AcceptsGitHubServerUrlSetToGHEC, async () => {
  106. await testAuthHeader(
  107. configureAuth_AcceptsGitHubServerUrl,
  108. 'https://github.com'
  109. )
  110. })
  111. const configureAuth_configuresAuthHeaderEvenWhenPersistCredentialsFalse =
  112. 'configureAuth configures auth header even when persist credentials false'
  113. it(
  114. configureAuth_configuresAuthHeaderEvenWhenPersistCredentialsFalse,
  115. async () => {
  116. // Arrange
  117. await setup(
  118. configureAuth_configuresAuthHeaderEvenWhenPersistCredentialsFalse
  119. )
  120. expect(settings.authToken).toBeTruthy() // sanity check
  121. settings.persistCredentials = false
  122. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  123. // Act
  124. await authHelper.configureAuth()
  125. // Assert config
  126. const configContent = (
  127. await fs.promises.readFile(localGitConfigPath)
  128. ).toString()
  129. expect(
  130. configContent.indexOf(
  131. `http.https://github.com/.extraheader AUTHORIZATION`
  132. )
  133. ).toBeGreaterThanOrEqual(0)
  134. }
  135. )
  136. const configureAuth_copiesUserKnownHosts =
  137. 'configureAuth copies user known hosts'
  138. it(configureAuth_copiesUserKnownHosts, async () => {
  139. if (!sshPath) {
  140. process.stdout.write(
  141. `Skipped test "${configureAuth_copiesUserKnownHosts}". Executable 'ssh' not found in the PATH.\n`
  142. )
  143. return
  144. }
  145. // Arange
  146. await setup(configureAuth_copiesUserKnownHosts)
  147. expect(settings.sshKey).toBeTruthy() // sanity check
  148. // Mock fs.promises.readFile
  149. const realReadFile = fs.promises.readFile
  150. jest.spyOn(fs.promises, 'readFile').mockImplementation(
  151. async (file: any, options: any): Promise<Buffer> => {
  152. const userKnownHostsPath = path.join(
  153. os.homedir(),
  154. '.ssh',
  155. 'known_hosts'
  156. )
  157. if (file === userKnownHostsPath) {
  158. return Buffer.from('some-domain.com ssh-rsa ABCDEF')
  159. }
  160. return await realReadFile(file, options)
  161. }
  162. )
  163. // Act
  164. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  165. await authHelper.configureAuth()
  166. // Assert known hosts
  167. const actualSshKnownHostsPath = await getActualSshKnownHostsPath()
  168. const actualSshKnownHostsContent = (
  169. await fs.promises.readFile(actualSshKnownHostsPath)
  170. ).toString()
  171. expect(actualSshKnownHostsContent).toMatch(
  172. /some-domain\.com ssh-rsa ABCDEF/
  173. )
  174. expect(actualSshKnownHostsContent).toMatch(/github\.com ssh-rsa AAAAB3N/)
  175. })
  176. const configureAuth_registersBasicCredentialAsSecret =
  177. 'configureAuth registers basic credential as secret'
  178. it(configureAuth_registersBasicCredentialAsSecret, async () => {
  179. // Arrange
  180. await setup(configureAuth_registersBasicCredentialAsSecret)
  181. expect(settings.authToken).toBeTruthy() // sanity check
  182. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  183. // Act
  184. await authHelper.configureAuth()
  185. // Assert secret
  186. const setSecretSpy = core.setSecret as jest.Mock<any, any>
  187. expect(setSecretSpy).toHaveBeenCalledTimes(1)
  188. const expectedSecret = Buffer.from(
  189. `x-access-token:${settings.authToken}`,
  190. 'utf8'
  191. ).toString('base64')
  192. expect(setSecretSpy).toHaveBeenCalledWith(expectedSecret)
  193. })
  194. const setsSshCommandEnvVarWhenPersistCredentialsFalse =
  195. 'sets SSH command env var when persist-credentials false'
  196. it(setsSshCommandEnvVarWhenPersistCredentialsFalse, async () => {
  197. if (!sshPath) {
  198. process.stdout.write(
  199. `Skipped test "${setsSshCommandEnvVarWhenPersistCredentialsFalse}". Executable 'ssh' not found in the PATH.\n`
  200. )
  201. return
  202. }
  203. // Arrange
  204. await setup(setsSshCommandEnvVarWhenPersistCredentialsFalse)
  205. settings.persistCredentials = false
  206. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  207. // Act
  208. await authHelper.configureAuth()
  209. // Assert git env var
  210. const actualKeyPath = await getActualSshKeyPath()
  211. const actualKnownHostsPath = await getActualSshKnownHostsPath()
  212. const expectedSshCommand = `"${sshPath}" -i "$RUNNER_TEMP/${path.basename(
  213. actualKeyPath
  214. )}" -o StrictHostKeyChecking=yes -o CheckHostIP=no -o "UserKnownHostsFile=$RUNNER_TEMP/${path.basename(
  215. actualKnownHostsPath
  216. )}"`
  217. expect(git.setEnvironmentVariable).toHaveBeenCalledWith(
  218. 'GIT_SSH_COMMAND',
  219. expectedSshCommand
  220. )
  221. // Asserty git config
  222. const gitConfigLines = (await fs.promises.readFile(localGitConfigPath))
  223. .toString()
  224. .split('\n')
  225. .filter(x => x)
  226. expect(gitConfigLines).toHaveLength(1)
  227. expect(gitConfigLines[0]).toMatch(/^http\./)
  228. })
  229. const configureAuth_setsSshCommandWhenPersistCredentialsTrue =
  230. 'sets SSH command when persist-credentials true'
  231. it(configureAuth_setsSshCommandWhenPersistCredentialsTrue, async () => {
  232. if (!sshPath) {
  233. process.stdout.write(
  234. `Skipped test "${configureAuth_setsSshCommandWhenPersistCredentialsTrue}". Executable 'ssh' not found in the PATH.\n`
  235. )
  236. return
  237. }
  238. // Arrange
  239. await setup(configureAuth_setsSshCommandWhenPersistCredentialsTrue)
  240. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  241. // Act
  242. await authHelper.configureAuth()
  243. // Assert git env var
  244. const actualKeyPath = await getActualSshKeyPath()
  245. const actualKnownHostsPath = await getActualSshKnownHostsPath()
  246. const expectedSshCommand = `"${sshPath}" -i "$RUNNER_TEMP/${path.basename(
  247. actualKeyPath
  248. )}" -o StrictHostKeyChecking=yes -o CheckHostIP=no -o "UserKnownHostsFile=$RUNNER_TEMP/${path.basename(
  249. actualKnownHostsPath
  250. )}"`
  251. expect(git.setEnvironmentVariable).toHaveBeenCalledWith(
  252. 'GIT_SSH_COMMAND',
  253. expectedSshCommand
  254. )
  255. // Asserty git config
  256. expect(git.config).toHaveBeenCalledWith(
  257. 'core.sshCommand',
  258. expectedSshCommand
  259. )
  260. })
  261. const configureAuth_writesExplicitKnownHosts = 'writes explicit known hosts'
  262. it(configureAuth_writesExplicitKnownHosts, async () => {
  263. if (!sshPath) {
  264. process.stdout.write(
  265. `Skipped test "${configureAuth_writesExplicitKnownHosts}". Executable 'ssh' not found in the PATH.\n`
  266. )
  267. return
  268. }
  269. // Arrange
  270. await setup(configureAuth_writesExplicitKnownHosts)
  271. expect(settings.sshKey).toBeTruthy() // sanity check
  272. settings.sshKnownHosts = 'my-custom-host.com ssh-rsa ABC123'
  273. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  274. // Act
  275. await authHelper.configureAuth()
  276. // Assert known hosts
  277. const actualSshKnownHostsPath = await getActualSshKnownHostsPath()
  278. const actualSshKnownHostsContent = (
  279. await fs.promises.readFile(actualSshKnownHostsPath)
  280. ).toString()
  281. expect(actualSshKnownHostsContent).toMatch(
  282. /my-custom-host\.com ssh-rsa ABC123/
  283. )
  284. expect(actualSshKnownHostsContent).toMatch(/github\.com ssh-rsa AAAAB3N/)
  285. })
  286. const configureAuth_writesSshKeyAndImplicitKnownHosts =
  287. 'writes SSH key and implicit known hosts'
  288. it(configureAuth_writesSshKeyAndImplicitKnownHosts, async () => {
  289. if (!sshPath) {
  290. process.stdout.write(
  291. `Skipped test "${configureAuth_writesSshKeyAndImplicitKnownHosts}". Executable 'ssh' not found in the PATH.\n`
  292. )
  293. return
  294. }
  295. // Arrange
  296. await setup(configureAuth_writesSshKeyAndImplicitKnownHosts)
  297. expect(settings.sshKey).toBeTruthy() // sanity check
  298. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  299. // Act
  300. await authHelper.configureAuth()
  301. // Assert SSH key
  302. const actualSshKeyPath = await getActualSshKeyPath()
  303. expect(actualSshKeyPath).toBeTruthy()
  304. const actualSshKeyContent = (
  305. await fs.promises.readFile(actualSshKeyPath)
  306. ).toString()
  307. expect(actualSshKeyContent).toBe(settings.sshKey + '\n')
  308. if (!isWindows) {
  309. // Assert read/write for user, not group or others.
  310. // Otherwise SSH client will error.
  311. expect((await fs.promises.stat(actualSshKeyPath)).mode & 0o777).toBe(
  312. 0o600
  313. )
  314. }
  315. // Assert known hosts
  316. const actualSshKnownHostsPath = await getActualSshKnownHostsPath()
  317. const actualSshKnownHostsContent = (
  318. await fs.promises.readFile(actualSshKnownHostsPath)
  319. ).toString()
  320. expect(actualSshKnownHostsContent).toMatch(/github\.com ssh-rsa AAAAB3N/)
  321. })
  322. const configureGlobalAuth_configuresUrlInsteadOfWhenSshKeyNotSet =
  323. 'configureGlobalAuth configures URL insteadOf when SSH key not set'
  324. it(configureGlobalAuth_configuresUrlInsteadOfWhenSshKeyNotSet, async () => {
  325. // Arrange
  326. await setup(configureGlobalAuth_configuresUrlInsteadOfWhenSshKeyNotSet)
  327. settings.sshKey = ''
  328. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  329. // Act
  330. await authHelper.configureAuth()
  331. await authHelper.configureGlobalAuth()
  332. // Assert temporary global config
  333. expect(git.env['HOME']).toBeTruthy()
  334. const configContent = (
  335. await fs.promises.readFile(path.join(git.env['HOME'], '.gitconfig'))
  336. ).toString()
  337. expect(
  338. configContent.indexOf(`url.https://github.com/.insteadOf git@github.com`)
  339. ).toBeGreaterThanOrEqual(0)
  340. })
  341. const configureGlobalAuth_copiesGlobalGitConfig =
  342. 'configureGlobalAuth copies global git config'
  343. it(configureGlobalAuth_copiesGlobalGitConfig, async () => {
  344. // Arrange
  345. await setup(configureGlobalAuth_copiesGlobalGitConfig)
  346. await fs.promises.writeFile(globalGitConfigPath, 'value-from-global-config')
  347. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  348. // Act
  349. await authHelper.configureAuth()
  350. await authHelper.configureGlobalAuth()
  351. // Assert original global config not altered
  352. let configContent = (
  353. await fs.promises.readFile(globalGitConfigPath)
  354. ).toString()
  355. expect(configContent).toBe('value-from-global-config')
  356. // Assert temporary global config
  357. expect(git.env['HOME']).toBeTruthy()
  358. const basicCredential = Buffer.from(
  359. `x-access-token:${settings.authToken}`,
  360. 'utf8'
  361. ).toString('base64')
  362. configContent = (
  363. await fs.promises.readFile(path.join(git.env['HOME'], '.gitconfig'))
  364. ).toString()
  365. expect(
  366. configContent.indexOf('value-from-global-config')
  367. ).toBeGreaterThanOrEqual(0)
  368. expect(
  369. configContent.indexOf(
  370. `http.https://github.com/.extraheader AUTHORIZATION: basic ${basicCredential}`
  371. )
  372. ).toBeGreaterThanOrEqual(0)
  373. })
  374. const configureGlobalAuth_createsNewGlobalGitConfigWhenGlobalDoesNotExist =
  375. 'configureGlobalAuth creates new git config when global does not exist'
  376. it(
  377. configureGlobalAuth_createsNewGlobalGitConfigWhenGlobalDoesNotExist,
  378. async () => {
  379. // Arrange
  380. await setup(
  381. configureGlobalAuth_createsNewGlobalGitConfigWhenGlobalDoesNotExist
  382. )
  383. await io.rmRF(globalGitConfigPath)
  384. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  385. // Act
  386. await authHelper.configureAuth()
  387. await authHelper.configureGlobalAuth()
  388. // Assert original global config not recreated
  389. try {
  390. await fs.promises.stat(globalGitConfigPath)
  391. throw new Error(
  392. `Did not expect file to exist: '${globalGitConfigPath}'`
  393. )
  394. } catch (err) {
  395. if ((err as any)?.code !== 'ENOENT') {
  396. throw err
  397. }
  398. }
  399. // Assert temporary global config
  400. expect(git.env['HOME']).toBeTruthy()
  401. const basicCredential = Buffer.from(
  402. `x-access-token:${settings.authToken}`,
  403. 'utf8'
  404. ).toString('base64')
  405. const configContent = (
  406. await fs.promises.readFile(path.join(git.env['HOME'], '.gitconfig'))
  407. ).toString()
  408. expect(
  409. configContent.indexOf(
  410. `http.https://github.com/.extraheader AUTHORIZATION: basic ${basicCredential}`
  411. )
  412. ).toBeGreaterThanOrEqual(0)
  413. }
  414. )
  415. const configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeyNotSet =
  416. 'configureSubmoduleAuth configures submodules when persist credentials false and SSH key not set'
  417. it(
  418. configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeyNotSet,
  419. async () => {
  420. // Arrange
  421. await setup(
  422. configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeyNotSet
  423. )
  424. settings.persistCredentials = false
  425. settings.sshKey = ''
  426. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  427. await authHelper.configureAuth()
  428. const mockSubmoduleForeach = git.submoduleForeach as jest.Mock<any, any>
  429. mockSubmoduleForeach.mockClear() // reset calls
  430. // Act
  431. await authHelper.configureSubmoduleAuth()
  432. // Assert
  433. expect(mockSubmoduleForeach).toBeCalledTimes(1)
  434. expect(mockSubmoduleForeach.mock.calls[0][0] as string).toMatch(
  435. /unset-all.*insteadOf/
  436. )
  437. }
  438. )
  439. const configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeySet =
  440. 'configureSubmoduleAuth configures submodules when persist credentials false and SSH key set'
  441. it(
  442. configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeySet,
  443. async () => {
  444. if (!sshPath) {
  445. process.stdout.write(
  446. `Skipped test "${configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeySet}". Executable 'ssh' not found in the PATH.\n`
  447. )
  448. return
  449. }
  450. // Arrange
  451. await setup(
  452. configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsFalseAndSshKeySet
  453. )
  454. settings.persistCredentials = false
  455. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  456. await authHelper.configureAuth()
  457. const mockSubmoduleForeach = git.submoduleForeach as jest.Mock<any, any>
  458. mockSubmoduleForeach.mockClear() // reset calls
  459. // Act
  460. await authHelper.configureSubmoduleAuth()
  461. // Assert
  462. expect(mockSubmoduleForeach).toHaveBeenCalledTimes(1)
  463. expect(mockSubmoduleForeach.mock.calls[0][0]).toMatch(
  464. /unset-all.*insteadOf/
  465. )
  466. }
  467. )
  468. const configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeyNotSet =
  469. 'configureSubmoduleAuth configures submodules when persist credentials true and SSH key not set'
  470. it(
  471. configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeyNotSet,
  472. async () => {
  473. // Arrange
  474. await setup(
  475. configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeyNotSet
  476. )
  477. settings.sshKey = ''
  478. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  479. await authHelper.configureAuth()
  480. const mockSubmoduleForeach = git.submoduleForeach as jest.Mock<any, any>
  481. mockSubmoduleForeach.mockClear() // reset calls
  482. // Act
  483. await authHelper.configureSubmoduleAuth()
  484. // Assert
  485. expect(mockSubmoduleForeach).toHaveBeenCalledTimes(4)
  486. expect(mockSubmoduleForeach.mock.calls[0][0]).toMatch(
  487. /unset-all.*insteadOf/
  488. )
  489. expect(mockSubmoduleForeach.mock.calls[1][0]).toMatch(/http.*extraheader/)
  490. expect(mockSubmoduleForeach.mock.calls[2][0]).toMatch(
  491. /url.*insteadOf.*git@github.com:/
  492. )
  493. expect(mockSubmoduleForeach.mock.calls[3][0]).toMatch(
  494. /url.*insteadOf.*org-123456@github.com:/
  495. )
  496. }
  497. )
  498. const configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeySet =
  499. 'configureSubmoduleAuth configures submodules when persist credentials true and SSH key set'
  500. it(
  501. configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeySet,
  502. async () => {
  503. if (!sshPath) {
  504. process.stdout.write(
  505. `Skipped test "${configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeySet}". Executable 'ssh' not found in the PATH.\n`
  506. )
  507. return
  508. }
  509. // Arrange
  510. await setup(
  511. configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeySet
  512. )
  513. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  514. await authHelper.configureAuth()
  515. const mockSubmoduleForeach = git.submoduleForeach as jest.Mock<any, any>
  516. mockSubmoduleForeach.mockClear() // reset calls
  517. // Act
  518. await authHelper.configureSubmoduleAuth()
  519. // Assert
  520. expect(mockSubmoduleForeach).toHaveBeenCalledTimes(3)
  521. expect(mockSubmoduleForeach.mock.calls[0][0]).toMatch(
  522. /unset-all.*insteadOf/
  523. )
  524. expect(mockSubmoduleForeach.mock.calls[1][0]).toMatch(/http.*extraheader/)
  525. expect(mockSubmoduleForeach.mock.calls[2][0]).toMatch(/core\.sshCommand/)
  526. }
  527. )
  528. const removeAuth_removesSshCommand = 'removeAuth removes SSH command'
  529. it(removeAuth_removesSshCommand, async () => {
  530. if (!sshPath) {
  531. process.stdout.write(
  532. `Skipped test "${removeAuth_removesSshCommand}". Executable 'ssh' not found in the PATH.\n`
  533. )
  534. return
  535. }
  536. // Arrange
  537. await setup(removeAuth_removesSshCommand)
  538. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  539. await authHelper.configureAuth()
  540. let gitConfigContent = (
  541. await fs.promises.readFile(localGitConfigPath)
  542. ).toString()
  543. expect(gitConfigContent.indexOf('core.sshCommand')).toBeGreaterThanOrEqual(
  544. 0
  545. ) // sanity check
  546. const actualKeyPath = await getActualSshKeyPath()
  547. expect(actualKeyPath).toBeTruthy()
  548. await fs.promises.stat(actualKeyPath)
  549. const actualKnownHostsPath = await getActualSshKnownHostsPath()
  550. expect(actualKnownHostsPath).toBeTruthy()
  551. await fs.promises.stat(actualKnownHostsPath)
  552. // Act
  553. await authHelper.removeAuth()
  554. // Assert git config
  555. gitConfigContent = (
  556. await fs.promises.readFile(localGitConfigPath)
  557. ).toString()
  558. expect(gitConfigContent.indexOf('core.sshCommand')).toBeLessThan(0)
  559. // Assert SSH key file
  560. try {
  561. await fs.promises.stat(actualKeyPath)
  562. throw new Error('SSH key should have been deleted')
  563. } catch (err) {
  564. if ((err as any)?.code !== 'ENOENT') {
  565. throw err
  566. }
  567. }
  568. // Assert known hosts file
  569. try {
  570. await fs.promises.stat(actualKnownHostsPath)
  571. throw new Error('SSH known hosts should have been deleted')
  572. } catch (err) {
  573. if ((err as any)?.code !== 'ENOENT') {
  574. throw err
  575. }
  576. }
  577. })
  578. const removeAuth_removesToken = 'removeAuth removes token'
  579. it(removeAuth_removesToken, async () => {
  580. // Arrange
  581. await setup(removeAuth_removesToken)
  582. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  583. await authHelper.configureAuth()
  584. let gitConfigContent = (
  585. await fs.promises.readFile(localGitConfigPath)
  586. ).toString()
  587. expect(gitConfigContent.indexOf('http.')).toBeGreaterThanOrEqual(0) // sanity check
  588. // Act
  589. await authHelper.removeAuth()
  590. // Assert git config
  591. gitConfigContent = (
  592. await fs.promises.readFile(localGitConfigPath)
  593. ).toString()
  594. expect(gitConfigContent.indexOf('http.')).toBeLessThan(0)
  595. })
  596. const removeGlobalConfig_removesOverride =
  597. 'removeGlobalConfig removes override'
  598. it(removeGlobalConfig_removesOverride, async () => {
  599. // Arrange
  600. await setup(removeGlobalConfig_removesOverride)
  601. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  602. await authHelper.configureAuth()
  603. await authHelper.configureGlobalAuth()
  604. const homeOverride = git.env['HOME'] // Sanity check
  605. expect(homeOverride).toBeTruthy()
  606. await fs.promises.stat(path.join(git.env['HOME'], '.gitconfig'))
  607. // Act
  608. await authHelper.removeGlobalConfig()
  609. // Assert
  610. expect(git.env['HOME']).toBeUndefined()
  611. try {
  612. await fs.promises.stat(homeOverride)
  613. throw new Error(`Should have been deleted '${homeOverride}'`)
  614. } catch (err) {
  615. if ((err as any)?.code !== 'ENOENT') {
  616. throw err
  617. }
  618. }
  619. })
  620. })
  621. async function setup(testName: string): Promise<void> {
  622. testName = testName.replace(/[^a-zA-Z0-9_]+/g, '-')
  623. // Directories
  624. workspace = path.join(testWorkspace, testName, 'workspace')
  625. runnerTemp = path.join(testWorkspace, testName, 'runner-temp')
  626. tempHomedir = path.join(testWorkspace, testName, 'home-dir')
  627. await fs.promises.mkdir(workspace, {recursive: true})
  628. await fs.promises.mkdir(runnerTemp, {recursive: true})
  629. await fs.promises.mkdir(tempHomedir, {recursive: true})
  630. process.env['RUNNER_TEMP'] = runnerTemp
  631. process.env['HOME'] = tempHomedir
  632. // Create git config
  633. globalGitConfigPath = path.join(tempHomedir, '.gitconfig')
  634. await fs.promises.writeFile(globalGitConfigPath, '')
  635. localGitConfigPath = path.join(workspace, '.git', 'config')
  636. await fs.promises.mkdir(path.dirname(localGitConfigPath), {recursive: true})
  637. await fs.promises.writeFile(localGitConfigPath, '')
  638. git = {
  639. branchDelete: jest.fn(),
  640. branchExists: jest.fn(),
  641. branchList: jest.fn(),
  642. sparseCheckout: jest.fn(),
  643. sparseCheckoutNonConeMode: jest.fn(),
  644. checkout: jest.fn(),
  645. checkoutDetach: jest.fn(),
  646. config: jest.fn(
  647. async (key: string, value: string, globalConfig?: boolean) => {
  648. const configPath = globalConfig
  649. ? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
  650. : localGitConfigPath
  651. await fs.promises.appendFile(configPath, `\n${key} ${value}`)
  652. }
  653. ),
  654. configExists: jest.fn(
  655. async (key: string, globalConfig?: boolean): Promise<boolean> => {
  656. const configPath = globalConfig
  657. ? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
  658. : localGitConfigPath
  659. const content = await fs.promises.readFile(configPath)
  660. const lines = content
  661. .toString()
  662. .split('\n')
  663. .filter(x => x)
  664. return lines.some(x => x.startsWith(key))
  665. }
  666. ),
  667. env: {},
  668. fetch: jest.fn(),
  669. getDefaultBranch: jest.fn(),
  670. getWorkingDirectory: jest.fn(() => workspace),
  671. init: jest.fn(),
  672. isDetached: jest.fn(),
  673. lfsFetch: jest.fn(),
  674. lfsInstall: jest.fn(),
  675. log1: jest.fn(),
  676. remoteAdd: jest.fn(),
  677. removeEnvironmentVariable: jest.fn((name: string) => delete git.env[name]),
  678. revParse: jest.fn(),
  679. setEnvironmentVariable: jest.fn((name: string, value: string) => {
  680. git.env[name] = value
  681. }),
  682. shaExists: jest.fn(),
  683. submoduleForeach: jest.fn(async () => {
  684. return ''
  685. }),
  686. submoduleSync: jest.fn(),
  687. submoduleStatus: jest.fn(async () => {
  688. return true
  689. }),
  690. submoduleUpdate: jest.fn(),
  691. tagExists: jest.fn(),
  692. tryClean: jest.fn(),
  693. tryConfigUnset: jest.fn(
  694. async (key: string, globalConfig?: boolean): Promise<boolean> => {
  695. const configPath = globalConfig
  696. ? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
  697. : localGitConfigPath
  698. let content = await fs.promises.readFile(configPath)
  699. let lines = content
  700. .toString()
  701. .split('\n')
  702. .filter(x => x)
  703. .filter(x => !x.startsWith(key))
  704. await fs.promises.writeFile(configPath, lines.join('\n'))
  705. return true
  706. }
  707. ),
  708. tryDisableAutomaticGarbageCollection: jest.fn(),
  709. tryGetFetchUrl: jest.fn(),
  710. tryReset: jest.fn()
  711. }
  712. settings = {
  713. authToken: 'some auth token',
  714. clean: true,
  715. commit: '',
  716. filter: undefined,
  717. sparseCheckout: [],
  718. sparseCheckoutConeMode: true,
  719. fetchDepth: 1,
  720. fetchTags: false,
  721. showProgress: true,
  722. lfs: false,
  723. submodules: false,
  724. nestedSubmodules: false,
  725. persistCredentials: true,
  726. ref: 'refs/heads/main',
  727. repositoryName: 'my-repo',
  728. repositoryOwner: 'my-org',
  729. repositoryPath: '',
  730. sshKey: sshPath ? 'some ssh private key' : '',
  731. sshKnownHosts: '',
  732. sshStrict: true,
  733. workflowOrganizationId: 123456,
  734. setSafeDirectory: true,
  735. githubServerUrl: githubServerUrl
  736. }
  737. }
  738. async function getActualSshKeyPath(): Promise<string> {
  739. let actualTempFiles = (await fs.promises.readdir(runnerTemp))
  740. .sort()
  741. .map(x => path.join(runnerTemp, x))
  742. if (actualTempFiles.length === 0) {
  743. return ''
  744. }
  745. expect(actualTempFiles).toHaveLength(2)
  746. expect(actualTempFiles[0].endsWith('_known_hosts')).toBeFalsy()
  747. return actualTempFiles[0]
  748. }
  749. async function getActualSshKnownHostsPath(): Promise<string> {
  750. let actualTempFiles = (await fs.promises.readdir(runnerTemp))
  751. .sort()
  752. .map(x => path.join(runnerTemp, x))
  753. if (actualTempFiles.length === 0) {
  754. return ''
  755. }
  756. expect(actualTempFiles).toHaveLength(2)
  757. expect(actualTempFiles[1].endsWith('_known_hosts')).toBeTruthy()
  758. expect(actualTempFiles[1].startsWith(actualTempFiles[0])).toBeTruthy()
  759. return actualTempFiles[1]
  760. }