Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"Permission denied" error in the Kentik dashboards #28

Merged
merged 2 commits into from
Dec 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/components/add_device.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import { BackendSrv } from 'grafana/app/core/services/backend_srv';

import * as _ from 'lodash';
import angular from 'angular';

import { BackendSrv } from 'grafana/app/core/services/backend_srv';

import { getRegion } from '../datasource/region_helper';

const defaults = {
device_name: '',
Expand All @@ -22,19 +21,11 @@ export class AddDeviceCtrl {
static templateUrl: string;
device: any;
sendingIps: any[];
region = '';

/** @ngInject */
constructor(public $location: ng.ILocationService, public backendSrv: BackendSrv, public alertSrv: any) {
this.device = angular.copy(defaults);
this.sendingIps = [{ ip: '' }];
// get region from datasource
this.initRegion();
}

async initRegion(): Promise<void> {
const datasources = await this.backendSrv.get('/api/datasources');
this.region = getRegion(datasources);
}

addIP() {
Expand All @@ -51,7 +42,7 @@ export class AddDeviceCtrl {
ips.push(ip.ip);
});
this.device.sending_ips = ips.join();
const resp = await this.backendSrv.post(`/api/plugin-proxy/kentik-app/${this.region}/api/v5/device`, this.device);
const resp = await this.backendSrv.post(`/api/plugin-proxy/kentik-app/api/v5/device`, this.device);
if ('err' in resp) {
this.alertSrv.set('Device Add failed.', resp.err, 'error');
throw new Error(`Device Add failed: ${resp.err}`);
Expand Down
17 changes: 4 additions & 13 deletions src/components/device_details.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
import { KentikAPI } from '../datasource/kentik_api';
import { showCustomAlert } from '../datasource/alert_helper';
import { getRegion } from '../datasource/region_helper';

import { BackendSrv } from 'grafana/app/core/services/backend_srv';
import { AlertSrv } from 'grafana/app/core/services/alert_srv';


export class DeviceDetailsCtrl {
static templateUrl: string;
device: any;
deviceDTO: any;
pageReady: boolean;
otherIps: any;
kentik: KentikAPI = {} as KentikAPI;
region = '';

/** @ngInject */
constructor(
Expand All @@ -25,19 +24,11 @@ export class DeviceDetailsCtrl {
this.device = {};
this.deviceDTO = {};
this.pageReady = false;
// get region from datasource
this.initRegion();
}

async initRegion(): Promise<void> {
const datasources = await this.backendSrv.get('/api/datasources');
this.region = getRegion(datasources);
this.kentik = new KentikAPI(this.backendSrv, this.$http);
this.kentik.setRegion(this.region);
await this.fetchDevice(this.$location.search().device);
this.fetchDevice(this.$location.search().device);
}


addIP() {
this.otherIps.push({ ip: '' });
}
Expand All @@ -48,7 +39,7 @@ export class DeviceDetailsCtrl {

async fetchDevice(deviceId: string): Promise<void> {
const resp = await this.backendSrv.get(
`/api/plugin-proxy/kentik-app/${this.region}/api/v5/device/${deviceId}`
`/api/plugin-proxy/kentik-app/api/v5/device/${deviceId}`
);
this.device = resp.device;
this.updateDeviceDTO();
Expand Down Expand Up @@ -90,7 +81,7 @@ export class DeviceDetailsCtrl {

try {
const resp = await this.backendSrv.put(
`/api/plugin-proxy/kentik-app/${this.region}/api/v5/device/${this.deviceDTO.device_id}`,
`/api/plugin-proxy/kentik-app/api/v5/device/${this.deviceDTO.device_id}`,
data
);
if ('err' in resp) {
Expand Down
34 changes: 29 additions & 5 deletions src/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,25 @@ import { BackendSrv } from 'grafana/app/core/services/backend_srv';

import * as _ from 'lodash';


enum Region {
DEFAULT = 'default',
EU = 'eu',
CUSTOM = 'custom'
}

class KentikConfigCtrl {
apiValidated = false;
apiError = false;
appEditCtrl: any;
appModel: any;
kentik: KentikAPI;
static template: any;
regionTypes = [{ value: 'default', text: 'US (default)' }, { value: 'eu', text: 'EU' }, { value: 'custom', text: 'Custom' }];
regionTypes = [
{ value: Region.DEFAULT, text: 'US (default)' },
{ value: Region.EU, text: 'EU' },
{ value: Region.CUSTOM, text: 'Custom' }
];

/** @ngInject */
constructor(public $scope: ng.IScope, $http: ng.IHttpService, public backendSrv: BackendSrv) {
Expand All @@ -27,11 +38,10 @@ class KentikConfigCtrl {
if (!this.appModel.secureJsonData) {
this.appModel.secureJsonData = {};
}
if (typeof this.appModel.jsonData.region === 'undefined') {
this.appModel.jsonData.region = 'default';
if (this.appModel.jsonData.region === undefined) {
this.appModel.jsonData.region = Region.DEFAULT;
}
this.kentik = new KentikAPI(this.backendSrv, $http);
this.kentik.setRegion(this.appModel.jsonData.region);
if (this.appModel.enabled && this.appModel.jsonData.tokenSet) {
this.validateApiConnection();
}
Expand Down Expand Up @@ -82,7 +92,7 @@ class KentikConfigCtrl {
reset() {
this.appModel.jsonData.email = '';
this.appModel.jsonData.tokenSet = false;
this.appModel.jsonData.region = 'default';
this.appModel.jsonData.region = Region.DEFAULT;
this.appModel.jsonData.dynamicUrl = '';
this.appModel.secureJsonData = {};
this.apiValidated = false;
Expand Down Expand Up @@ -112,6 +122,7 @@ class KentikConfigCtrl {
});
const promisesResults: any[] = [];
if (!foundKentikDS || updateKentikDS) {
this.appModel.jsonData.url = this._getUrlByRegion(this.appModel.jsonData.region);
// create datasource
const kentik = {
name: 'kentik',
Expand All @@ -128,6 +139,19 @@ class KentikConfigCtrl {
}
return promisesResults;
}

private _getUrlByRegion(region: Region): string {
switch (region) {
case Region.DEFAULT:
return 'https://grafana-api.kentik.com/api/v5';
case Region.EU:
return 'https://api.kentik.eu/api/v5';
case Region.CUSTOM:
return this.appModel.jsonData.dynamicUrl;
default:
throw new Error(`Unknown region type: "${region}"`);
}
}
}

KentikConfigCtrl.template = configTemplate;
Expand Down
1 change: 0 additions & 1 deletion src/datasource/datasource.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ function createDatasourceInstance(ctx: any, data: any) {
};

ctx.kentikAPI = new KentikAPI(ctx.backendSrv, ctx.$http);
ctx.kentikAPI.setRegion('default');

ctx.kentikProxy = new KentikProxy(ctx.kentikAPI);

Expand Down
27 changes: 2 additions & 25 deletions src/datasource/kentik_api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { getRegion } from './region_helper';
import { showAlert } from '../datasource/alert_helper';

import { BackendSrv } from 'grafana/app/core/services/backend_srv';
Expand All @@ -9,25 +8,11 @@ import angular from 'angular';

export class KentikAPI {
baseUrl: string;
apiReady: boolean;
region?: string;
/** @ngInject */
constructor(public backendSrv: BackendSrv, public $http: ng.IHttpService) {
this.apiReady = false;
this.baseUrl = '/api/plugin-proxy/kentik-app';
}

private async _getRegionFromDatasource(): Promise<void> {
const allDatasources = await this.backendSrv.get('/api/datasources');

this.region = getRegion(allDatasources);
this.apiReady = true;
}

setRegion(region: string): void {
this.region = region;
}

async getDevices(): Promise<any> {
const resp = await this._get('/api/v5/devices');

Expand Down Expand Up @@ -74,14 +59,10 @@ export class KentikAPI {
}

private async _get(url: string): Promise<any> {
if (this.region === undefined) {
await this._getRegionFromDatasource();
}

try {
const resp = await this.$http({
method: 'GET',
url: this.baseUrl + '/' + this.region + url,
url: this.baseUrl + url,
});

return resp;
Expand All @@ -96,14 +77,10 @@ export class KentikAPI {
}

private async _post(url: string, data: any): Promise<any> {
if (this.region === undefined) {
await this._getRegionFromDatasource();
}

try {
const resp = await this.$http({
method: 'POST',
url: this.baseUrl + '/' + this.region + url,
url: this.baseUrl + url,
data: data,
});

Expand Down
1 change: 0 additions & 1 deletion src/datasource/kentik_proxy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,5 @@ function getKentikProxyInstance(ctx: any, data: any) {
};

ctx.kentikAPI = new KentikAPI(ctx.backendSrv, ctx.$http);
ctx.kentikAPI.setRegion('default');
ctx.kentikProxy = new KentikProxy(ctx.kentikAPI);
}
20 changes: 0 additions & 20 deletions src/datasource/region_helper.ts

This file was deleted.

14 changes: 3 additions & 11 deletions src/panel/call_to_action/module.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { KentikAPI } from '../../datasource/kentik_api';
import { showAlert } from '../../datasource/alert_helper';
import { PanelCtrl, loadPluginCss } from 'grafana/app/plugins/sdk';
import { getRegion } from '../../datasource/region_helper';

import { PanelCtrl, loadPluginCss } from 'grafana/app/plugins/sdk';
import { BackendSrv } from 'grafana/app/core/services/backend_srv';

import * as _ from 'lodash';


loadPluginCss({
dark: 'plugins/kentik-app/styles/dark.css',
light: 'plugins/kentik-app/styles/light.css',
Expand All @@ -21,7 +21,6 @@ class CallToActiontCtrl extends PanelCtrl {
deviceStatus: string;
allDone: boolean;
kentik: KentikAPI = {} as KentikAPI;
region = '';

/** @ngInject */
constructor(
Expand All @@ -34,16 +33,9 @@ class CallToActiontCtrl extends PanelCtrl {
_.defaults(this.panel, panelDefaults);
this.deviceStatus = '';
this.allDone = false;
// get region from datasource
this.initRegion();
}

async initRegion(): Promise<void> {
const datasources = await this.backendSrv.get('/api/datasources');
this.region = getRegion(datasources);
this.kentik = new KentikAPI(this.backendSrv, this.$http);
this.kentik.setRegion(this.region);
await this.fetchTaskStatus();
this.fetchTaskStatus();
}

async fetchTaskStatus() {
Expand Down
11 changes: 1 addition & 10 deletions src/panel/device_list/module.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { KentikAPI } from '../../datasource/kentik_api';
import { showAlert } from '../../datasource/alert_helper';
import { getRegion } from '../../datasource/region_helper';

import { PanelCtrl } from 'grafana/app/plugins/sdk';
import { loadPluginCss } from 'grafana/app/plugins/sdk';
Expand All @@ -23,7 +22,6 @@ class DeviceListCtrl extends PanelCtrl {
devices: any[];
pageReady: boolean;
kentik: KentikAPI = {} as KentikAPI;
region = '';

/** @ngInject */
constructor(
Expand All @@ -37,16 +35,9 @@ class DeviceListCtrl extends PanelCtrl {
_.defaults(this.panel, panelDefaults);
this.devices = [];
this.pageReady = false;
// get region from datasource
this.initRegion();
}

async initRegion(): Promise<void> {
const datasources = await this.backendSrv.get('/api/datasources');
this.region = getRegion(datasources);
this.kentik = new KentikAPI(this.backendSrv, this.$http);
this.kentik.setRegion(this.region);
await this.fetchDevices();
this.fetchDevices();
}

async fetchDevices() {
Expand Down
40 changes: 2 additions & 38 deletions src/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,9 @@

"routes": [
{
"path": "default/api/v5/*",
"path": "api/v5/*",
"method": "*",
"url": "https://grafana-api.kentik.com/api/v5",
"headers": [
{"name": "X-CH-Auth-API-Token", "content": "{{.SecureJsonData.token}}"},
{"name": "X-CH-Auth-Email", "content": "{{.JsonData.email}}"}
]
},
{
"path": "custom/api/v5/*",
"method": "*",
"url": "{{.JsonData.dynamicUrl}}",
"headers": [
{"name": "X-CH-Auth-API-Token", "content": "{{.SecureJsonData.token}}"},
{"name": "X-CH-Auth-Email", "content": "{{.JsonData.email}}"}
]
},
{
"path": "eu/api/v5/*",
"method": "*",
"url": "https://api.kentik.eu/api/v5/",
"headers": [
{"name": "X-CH-Auth-API-Token", "content": "{{.SecureJsonData.token}}"},
{"name": "X-CH-Auth-Email", "content": "{{.JsonData.email}}"}
]
},
{
"path": "default/api/v4/*",
"method": "*",
"url": "https://grafanav4api.kentik.com/api/v4/",
"headers": [
{"name": "X-CH-Auth-API-Token", "content": "{{.SecureJsonData.token}}"},
{"name": "X-CH-Auth-Email", "content": "{{.JsonData.email}}"}
]
},
{
"path": "default/api/v1/*",
"method": "*",
"url": "https://grafanav1api.kentik.com/api/v1",
"url": "{{.JsonData.url}}",
"headers": [
{"name": "X-CH-Auth-API-Token", "content": "{{.SecureJsonData.token}}"},
{"name": "X-CH-Auth-Email", "content": "{{.JsonData.email}}"}
Expand Down