From 4440dd74e0ddc4848fb0fa88e4d689a2661254a3 Mon Sep 17 00:00:00 2001 From: Debanitrkl Date: Tue, 6 Aug 2024 14:05:22 +0530 Subject: [PATCH 1/2] Added bearer prefix to ci/cd plugin token --- .../src/hooks/useGetExecutionsList.ts | 12 +++++----- .../src/hooks/useGetLicenseWithAuth.ts | 11 +++++---- .../src/hooks/useMutateRunPipeline.ts | 23 ++++++++++++------- 3 files changed, 27 insertions(+), 19 deletions(-) diff --git a/plugins/harness-ci-cd/src/hooks/useGetExecutionsList.ts b/plugins/harness-ci-cd/src/hooks/useGetExecutionsList.ts index 74aabb1..53305e0 100644 --- a/plugins/harness-ci-cd/src/hooks/useGetExecutionsList.ts +++ b/plugins/harness-ci-cd/src/hooks/useGetExecutionsList.ts @@ -46,12 +46,12 @@ const useGetExecutionsList = ({ const { token: apiToken } = await identityApi.getCredentials(); const token = getSecureHarnessKey('token') || apiToken; - const value = token ? `${token}` : ''; - - const headers = new Headers({ - 'content-type': 'application/json', - Authorization: value, - }); + const value = token && token === apiToken ? `Bearer ${token}` : token; + + const headers = new Headers(); + if (value) { + headers.append('Authorization', value); + } let body; const identifiers: string[] = []; diff --git a/plugins/harness-ci-cd/src/hooks/useGetLicenseWithAuth.ts b/plugins/harness-ci-cd/src/hooks/useGetLicenseWithAuth.ts index 0c649fb..df73d4d 100644 --- a/plugins/harness-ci-cd/src/hooks/useGetLicenseWithAuth.ts +++ b/plugins/harness-ci-cd/src/hooks/useGetLicenseWithAuth.ts @@ -18,11 +18,12 @@ const useGetLicenseWithAuth = ({ useAsyncRetry(async () => { const { token: apiToken } = await identityApi.getCredentials(); const token = getSecureHarnessKey('token') || apiToken; - const value = token ? `${token}` : ''; - - const headers = new Headers({ - Authorization: value, - }); + const value = token && token === apiToken ? `Bearer ${token}` : token; + + const headers = new Headers(); + if (value) { + headers.append('Authorization', value); + } const response = await fetch( `${await backendBaseUrl}/harness/${env}/gateway/ng/api/licenses/account?routingId=${accountId}&accountIdentifier=${accountId}`, diff --git a/plugins/harness-ci-cd/src/hooks/useMutateRunPipeline.ts b/plugins/harness-ci-cd/src/hooks/useMutateRunPipeline.ts index 1503468..7e20fc6 100644 --- a/plugins/harness-ci-cd/src/hooks/useMutateRunPipeline.ts +++ b/plugins/harness-ci-cd/src/hooks/useMutateRunPipeline.ts @@ -15,10 +15,12 @@ const useMutateRunPipeline = ({ const runPipeline = async (row: TableData, query1: string) => { const { token: apiToken } = await identityApi.getCredentials(); const token = getSecureHarnessKey('token') || apiToken; - const value = token ? `${token}` : ''; - const headers = new Headers({ - Authorization: value, - }); + const value = token && token === apiToken ? `Bearer ${token}` : token; + + const headers = new Headers(); + if (value) { + headers.append('Authorization', value); + } const response = await fetch( `${await backendBaseUrl}/harness/${env}/gateway/pipeline/api/pipelines/execution/${ @@ -30,16 +32,21 @@ const useMutateRunPipeline = ({ ); const data = await response.text(); + + const postHeaders: Record = { + 'content-type': 'application/yaml', + }; + + if (value) { + postHeaders.Authorization = value; + } return await fetch( `${await backendBaseUrl}/harness/${env}/gateway/pipeline/api/pipeline/execute/rerun/${ row.planExecutionId }/${row.pipelineId}?${query1}&moduleType=ci`, { - headers: { - 'content-type': 'application/yaml', - Authorization: value, - }, + headers: postHeaders, body: `${data}`, method: 'POST', }, From 44c2b48af24d9cb4a99c1e8e176b33c079f2bacb Mon Sep 17 00:00:00 2001 From: Debanitrkl Date: Tue, 6 Aug 2024 14:28:34 +0530 Subject: [PATCH 2/2] Added bearer prefix to ci/cd plugin token --- plugins/harness-ci-cd/src/hooks/useGetExecutionsList.ts | 2 +- plugins/harness-ci-cd/src/hooks/useGetLicenseWithAuth.ts | 2 +- plugins/harness-ci-cd/src/hooks/useMutateRunPipeline.ts | 6 +++--- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/plugins/harness-ci-cd/src/hooks/useGetExecutionsList.ts b/plugins/harness-ci-cd/src/hooks/useGetExecutionsList.ts index 53305e0..a3b9bac 100644 --- a/plugins/harness-ci-cd/src/hooks/useGetExecutionsList.ts +++ b/plugins/harness-ci-cd/src/hooks/useGetExecutionsList.ts @@ -47,7 +47,7 @@ const useGetExecutionsList = ({ const { token: apiToken } = await identityApi.getCredentials(); const token = getSecureHarnessKey('token') || apiToken; const value = token && token === apiToken ? `Bearer ${token}` : token; - + const headers = new Headers(); if (value) { headers.append('Authorization', value); diff --git a/plugins/harness-ci-cd/src/hooks/useGetLicenseWithAuth.ts b/plugins/harness-ci-cd/src/hooks/useGetLicenseWithAuth.ts index df73d4d..6cffc9c 100644 --- a/plugins/harness-ci-cd/src/hooks/useGetLicenseWithAuth.ts +++ b/plugins/harness-ci-cd/src/hooks/useGetLicenseWithAuth.ts @@ -19,7 +19,7 @@ const useGetLicenseWithAuth = ({ const { token: apiToken } = await identityApi.getCredentials(); const token = getSecureHarnessKey('token') || apiToken; const value = token && token === apiToken ? `Bearer ${token}` : token; - + const headers = new Headers(); if (value) { headers.append('Authorization', value); diff --git a/plugins/harness-ci-cd/src/hooks/useMutateRunPipeline.ts b/plugins/harness-ci-cd/src/hooks/useMutateRunPipeline.ts index 7e20fc6..a87b322 100644 --- a/plugins/harness-ci-cd/src/hooks/useMutateRunPipeline.ts +++ b/plugins/harness-ci-cd/src/hooks/useMutateRunPipeline.ts @@ -16,7 +16,7 @@ const useMutateRunPipeline = ({ const { token: apiToken } = await identityApi.getCredentials(); const token = getSecureHarnessKey('token') || apiToken; const value = token && token === apiToken ? `Bearer ${token}` : token; - + const headers = new Headers(); if (value) { headers.append('Authorization', value); @@ -32,11 +32,11 @@ const useMutateRunPipeline = ({ ); const data = await response.text(); - + const postHeaders: Record = { 'content-type': 'application/yaml', }; - + if (value) { postHeaders.Authorization = value; }