From 5d12393bc9a04c57e277cc7c05892a7ec0601057 Mon Sep 17 00:00:00 2001 From: "Francisco.Ribas" Date: Sun, 16 Apr 2023 11:03:58 -0300 Subject: [PATCH 1/6] Fix github app installation credentials Signed-off-by: Francisco.Ribas --- ...eInstanceGithubCredentialsProvider.test.ts | 60 +++++++++++++++++++ ...SingleInstanceGithubCredentialsProvider.ts | 6 +- 2 files changed, 64 insertions(+), 2 deletions(-) diff --git a/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.test.ts b/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.test.ts index a1544fbc68..ec07d1b465 100644 --- a/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.test.ts +++ b/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.test.ts @@ -133,6 +133,66 @@ describe('SingleInstanceGithubCredentialsProvider tests', () => { expect(token).toEqual('secret_token'); }); + it('creates tokens for an organization with multiple configured apps', async () => { + const multipleGithubApps = SingleInstanceGithubCredentialsProvider.create({ + host: 'github.com', + apps: [ + { + appId: 1, + privateKey: 'privateKey', + webhookSecret: '123', + clientId: 'CLIENT_ID', + clientSecret: 'CLIENT_SECRET', + allowedInstallationOwners: ['not-allowed'], + }, + { + appId: 2, + privateKey: 'privateKey_2', + webhookSecret: '456', + clientId: 'CLIENT_ID_2', + clientSecret: 'CLIENT_SECRET_2', + allowedInstallationOwners: ['allowed'], + }, + ], + }); + + octokit.apps.listInstallations.mockResolvedValue({ + headers: { + etag: '123', + }, + data: [ + { + id: 2, + repository_selection: 'all', + account: { + login: 'allowed', + }, + }, + { + id: 2, + repository_selection: 'all', + account: { + login: 'not-allowed', + }, + }, + ], + } as RestEndpointMethodTypes['apps']['listInstallations']['response']); + + octokit.apps.createInstallationAccessToken.mockResolvedValue({ + data: { + expires_at: DateTime.local().plus({ hours: 1 }).toString(), + token: 'secret_token', + }, + } as RestEndpointMethodTypes['apps']['createInstallationAccessToken']['response']); + + const { token, headers } = await multipleGithubApps.getCredentials({ + url: 'https://github.com/allowed', + }); + + expect(headers).toEqual({ Authorization: 'Bearer secret_token' }); + expect(token).toEqual('secret_token'); + }); + it('does not return a token where the organisation is not in the allowedInstallationsList', async () => { github = SingleInstanceGithubCredentialsProvider.create({ host: 'github.com', diff --git a/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.ts b/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.ts index d42ad4597d..402a62d97c 100644 --- a/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.ts +++ b/packages/integration/src/github/SingleInstanceGithubCredentialsProvider.ts @@ -232,13 +232,15 @@ export class GithubAppCredentialsMux { ), ); - const result = results.find(resultItem => resultItem.credentials); + const result = results.find( + resultItem => resultItem.credentials?.accessToken, + ); if (result) { return result.credentials!.accessToken; } const errors = results.map(r => r.error); - const notNotFoundError = errors.find(err => err.name !== 'NotFoundError'); + const notNotFoundError = errors.find(err => err?.name !== 'NotFoundError'); if (notNotFoundError) { throw notNotFoundError; } From b026275bcc8b86dc59049f18b8f7cf7edbbb8612 Mon Sep 17 00:00:00 2001 From: "Francisco.Ribas" Date: Sun, 16 Apr 2023 11:07:15 -0300 Subject: [PATCH 2/6] Changeset Signed-off-by: Francisco.Ribas --- .changeset/good-lions-approve.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/good-lions-approve.md diff --git a/.changeset/good-lions-approve.md b/.changeset/good-lions-approve.md new file mode 100644 index 0000000000..90d56d00d4 --- /dev/null +++ b/.changeset/good-lions-approve.md @@ -0,0 +1,5 @@ +--- +'@backstage/integration': patch +--- + +Bug fix for multiples github apps integration From 614ac4460ecbcae4037a2c9104d2d17ce6025dd5 Mon Sep 17 00:00:00 2001 From: "Francisco.Ribas" Date: Sun, 16 Apr 2023 23:56:19 -0300 Subject: [PATCH 3/6] Bump integration pckage Signed-off-by: Francisco.Ribas --- .changeset/sweet-chairs-draw.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/sweet-chairs-draw.md diff --git a/.changeset/sweet-chairs-draw.md b/.changeset/sweet-chairs-draw.md new file mode 100644 index 0000000000..e01e84a9e6 --- /dev/null +++ b/.changeset/sweet-chairs-draw.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-catalog-backend-module-github': patch +--- + +Bump integration package From 97ce19f39434e41accb9798a4ae555e1d9a51247 Mon Sep 17 00:00:00 2001 From: "Francisco.Ribas" Date: Mon, 17 Apr 2023 14:49:15 -0300 Subject: [PATCH 4/6] Update package integration Signed-off-by: Francisco.Ribas --- .changeset/sweet-chairs-draw.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/sweet-chairs-draw.md b/.changeset/sweet-chairs-draw.md index e01e84a9e6..0dea9ce5d5 100644 --- a/.changeset/sweet-chairs-draw.md +++ b/.changeset/sweet-chairs-draw.md @@ -2,4 +2,4 @@ '@backstage/plugin-catalog-backend-module-github': patch --- -Bump integration package +Bump integration package to solve bug on multiple Github Apps credentials. From 8eff2cbb306c97373c00bd7b51e360ad513c7c47 Mon Sep 17 00:00:00 2001 From: "Francisco.Ribas" Date: Mon, 17 Apr 2023 18:29:59 -0300 Subject: [PATCH 5/6] Deleting changeset file included by mistake Signed-off-by: Francisco.Ribas --- .changeset/sweet-chairs-draw.md | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .changeset/sweet-chairs-draw.md diff --git a/.changeset/sweet-chairs-draw.md b/.changeset/sweet-chairs-draw.md deleted file mode 100644 index 0dea9ce5d5..0000000000 --- a/.changeset/sweet-chairs-draw.md +++ /dev/null @@ -1,5 +0,0 @@ ---- -'@backstage/plugin-catalog-backend-module-github': patch ---- - -Bump integration package to solve bug on multiple Github Apps credentials. From a5909f9e9b389db59180e14ea629d2a7452f4b95 Mon Sep 17 00:00:00 2001 From: Francisco Ribas Date: Tue, 18 Apr 2023 08:58:30 -0300 Subject: [PATCH 6/6] Update .changeset/good-lions-approve.md Co-authored-by: Patrik Oldsberg Signed-off-by: Francisco Ribas --- .changeset/good-lions-approve.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/good-lions-approve.md b/.changeset/good-lions-approve.md index 90d56d00d4..f4065a6ab3 100644 --- a/.changeset/good-lions-approve.md +++ b/.changeset/good-lions-approve.md @@ -2,4 +2,4 @@ '@backstage/integration': patch --- -Bug fix for multiples github apps integration +Fixed a bug where the wrong credentials would be selected when using multiple GitHub app integrations.