Skip to content

Commit

Permalink
feat: add support for auth to grafana datasource plugin (#844)
Browse files Browse the repository at this point in the history
  • Loading branch information
kolesnikovae committed Feb 16, 2022
1 parent 8bd6eb8 commit 8712404
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ datasources:
uid: pyroscope
jsonData:
path: http://pyroscope:4040
# secureJsonData:
# apiKey: {YOUR_API_KEY}
53 changes: 47 additions & 6 deletions grafana-plugin/datasource/src/ConfigEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
import React, { ChangeEvent, PureComponent } from 'react';
import { LegacyForms } from '@grafana/ui';
import { DataSourcePluginOptionsEditorProps } from '@grafana/data';
import { MyDataSourceOptions } from './types';
import { MyDataSourceOptions, MySecureJsonData } from './types';

const { /* SecretFormField, */ FormField } = LegacyForms;
const { SecretFormField, FormField } = LegacyForms;

type Props = DataSourcePluginOptionsEditorProps<MyDataSourceOptions>;

// interface State {}

export class ConfigEditor extends PureComponent<Props, unknown> {
onPathChange = (event: ChangeEvent<HTMLInputElement>) => {
const { onOptionsChange, options } = this.props;
Expand All @@ -20,22 +18,65 @@ export class ConfigEditor extends PureComponent<Props, unknown> {
onOptionsChange({ ...options, jsonData });
};

// API Key: secure field (only sent to the backend)
onAPIKeyChange = (event: ChangeEvent<HTMLInputElement>) => {
const { onOptionsChange, options } = this.props;
onOptionsChange({
...options,
secureJsonData: {
...options.secureJsonData,
apiKey: event.target.value,
},
});
};

onAPIKeyReset = () => {
const { onOptionsChange, options } = this.props;
onOptionsChange({
...options,
secureJsonFields: {
...options.secureJsonFields,
apiKey: false,
},
secureJsonData: {
...options.secureJsonData,
apiKey: '',
},
});
};

render() {
const { options } = this.props;
const { jsonData } = options;
const { jsonData, secureJsonFields } = options;
const secureJsonData = (options.secureJsonData || {}) as MySecureJsonData;

return (
<div className="gf-form-group">
<div className="gf-form">
<FormField
label="Pyroscope instance"
labelWidth={6}
labelWidth={10}
inputWidth={20}
onChange={this.onPathChange}
value={jsonData.path || ''}
placeholder="url to your pyroscope instance"
/>
</div>

<div className="gf-form">
<SecretFormField
label="API Key"
labelWidth={10}
inputWidth={20}
onChange={this.onAPIKeyChange}
onReset={this.onAPIKeyReset}
isConfigured={
(secureJsonFields && secureJsonFields.apiKey) as boolean
}
value={secureJsonData.apiKey || ''}
placeholder="Your API Key"
/>
</div>
</div>
);
}
Expand Down
8 changes: 7 additions & 1 deletion grafana-plugin/datasource/src/plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@
"routes": [
{
"path": "render",
"url": "{{ .JsonData.path }}"
"url": "{{ .JsonData.path }}",
"headers": [
{
"name": "Authorization",
"content": "Bearer {{ .SecureJsonData.apiKey }}"
}
]
}
],
"dependencies": {
Expand Down
6 changes: 3 additions & 3 deletions grafana-plugin/datasource/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ export interface MyDataSourceOptions extends DataSourceJsonData {
/**
* Value that is used in the backend, but never sent over HTTP to the frontend
*/
// export interface MySecureJsonData {
// apiKey?: string;
// }
export interface MySecureJsonData {
apiKey?: string;
}

0 comments on commit 8712404

Please sign in to comment.