Skip to content

Commit

Permalink
feat(dashboardSecurity): enhance typing and add docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Desvelao committed Dec 19, 2024
1 parent 3134db4 commit 3644624
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 34 deletions.
67 changes: 67 additions & 0 deletions plugins/wazuh-core/public/services/dashboard-security/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Dashboard security

The `dashboardSecurity` service is created in the core plugin and manage the security related to the Wazuh dashboard.

- Fetch data about the security platform (Wazuh dashboard security enabled or disabled)
- Store information about the current user account data
- administrator
- administrator requirements
- Expose hooks and HOCs for using with ReactJS

## Get account data

### Using the service

```ts
plugins.wazuhCore.dashboardSecurity.account;
```

### In ReactJS components

- hook

```ts
const MyComponent = props => {
const [dashboardSecurityAccount, setDashboardSecurityAccount] =
getWazuhCorePlugin().hooks.useDashboardSecurityAccount();
};
```

- HOC

```ts
const MyComponent = getWazuhCorePlugin().hocs.withDashboardSecurityAccount(
({ dashboardSecurityAccount }) => {
// dashboardSecurityAccount contains the dashboard account data
},
);
```

## Get if the user is an administrator

### Using the service

```ts
plugins.wazuhCore.dashboardSecurity.account.administrator;
```

### In ReactJS components

- hook

```ts
const MyComponent = props => {
const dashboardSecurityAccountAdmin =
getWazuhCorePlugin().hooks.useDashboardSecurityAccountAdmin();
};
```

- HOC

```ts
const MyComponent = getWazuhCorePlugin().hocs.withDashboardSecurityAccountAdmin(
({ dashboardSecurityAccountAdmin }) => {
// dashboardSecurityAccountAdmin contains if the user is admin or not
},
);
```
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import { BehaviorSubject } from 'rxjs';
import jwtDecode from 'jwt-decode';
import { WAZUH_ROLE_ADMINISTRATOR_ID } from '../../common/constants';
import { Logger } from '../../common/services/configuration';

function createDashboardSecurityHooks({ getAccount, getAccount$ }) {
return {
useDashboardSecurityAccount: function useDashboardSecurityAccount() {
return [getAccount(), getAccount$()];
},
useDashboardSecurityIsAdmin: function useDashboardSecurityIsAdmin() {
return getAccount()?.administrator;
},
};
}

