-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into investigations-dependency
- Loading branch information
Showing
16 changed files
with
441 additions
and
30 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
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
51 changes: 51 additions & 0 deletions
51
x-pack/plugins/cloud_security_posture/server/routes/benchmark_rules/get_states/get_states.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,51 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
|
||
import { transformError } from '@kbn/securitysolution-es-utils'; | ||
import { CspRouter } from '../../../types'; | ||
import { CSP_GET_BENCHMARK_RULES_STATE_ROUTE_PATH } from '../../../../common/constants'; | ||
import { CspBenchmarkRulesStates } from '../../../../common/types/rules/v3'; | ||
import { getCspBenchmarkRulesStatesHandler } from './v1'; | ||
|
||
export const defineGetCspBenchmarkRulesStatesRoute = (router: CspRouter) => | ||
router.versioned | ||
.get({ | ||
access: 'internal', | ||
path: CSP_GET_BENCHMARK_RULES_STATE_ROUTE_PATH, | ||
}) | ||
.addVersion( | ||
{ | ||
version: '1', | ||
validate: {}, | ||
}, | ||
async (context, request, response) => { | ||
if (!(await context.fleet).authz.fleet.all) { | ||
return response.forbidden(); | ||
} | ||
const cspContext = await context.csp; | ||
|
||
try { | ||
const encryptedSoClient = cspContext.encryptedSavedObjects; | ||
|
||
const rulesStates: CspBenchmarkRulesStates = await getCspBenchmarkRulesStatesHandler( | ||
encryptedSoClient | ||
); | ||
|
||
return response.ok({ | ||
body: rulesStates, | ||
}); | ||
} catch (err) { | ||
const error = transformError(err); | ||
|
||
cspContext.logger.error(`Failed to fetch CSP benchmark rules state: ${error.message}`); | ||
return response.customError({ | ||
body: { message: error.message }, | ||
statusCode: error.statusCode || 500, // Default to 500 if no specific status code is provided | ||
}); | ||
} | ||
} | ||
); |
45 changes: 45 additions & 0 deletions
45
x-pack/plugins/cloud_security_posture/server/routes/benchmark_rules/get_states/v1.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,45 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0; you may not use this file except in compliance with the Elastic License | ||
* 2.0. | ||
*/ | ||
import { SavedObjectsClientContract } from '@kbn/core-saved-objects-api-server'; | ||
import { transformError } from '@kbn/securitysolution-es-utils'; | ||
import { CspBenchmarkRulesStates, CspSettings } from '../../../../common/types/rules/v3'; | ||
import { | ||
INTERNAL_CSP_SETTINGS_SAVED_OBJECT_ID, | ||
INTERNAL_CSP_SETTINGS_SAVED_OBJECT_TYPE, | ||
} from '../../../../common/constants'; | ||
|
||
export const createCspSettingObject = async (soClient: SavedObjectsClientContract) => { | ||
return soClient.create<CspSettings>( | ||
INTERNAL_CSP_SETTINGS_SAVED_OBJECT_TYPE, | ||
{ | ||
rules: {}, | ||
}, | ||
{ id: INTERNAL_CSP_SETTINGS_SAVED_OBJECT_ID } | ||
); | ||
}; | ||
|
||
export const getCspBenchmarkRulesStatesHandler = async ( | ||
encryptedSoClient: SavedObjectsClientContract | ||
): Promise<CspBenchmarkRulesStates> => { | ||
try { | ||
const getSoResponse = await encryptedSoClient.get<CspSettings>( | ||
INTERNAL_CSP_SETTINGS_SAVED_OBJECT_TYPE, | ||
INTERNAL_CSP_SETTINGS_SAVED_OBJECT_ID | ||
); | ||
return getSoResponse.attributes.rules; | ||
} catch (err) { | ||
const error = transformError(err); | ||
if (error.statusCode === 404) { | ||
const newCspSettings = await createCspSettingObject(encryptedSoClient); | ||
return newCspSettings.attributes.rules; | ||
} | ||
|
||
throw new Error( | ||
`An error occurred while trying to fetch csp settings: ${error.message}, ${error.statusCode}` | ||
); | ||
} | ||
}; |
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
Oops, something went wrong.