AzureAuthSettings.tsx 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import React, { FunctionComponent, FormEvent, useMemo, useState } from 'react';
  2. import { config } from '@grafana/runtime';
  3. import { InlineField, InlineFieldRow, InlineSwitch, Input } from '@grafana/ui';
  4. import { HttpSettingsBaseProps } from '@grafana/ui/src/components/DataSourceSettings/types';
  5. import { KnownAzureClouds, AzureCredentials } from './AzureCredentials';
  6. import { getCredentials, updateCredentials } from './AzureCredentialsConfig';
  7. import { AzureCredentialsForm } from './AzureCredentialsForm';
  8. export const AzureAuthSettings: FunctionComponent<HttpSettingsBaseProps> = (props: HttpSettingsBaseProps) => {
  9. const { dataSourceConfig, onChange } = props;
  10. const [overrideAudienceAllowed] = useState<boolean>(
  11. config.featureToggles.prometheusAzureOverrideAudience || !!dataSourceConfig.jsonData.azureEndpointResourceId
  12. );
  13. const [overrideAudienceChecked, setOverrideAudienceChecked] = useState<boolean>(
  14. !!dataSourceConfig.jsonData.azureEndpointResourceId
  15. );
  16. const credentials = useMemo(() => getCredentials(dataSourceConfig), [dataSourceConfig]);
  17. const onCredentialsChange = (credentials: AzureCredentials): void => {
  18. onChange(updateCredentials(dataSourceConfig, credentials));
  19. };
  20. const onOverrideAudienceChange = (ev: FormEvent<HTMLInputElement>): void => {
  21. setOverrideAudienceChecked(ev.currentTarget.checked);
  22. if (!ev.currentTarget.checked) {
  23. onChange({
  24. ...dataSourceConfig,
  25. jsonData: { ...dataSourceConfig.jsonData, azureEndpointResourceId: undefined },
  26. });
  27. }
  28. };
  29. const onResourceIdChange = (ev: FormEvent<HTMLInputElement>): void => {
  30. if (overrideAudienceChecked) {
  31. onChange({
  32. ...dataSourceConfig,
  33. jsonData: { ...dataSourceConfig.jsonData, azureEndpointResourceId: ev.currentTarget.value },
  34. });
  35. }
  36. };
  37. return (
  38. <>
  39. <h6>Azure Authentication</h6>
  40. <AzureCredentialsForm
  41. managedIdentityEnabled={config.azure.managedIdentityEnabled}
  42. credentials={credentials}
  43. azureCloudOptions={KnownAzureClouds}
  44. onCredentialsChange={onCredentialsChange}
  45. />
  46. {overrideAudienceAllowed && (
  47. <>
  48. <h6>Azure Configuration</h6>
  49. <div className="gf-form-group">
  50. <InlineFieldRow>
  51. <InlineField labelWidth={26} label="Override AAD audience">
  52. <InlineSwitch value={overrideAudienceChecked} onChange={onOverrideAudienceChange} />
  53. </InlineField>
  54. </InlineFieldRow>
  55. {overrideAudienceChecked && (
  56. <InlineFieldRow>
  57. <InlineField labelWidth={26} label="Resource ID">
  58. <Input
  59. className="width-30"
  60. value={dataSourceConfig.jsonData.azureEndpointResourceId || ''}
  61. onChange={onResourceIdChange}
  62. />
  63. </InlineField>
  64. </InlineFieldRow>
  65. )}
  66. </div>
  67. </>
  68. )}
  69. </>
  70. );
  71. };
  72. export default AzureAuthSettings;