config_ctrl.ts 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import DatasourceSrv from 'app/features/plugins/datasource_srv';
  2. export interface JWT {
  3. private_key: any;
  4. token_uri: any;
  5. client_email: any;
  6. project_id: any;
  7. }
  8. export class StackdriverConfigCtrl {
  9. static templateUrl = 'public/app/plugins/datasource/stackdriver/partials/config.html';
  10. datasourceSrv: any;
  11. current: any;
  12. jsonText: string;
  13. validationErrors: string[] = [];
  14. inputDataValid: boolean;
  15. authenticationTypes: any[];
  16. defaultAuthenticationType: string;
  17. /** @ngInject */
  18. constructor(datasourceSrv: DatasourceSrv) {
  19. this.defaultAuthenticationType = 'jwt';
  20. this.datasourceSrv = datasourceSrv;
  21. this.current.jsonData = this.current.jsonData || {};
  22. this.current.jsonData.authenticationType = this.current.jsonData.authenticationType
  23. ? this.current.jsonData.authenticationType
  24. : this.defaultAuthenticationType;
  25. this.current.secureJsonData = this.current.secureJsonData || {};
  26. this.current.secureJsonFields = this.current.secureJsonFields || {};
  27. this.authenticationTypes = [
  28. { key: this.defaultAuthenticationType, value: 'Google JWT File' },
  29. { key: 'gce', value: 'GCE Default Service Account' },
  30. ];
  31. }
  32. save(jwt: JWT) {
  33. this.current.secureJsonData.privateKey = jwt.private_key;
  34. this.current.jsonData.tokenUri = jwt.token_uri;
  35. this.current.jsonData.clientEmail = jwt.client_email;
  36. this.current.jsonData.defaultProject = jwt.project_id;
  37. }
  38. validateJwt(jwt: JWT) {
  39. this.resetValidationMessages();
  40. if (!jwt.private_key || jwt.private_key.length === 0) {
  41. this.validationErrors.push('Private key field missing in JWT file.');
  42. }
  43. if (!jwt.token_uri || jwt.token_uri.length === 0) {
  44. this.validationErrors.push('Token URI field missing in JWT file.');
  45. }
  46. if (!jwt.client_email || jwt.client_email.length === 0) {
  47. this.validationErrors.push('Client Email field missing in JWT file.');
  48. }
  49. if (!jwt.project_id || jwt.project_id.length === 0) {
  50. this.validationErrors.push('Project Id field missing in JWT file.');
  51. }
  52. if (this.validationErrors.length === 0) {
  53. this.inputDataValid = true;
  54. return true;
  55. }
  56. return false;
  57. }
  58. onUpload(json: JWT) {
  59. this.jsonText = '';
  60. if (this.validateJwt(json)) {
  61. this.save(json);
  62. }
  63. }
  64. onPasteJwt(e: any) {
  65. try {
  66. const json = JSON.parse(e.originalEvent.clipboardData.getData('text/plain') || this.jsonText);
  67. if (this.validateJwt(json)) {
  68. this.save(json);
  69. }
  70. } catch (error) {
  71. this.resetValidationMessages();
  72. this.validationErrors.push(`Invalid json: ${error.message}`);
  73. }
  74. }
  75. resetValidationMessages() {
  76. this.validationErrors = [];
  77. this.inputDataValid = false;
  78. this.jsonText = '';
  79. this.current.jsonData = Object.assign({}, { authenticationType: this.current.jsonData.authenticationType });
  80. this.current.secureJsonData = {};
  81. this.current.secureJsonFields = {};
  82. }
  83. }