import React, { SyntheticEvent, useState } from 'react'; import { DataSourcePluginOptionsEditorProps, onUpdateDatasourceJsonDataOption, onUpdateDatasourceSecureJsonDataOption, SelectableValue, updateDatasourcePluginJsonDataOption, updateDatasourcePluginResetOption, } from '@grafana/data'; import { Alert, InlineSwitch, FieldSet, InlineField, InlineFieldRow, Input, Select, SecretInput, Link, } from '@grafana/ui'; import { config } from 'app/core/config'; import { ConnectionLimits } from 'app/features/plugins/sql/components/configuration/ConnectionLimits'; import { TLSSecretsConfig } from 'app/features/plugins/sql/components/configuration/TLSSecretsConfig'; import { useMigrateDatabaseFields } from 'app/features/plugins/sql/components/configuration/useMigrateDatabaseFields'; import { PostgresOptions, PostgresTLSMethods, PostgresTLSModes, SecureJsonData } from '../types'; import { useAutoDetectFeatures } from './useAutoDetectFeatures'; export const postgresVersions: Array> = [ { label: '9.0', value: 900 }, { label: '9.1', value: 901 }, { label: '9.2', value: 902 }, { label: '9.3', value: 903 }, { label: '9.4', value: 904 }, { label: '9.5', value: 905 }, { label: '9.6', value: 906 }, { label: '10', value: 1000 }, { label: '11', value: 1100 }, { label: '12', value: 1200 }, { label: '13', value: 1300 }, { label: '14', value: 1400 }, { label: '15', value: 1500 }, ]; export const PostgresConfigEditor = (props: DataSourcePluginOptionsEditorProps) => { const [versionOptions, setVersionOptions] = useState(postgresVersions); useAutoDetectFeatures({ props, setVersionOptions }); useMigrateDatabaseFields(props); const { options, onOptionsChange } = props; const jsonData = options.jsonData; const onResetPassword = () => { updateDatasourcePluginResetOption(props, 'password'); }; const tlsModes: Array> = [ { value: PostgresTLSModes.disable, label: 'disable' }, { value: PostgresTLSModes.require, label: 'require' }, { value: PostgresTLSModes.verifyCA, label: 'verify-ca' }, { value: PostgresTLSModes.verifyFull, label: 'verify-full' }, ]; const tlsMethods: Array> = [ { value: PostgresTLSMethods.filePath, label: 'File system path' }, { value: PostgresTLSMethods.fileContent, label: 'Certificate content' }, ]; const onJSONDataOptionSelected = (property: keyof PostgresOptions) => { return (value: SelectableValue) => { updateDatasourcePluginJsonDataOption(props, property, value.value); }; }; const onTimeScaleDBChanged = (event: SyntheticEvent) => { updateDatasourcePluginJsonDataOption(props, 'timescaledb', event.currentTarget.checked); }; const onDSOptionChanged = (property: keyof PostgresOptions) => { return (event: SyntheticEvent) => { onOptionsChange({ ...options, ...{ [property]: event.currentTarget.value } }); }; }; const labelWidthSSLDetails = 25; const labelWidthConnection = 20; const labelWidthShort = 20; return ( <>
{options.jsonData.sslmode !== PostgresTLSModes.disable ? ( This option determines how TLS/SSL certifications are configured. Selecting File system path will allow you to configure certificates by specifying paths to existing certificates on the local file system where Grafana is running. Be sure that the file is readable by the user executing the Grafana process.

Selecting Certificate content will allow you to configure certificates by specifying its content. The content will be stored encrypted in Grafana's database. When connecting to the database the certificates will be written as files to Grafana's configured data path on the local file system. } >
) : null}
{config.featureToggles.secureSocksDatasourceProxy && (
onOptionsChange({ ...options, jsonData: { ...options.jsonData, enableSecureSocksProxy: event!.currentTarget.checked }, }) } />
)} {jsonData.sslmode !== PostgresTLSModes.disable ? (
{jsonData.tlsConfigurationMethod === PostgresTLSMethods.fileContent ? ( ) : ( <> If the selected TLS/SSL mode requires a server root certificate, provide the path to the file here. } labelWidth={labelWidthSSLDetails} label="TLS/SSL Root Certificate" > To authenticate with an TLS/SSL client certificate, provide the path to the file here. Be sure that the file is readable by the user executing the grafana process. } labelWidth={labelWidthSSLDetails} label="TLS/SSL Client Certificate" > To authenticate with a client TLS/SSL certificate, provide the path to the corresponding key file here. Be sure that the file is only readable by the user executing the grafana process. } labelWidth={labelWidthSSLDetails} label="TLS/SSL Client Key" > )}
) : null}
TimescaleDB is a time-series database built as a PostgreSQL extension. If enabled, Grafana will use time_bucket in the $__timeGroup macro and display TimescaleDB specific aggregate functions in the query builder. } labelWidth={labelWidthShort} label="TimescaleDB" htmlFor="timescaledb" > A lower limit for the auto group by time interval. Recommended to be set to write frequency, for example 1m if your data is written every minute. } labelWidth={labelWidthShort} label="Min time interval" >
The database user should only be granted SELECT permissions on the specified database & tables you want to query. Grafana does not validate that queries are safe so queries can contain any SQL statement. For example, statements like DELETE FROM user; and DROP TABLE user; would be executed. To protect against this we Highly recommend you create a specific PostgreSQL user with restricted permissions. Check out the{' '} PostgreSQL Data Source Docs {' '} for more information. ); };