AzureCredentialsConfig.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import { DataSourceSettings } from '@grafana/data';
  2. import { config } from '@grafana/runtime';
  3. import { AzureCloud, AzureCredentials, ConcealedSecret } from './AzureCredentials';
  4. const concealed: ConcealedSecret = Symbol('Concealed client secret');
  5. function getDefaultAzureCloud(): string {
  6. return config.azure.cloud || AzureCloud.Public;
  7. }
  8. function getSecret(options: DataSourceSettings<any, any>): undefined | string | ConcealedSecret {
  9. if (options.secureJsonFields.azureClientSecret) {
  10. // The secret is concealed on server
  11. return concealed;
  12. } else {
  13. const secret = options.secureJsonData?.azureClientSecret;
  14. return typeof secret === 'string' && secret.length > 0 ? secret : undefined;
  15. }
  16. }
  17. export function hasCredentials(options: DataSourceSettings<any, any>): boolean {
  18. return !!options.jsonData.azureCredentials;
  19. }
  20. export function getDefaultCredentials(): AzureCredentials {
  21. if (config.azure.managedIdentityEnabled) {
  22. return { authType: 'msi' };
  23. } else {
  24. return { authType: 'clientsecret', azureCloud: getDefaultAzureCloud() };
  25. }
  26. }
  27. export function getCredentials(options: DataSourceSettings<any, any>): AzureCredentials {
  28. const credentials = options.jsonData.azureCredentials as AzureCredentials | undefined;
  29. // If no credentials saved, then return empty credentials
  30. // of type based on whether the managed identity enabled
  31. if (!credentials) {
  32. return getDefaultCredentials();
  33. }
  34. switch (credentials.authType) {
  35. case 'msi':
  36. if (config.azure.managedIdentityEnabled) {
  37. return {
  38. authType: 'msi',
  39. };
  40. } else {
  41. // If authentication type is managed identity but managed identities were disabled in Grafana config,
  42. // then we should fallback to an empty app registration (client secret) configuration
  43. return {
  44. authType: 'clientsecret',
  45. azureCloud: getDefaultAzureCloud(),
  46. };
  47. }
  48. case 'clientsecret':
  49. return {
  50. authType: 'clientsecret',
  51. azureCloud: credentials.azureCloud || getDefaultAzureCloud(),
  52. tenantId: credentials.tenantId,
  53. clientId: credentials.clientId,
  54. clientSecret: getSecret(options),
  55. };
  56. }
  57. }
  58. export function updateCredentials(
  59. options: DataSourceSettings<any, any>,
  60. credentials: AzureCredentials
  61. ): DataSourceSettings<any, any> {
  62. switch (credentials.authType) {
  63. case 'msi':
  64. if (!config.azure.managedIdentityEnabled) {
  65. throw new Error('Managed Identity authentication is not enabled in Grafana config.');
  66. }
  67. options = {
  68. ...options,
  69. jsonData: {
  70. ...options.jsonData,
  71. azureCredentials: {
  72. authType: 'msi',
  73. },
  74. },
  75. };
  76. return options;
  77. case 'clientsecret':
  78. options = {
  79. ...options,
  80. jsonData: {
  81. ...options.jsonData,
  82. azureCredentials: {
  83. authType: 'clientsecret',
  84. azureCloud: credentials.azureCloud || getDefaultAzureCloud(),
  85. tenantId: credentials.tenantId,
  86. clientId: credentials.clientId,
  87. },
  88. },
  89. secureJsonData: {
  90. ...options.secureJsonData,
  91. azureClientSecret:
  92. typeof credentials.clientSecret === 'string' && credentials.clientSecret.length > 0
  93. ? credentials.clientSecret
  94. : undefined,
  95. },
  96. secureJsonFields: {
  97. ...options.secureJsonFields,
  98. azureClientSecret: typeof credentials.clientSecret === 'symbol',
  99. },
  100. };
  101. return options;
  102. }
  103. }
  104. export function setDefaultCredentials(options: DataSourceSettings<any, any>): Partial<DataSourceSettings<any, any>> {
  105. return {
  106. jsonData: {
  107. ...options.jsonData,
  108. azureCredentials: getDefaultCredentials(),
  109. },
  110. };
  111. }
  112. export function resetCredentials(options: DataSourceSettings<any, any>): Partial<DataSourceSettings<any, any>> {
  113. return {
  114. jsonData: {
  115. ...options.jsonData,
  116. azureAuth: undefined,
  117. azureCredentials: undefined,
  118. azureEndpointResourceId: undefined,
  119. },
  120. };
  121. }