From 449cddaa56ad3aeebec185b69965e7a75cc60a6a Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 23 Feb 2021 09:01:13 +0100 Subject: [PATCH 1/4] chore: reworking authorization for bitbucket so we can use either appPassword or token --- .../scaffolder/stages/publish/bitbucket.ts | 30 ++++++++++++++----- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts index 373920c005..eb89a4defc 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts @@ -98,10 +98,6 @@ export class BitbucketPublisher implements PublisherBase { const { project, name, description } = opts; let response: Response; - const buffer = Buffer.from( - `${this.config.username}:${this.config.appPassword}`, - 'utf8', - ); const options: RequestInit = { method: 'POST', @@ -111,7 +107,7 @@ export class BitbucketPublisher implements PublisherBase { is_private: this.config.repoVisibility === 'private', }), headers: { - Authorization: `Basic ${buffer.toString('base64')}`, + Authorization: this.getAuthorizationHeader(), 'Content-Type': 'application/json', }, }; @@ -132,13 +128,32 @@ export class BitbucketPublisher implements PublisherBase { } } - // TODO use the urlReader to get the defautl branch + // TODO use the urlReader to get the default branch const catalogInfoUrl = `${r.links.html.href}/src/master/catalog-info.yaml`; return { remoteUrl, catalogInfoUrl }; } throw new Error(`Not a valid response code ${await response.text()}`); } + private getAuthorizationHeader(): string { + if (this.config.username && this.config.appPassword) { + const buffer = Buffer.from( + `${this.config.username}:${this.config.appPassword}`, + 'utf8', + ); + + return `Basic ${buffer.toString('base64')}`; + } + + if (this.config.token) { + return `Bearer ${this.config.token}`; + } + + throw new Error( + `Authorization has not been provided for Bitbucket. Please add username + appPassword or token to the integration config `, + ); + } + private async createBitbucketServerRepository(opts: { project: string; name: string; @@ -155,10 +170,11 @@ export class BitbucketPublisher implements PublisherBase { is_private: this.config.repoVisibility === 'private', }), headers: { - Authorization: `Bearer ${this.config.token}`, + Authorization: this.getAuthorizationHeader(), 'Content-Type': 'application/json', }, }; + try { response = await fetch( `https://${this.config.host}/rest/api/1.0/projects/${project}/repos`, From 599aeb70d51042ab62a9e6cf51580eccd1b82ff5 Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 23 Feb 2021 09:06:19 +0100 Subject: [PATCH 2/4] chore: updating logging --- .../src/scaffolder/stages/publish/bitbucket.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts index eb89a4defc..102a3a7d02 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/bitbucket.ts @@ -150,7 +150,7 @@ export class BitbucketPublisher implements PublisherBase { } throw new Error( - `Authorization has not been provided for Bitbucket. Please add username + appPassword or token to the integration config `, + `Authorization has not been provided for Bitbucket. Please add either username + appPassword or token to the Integrations config`, ); } From f31b76b4443a567bd4163c0b984d831855b57738 Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 23 Feb 2021 09:11:31 +0100 Subject: [PATCH 3/4] chore(changeset): added changeset for scaffolder-backend --- .changeset/hungry-frogs-flow.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/hungry-frogs-flow.md diff --git a/.changeset/hungry-frogs-flow.md b/.changeset/hungry-frogs-flow.md new file mode 100644 index 0000000000..e41fe71627 --- /dev/null +++ b/.changeset/hungry-frogs-flow.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +Consider both authentication methods for both onprem and cloud bitbucket From 315f909bc3990ed45062de3a51d7d316f0af35f7 Mon Sep 17 00:00:00 2001 From: blam Date: Tue, 23 Feb 2021 09:43:16 +0100 Subject: [PATCH 4/4] chore(changeset): hmm spelling... --- .changeset/hungry-frogs-flow.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/hungry-frogs-flow.md b/.changeset/hungry-frogs-flow.md index e41fe71627..91a6ab0892 100644 --- a/.changeset/hungry-frogs-flow.md +++ b/.changeset/hungry-frogs-flow.md @@ -2,4 +2,4 @@ '@backstage/plugin-scaffolder-backend': patch --- -Consider both authentication methods for both onprem and cloud bitbucket +Consider both authentication methods for both `onprem` and `cloud` BitBucket