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

[Cloud Security] posture_type Backward compatibility changes #151647

Merged
merged 5 commits into from
Feb 22, 2023

Conversation

JordanSh
Copy link
Contributor

@JordanSh JordanSh commented Feb 20, 2023

Resolves #150608

Summary

This PR adds BC to the newly added posture_type field. Since this field only exists in findings from our latest integration version, older versions of findings won't have it. Thus some of our code on Kibana's side needs to be able to accomplish certain functionalities even without it.

ESQuery filtering

We used the posture_type field to filter the different dashboard tabs. In order to keep the same filtering functionalities, a new runtime field was added.

export const getSafePostureTypeRuntimeMapping = (): MappingRuntimeFields => ({
  safe_posture_type: {
    type: 'keyword',
    script: {
      source: `
        if (!doc.containsKey('rule.benchmark.posture_type'))
          {
            def safe_posture_type = 'kspm';
            emit(safe_posture_type);
            return
          }
        else
        {
            def safe_posture_type = doc["rule.benchmark.posture_type"].value;
            emit(safe_posture_type);
            return
        }
      `,
    },
  },
});

This runtime field creates a safe to use field which will be equal to the posture type even for older findings. we will then use this field instead in order to query over the findings in all of our getter functions

const runtimeMappings: MappingRuntimeFields = getSafePostureTypeRuntimeMapping();
const query: QueryDslQueryContainer = {
  bool: {
    filter: [{ term: { safe_posture_type: policyTemplate } }],
  },
};

const [stats, groupedFindingsEvaluation, clustersWithoutTrends, trends] = await Promise.all(
  [
    getStats(esClient, query, pitId, runtimeMappings),
    getGroupedFindingsEvaluation(esClient, query, pitId, runtimeMappings),
    getClusters(esClient, query, pitId, runtimeMappings),
    getTrends(esClient, policyTemplate),
  ]
);

Dashboard navigation query

Since we don't always have posture_type, we need to have a solution for dashboard navigations for those cases as well.
In order to do that, the navigation option of using negated filters was added to the useNavigate hook. it can be used like so

export const getPolicyTemplateQuery = (policyTemplate: PosturePolicyTemplate): NavFilter => {
  if (policyTemplate === CSPM_POLICY_TEMPLATE) {
    return { 'rule.benchmark.posture_type': CSPM_POLICY_TEMPLATE };
  }

  return { 'rule.benchmark.posture_type': { value: CSPM_POLICY_TEMPLATE, negate: true } };
};

will result in
image

Removal of direct posture_type === 'kspm' checks

We no longer check if kspm is the posture type directly, all checks are now for posture_type === 'cspm' and if not, resulting in a default value. For example:

Before

export const getClusterIdQuery = (cluster: Cluster) => {
  if (cluster.meta.benchmark.posture_type === CSPM_POLICY_TEMPLATE) {
    return { 'cloud.account.name': cluster.meta.cloud?.account.name };
  }
  if (cluster.meta.benchmark.posture_type === 'kspm') {
    return { cluster_id: cluster.meta.assetIdentifierId };
  }

  return {};
};

After

export const getClusterIdQuery = (cluster: Cluster) => {
  if (cluster.meta.benchmark.posture_type === CSPM_POLICY_TEMPLATE) {
    return { 'cloud.account.name': cluster.meta.cloud?.account.name };
  }
  return { cluster_id: cluster.meta.assetIdentifierId };
};

More of those were already done in #151265 as well

How to check

You will need to have findings without the posture_type field and run on Kibana 8.7. this can be done either with mocks or persistent ES, from there all dashboard functionalities should work as expected.

@JordanSh JordanSh changed the title removing easier cases [Cloud Security] Removing reliance on posture_type Feb 20, 2023
@JordanSh JordanSh changed the title [Cloud Security] Removing reliance on posture_type [Cloud Security] posture_type Backward compatibility changes Feb 21, 2023
@JordanSh JordanSh marked this pull request as ready for review February 21, 2023 16:28
@JordanSh JordanSh requested a review from a team as a code owner February 21, 2023 16:28
@JordanSh JordanSh self-assigned this Feb 21, 2023
@JordanSh JordanSh added v8.7.0 release_note:skip Skip the PR/issue when compiling release notes Team:Cloud Security Cloud Security team related labels Feb 21, 2023
@elasticmachine
Copy link
Contributor

