Skip to content

Commit 254c559

Browse files
author
Nicolai Buchwitz
committed
fix: make password available via securejsondata
1 parent a1c670e commit 254c559

File tree

3 files changed

+43
-10
lines changed

3 files changed

+43
-10
lines changed

pkg/mqtt/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ type Options struct {
1313
Host string `json:"host"`
1414
Port uint16 `json:"port"`
1515
Username string `json:"username"`
16-
Password string `json:"-"`
16+
Password string `json:"password"`
1717
}
1818

1919
type StreamMessage struct {

pkg/plugin/datasource.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,15 @@ func NewMQTTInstance(s backend.DataSourceInstanceSettings) (instancemgmt.Instanc
3030

3131
func getDatasourceSettings(s backend.DataSourceInstanceSettings) (*mqtt.Options, error) {
3232
settings := &mqtt.Options{}
33+
3334
if err := json.Unmarshal(s.JSONData, settings); err != nil {
3435
return nil, err
3536
}
37+
38+
if password, exists := s.DecryptedSecureJSONData["password"]; exists {
39+
settings.Password = password
40+
}
41+
3642
return settings, nil
3743
}
3844

src/ConfigEditor.tsx

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,42 @@ import { DataSourcePluginOptionsEditorProps } from '@grafana/data';
44
import { MqttDataSourceOptions, MqttSecureJsonData } from './types';
55
import { handlerFactory } from './handleEvent';
66

7-
interface Props extends DataSourcePluginOptionsEditorProps<MqttDataSourceOptions> {}
7+
interface Props extends DataSourcePluginOptionsEditorProps<MqttDataSourceOptions, MqttSecureJsonData> {}
88

99
export const ConfigEditor = (props: Props) => {
1010
const {
1111
onOptionsChange,
1212
options,
13-
options: { jsonData, secureJsonData },
13+
options: { jsonData, secureJsonData, secureJsonFields },
1414
} = props;
1515
const { host, port, username } = jsonData;
16-
const { password } = (secureJsonData ?? {}) as MqttSecureJsonData;
16+
17+
// const { password } = (secureJsonData ?? {}) as MqttSecureJsonData;
1718
const handleChange = handlerFactory(options, onOptionsChange);
1819

20+
const onPasswordChange = (event: ChangeEvent<HTMLInputElement>) => {
21+
onOptionsChange({
22+
...options,
23+
secureJsonData: {
24+
password: event.target.value,
25+
},
26+
});
27+
};
28+
29+
const onResetPassword = () => {
30+
onOptionsChange({
31+
...options,
32+
secureJsonFields: {
33+
...options.secureJsonFields,
34+
password: false,
35+
},
36+
secureJsonData: {
37+
...options.secureJsonData,
38+
password: '',
39+
},
40+
});
41+
};
42+
1943
return (
2044
<Form onSubmit={() => {}}>
2145
{() => (
@@ -60,12 +84,15 @@ export const ConfigEditor = (props: Props) => {
6084
name="password"
6185
css=""
6286
autoComplete="off"
63-
placeholder="************************"
64-
value={password}
65-
onChange={(event: ChangeEvent<HTMLInputElement>) => {
66-
handleChange('secureJsonData.password')(event);
67-
handleChange('secureJsonFields.password', Boolean)(event);
68-
}}
87+
// placeholder="************************"
88+
placeholder={secureJsonFields?.password ? 'configured' : ''}
89+
value={secureJsonData?.password ?? ''}
90+
onChange={onPasswordChange}
91+
onReset={onResetPassword}
92+
// onChange={(event: ChangeEvent<HTMLInputElement>) => {
93+
// handleChange('secureJsonData.password')(event);
94+
// handleChange('secureJsonFields.password', Boolean)(event);
95+
// }}
6996
/>
7097
</Field>
7198
</FieldSet>

0 commit comments

Comments
 (0)