Skip to content

Commit 708febe

Browse files
authored
Merge pull request #19 from Jonas-Beck/feat/fetchApi-backstage-token
feat: fetch api backstage token
2 parents 8bf817a + cf165fb commit 708febe

File tree

10 files changed

+127
-63
lines changed

10 files changed

+127
-63
lines changed

package.json

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sysdig/backstage-plugin-sysdig",
3-
"version": "1.3.1",
3+
"version": "1.3.2",
44
"main": "dist/index.esm.js",
55
"types": "dist/index.d.ts",
66
"license": "Apache-2.0",
@@ -25,6 +25,7 @@
2525
"dependencies": {
2626
"@backstage/core-components": "^0.14.0",
2727
"@backstage/core-plugin-api": "^1.9.0",
28+
"@backstage/plugin-catalog-react": "^1.13.0",
2829
"@backstage/theme": "^0.5.1",
2930
"@material-ui/core": "^4.9.13",
3031
"@material-ui/icons": "^4.9.1",
@@ -39,10 +40,14 @@
3940
"@backstage/core-app-api": "^1.12.0",
4041
"@backstage/dev-utils": "^1.0.27",
4142
"@backstage/test-utils": "^1.5.0",
42-
"@testing-library/jest-dom": "^5.10.1",
43+
"@testing-library/jest-dom": "^6.5.0",
4344
"@testing-library/react": "^12.1.3",
4445
"@testing-library/user-event": "^14.0.0",
45-
"msw": "^1.0.0"
46+
"@types/jest": "^29.5.13",
47+
"@types/react": "^18.3.9",
48+
"@types/react-dom": "^18.3.0",
49+
"msw": "^1.0.0",
50+
"typescript": "^5.6.2"
4651
},
4752
"files": [
4853
"config.d.ts",

src/api/SysdigApi.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/* src/api.ts */
2+
import { createApiRef } from "@backstage/core-plugin-api";
3+
4+
export interface SysdigApi {
5+
fetchVulnRuntime: <T = any>(filters?: string, init?: RequestInit) => Promise<T>;
6+
fetchVulnRegistry: <T = any>(
7+
filters?: string,
8+
init?: RequestInit,
9+
) => Promise<T>;
10+
fetchVulnPipeline: <T = any>(
11+
fitlers?: string,
12+
init?: RequestInit,
13+
) => Promise<T>;
14+
fetchInventory: <T = any>(filters?: string, init?: RequestInit) => Promise<T>;
15+
}
16+
17+
export const sysdigApiRef = createApiRef<SysdigApi>({
18+
id: "plugin.sysdig.service",
19+
});

src/api/SysdigApiClient.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import { ConfigApi, FetchApi } from "@backstage/core-plugin-api";
2+
import { SysdigApi } from "./SysdigApi";
3+
import {
4+
API_INVENTORY,
5+
API_PROXY_BASE_PATH,
6+
API_VULN_PIPELINE,
7+
API_VULN_REGISTRY,
8+
API_VULN_RUNTIME,
9+
} from "../lib";
10+
11+
export class SysdigApiClient implements SysdigApi {
12+
private readonly fetchApi: FetchApi;
13+
private readonly configApi: ConfigApi;
14+
private readonly baseUrl: string;
15+
16+
constructor(options: { configApi: ConfigApi; fetchApi: FetchApi }) {
17+
this.configApi = options.configApi;
18+
this.fetchApi = options.fetchApi;
19+
this.baseUrl = this.configApi.getString("backend.baseUrl");
20+
}
21+
22+
private async fetch<T>(endpoint: string, init?: RequestInit): Promise<T> {
23+
const uri = `${this.baseUrl}${API_PROXY_BASE_PATH}${endpoint}`;
24+
25+
const response = await this.fetchApi.fetch(uri, init);
26+
27+
if (!response.ok) throw new Error(response.statusText);
28+
29+
return await response.json();
30+
}
31+
32+
async fetchVulnRuntime<T = any>(
33+
filters?: string,
34+
init?: RequestInit,
35+
): Promise<T> {
36+
return await this.fetch<T>(`${API_VULN_RUNTIME}${filters}`, init);
37+
}
38+
39+
async fetchVulnRegistry<T = any>(
40+
filters?: string,
41+
init?: RequestInit,
42+
): Promise<T> {
43+
return await this.fetch<T>(`${API_VULN_REGISTRY}${filters}`, init);
44+
}
45+
46+
async fetchVulnPipeline<T = any>(
47+
filters?: string,
48+
init?: RequestInit,
49+
): Promise<T> {
50+
return await this.fetch<T>(`${API_VULN_PIPELINE}${filters}`, init);
51+
}
52+
53+
async fetchInventory<T = any>(
54+
filters?: string,
55+
init?: RequestInit,
56+
): Promise<T> {
57+
return await this.fetch<T>(`${API_INVENTORY}${filters}`, init);
58+
}
59+
}

src/api/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./SysdigApi";
2+
export * from "./SysdigApiClient";

src/components/SysdigPostureFetchComponent/SysdigPostureFetchComponent.tsx

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import useAsync from 'react-use/lib/useAsync';
1919
import Alert from '@material-ui/lab/Alert';
2020
import { useEntity } from '@backstage/plugin-catalog-react';
2121
import { useApi, configApiRef } from '@backstage/core-plugin-api';
22+
import { sysdigApiRef } from '../../api';
2223

2324
import {
2425
// annotations
@@ -56,9 +57,6 @@ import {
5657
getScope,
5758
getResourceName,
5859
getTitleWithBacklink,
59-
60-
API_PROXY_BASE_PATH,
61-
API_INVENTORY,
6260
getBacklink
6361
} from '../../lib'
6462

@@ -212,20 +210,20 @@ export const DenseTable = ({ postureScans, title }: DenseTableProps) => {
212210
export const SysdigPostureFetchComponent = () => {
213211
const { entity } = useEntity();
214212
const backendUrl = useApi(configApiRef).getString('backend.baseUrl');
213+
const sysdigApiClient = useApi(sysdigApiRef);
215214
let endpoint: string | undefined = useApi(configApiRef).getOptionalString("sysdig.endpoint");
216215
let backlink_config: string | undefined = useApi(configApiRef).getOptionalString("sysdig.backlink");
217216

218217
var backlink = getBacklink(endpoint, backlink_config, "inventory");
219218
const annotations = entity.metadata.annotations;
220219

221-
let uri = backendUrl + API_PROXY_BASE_PATH + API_INVENTORY;
222220
let filter = '?filter=';
223221
var name;
224222

225223
if (annotations) {
226224

227225
if (SYSDIG_CUSTOM_FILTER_ANNOTATION in annotations) {
228-
uri += '?filter=' + annotations[SYSDIG_CUSTOM_FILTER_ANNOTATION]
226+
filter += annotations[SYSDIG_CUSTOM_FILTER_ANNOTATION]
229227
} else {
230228

231229
var filters = []
@@ -355,17 +353,11 @@ export const SysdigPostureFetchComponent = () => {
355353
}
356354

357355
filter += filters.join(' and ');
358-
uri += filter;
359356
backlink += filter;
360357
}
361358

362359
const { value, loading, error } = useAsync(async (): Promise<PostureScan[]> => {
363-
const requestOptions = {
364-
method: 'GET',
365-
};
366-
367-
const response = await fetch(uri, requestOptions);
368-
const data = await response.json();
360+
const data = await sysdigApiClient.fetchInventory(filter)
369361
return data.data;
370362
}, []);
371363

@@ -380,4 +372,4 @@ export const SysdigPostureFetchComponent = () => {
380372
} else {
381373
return <Alert severity="warning">Please, add annotations to the entity.</Alert>;
382374
}
383-
};
375+
};

src/components/SysdigVMPipelineFetchComponent/SysdigVMPipelineFetchComponent.tsx

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@ import {
2828
getStatusColorSpan,
2929
getTitleWithBacklink,
3030
getChips,
31-
32-
API_PROXY_BASE_PATH,
33-
API_VULN_PIPELINE,
3431
getBacklink
3532
} from '../../lib'
33+
import { sysdigApiRef } from '../../api';
3634

3735
type PipelineScan = {
3836
createdAt: Date,
@@ -113,20 +111,19 @@ export const DenseTable = ({ pipelineScans, title }: DenseTableProps) => {
113111

114112
export const SysdigVMPipelineFetchComponent = () => {
115113
const { entity } = useEntity();
116-
const backendUrl = useApi(configApiRef).getString('backend.baseUrl');
114+
const sysdigApiClient = useApi(sysdigApiRef)
117115
let endpoint: string | undefined = useApi(configApiRef).getOptionalString("sysdig.endpoint");
118116
let backlink_config: string | undefined = useApi(configApiRef).getOptionalString("sysdig.backlink");
119117

120118
var backlink = getBacklink(endpoint, backlink_config, "vm-pipeline");
121119

122-
let uri = backendUrl + API_PROXY_BASE_PATH + API_VULN_PIPELINE;
123120
let filter = '?filter=';
124121
var name;
125122

126123
const annotations = entity.metadata.annotations;
127124
if (annotations) {
128125
if (SYSDIG_CUSTOM_FILTER_ANNOTATION in annotations) {
129-
uri += '?filter=' + annotations[SYSDIG_CUSTOM_FILTER_ANNOTATION]
126+
filter += annotations[SYSDIG_CUSTOM_FILTER_ANNOTATION]
130127
} else {
131128

132129
var filters = []
@@ -141,17 +138,11 @@ export const SysdigVMPipelineFetchComponent = () => {
141138
}
142139

143140
filter += filters.join(' and ');
144-
uri += filter;
145141
backlink += filter;
146142
}
147143

148144
const { value, loading, error } = useAsync(async (): Promise<PipelineScan[]> => {
149-
const requestOptions = {
150-
method: 'GET',
151-
};
152-
153-
const response = await fetch(uri, requestOptions);
154-
const data = await response.json();
145+
const data = await sysdigApiClient.fetchVulnPipeline(filter)
155146
return data.data;
156147
}, []);
157148

@@ -166,4 +157,4 @@ export const SysdigVMPipelineFetchComponent = () => {
166157
} else {
167158
return <Alert severity="warning">Please, add annotations to the entity.</Alert>;
168159
}
169-
};
160+
};

src/components/SysdigVMRegistryFetchComponent/SysdigVMRegistryFetchComponent.tsx

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ import {
2929
// methods
3030
getChips,
3131
getTitleWithBacklink,
32-
33-
API_PROXY_BASE_PATH,
34-
API_VULN_REGISTRY,
3532
getBacklink
3633
} from '../../lib';
34+
import { sysdigApiRef } from '../../api';
3735

3836
type RegistryScan = {
3937
mainAssetName: string,
@@ -102,20 +100,19 @@ export const DenseTable = ({ registryScans, title }: DenseTableProps) => {
102100

103101
export const SysdigVMRegistryFetchComponent = () => {
104102
const { entity } = useEntity();
105-
const backendUrl = useApi(configApiRef).getString('backend.baseUrl');
103+
const sysdigApiClient = useApi(sysdigApiRef)
106104
let endpoint: string | undefined = useApi(configApiRef).getOptionalString("sysdig.endpoint");
107105
let backlink_config: string | undefined = useApi(configApiRef).getOptionalString("sysdig.backlink");
108106

109107
var backlink = getBacklink(endpoint, backlink_config, "vm-registry");
110108

111-
let uri = backendUrl + API_PROXY_BASE_PATH + API_VULN_REGISTRY;
112109
let filter = '?filter=';
113110
var name;
114111

115112
const annotations = entity.metadata.annotations;
116113
if (annotations) {
117114
if (SYSDIG_CUSTOM_FILTER_ANNOTATION in annotations) {
118-
uri += '?filter=' + annotations[SYSDIG_CUSTOM_FILTER_ANNOTATION]
115+
filter += annotations[SYSDIG_CUSTOM_FILTER_ANNOTATION]
119116
} else {
120117

121118
var filters = []
@@ -140,19 +137,11 @@ export const SysdigVMRegistryFetchComponent = () => {
140137
}
141138

142139
filter += filters.join(' and ');
143-
uri += filter;
144140
backlink += filter;
145141
}
146142

147143
const { value, loading, error } = useAsync(async (): Promise<RegistryScan[]> => {
148-
const requestOptions = {
149-
method: 'GET',
150-
};
151-
152-
153-
const response = await fetch(uri, requestOptions);
154-
const data = await response.json();
155-
console.log(data.data)
144+
const data = await sysdigApiClient.fetchVulnRegistry(filter)
156145
return data.data;
157146
}, []);
158147

@@ -166,4 +155,4 @@ export const SysdigVMRegistryFetchComponent = () => {
166155
} else {
167156
return <Alert severity="warning">Please, add annotations to the entity.</Alert>;
168157
}
169-
};
158+
};

src/components/SysdigVMRuntimeFetchComponent/SysdigVMRuntimeFetchComponent.tsx

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ import {
3434
getChips,
3535
getDetails,
3636
getTitleWithBacklink,
37-
38-
API_PROXY_BASE_PATH,
39-
API_VULN_RUNTIME,
4037
getBacklink
4138
} from '../../lib'
39+
import { sysdigApiRef } from '../../api';
4240

4341

4442
type RuntimeScan = {
@@ -153,21 +151,21 @@ export const DenseTable = ({ runtimeScans, title }: DenseTableProps) => {
153151

154152
export const SysdigVMRuntimeFetchComponent = () => {
155153
const { entity } = useEntity();
156-
const backendUrl = useApi(configApiRef).getString('backend.baseUrl');
154+
const sysdigApiClient = useApi(sysdigApiRef)
157155
let endpoint: string | undefined = useApi(configApiRef).getOptionalString("sysdig.endpoint");
158156
let backlink_config: string | undefined = useApi(configApiRef).getOptionalString("sysdig.backlink");
159157

160158
var backlink = getBacklink(endpoint, backlink_config, "vm-runtime");
161159

162-
let uri = backendUrl + API_PROXY_BASE_PATH + API_VULN_RUNTIME;
160+
163161
let filter = '?filter=';
164162
var names;
165163

166164
const annotations = entity.metadata.annotations;
167165
if (annotations) {
168166

169167
if (SYSDIG_CUSTOM_FILTER_ANNOTATION in annotations) {
170-
uri += '?filter=' + annotations[SYSDIG_CUSTOM_FILTER_ANNOTATION]
168+
filter += annotations[SYSDIG_CUSTOM_FILTER_ANNOTATION]
171169
} else {
172170

173171
var filters = []
@@ -202,17 +200,11 @@ export const SysdigVMRuntimeFetchComponent = () => {
202200
}
203201

204202
filter += filters.join(' and ');
205-
uri += filter;
206203
backlink += filter;
207204
}
208205

209206
const { value, loading, error } = useAsync(async (): Promise<RuntimeScan[]> => {
210-
const requestOptions = {
211-
method: 'GET',
212-
};
213-
214-
const response = await fetch(uri, requestOptions);
215-
const data = await response.json();
207+
const data = await sysdigApiClient.fetchVulnRuntime(filter)
216208
return data.data;
217209
}, []);
218210

src/plugin.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,20 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
import { createPlugin, createRoutableExtension } from '@backstage/core-plugin-api';
16+
import { configApiRef, createApiFactory, createPlugin, createRoutableExtension, fetchApiRef } from '@backstage/core-plugin-api';
17+
import { SysdigApiClient, sysdigApiRef } from './api';
1718

1819
import { rootRouteRef } from './routes';
1920

2021
export const sysdigPlugin = createPlugin({
2122
id: 'sysdig',
23+
apis: [
24+
createApiFactory({
25+
api: sysdigApiRef,
26+
deps: { configApi: configApiRef, fetchApi: fetchApiRef},
27+
factory: ({ configApi, fetchApi }) => new SysdigApiClient({ configApi, fetchApi})
28+
})
29+
],
2230
routes: {
2331
root: rootRouteRef,
2432
},

0 commit comments

Comments
 (0)