export interface DashboardSecurityAccount {
administrator: boolean;
administrator_requirements: string | null;
}
export class DashboardSecurity {
import { Logger } from '../../../common/services/configuration';
import { WAZUH_ROLE_ADMINISTRATOR_ID } from '../../../common/constants';
import { createDashboardSecurityHooks } from './ui/hooks/creator';
import { createDashboardSecurityHOCs } from './ui/hocs/creator';
import {
DashboardSecurityServiceAccount,
DashboardSecurityService,
DashboardSecurityServiceSetupDeps,
DashboardSecurityServiceSetupReturn,
} from './types';

export class DashboardSecurity implements DashboardSecurityService {
private securityPlatform = '';
public account$: BehaviorSubject<DashboardSecurityAccount>;
public account$: BehaviorSubject<DashboardSecurityServiceAccount>;

constructor(
private readonly logger: Logger,
Expand Down Expand Up @@ -57,28 +50,22 @@ export class DashboardSecurity {

async setup({
updateData$,
}: {
updateData$: BehaviorSubject<{ token: string }>;
}): Promise<{
hooks: {
useDashboardSecurityAccount: () => [DashboardSecurityAccount];
useDashboardSecurityIsAdmin: () => boolean;
};
}> {
}: DashboardSecurityServiceSetupDeps): Promise<DashboardSecurityServiceSetupReturn> {
this.logger.debug('Setup');

let hooks;
let hooks, hocs;

try {
this.logger.debug('Creating hooks');
this.logger.debug('Creating ui utilities');

hooks = createDashboardSecurityHooks({
getAccount: () => this.account$.getValue(),
getAccount$: () => this.account$,
account$: this.account$,
});

hocs = createDashboardSecurityHOCs(hooks);
this.logger.debug('Created hooks');
} catch (error) {
this.logger.error(`Error creating the hooks: ${error.message}`);
this.logger.error(`Error creating the ui utilities: ${error.message}`);
throw error;
}

Expand All @@ -94,13 +81,16 @@ export class DashboardSecurity {

// Update the dashboard security account information based on server API token
updateData$.subscribe(({ token }: { token: string }) => {
const jwtPayload = token ? jwtDecode(token) : null;
const jwtPayload: {
rbac_roles?: number[];
} | null = token ? jwtDecode(token) : null;

this.account$.next(this.getAccountFromJWTAPIDecodedToken(jwtPayload));
});

return {
hooks,
hocs,
};
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './dashboard-security';
export * from './types';
40 changes: 40 additions & 0 deletions plugins/wazuh-core/public/services/dashboard-security/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import { BehaviorSubject } from 'rxjs';

export interface DashboardSecurityServiceAccount {
administrator: boolean;
administrator_requirements: string | null;
}

export interface DashboardSecurityServiceSetupReturn {
hooks: {
useDashboardSecurityAccount: () => [
DashboardSecurityServiceAccount,
(accountData: any) => void,
];
useDashboardSecurityIsAdmin: () => boolean;
};
hocs: {
// FIXME: enhance typing
withDashboardSecurityAccount: (
WrappedComponent: React.ElementType,
) => (props: any) => React.ElementRef;
withDashboardSecurityAccountAdmin: (
WrappedComponent: React.ElementType,
) => (props: any) => React.ElementRef;
};
}

export interface DashboardSecurityServiceSetupDeps {
updateData$: BehaviorSubject<DashboardSecurityServiceAccount>;
}

export interface DashboardSecurityService {
account: DashboardSecurityServiceAccount;
account$: BehaviorSubject<DashboardSecurityServiceAccount>;
setup: (
deps: DashboardSecurityServiceSetupDeps,
) => Promise<DashboardSecurityServiceSetupReturn>;
start: () => void;
stop: () => void;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import React from 'react';
import { DashboardSecurityServiceSetupReturn } from '../../types';

export function createDashboardSecurityHOCs({
useDashboardSecurityAccount,
useDashboardSecurityIsAdmin,
}: DashboardSecurityServiceSetupReturn['hooks']): DashboardSecurityServiceSetupReturn['hocs'] {
const withDashboardSecurityAccount = (WrappedComponent: React.ElementType) =>
function WithDashboardSecurityAccount(props: any) {
const dashboardSecurityAccount = useDashboardSecurityAccount();

return (
<WrappedComponent
{...props}
dashboardSecurityAccount={dashboardSecurityAccount}
/>
);
};

const withDashboardSecurityAccountAdmin = (
WrappedComponent: React.ElementType,
) =>
function WithDashboardSecurityAccountAdmin(props: any) {
const dashboardSecurityAccountAdmin = useDashboardSecurityIsAdmin();

return (
<WrappedComponent
{...props}
dashboardSecurityAccountAdmin={dashboardSecurityAccountAdmin}
/>
);
};

return {
withDashboardSecurityAccount,
withDashboardSecurityAccountAdmin,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
DashboardSecurityService,
DashboardSecurityServiceSetupReturn,
} from '../../types';

export function createDashboardSecurityHooks({
account$,
}: {
account$: DashboardSecurityService['account$'];
}): DashboardSecurityServiceSetupReturn['hooks'] {
const setAccount = data => {
account$.next(data);
};

return {
useDashboardSecurityAccount: function useDashboardSecurityAccount() {
return [account$.getValue(), setAccount];
},
useDashboardSecurityIsAdmin: function useDashboardSecurityIsAdmin() {
return account$.getValue()?.administrator;
},
};
}

0 comments on commit 3644624

Please sign in to comment.