git-auth-helper.test.ts 26 KB

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