import { Role } from '../../../../security/common/model' ; import { callKibana , isAxiosError } from './call_kibana' ; import { Elasticsearch , Kibana } from '../create_apm_users_and_roles' ; type Privilege = [ ] | [ 'read' ] | [ 'all' ] ; export interface KibanaPrivileges { base ? : Privilege ; feature ? : Record < string , Privilege > ; } export type RoleType = Omit < Role , 'name' | 'metadata' > ; export async function createRole ( { elasticsearch , kibana , roleName , role , } ) { const roleFound = await getRole ( { elasticsearch , kibana , roleName , } ) ; if ( roleFound ) { console . log ( `Skipping: Role "${ roleName }" already exists` ) ; return Promise . resolve ( ) ; } await callKibana ( { elasticsearch , kibana , options : { method : 'PUT' , url : `/api/security/role/${ roleName }` , data : { metadata : { version : 1 } , ... role , } , } , } ) ; console . log ( `Created role "${ roleName }"` ) ; } async function getRole ( { elasticsearch , kibana , roleName , } ) { try { return await callKibana ( { elasticsearch , kibana , options : { method : 'GET' , url : `/api/security/role/${ roleName }` , } , } ) ; } catch ( e ) { if ( isAxiosError ( e ) && e . response ?. status === 404 ) { return null ; } throw e ; } }