-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into enhancement/migrate-wazuh-configuration
- Loading branch information
Showing
41 changed files
with
1,824 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
82 changes: 82 additions & 0 deletions
82
plugins/wazuh-core/public/services/dashboard-security/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}, | ||
); | ||
``` |
121 changes: 121 additions & 0 deletions
121
plugins/wazuh-core/public/services/dashboard-security/dashboard-security.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
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, | ||
}); | ||
} | ||
|
||
get securityPlatform() { | ||
return this._securityPlatform; | ||
} | ||
|
||
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'); | ||
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.', | ||
}; | ||
} | ||
} |
2 changes: 2 additions & 0 deletions
2
plugins/wazuh-core/public/services/dashboard-security/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './dashboard-security'; | ||
export * from './types'; |
Oops, something went wrong.