From 5a223eddb1461e8660d144275d242c324aa83c07 Mon Sep 17 00:00:00 2001 From: rozetko Date: Thu, 22 Jun 2023 14:43:06 +0300 Subject: [PATCH 1/2] generate unique API key names --- package.json | 2 ++ src/plugin_state.ts | 24 +++++++++++++++++------- yarn.lock | 7 ++++++- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 46a1f69..c8319f7 100644 --- a/package.json +++ b/package.json @@ -55,6 +55,7 @@ "@types/node": "^17.0.19", "@types/react-copy-to-clipboard": "^5.0.4", "@types/react-router-dom": "^5.3.3", + "@types/uuid": "^9.0.2", "@typescript-eslint/eslint-plugin": "^4.33.0", "@typescript-eslint/parser": "^4.33.0", "axios": "^1.2.1", @@ -84,6 +85,7 @@ "ts-node": "^10.5.0", "tsconfig-paths": "^3.12.0", "typescript": "^4.4.0", + "uuid": "^9.0.0", "webpack": "^5.69.1", "webpack-cli": "^4.9.2", "webpack-livereload-plugin": "^3.0.2" diff --git a/src/plugin_state.ts b/src/plugin_state.ts index 0d7fae6..762ecfb 100644 --- a/src/plugin_state.ts +++ b/src/plugin_state.ts @@ -7,8 +7,12 @@ import { import { getBackendSrv } from '@grafana/runtime'; +import { v4 as uuidv4 } from 'uuid'; + import axios from 'axios'; +import * as _ from 'lodash'; + export type UpdateGrafanaPluginSettingsProps = { jsonData?: Partial; secureJsonData?: Partial; @@ -97,19 +101,25 @@ class PluginState { static createGrafanaToken = async () => { const baseUrl = '/api/auth/keys'; - const keys = await this.grafanaBackend.get(baseUrl); - const existingKey = keys.find((key: { id: number; name: string; role: string }) => key.name === 'DataExporter'); + const keys = await this.grafanaBackend.get(baseUrl, { includeExpired: true }); + const existingKeys = keys.filter((key: { id: number; name: string; role: string }) => + _.includes(key.name, 'DataExporter') + ); - if (existingKey) { - // @ts-ignore - // for some reason, there is no `options` argument in Grafana's public types for BackendSrv but it exists - await this.grafanaBackend.delete(`${baseUrl}/${existingKey.id}`, undefined, { showSuccessAlert: false }); + console.log('existingKeys', existingKeys); + + if (!_.isEmpty(existingKeys)) { + for (let key of existingKeys) { + // @ts-ignore + // for some reason, there is no `options` argument in Grafana's public types for BackendSrv but it exists + await this.grafanaBackend.delete(`${baseUrl}/${key.id}`, undefined, { showSuccessAlert: false }); + } } return await this.grafanaBackend.post( baseUrl, { - name: 'DataExporter', + name: `DataExporter_${uuidv4()}`, role: 'Admin', secondsToLive: null, }, diff --git a/yarn.lock b/yarn.lock index 5dfa76b..52bd088 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3945,6 +3945,11 @@ dependencies: source-map "^0.6.1" +"@types/uuid@^9.0.2": + version "9.0.2" + resolved "https://registry.yarnpkg.com/@types/uuid/-/uuid-9.0.2.tgz#ede1d1b1e451548d44919dc226253e32a6952c4b" + integrity sha512-kNnC1GFBLuhImSnV7w4njQkUiJi0ZXUycu1rUaouPqiKlXkh77JKgdRnTAp1x5eBwcIwbtI+3otwzuIDEuDoxQ== + "@types/webpack-dev-server@3": version "3.11.6" resolved "https://registry.yarnpkg.com/@types/webpack-dev-server/-/webpack-dev-server-3.11.6.tgz#d8888cfd2f0630203e13d3ed7833a4d11b8a34dc" @@ -12944,7 +12949,7 @@ uuid@8.3.2, uuid@^8.3.2: resolved "https://registry.yarnpkg.com/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2" integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg== -uuid@9.0.0: +uuid@9.0.0, uuid@^9.0.0: version "9.0.0" resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.0.tgz#592f550650024a38ceb0c562f2f6aa435761efb5" integrity sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg== From d016a3b4e611d602bd19777492a5a90fb4ae6a9e Mon Sep 17 00:00:00 2001 From: rozetko Date: Thu, 22 Jun 2023 14:43:19 +0300 Subject: [PATCH 2/2] 1.0.9 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index c8319f7..1eb2e46 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "corpglory-dataexporter-app", - "version": "1.0.8", + "version": "1.0.9", "description": "", "scripts": { "lint": "eslint --cache --ext .js,.jsx,.ts,.tsx --max-warnings=0 ./src",