Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add frontend serverSecurity service #7026

Merged
Show file tree
Hide file tree
Changes from 26 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
6effbcd
feat(core): create serverSecurity service
Desvelao Sep 27, 2024
e2dd1be
fix: move constant to begining of file
Desvelao Nov 28, 2024
e10726d
Apply suggestions from code review
Desvelao Nov 28, 2024
bb3ca50
fix: add enum related to ServerButtonPermission types
Desvelao Nov 28, 2024
a142c2f
Merge branch 'master' of https://github.com/wazuh/wazuh-kibana-app in…
Desvelao Nov 28, 2024
aee0026
chore(changelog): add entry
Desvelao Nov 28, 2024
1ada4ae
fix: apply code suggestions and add typing to ServerSecurityPermissions
Desvelao Nov 28, 2024
9899a39
Apply suggestions from code review
Desvelao Nov 28, 2024
868b5a4
fix: typing
Desvelao Nov 28, 2024
df13364
chore(prettier): code format
Desvelao Nov 28, 2024
c99104a
Merge branch 'master' of https://github.com/wazuh/wazuh-kibana-app in…
Desvelao Dec 10, 2024
0f4be1d
fix(serverSecurity): lint
Desvelao Dec 10, 2024
84a0824
fix(serverSecurity): lint
Desvelao Dec 10, 2024
2a69f36
fix(serverSecurity): lint
Desvelao Dec 10, 2024
80ef166
fix(serverSecurity): remove unused type comment
Desvelao Dec 10, 2024
d3c6f49
Merge branch 'master' into enhancement/6959-extract-common-services-f…
guidomodarelli Dec 16, 2024
c1fdc9b
feat: change the loading render of withServerUserLogged HOC to seems …
Desvelao Dec 18, 2024
a185ab4
feat: move user data to serverSecurity and dashboardSecurity services
Desvelao Dec 19, 2024
3134db4
change: move dashboardSecurity service to services folder
Desvelao Dec 19, 2024
3644624
feat(dashboardSecurity): enhance typing and add docs
Desvelao Dec 19, 2024
145b71c
fix: test
Desvelao Dec 19, 2024
8f7c944
feat(core): enhance types and expose dashboardSecurity HOCS
Desvelao Dec 19, 2024
5258fe2
fix: serverSecurity and dashboardSecurity bugs and add comment to the…
Desvelao Dec 19, 2024
12187ab
chore: request changes
Desvelao Dec 20, 2024
1cd6154
fix: request changes
Desvelao Dec 20, 2024
a2e5df5
feat(dashboardSecurity): enhance the docs
Desvelao Dec 20, 2024
49a4509
fix(dashboardSecurity): refactor management of securityPlatform variable
Desvelao Dec 20, 2024
e98d91c
fix(dashboardSecurity): add type for securityPlatform variable
Desvelao Dec 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ All notable changes to the Wazuh app project will be documented in this file.
- Support for Wazuh 5.0.0
- Added creation of report definition when creating dashboard by reference and the button to reset the report [#7091](https://github.com/wazuh/wazuh-dashboard-plugins/pull/7091)
- Added a frontend http client to core plugin [#7000](https://github.com/wazuh/wazuh-dashboard-plugins/pull/7000)
- Added serverSecurity service to core plugin [#7026](https://github.com/wazuh/wazuh-dashboard-plugins/pull/7026)
- Added an initilization service to core plugin to run the initilization tasks related to user scope [#7145](https://github.com/wazuh/wazuh-dashboard-plugins/pull/7145)

### Removed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,10 @@ export const PromptCheckIndex = (props: {
);
};

const mapStateToProps = state => {
return {
vulnerabilitiesStatesindexPatternID:
state.appConfig.data['vulnerabilities.pattern'],
};
};
const mapStateToProps = state => ({
vulnerabilitiesStatesindexPatternID:
state.appConfig.data['vulnerabilities.pattern'],
});

export const withVulnerabilitiesStateDataSource = compose(
connect(mapStateToProps),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const initializationTask = {
export const INITIALIZATION_TASK = {
RUN_STATUS: {
NOT_STARTED: 'not_started',
RUNNING: 'running',
Expand Down
8 changes: 4 additions & 4 deletions plugins/wazuh-core/common/services/initialization/types.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { initializationTask } from './constants';
import { INITIALIZATION_TASK } from './constants';

type RunStatusEnum = (typeof initializationTask)['RUN_STATUS'];
type RunStatusEnum = (typeof INITIALIZATION_TASK)['RUN_STATUS'];

export type InitializationTaskRunStatus = RunStatusEnum[keyof RunStatusEnum];

type RunResultEnum = (typeof initializationTask)['RUN_RESULT'];
type RunResultEnum = (typeof INITIALIZATION_TASK)['RUN_RESULT'];

export type InitializationTaskRunResult = RunResultEnum[keyof RunResultEnum];

type ContextEnum = (typeof initializationTask)['CONTEXT'];
type ContextEnum = (typeof INITIALIZATION_TASK)['CONTEXT'];

export type InitializationTaskContext = ContextEnum[keyof ContextEnum];
6 changes: 4 additions & 2 deletions plugins/wazuh-core/docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ This plugin provides some core services:

## Frontend

- Configuration: manage the plugins configuration
- Utils
- Constants
- Utils
- Configuration: manage the plugins configuration
- Dashboard Security: manage the security related to Wazuh dashboard
- Server Security: manage the security related to Wazuh server
45 changes: 41 additions & 4 deletions plugins/wazuh-core/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,17 @@ import { setChrome, setCore, setUiSettings } from './plugin-services';
import * as utils from './utils';
import * as uiComponents from './components';
import { ConfigurationStore } from './utils/configuration-store';
import { DashboardSecurity } from './utils/dashboard-security';
import { DashboardSecurity } from './services/dashboard-security';
import * as hooks from './hooks';
import { CoreServerSecurity } from './services';
import { CoreHTTPClient } from './services/http/http-client';

const noop = () => {};

export class WazuhCorePlugin
implements Plugin<WazuhCorePluginSetup, WazuhCorePluginStart>
{
runtime = { setup: {} };
runtime: Record<string, any> = { setup: {}, start: {} };
internal: Record<string, any> = {};
services: Record<string, any> = {};

Expand Down Expand Up @@ -55,6 +56,8 @@ export class WazuhCorePlugin
// Create dashboardSecurity
this.services.dashboardSecurity = new DashboardSecurity(logger, core.http);

this.services.serverSecurity = new CoreServerSecurity(logger);

// Create http
this.services.http = new CoreHTTPClient(logger, {
getTimeout: async () =>
Expand All @@ -66,16 +69,37 @@ export class WazuhCorePlugin
});

// Setup services
await this.services.dashboardSecurity.setup();
this.runtime.setup.dashboardSecurity =
await this.services.dashboardSecurity.setup({
updateData$: this.services.http.server.auth$,
});
this.runtime.setup.http = await this.services.http.setup({ core });

this.runtime.setup.serverSecurity = this.services.serverSecurity.setup({
useDashboardSecurityAccount:
this.runtime.setup.dashboardSecurity.hooks.useDashboardSecurityIsAdmin,
auth$: this.services.http.server.auth$,
useLoadingLogo: () =>
this.runtime.start.serverSecurityDeps.chrome.logos.AnimatedMark,
});

return {
...this.services,
utils,
API_USER_STATUS_RUN_AS,
hooks: {
...hooks,
...this.runtime.setup.dashboardSecurity.hooks,
...this.runtime.setup.serverSecurity.hooks,
},
hocs: {
...this.runtime.setup.dashboardSecurity.hocs,
...this.runtime.setup.serverSecurity.hocs,
},
ui: {
...uiComponents,
...this.runtime.setup.http.ui,
...this.runtime.setup.serverSecurity.ui,
},
};
}
Expand All @@ -90,14 +114,27 @@ export class WazuhCorePlugin
await this.services.dashboardSecurity.start();
await this.services.http.start();

this.runtime.start.serverSecurityDeps = {
chrome: core.chrome,
};

return {
...this.services,
utils,
API_USER_STATUS_RUN_AS,
hooks,
hooks: {
...hooks,
...this.runtime.setup.dashboardSecurity.hooks,
...this.runtime.setup.serverSecurity.hooks,
},
hocs: {
...this.runtime.setup.dashboardSecurity.hocs,
...this.runtime.setup.serverSecurity.hocs,
},
ui: {
...uiComponents,
...this.runtime.setup.http.ui,
...this.runtime.setup.serverSecurity.ui,
},
};
}
Expand Down
82 changes: 82 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,82 @@
# 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

## Account data

```ts
export interface DashboardSecurityServiceAccount {
administrator: boolean; // user is considered as administrator of Wazuh dashboard. This can be used for some Wazuh plugin features with no dependency of Wazuh indexer permissions
administrator_requirements: string | null; // display a message about the requirements to be administrator if the user has not an administrator
}
```

## Get account data

See the [account data](#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

Get if the user is considered as an administrator for Wazuh plugins.

> NOTE: this consideration is not related to Wazuh indexer permissions.

### 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
@@ -0,0 +1,117 @@
import { BehaviorSubject } from 'rxjs';
import jwtDecode from 'jwt-decode';
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<DashboardSecurityServiceAccount>;

constructor(
private readonly logger: Logger,
private readonly http: { get: (path: string) => any },
) {
this.account$ = new BehaviorSubject({
administrator: false,
administrator_requirements: null,
});
}

private async fetchCurrentPlatform() {
try {
this.logger.debug('Fetching the security platform');

const response = await this.http.get(
'/elastic/security/current-platform',
);

this.securityPlatform = response.platform;
this.logger.debug(`Security platform: ${this.securityPlatform}`);

return this.securityPlatform;
} catch (error) {
this.logger.error(error.message);
throw error;
}
}

get account() {
return this.account$.getValue();
}

async setup({
updateData$,
}: DashboardSecurityServiceSetupDeps): Promise<DashboardSecurityServiceSetupReturn> {
this.logger.debug('Setup');

let hooks, hocs;

try {
this.logger.debug('Creating the UI utilities');

this.logger.debug('Creating hooks');
hooks = createDashboardSecurityHooks({
account$: this.account$,
});
this.logger.debug('Created hooks');

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

try {
this.logger.debug('Getting security platform');
this.securityPlatform = await this.fetchCurrentPlatform();
} catch (error) {
this.logger.error(
`Error fetching the current platform: ${error.message}`,
);
}

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

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

return {
hooks,
hocs,
};
}

async start() {}

async stop() {}

private getAccountFromJWTAPIDecodedToken(decodedToken: {
rbac_roles?: number[];
}) {
const isAdministrator = decodedToken?.rbac_roles?.some?.(
role => role === WAZUH_ROLE_ADMINISTRATOR_ID,
);

return {
administrator: isAdministrator,
administrator_requirements: isAdministrator
? null
: 'User has no administrator role in the selected API connection.',
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './dashboard-security';
export * from './types';
Loading
Loading