Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Merge pull request #113 from influxdata/fix/secure-json-fields
Browse files Browse the repository at this point in the history
fix: move password and token into secure json data
  • Loading branch information
helenosheaa authored Mar 13, 2023
2 parents e953de6 + 3aa5748 commit 0d13e6a
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 29 deletions.
8 changes: 8 additions & 0 deletions pkg/flightsql/flightsql.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ func NewDatasource(settings backend.DataSourceInstanceSettings) (instancemgmt.In
return nil, fmt.Errorf("config: %s", err)
}

if token, exists := settings.DecryptedSecureJSONData["token"]; exists {
cfg.Token = token
}

if password, exists := settings.DecryptedSecureJSONData["password"]; exists {
cfg.Password = password
}

if err := cfg.validate(); err != nil {
return nil, fmt.Errorf("config validation: %v", err)
}
Expand Down
1 change: 0 additions & 1 deletion src/components/BuilderView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ export function BuilderView({query, datasource, onChange, fromRawSql}: any) {
}
}
if (!tableExists) {
console.log('query', query)
query.queryText = ''
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/components/ConfigEditor.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,15 @@ const setup = async (optionOverrides?: object) => {
isDefault: false,
jsonData: {
host: '',
token: '',
secure: true,
username: '',
password: '',
selectedAuthType: '',
metadata: [],
},
secureJsonFields: {},
secureJsonFields: {
token: false,
password: false,
},
readOnly: false,
withCredentials: false,
typeName: '',
Expand Down
20 changes: 12 additions & 8 deletions src/components/ConfigEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useEffect, useState} from 'react'
import {InlineSwitch, FieldSet, InlineField, SecretInput, Input, Select, InlineFieldRow, InlineLabel} from '@grafana/ui'
import {DataSourcePluginOptionsEditorProps, SelectableValue} from '@grafana/data'
import {FlightSQLDataSourceOptions, authTypeOptions} from '../types'
import {FlightSQLDataSourceOptions, authTypeOptions, SecureJsonData} from '../types'
import {
onHostChange,
onTokenChange,
Expand All @@ -13,11 +13,15 @@ import {
onValueChange,
addMetaData,
removeMetaData,
onResetToken,
onResetPassword,
} from './utils'

export function ConfigEditor(props: DataSourcePluginOptionsEditorProps<FlightSQLDataSourceOptions>) {
export function ConfigEditor(props: DataSourcePluginOptionsEditorProps<FlightSQLDataSourceOptions, SecureJsonData>) {
const {options, onOptionsChange} = props
const {jsonData} = options
const {secureJsonData, secureJsonFields} = options

const [selectedAuthType, setAuthType] = useState<SelectableValue<string>>({
value: jsonData?.selectedAuthType,
label: jsonData?.selectedAuthType,
Expand Down Expand Up @@ -70,11 +74,11 @@ export function ConfigEditor(props: DataSourcePluginOptionsEditorProps<FlightSQL
width={40}
name="token"
type="text"
value={jsonData.token || ''}
value={secureJsonData?.token || ''}
placeholder="****************"
onChange={(e) => onTokenChange(e, options, onOptionsChange)}
onReset={() => onTokenChange(null, options, onOptionsChange)}
isConfigured={false}
onReset={() => onResetToken(options, onOptionsChange)}
isConfigured={secureJsonFields?.token}
></SecretInput>
</InlineField>
)}
Expand All @@ -95,11 +99,11 @@ export function ConfigEditor(props: DataSourcePluginOptionsEditorProps<FlightSQL
width={40}
name="password"
type="text"
value={jsonData.password || ''}
value={secureJsonData?.password || ''}
placeholder="****************"
onChange={(e) => onPasswordChange(e, options, onOptionsChange)}
onReset={() => onPasswordChange(null, options, onOptionsChange)}
isConfigured={false}
onReset={() => onResetPassword(options, onOptionsChange)}
isConfigured={secureJsonFields?.password}
></SecretInput>
</InlineField>
</InlineFieldRow>
Expand Down
52 changes: 40 additions & 12 deletions src/components/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,6 @@ export const onHostChange = (event: any, options: any, onOptionsChange: any) =>
onOptionsChange({...options, jsonData})
}

export const onTokenChange = (event: any, options: any, onOptionsChange: any) => {
const jsonData = {
...options.jsonData,
token: event?.target?.value || '',
}
onOptionsChange({...options, jsonData})
}

export const onSecureChange = (options: any, onOptionsChange: any) => {
const jsonData = {
...options.jsonData,
Expand All @@ -131,11 +123,11 @@ export const onUsernameChange = (event: any, options: any, onOptionsChange: any)
}

export const onPasswordChange = (event: any, options: any, onOptionsChange: any) => {
const jsonData = {
...options.jsonData,
password: event.target.value,
const secureJsonData = {
...options.secureJsonData,
password: event?.target?.value || '',
}
onOptionsChange({...options, jsonData})
onOptionsChange({...options, secureJsonData})
}

export const onAuthTypeChange = (selectedAuthType: any, options: any, onOptionsChange: any) => {
Expand Down Expand Up @@ -190,3 +182,39 @@ export const onValueChange = (event: any, metaDataArr: any, index: any, setMetaD
newMetaValues[index]['value'] = event.target.value
setMetaData(newMetaValues)
}

export const onTokenChange = (event: any, options: any, onOptionsChange: any) => {
const secureJsonData = {
...options.secureJsonData,
token: event?.target?.value || '',
}
onOptionsChange({...options, secureJsonData})
}

export const onResetToken = (options: any, onOptionsChange: any) => {
onOptionsChange({
...options,
secureJsonFields: {
...options.secureJsonFields,
token: false,
},
secureJsonData: {
...options.secureJsonData,
token: '',
},
})
}

export const onResetPassword = (options: any, onOptionsChange: any) => {
onOptionsChange({
...options,
secureJsonFields: {
...options.secureJsonFields,
password: false,
},
secureJsonData: {
...options.secureJsonData,
password: '',
},
})
}
7 changes: 4 additions & 3 deletions src/mock-datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ export const mockDatasourceOptions: DataSourcePluginOptionsEditorProps<FlightSQL
isDefault: false,
jsonData: {
host: '',
token: '',
secure: true,
username: '',
password: '',
selectedAuthType: '',
metadata: [],
},
secureJsonFields: {},
secureJsonFields: {
token: false,
password: false,
},
readOnly: false,
withCredentials: false,
typeName: '',
Expand Down
4 changes: 2 additions & 2 deletions src/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import {DataSourcePlugin} from '@grafana/data'
import {FlightSQLDataSource} from './datasource'
import {ConfigEditor} from './components/ConfigEditor'
import {QueryEditor} from './components/QueryEditor'
import {SQLQuery, FlightSQLDataSourceOptions} from './types'
import {SQLQuery, FlightSQLDataSourceOptions, SecureJsonData} from './types'

export const plugin = new DataSourcePlugin<FlightSQLDataSource, SQLQuery, FlightSQLDataSourceOptions>(
export const plugin = new DataSourcePlugin<FlightSQLDataSource, SQLQuery, FlightSQLDataSourceOptions, SecureJsonData>(
FlightSQLDataSource
)
.setConfigEditor(ConfigEditor)
Expand Down
5 changes: 5 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ export interface FlightSQLDataSourceOptions extends DataSourceJsonData {
metadata?: any
}

export interface SecureJsonData {
password?: string
token?: string
}

export type TablesResponse = {
tables: string[]
}
Expand Down

0 comments on commit 0d13e6a

Please sign in to comment.