git-auth-helper.test.ts 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809
  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 as any)?.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(4)
  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(
  463. /url.*insteadOf.*git@github.com:/
  464. )
  465. expect(mockSubmoduleForeach.mock.calls[3][0]).toMatch(
  466. /url.*insteadOf.*org-123456@github.com:/
  467. )
  468. }
  469. )
  470. const configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeySet =
  471. 'configureSubmoduleAuth configures submodules when persist credentials true and SSH key set'
  472. it(
  473. configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeySet,
  474. async () => {
  475. if (!sshPath) {
  476. process.stdout.write(
  477. `Skipped test "${configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeySet}". Executable 'ssh' not found in the PATH.\n`
  478. )
  479. return
  480. }
  481. // Arrange
  482. await setup(
  483. configureSubmoduleAuth_configuresSubmodulesWhenPersistCredentialsTrueAndSshKeySet
  484. )
  485. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  486. await authHelper.configureAuth()
  487. const mockSubmoduleForeach = git.submoduleForeach as jest.Mock<any, any>
  488. mockSubmoduleForeach.mockClear() // reset calls
  489. // Act
  490. await authHelper.configureSubmoduleAuth()
  491. // Assert
  492. expect(mockSubmoduleForeach).toHaveBeenCalledTimes(3)
  493. expect(mockSubmoduleForeach.mock.calls[0][0]).toMatch(
  494. /unset-all.*insteadOf/
  495. )
  496. expect(mockSubmoduleForeach.mock.calls[1][0]).toMatch(/http.*extraheader/)
  497. expect(mockSubmoduleForeach.mock.calls[2][0]).toMatch(/core\.sshCommand/)
  498. }
  499. )
  500. const removeAuth_removesSshCommand = 'removeAuth removes SSH command'
  501. it(removeAuth_removesSshCommand, async () => {
  502. if (!sshPath) {
  503. process.stdout.write(
  504. `Skipped test "${removeAuth_removesSshCommand}". Executable 'ssh' not found in the PATH.\n`
  505. )
  506. return
  507. }
  508. // Arrange
  509. await setup(removeAuth_removesSshCommand)
  510. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  511. await authHelper.configureAuth()
  512. let gitConfigContent = (
  513. await fs.promises.readFile(localGitConfigPath)
  514. ).toString()
  515. expect(gitConfigContent.indexOf('core.sshCommand')).toBeGreaterThanOrEqual(
  516. 0
  517. ) // sanity check
  518. const actualKeyPath = await getActualSshKeyPath()
  519. expect(actualKeyPath).toBeTruthy()
  520. await fs.promises.stat(actualKeyPath)
  521. const actualKnownHostsPath = await getActualSshKnownHostsPath()
  522. expect(actualKnownHostsPath).toBeTruthy()
  523. await fs.promises.stat(actualKnownHostsPath)
  524. // Act
  525. await authHelper.removeAuth()
  526. // Assert git config
  527. gitConfigContent = (
  528. await fs.promises.readFile(localGitConfigPath)
  529. ).toString()
  530. expect(gitConfigContent.indexOf('core.sshCommand')).toBeLessThan(0)
  531. // Assert SSH key file
  532. try {
  533. await fs.promises.stat(actualKeyPath)
  534. throw new Error('SSH key should have been deleted')
  535. } catch (err) {
  536. if ((err as any)?.code !== 'ENOENT') {
  537. throw err
  538. }
  539. }
  540. // Assert known hosts file
  541. try {
  542. await fs.promises.stat(actualKnownHostsPath)
  543. throw new Error('SSH known hosts should have been deleted')
  544. } catch (err) {
  545. if ((err as any)?.code !== 'ENOENT') {
  546. throw err
  547. }
  548. }
  549. })
  550. const removeAuth_removesToken = 'removeAuth removes token'
  551. it(removeAuth_removesToken, async () => {
  552. // Arrange
  553. await setup(removeAuth_removesToken)
  554. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  555. await authHelper.configureAuth()
  556. let gitConfigContent = (
  557. await fs.promises.readFile(localGitConfigPath)
  558. ).toString()
  559. expect(gitConfigContent.indexOf('http.')).toBeGreaterThanOrEqual(0) // sanity check
  560. // Act
  561. await authHelper.removeAuth()
  562. // Assert git config
  563. gitConfigContent = (
  564. await fs.promises.readFile(localGitConfigPath)
  565. ).toString()
  566. expect(gitConfigContent.indexOf('http.')).toBeLessThan(0)
  567. })
  568. const removeGlobalConfig_removesOverride =
  569. 'removeGlobalConfig removes override'
  570. it(removeGlobalConfig_removesOverride, async () => {
  571. // Arrange
  572. await setup(removeGlobalConfig_removesOverride)
  573. const authHelper = gitAuthHelper.createAuthHelper(git, settings)
  574. await authHelper.configureAuth()
  575. await authHelper.configureGlobalAuth()
  576. const homeOverride = git.env['HOME'] // Sanity check
  577. expect(homeOverride).toBeTruthy()
  578. await fs.promises.stat(path.join(git.env['HOME'], '.gitconfig'))
  579. // Act
  580. await authHelper.removeGlobalConfig()
  581. // Assert
  582. expect(git.env['HOME']).toBeUndefined()
  583. try {
  584. await fs.promises.stat(homeOverride)
  585. throw new Error(`Should have been deleted '${homeOverride}'`)
  586. } catch (err) {
  587. if ((err as any)?.code !== 'ENOENT') {
  588. throw err
  589. }
  590. }
  591. })
  592. })
  593. async function setup(testName: string): Promise<void> {
  594. testName = testName.replace(/[^a-zA-Z0-9_]+/g, '-')
  595. // Directories
  596. workspace = path.join(testWorkspace, testName, 'workspace')
  597. runnerTemp = path.join(testWorkspace, testName, 'runner-temp')
  598. tempHomedir = path.join(testWorkspace, testName, 'home-dir')
  599. await fs.promises.mkdir(workspace, {recursive: true})
  600. await fs.promises.mkdir(runnerTemp, {recursive: true})
  601. await fs.promises.mkdir(tempHomedir, {recursive: true})
  602. process.env['RUNNER_TEMP'] = runnerTemp
  603. process.env['HOME'] = tempHomedir
  604. // Create git config
  605. globalGitConfigPath = path.join(tempHomedir, '.gitconfig')
  606. await fs.promises.writeFile(globalGitConfigPath, '')
  607. localGitConfigPath = path.join(workspace, '.git', 'config')
  608. await fs.promises.mkdir(path.dirname(localGitConfigPath), {recursive: true})
  609. await fs.promises.writeFile(localGitConfigPath, '')
  610. git = {
  611. branchDelete: jest.fn(),
  612. branchExists: jest.fn(),
  613. branchList: jest.fn(),
  614. checkout: jest.fn(),
  615. checkoutDetach: jest.fn(),
  616. config: jest.fn(
  617. async (key: string, value: string, globalConfig?: boolean) => {
  618. const configPath = globalConfig
  619. ? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
  620. : localGitConfigPath
  621. await fs.promises.appendFile(configPath, `\n${key} ${value}`)
  622. }
  623. ),
  624. configExists: jest.fn(
  625. async (key: string, globalConfig?: boolean): Promise<boolean> => {
  626. const configPath = globalConfig
  627. ? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
  628. : localGitConfigPath
  629. const content = await fs.promises.readFile(configPath)
  630. const lines = content
  631. .toString()
  632. .split('\n')
  633. .filter(x => x)
  634. return lines.some(x => x.startsWith(key))
  635. }
  636. ),
  637. env: {},
  638. fetch: jest.fn(),
  639. getDefaultBranch: jest.fn(),
  640. getWorkingDirectory: jest.fn(() => workspace),
  641. init: jest.fn(),
  642. isDetached: jest.fn(),
  643. lfsFetch: jest.fn(),
  644. lfsInstall: jest.fn(),
  645. log1: jest.fn(),
  646. remoteAdd: jest.fn(),
  647. removeEnvironmentVariable: jest.fn((name: string) => delete git.env[name]),
  648. revParse: jest.fn(),
  649. setEnvironmentVariable: jest.fn((name: string, value: string) => {
  650. git.env[name] = value
  651. }),
  652. shaExists: jest.fn(),
  653. submoduleForeach: jest.fn(async () => {
  654. return ''
  655. }),
  656. submoduleSync: jest.fn(),
  657. submoduleUpdate: jest.fn(),
  658. tagExists: jest.fn(),
  659. tryClean: jest.fn(),
  660. tryConfigUnset: jest.fn(
  661. async (key: string, globalConfig?: boolean): Promise<boolean> => {
  662. const configPath = globalConfig
  663. ? path.join(git.env['HOME'] || tempHomedir, '.gitconfig')
  664. : localGitConfigPath
  665. let content = await fs.promises.readFile(configPath)
  666. let lines = content
  667. .toString()
  668. .split('\n')
  669. .filter(x => x)
  670. .filter(x => !x.startsWith(key))
  671. await fs.promises.writeFile(configPath, lines.join('\n'))
  672. return true
  673. }
  674. ),
  675. tryDisableAutomaticGarbageCollection: jest.fn(),
  676. tryGetFetchUrl: jest.fn(),
  677. tryReset: jest.fn()
  678. }
  679. settings = {
  680. authToken: 'some auth token',
  681. clean: true,
  682. commit: '',
  683. fetchDepth: 1,
  684. lfs: false,
  685. submodules: false,
  686. nestedSubmodules: false,
  687. persistCredentials: true,
  688. ref: 'refs/heads/main',
  689. repositoryName: 'my-repo',
  690. repositoryOwner: 'my-org',
  691. repositoryPath: '',
  692. sshKey: sshPath ? 'some ssh private key' : '',
  693. sshKnownHosts: '',
  694. sshStrict: true,
  695. workflowOrganizationId: 123456
  696. }
  697. }
  698. async function getActualSshKeyPath(): Promise<string> {
  699. let actualTempFiles = (await fs.promises.readdir(runnerTemp))
  700. .sort()
  701. .map(x => path.join(runnerTemp, x))
  702. if (actualTempFiles.length === 0) {
  703. return ''
  704. }
  705. expect(actualTempFiles).toHaveLength(2)
  706. expect(actualTempFiles[0].endsWith('_known_hosts')).toBeFalsy()
  707. return actualTempFiles[0]
  708. }
  709. async function getActualSshKnownHostsPath(): Promise<string> {
  710. let actualTempFiles = (await fs.promises.readdir(runnerTemp))
  711. .sort()
  712. .map(x => path.join(runnerTemp, x))
  713. if (actualTempFiles.length === 0) {
  714. return ''
  715. }
  716. expect(actualTempFiles).toHaveLength(2)
  717. expect(actualTempFiles[1].endsWith('_known_hosts')).toBeTruthy()
  718. expect(actualTempFiles[1].startsWith(actualTempFiles[0])).toBeTruthy()
  719. return actualTempFiles[1]
  720. }