From 2a50cbf40558e508a8abce025e8b95f096bf11a2 Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Tue, 30 Jan 2024 20:37:18 -0600 Subject: [PATCH 1/6] Add key and values to the GkeClusterLocator.ts script and add an unit test to check if those are there Signed-off-by: armandocomellas1 --- .changeset/mighty-tomatoes-visit.md | 5 +++++ .../cluster-locator/GkeClusterLocator.test.ts | 19 +++++++++++++++++++ .../src/cluster-locator/GkeClusterLocator.ts | 7 ++++++- 3 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 .changeset/mighty-tomatoes-visit.md diff --git a/.changeset/mighty-tomatoes-visit.md b/.changeset/mighty-tomatoes-visit.md new file mode 100644 index 0000000000..08e443d932 --- /dev/null +++ b/.changeset/mighty-tomatoes-visit.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-kubernetes-backend': minor +--- + +At Line 91, in the second parameter enter a value for the property libName and libVersion to post headers to the key `x-goog-api-client` diff --git a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts index 849a7839e2..ba46e03ae2 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts @@ -18,6 +18,7 @@ import { ANNOTATION_KUBERNETES_AUTH_PROVIDER } from '@backstage/plugin-kubernete import '@backstage/backend-common'; import { ConfigReader, Config } from '@backstage/config'; import { GkeClusterLocator } from './GkeClusterLocator'; +import { Duration } from 'luxon'; const mockedListClusters = jest.fn(); @@ -485,5 +486,23 @@ describe('GkeClusterLocator', () => { }, ]); }); + it('Check if new container.v1.ClusterManagerClient has key and values as parameter', async () => { + const configs: Config = new ConfigReader({ + type: 'gke', + projectId: 'some-project', + }); + + const refreshIntervals: Duration | undefined = undefined; + const getHeaders: GkeClusterLocator = GkeClusterLocator.fromConfig( + configs, + refreshIntervals, + ); + const getKeyName = getHeaders.client._opts.hasOwnProperty(['libName']); + const getKeyVersion = getHeaders.client._opts.hasOwnProperty([ + 'libVersion', + ]); + expect(getKeyName).toBe(true); + expect(getKeyVersion).toBe(true); + }); }); }); diff --git a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts index 03f5bda103..33b9ee74b9 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts @@ -21,6 +21,7 @@ import * as container from '@google-cloud/container'; import { Duration } from 'luxon'; import { runPeriodically } from '../service/runPeriodically'; import { ClusterDetails, KubernetesClustersSupplier } from '../types/types'; +import packageinfo from '../../package.json'; interface MatchResourceLabelEntry { key: string; @@ -80,13 +81,17 @@ export class GkeClusterLocator implements KubernetesClustersSupplier { return gkeClusterLocator; } + // At Line 91, in the second parameter enter a value for the property libName and libVersion to post headers to the key 'x-goog-api-client' static fromConfig( config: Config, refreshInterval: Duration | undefined = undefined, ): GkeClusterLocator { return GkeClusterLocator.fromConfigWithClient( config, - new container.v1.ClusterManagerClient(), + new container.v1.ClusterManagerClient({ + libName: 'backstage/gke', + libVersion: packageinfo.version, + }), refreshInterval, ); } From 77b42e63c433c2ca4d1cd13611e3cec9cf90b411 Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Tue, 30 Jan 2024 20:51:12 -0600 Subject: [PATCH 2/6] Used special chars to identify some sensitive variable names by the linter errors tools Signed-off-by: armandocomellas1 --- .changeset/mighty-tomatoes-visit.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.changeset/mighty-tomatoes-visit.md b/.changeset/mighty-tomatoes-visit.md index 08e443d932..7c12a0fa00 100644 --- a/.changeset/mighty-tomatoes-visit.md +++ b/.changeset/mighty-tomatoes-visit.md @@ -2,4 +2,4 @@ '@backstage/plugin-kubernetes-backend': minor --- -At Line 91, in the second parameter enter a value for the property libName and libVersion to post headers to the key `x-goog-api-client` +At Line 91, in the second parameter enter a value for the property `libName` and `libVersion` to post headers to the key `x-goog-api-client` From 136d1666ada725ecb64de6d1370c59f64ef0da18 Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Tue, 30 Jan 2024 21:08:12 -0600 Subject: [PATCH 3/6] add an unit test to check if those are theres Signed-off-by: armandocomellas1 --- .../src/cluster-locator/GkeClusterLocator.test.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts index ba46e03ae2..6b06fd26a2 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts @@ -497,10 +497,9 @@ describe('GkeClusterLocator', () => { configs, refreshIntervals, ); - const getKeyName = getHeaders.client._opts.hasOwnProperty(['libName']); - const getKeyVersion = getHeaders.client._opts.hasOwnProperty([ - 'libVersion', - ]); + const getClientMethod = getHeaders.client._opts; + const getKeyName = getClientMethod.hasOwnProperty('libName'); + const getKeyVersion = getClientMethod.hasOwnProperty('libVersion'); expect(getKeyName).toBe(true); expect(getKeyVersion).toBe(true); }); From e749ed8aad03848cee9a8732b5de44c4fd4939ff Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Tue, 30 Jan 2024 21:25:23 -0600 Subject: [PATCH 4/6] Private fn doesnt not allow acces method, change into expect unit test Signed-off-by: armandocomellas1 --- .../src/cluster-locator/GkeClusterLocator.test.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts index 6b06fd26a2..54b90b6033 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts @@ -497,11 +497,9 @@ describe('GkeClusterLocator', () => { configs, refreshIntervals, ); - const getClientMethod = getHeaders.client._opts; - const getKeyName = getClientMethod.hasOwnProperty('libName'); - const getKeyVersion = getClientMethod.hasOwnProperty('libVersion'); - expect(getKeyName).toBe(true); - expect(getKeyVersion).toBe(true); + + expect(getHeaders.client._opts).toHaveProperty('libName'); + expect(getHeaders.client._opts).toHaveProperty('libVersion'); }); }); }); From a3df97fc4106affa90389c428c9b1aed94475a89 Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Tue, 30 Jan 2024 21:27:39 -0600 Subject: [PATCH 5/6] Private fn doesnt not allow acces method, change into expect unit test Signed-off-by: armandocomellas1 --- .../src/cluster-locator/GkeClusterLocator.test.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts index 54b90b6033..de77000249 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts @@ -498,8 +498,8 @@ describe('GkeClusterLocator', () => { refreshIntervals, ); - expect(getHeaders.client._opts).toHaveProperty('libName'); - expect(getHeaders.client._opts).toHaveProperty('libVersion'); + expect(getHeaders).toHaveProperty('client._opts.libName'); + expect(getHeaders).toHaveProperty('client._opts.libVersion'); }); }); }); From 81a73b655930f24cbeae71a4e0446a8b506201b1 Mon Sep 17 00:00:00 2001 From: armandocomellas1 Date: Wed, 7 Feb 2024 14:42:51 -0600 Subject: [PATCH 6/6] adds a x-goog-api-client header to existing API requests in this plugin to clearly identify API requests from this GKE plugin Signed-off-by: armandocomellas1 --- .changeset/mighty-tomatoes-visit.md | 4 +-- .../cluster-locator/GkeClusterLocator.test.ts | 25 ++++++++++++------- .../src/cluster-locator/GkeClusterLocator.ts | 4 +-- 3 files changed, 20 insertions(+), 13 deletions(-) diff --git a/.changeset/mighty-tomatoes-visit.md b/.changeset/mighty-tomatoes-visit.md index 7c12a0fa00..beb37d02ed 100644 --- a/.changeset/mighty-tomatoes-visit.md +++ b/.changeset/mighty-tomatoes-visit.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-kubernetes-backend': minor +'@backstage/plugin-kubernetes-backend': patch --- -At Line 91, in the second parameter enter a value for the property `libName` and `libVersion` to post headers to the key `x-goog-api-client` +adds a x-goog-api-client header to existing API requests in this plugin to clearly identify API requests from this GKE plugin. headers are formatted as follows where `libVersion` represents the current dotted version number of the Backstage GKE plugin and `libName` represent the current Google API used at backstage. diff --git a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts index de77000249..e6e19a6aaa 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.test.ts @@ -18,9 +18,18 @@ import { ANNOTATION_KUBERNETES_AUTH_PROVIDER } from '@backstage/plugin-kubernete import '@backstage/backend-common'; import { ConfigReader, Config } from '@backstage/config'; import { GkeClusterLocator } from './GkeClusterLocator'; -import { Duration } from 'luxon'; +import * as container from '@google-cloud/container'; const mockedListClusters = jest.fn(); +jest.mock('@google-cloud/container', () => { + return { + v1: { + ClusterManagerClient: jest.fn().mockImplementation(() => { + mockedListClusters(); + }), + }, + }; +}); describe('GkeClusterLocator', () => { beforeEach(() => { @@ -486,20 +495,18 @@ describe('GkeClusterLocator', () => { }, ]); }); - it('Check if new container.v1.ClusterManagerClient has key and values as parameter', async () => { + it('constructs ClusterManagerClient with identifying metadata', async () => { const configs: Config = new ConfigReader({ type: 'gke', projectId: 'some-project', }); - const refreshIntervals: Duration | undefined = undefined; - const getHeaders: GkeClusterLocator = GkeClusterLocator.fromConfig( - configs, - refreshIntervals, - ); + GkeClusterLocator.fromConfig(configs); - expect(getHeaders).toHaveProperty('client._opts.libName'); - expect(getHeaders).toHaveProperty('client._opts.libVersion'); + expect(container.v1.ClusterManagerClient).toHaveBeenCalledWith({ + libName: 'backstage/kubernetes-backend.GkeClusterLocator', + libVersion: expect.any(String), + }); }); }); }); diff --git a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts index 33b9ee74b9..3b3d74e17f 100644 --- a/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts +++ b/plugins/kubernetes-backend/src/cluster-locator/GkeClusterLocator.ts @@ -81,7 +81,7 @@ export class GkeClusterLocator implements KubernetesClustersSupplier { return gkeClusterLocator; } - // At Line 91, in the second parameter enter a value for the property libName and libVersion to post headers to the key 'x-goog-api-client' + // Added an `x-goog-api-client` header to API requests made by the GKE cluster locator to clearly identify API requests from this plugin. static fromConfig( config: Config, refreshInterval: Duration | undefined = undefined, @@ -89,7 +89,7 @@ export class GkeClusterLocator implements KubernetesClustersSupplier { return GkeClusterLocator.fromConfigWithClient( config, new container.v1.ClusterManagerClient({ - libName: 'backstage/gke', + libName: `backstage/kubernetes-backend.GkeClusterLocator`, libVersion: packageinfo.version, }), refreshInterval,