Skip to content

Commit

Permalink
Fix bugs in kibana roles lib
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-tavares committed Jul 20, 2023
1 parent 2beaa78 commit 138c117
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 23 deletions.
Expand Up @@ -11,7 +11,7 @@ import * as path from 'path';
import { cloneDeep } from 'lodash';
import { FeaturesPrivileges, Role, RoleIndexPrivilege } from '@kbn/security-plugin/common';

type ServerlessRoleName =
export type ServerlessRoleName =
| 't1_analyst'
| 't2_analyst'
| 't3_analyst'
Expand Down
Expand Up @@ -528,7 +528,7 @@ endpoint_operations_manager:
- .siem-signals-*
- .preview.alerts-security*
- .internal.preview.alerts-security*
- privileges:
privileges:
- read
- write
- manage
Expand Down Expand Up @@ -572,5 +572,5 @@ endpoint_operations_manager:
resources: "*"
- application: spaces
privileges:
- "*"
- all
resources: "*"
Expand Up @@ -9,19 +9,32 @@

import { KbnClient } from '@kbn/test';
import { Role } from '@kbn/security-plugin/common';
import { ToolingLog } from '@kbn/tooling-log';
import { inspect } from 'util';
import {
getServerlessSecurityKibanaRoleDefinitions,
ServerlessSecurityRoles,
} from './kibana_roles';

interface LoadedRoleAndUser {
export interface LoadedRoleAndUser {
role: string;
username: string;
password: string;
}

export class RoleAndUserLoader<R extends Record<string, Role> = Record<string, Role>> {
constructor(private readonly kbnClient: KbnClient, private readonly roles: R) {}
protected readonly logPromiseError: (error: Error) => never;

constructor(
protected readonly kbnClient: KbnClient,
protected readonly logger: ToolingLog,
protected readonly roles: R
) {
this.logPromiseError = (error) => {
this.logger.error(inspect(error, { depth: 5 }));
throw error;
};
}

async load(name: keyof R): Promise<LoadedRoleAndUser> {
const role = this.roles[name];
Expand All @@ -40,34 +53,38 @@ export class RoleAndUserLoader<R extends Record<string, Role> = Record<string, R
private async createRole(role: Role): Promise<void> {
const { name: roleName, ...roleDefinition } = role;

await this.kbnClient.request({
method: 'PUT',
path: `/api/security/role/${name}?createOnly=true`,
body: roleDefinition,
});
await this.kbnClient
.request({
method: 'PUT',
path: `/api/security/role/${roleName}?createOnly=true`,
body: roleDefinition,
})
.catch(this.logPromiseError);
}

private async createUser(
username: string,
password: string,
roles: string[] = []
): Promise<void> {
await this.kbnClient.request({
method: 'POST',
path: `/internal/security/users/${username}`,
body: {
username,
password,
roles,
full_name: username,
email: '',
},
});
await this.kbnClient
.request({
method: 'POST',
path: `/internal/security/users/${username}`,
body: {
username,
password,
roles,
full_name: username,
email: '',
},
})
.catch(this.logPromiseError);
}
}

export class SecurityRoleAndUserLoader extends RoleAndUserLoader<ServerlessSecurityRoles> {
constructor(kbnClient: KbnClient) {
super(kbnClient, getServerlessSecurityKibanaRoleDefinitions());
constructor(kbnClient: KbnClient, logger: ToolingLog) {
super(kbnClient, logger, getServerlessSecurityKibanaRoleDefinitions());
}
}

0 comments on commit 138c117

Please sign in to comment.