Pinging @elastic/kibana-cloud-security-posture (Team:Cloud Security)

Comment on lines +129 to +135
<EuiFlexGroup
gutterSize="l"
css={css`
// height for compliance by cis section with max rows
height: 310px;
`}
>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated but this fixes problems of unmatching summary sizes between dashboard tabs when there are not enough items in the compliance by score table

@JordanSh JordanSh requested a review from opauloh February 21, 2023 16:32
Copy link
Contributor

@opauloh opauloh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great 🚀

* Creates the `safe_posture_type` runtime field with the value of either
* `kspm` or `cspm` based on the value of `rule.benchmark.posture_type`
*/
export const getSafePostureTypeRuntimeMapping = (): MappingRuntimeFields => ({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, usually I'm against runtime mappings, but since it's on the latest index, performance won't be an issue, great BC solution

@JordanSh JordanSh enabled auto-merge (squash) February 22, 2023 10:52
@kibana-ci
Copy link
Collaborator

💚 Build Succeeded

Metrics [docs]

Async chunks

Total size of all lazy-loaded chunks that will be downloaded as the user navigates the app

id before after diff
cloudSecurityPosture 145.3KB 145.3KB +12.0B

History

To update your PR or re-run it, just comment with:
@elasticmachine merge upstream

cc @JordanSh

@JordanSh JordanSh merged commit 5037ca1 into elastic:main Feb 22, 2023
@kibanamachine
Copy link
Contributor

💔 All backports failed

Status Branch Result
8.7 Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 151647

Questions ?

Please refer to the Backport tool documentation

@opauloh
Copy link
Contributor

opauloh commented Mar 16, 2023

💔 All backports failed

Status Branch Result
❌ 8.7 Backport failed because of merge conflicts

Manual backport

To create the backport manually run:

node scripts/backport --pr 151647

Questions ?

Please refer to the Backport tool documentation

I am going to manually backport this PR, and that should fix this issue

kfirpeled pushed a commit to kfirpeled/kibana that referenced this pull request Mar 19, 2023
…ic#151647)

(cherry picked from commit 5037ca1)

# Conflicts:
#	x-pack/plugins/cloud_security_posture/public/pages/compliance_dashboard/dashboard_sections/cluster_details_box.tsx
kfirpeled added a commit that referenced this pull request Mar 20, 2023
…151647) (#153276)

# Backport

This will backport the following commits from `main` to `8.7`:
- [[Cloud Security] `posture_type` Backward compatibility changes
(#151647)](#151647)

<!--- Backport version: 8.9.7 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT
[{"author":{"name":"Jordan","email":"[email protected]"},"sourceCommit":{"committedDate":"2023-02-22T12:00:53Z","message":"[Cloud
Security] `posture_type` Backward compatibility changes
(#151647)","sha":"5037ca12bee23fc1b51ae6ec7a3091a4139e444f","branchLabelMapping":{"^v8.8.0$":"main","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["release_note:skip","Team:Cloud
Security","v8.7.0","v8.8.0"],"number":151647,"url":"https://github.com/elastic/kibana/pull/151647","mergeCommit":{"message":"[Cloud
Security] `posture_type` Backward compatibility changes
(#151647)","sha":"5037ca12bee23fc1b51ae6ec7a3091a4139e444f"}},"sourceBranch":"main","suggestedTargetBranches":["8.7"],"targetPullRequestStates":[{"branch":"8.7","label":"v8.7.0","labelRegex":"^v(\\d+).(\\d+).\\d+$","isSourceBranch":false,"state":"NOT_CREATED"},{"branch":"main","label":"v8.8.0","labelRegex":"^v8.8.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/151647","number":151647,"mergeCommit":{"message":"[Cloud
Security] `posture_type` Backward compatibility changes
(#151647)","sha":"5037ca12bee23fc1b51ae6ec7a3091a4139e444f"}}]}]
BACKPORT-->

---------

Co-authored-by: Jordan <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release_note:skip Skip the PR/issue when compiling release notes Team:Cloud Security Cloud Security team related v8.7.0 v8.8.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Cloud Posture] Add backward compatibility to posture_type
5 participants