forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Cloud Security][CIS GCP]Migration for new fields (elastic#167545)
## Summary This PR is for migrating account.type fields for GCP when it doesnt have it --------- Co-authored-by: kibanamachine <[email protected]> Co-authored-by: Kfir Peled <[email protected]> Co-authored-by: Kfir Peled <[email protected]>
- Loading branch information
1 parent
60f66ea
commit 959f623
Showing
5 changed files
with
184 additions
and
6 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
8 changes: 8 additions & 0 deletions
8
x-pack/plugins/fleet/server/saved_objects/migrations/cloud_security_posture/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,8 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
export { migrateCspPackagePolicyToV8110 } from './to_v8_11_0'; |
125 changes: 125 additions & 0 deletions
125
...k/plugins/fleet/server/saved_objects/migrations/cloud_security_posture/to_v8_11_0.test.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,125 @@ | ||
/* | ||
* 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 type { SavedObjectModelTransformationContext } from '@kbn/core-saved-objects-server'; | ||
|
||
import { migrateCspPackagePolicyToV8110 as migration } from './to_v8_11_0'; | ||
|
||
describe('8.11.0 Cloud Security Posture Package Policy migration', () => { | ||
const policyDoc = ( | ||
accountType: string, | ||
isAccountTypeCorrect: boolean, | ||
packageName: string | ||
): any => { | ||
return { | ||
id: 'mock-saved-csp-object-id', | ||
attributes: { | ||
name: 'cloud_security_posture_test', | ||
package: { | ||
name: packageName, | ||
title: '', | ||
version: '', | ||
}, | ||
id: 'ID_123', | ||
policy_id: '', | ||
enabled: true, | ||
namespace: '', | ||
revision: 0, | ||
updated_at: '', | ||
updated_by: '', | ||
created_at: '', | ||
created_by: '', | ||
inputs: [ | ||
{ | ||
type: accountType, | ||
enabled: true, | ||
streams: [ | ||
{ | ||
vars: { | ||
...(isAccountTypeCorrect && { | ||
'gcp.account_type': { value: 'single-account', type: 'text' }, | ||
}), | ||
}, | ||
}, | ||
], | ||
config: {}, | ||
}, | ||
], | ||
}, | ||
type: ' nested', | ||
}; | ||
}; | ||
|
||
it('adds gcp.account_type to policy, set to single', () => { | ||
const initialDoc = policyDoc('cloudbeat/cis_gcp', false, 'cloud_security_posture'); | ||
const migratedDoc = policyDoc('cloudbeat/cis_gcp', true, 'cloud_security_posture'); | ||
expect(migration(initialDoc, {} as SavedObjectModelTransformationContext)).toEqual({ | ||
attributes: migratedDoc.attributes, | ||
}); | ||
}); | ||
|
||
it('if there are no type cloudbeat/cis_gcp, do not add gcp.account_type', () => { | ||
const initialDoc = policyDoc('cloudbeat/cis_aws', false, 'cloud_security_posture'); | ||
const migratedDoc = policyDoc('cloudbeat/cis_aws', false, 'cloud_security_posture'); | ||
expect(migration(initialDoc, {} as SavedObjectModelTransformationContext)).toEqual({ | ||
attributes: migratedDoc.attributes, | ||
}); | ||
}); | ||
|
||
it('if there are no cloud_security_posture package, do not change the doc', () => { | ||
const initialDoc = policyDoc('cloudbeat/cis_gcp', false, 'NOT_cloud_security_posture'); | ||
const migratedDoc = policyDoc('cloudbeat/cis_gcp', false, 'NOT_cloud_security_posture'); | ||
expect(migration(initialDoc, {} as SavedObjectModelTransformationContext)).toEqual({ | ||
attributes: migratedDoc.attributes, | ||
}); | ||
}); | ||
|
||
it('if gcp.account_type exist and already has a value, do not set it to single-account', () => { | ||
const policyDocWithAccountType = (): any => { | ||
return { | ||
id: 'mock-saved-csp-object-id', | ||
attributes: { | ||
name: 'cloud_security_posture_test', | ||
package: { | ||
name: 'cloud_security_posture', | ||
title: '', | ||
version: '', | ||
}, | ||
id: 'ID_1234', | ||
policy_id: '', | ||
enabled: true, | ||
namespace: '', | ||
revision: 0, | ||
updated_at: '', | ||
updated_by: '', | ||
created_at: '', | ||
created_by: '', | ||
inputs: [ | ||
{ | ||
type: 'cloudbeat/cis_gcp', | ||
enabled: true, | ||
streams: [ | ||
{ | ||
vars: { | ||
'gcp.account_type': { value: 'single-account-MAYBE', type: 'text' }, | ||
}, | ||
}, | ||
], | ||
config: {}, | ||
}, | ||
], | ||
}, | ||
type: ' nested', | ||
}; | ||
}; | ||
const initialDoc = policyDocWithAccountType(); | ||
const migratedDoc = policyDocWithAccountType(); | ||
expect(migration(initialDoc, {} as SavedObjectModelTransformationContext)).toEqual({ | ||
attributes: migratedDoc.attributes, | ||
}); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
x-pack/plugins/fleet/server/saved_objects/migrations/cloud_security_posture/to_v8_11_0.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,36 @@ | ||
/* | ||
* 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 type { SavedObjectModelDataBackfillFn } from '@kbn/core-saved-objects-server'; | ||
|
||
import type { PackagePolicy } from '../../../../common'; | ||
|
||
export const migrateCspPackagePolicyToV8110: SavedObjectModelDataBackfillFn< | ||
PackagePolicy, | ||
PackagePolicy | ||
> = (packagePolicyDoc) => { | ||
if (packagePolicyDoc.attributes.package?.name !== 'cloud_security_posture') { | ||
return { attributes: packagePolicyDoc.attributes }; | ||
} | ||
|
||
const updatedAttributes = packagePolicyDoc.attributes; | ||
|
||
const gcpPackage = updatedAttributes.inputs.find((input) => input.type === 'cloudbeat/cis_gcp'); | ||
|
||
if (gcpPackage) { | ||
const isGcpAccountTypeExists = gcpPackage.streams[0]?.vars?.hasOwnProperty('gcp.account_type'); | ||
|
||
if (!isGcpAccountTypeExists) { | ||
const migratedPolicy = { 'gcp.account_type': { value: 'single-account', type: 'text' } }; | ||
gcpPackage.streams[0].vars = { ...(gcpPackage.streams[0].vars || {}), ...migratedPolicy }; | ||
} | ||
} | ||
|
||
return { | ||
attributes: updatedAttributes, | ||
}; | ||
}; |