Skip to content

Commit 30a7d8d

Browse files
TheSpacyCatBenediktSeidl
authored andcommitted
check for automation user in testDatasource()
1 parent 4616ac9 commit 30a7d8d

File tree

3 files changed

+17
-0
lines changed

3 files changed

+17
-0
lines changed

src/DataSource.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,8 @@ export class DataSource extends DataSourceApi<CmkQuery> {
5454
}
5555
return this.restBackend;
5656
}
57+
58+
getUsername(): string {
59+
return this.instanceSettings.jsonData.username!;
60+
}
5761
}

src/backend/rest.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,24 @@ export default class RestApiBackend implements Backend {
8989
'The data source specified the Checkmk Enterprise Edition, but Checkmk Raw Edition was detected. Please choose the raw edition in the data source settings.'
9090
);
9191
}
92+
// The REST API would be ok with other users, but the autocompleter are not
93+
if (!(await this.isAutomationUser(this.datasource.getUsername()))) {
94+
throw new Error('This data source must authenticate against Checkmk using an automation user.');
95+
}
9296
return {
9397
status: 'success',
9498
message: `Data source is working, reached version ${checkMkVersion} of checkmk`,
9599
title: 'Success',
96100
};
97101
}
98102

103+
async isAutomationUser(username: string): Promise<boolean> {
104+
const response = await this.api<any>({
105+
url: `/objects/user_config/${username}`,
106+
});
107+
return response.data['extensions']['auth_option']['auth_type'] === 'automation';
108+
}
109+
99110
async api<T>(request: BackendSrvRequest): Promise<FetchResponse<T>> {
100111
const defaults = {
101112
headers: { 'Content-Type': 'application/json' },

src/backend/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,6 @@ export interface DatasourceOptions {
1111
getBackend: () => Backend;
1212
getEdition: () => Edition;
1313
getUrl: () => string | undefined;
14+
15+
getUsername(): string;
1416
}

0 commit comments

Comments
 (0)