2019-06-27 08:56:02 -05:00
|
|
|
import DatasourceSrv from 'app/features/plugins/datasource_srv';
|
2020-03-02 08:31:09 -06:00
|
|
|
import { AuthType, authTypes } from './types';
|
2019-06-27 08:56:02 -05:00
|
|
|
|
|
|
|
export interface JWT {
|
2020-03-02 08:31:09 -06:00
|
|
|
private_key: string;
|
|
|
|
token_uri: string;
|
|
|
|
client_email: string;
|
|
|
|
project_id: string;
|
2019-06-27 08:56:02 -05:00
|
|
|
}
|
|
|
|
|
2020-06-30 10:47:13 -05:00
|
|
|
export class CloudMonitoringConfigCtrl {
|
|
|
|
static templateUrl = 'public/app/plugins/datasource/cloud-monitoring/partials/config.html';
|
2020-03-02 08:31:09 -06:00
|
|
|
datasourceSrv: DatasourceSrv;
|
2018-09-04 06:21:02 -05:00
|
|
|
current: any;
|
2020-03-02 08:31:09 -06:00
|
|
|
meta: any;
|
2018-09-05 02:01:51 -05:00
|
|
|
jsonText: string;
|
|
|
|
validationErrors: string[] = [];
|
|
|
|
inputDataValid: boolean;
|
2020-03-02 08:31:09 -06:00
|
|
|
authenticationTypes: Array<{ key: AuthType; value: string }>;
|
2018-10-09 09:06:59 -05:00
|
|
|
defaultAuthenticationType: string;
|
2020-03-02 08:31:09 -06:00
|
|
|
name: string;
|
2018-09-04 06:21:02 -05:00
|
|
|
|
|
|
|
/** @ngInject */
|
2020-03-18 02:11:14 -05:00
|
|
|
constructor(datasourceSrv: DatasourceSrv) {
|
2020-03-02 08:31:09 -06:00
|
|
|
this.defaultAuthenticationType = AuthType.JWT;
|
2018-09-04 06:21:02 -05:00
|
|
|
this.datasourceSrv = datasourceSrv;
|
2020-03-02 08:31:09 -06:00
|
|
|
this.name = this.meta.name;
|
2018-09-04 06:21:02 -05:00
|
|
|
this.current.jsonData = this.current.jsonData || {};
|
2018-10-09 09:06:59 -05:00
|
|
|
this.current.jsonData.authenticationType = this.current.jsonData.authenticationType
|
|
|
|
? this.current.jsonData.authenticationType
|
|
|
|
: this.defaultAuthenticationType;
|
2018-09-05 02:01:51 -05:00
|
|
|
this.current.secureJsonData = this.current.secureJsonData || {};
|
2018-09-06 13:50:46 -05:00
|
|
|
this.current.secureJsonFields = this.current.secureJsonFields || {};
|
2020-03-02 08:31:09 -06:00
|
|
|
this.authenticationTypes = authTypes;
|
2018-09-05 02:01:51 -05:00
|
|
|
}
|
|
|
|
|
2019-06-27 08:56:02 -05:00
|
|
|
save(jwt: JWT) {
|
2018-09-05 02:01:51 -05:00
|
|
|
this.current.secureJsonData.privateKey = jwt.private_key;
|
|
|
|
this.current.jsonData.tokenUri = jwt.token_uri;
|
|
|
|
this.current.jsonData.clientEmail = jwt.client_email;
|
2018-09-12 06:08:30 -05:00
|
|
|
this.current.jsonData.defaultProject = jwt.project_id;
|
2018-09-05 02:01:51 -05:00
|
|
|
}
|
|
|
|
|
2019-06-27 08:56:02 -05:00
|
|
|
validateJwt(jwt: JWT) {
|
2018-09-05 02:01:51 -05:00
|
|
|
this.resetValidationMessages();
|
|
|
|
if (!jwt.private_key || jwt.private_key.length === 0) {
|
|
|
|
this.validationErrors.push('Private key field missing in JWT file.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!jwt.token_uri || jwt.token_uri.length === 0) {
|
|
|
|
this.validationErrors.push('Token URI field missing in JWT file.');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!jwt.client_email || jwt.client_email.length === 0) {
|
|
|
|
this.validationErrors.push('Client Email field missing in JWT file.');
|
|
|
|
}
|
|
|
|
|
2018-10-10 04:01:02 -05:00
|
|
|
if (!jwt.project_id || jwt.project_id.length === 0) {
|
|
|
|
this.validationErrors.push('Project Id field missing in JWT file.');
|
|
|
|
}
|
|
|
|
|
2018-09-05 02:01:51 -05:00
|
|
|
if (this.validationErrors.length === 0) {
|
|
|
|
this.inputDataValid = true;
|
|
|
|
return true;
|
|
|
|
}
|
2018-09-12 06:08:30 -05:00
|
|
|
|
|
|
|
return false;
|
2018-09-05 02:01:51 -05:00
|
|
|
}
|
|
|
|
|
2019-06-27 08:56:02 -05:00
|
|
|
onUpload(json: JWT) {
|
2018-09-05 02:01:51 -05:00
|
|
|
this.jsonText = '';
|
|
|
|
if (this.validateJwt(json)) {
|
|
|
|
this.save(json);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-27 08:56:02 -05:00
|
|
|
onPasteJwt(e: any) {
|
2018-09-05 02:01:51 -05:00
|
|
|
try {
|
|
|
|
const json = JSON.parse(e.originalEvent.clipboardData.getData('text/plain') || this.jsonText);
|
|
|
|
if (this.validateJwt(json)) {
|
|
|
|
this.save(json);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
this.resetValidationMessages();
|
|
|
|
this.validationErrors.push(`Invalid json: ${error.message}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resetValidationMessages() {
|
|
|
|
this.validationErrors = [];
|
|
|
|
this.inputDataValid = false;
|
|
|
|
this.jsonText = '';
|
2018-09-06 13:50:46 -05:00
|
|
|
|
2018-10-09 09:06:59 -05:00
|
|
|
this.current.jsonData = Object.assign({}, { authenticationType: this.current.jsonData.authenticationType });
|
2018-09-06 13:50:46 -05:00
|
|
|
this.current.secureJsonData = {};
|
|
|
|
this.current.secureJsonFields = {};
|
2018-09-04 06:21:02 -05:00
|
|
|
}
|
|
|
|
}
|