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

[Security Solution] [Sourcerer] Replaces references to sourcerer search strategy with api provided by kibana data views plugin #149360

Merged
merged 64 commits into from
Apr 25, 2023

Conversation

dhurley14
Copy link
Contributor

@dhurley14 dhurley14 commented Jan 23, 2023

Summary

Original outline: #138181

Issues outlining the objective of this pr: #142903 and #142907

---
title: Sourcerer Deprecation Journey
---
flowchart LR
    A[8.7 matchedIndices <a href='https://github.com/elastic/kibana/pull/139671'>#139671</a>] --> B[Replace Sourcerer \n Search Strategy apis]
    subgraph you are here
    B
    end
    B --> C[Replace Sourcerer UI \nwith data view picker <a href='https://github.com/elastic/security-team/issues/6235'>#6235</a>]
    B --> D[Update code to use \n properties from \nkibana data view, \nnot sourcerer data view <a href='https://github.com/elastic/kibana/issues/155100'>#155100</a>]
    C --> E[Replace useSourcererDataView \nhook with Redux selector]
    D --> E[Replace useSourcererDataView \nhook with Redux selector]
    E --> F[Finished!]
Loading

Overview

Since the data views plugin was introduced, maintaining our own apis for fetching sourcerer saved objects (data views) and additional types has become cumbersome and inefficient. The data views plugin provides both an efficient caching of data view saved objects and a unified interface for creating ad-hoc data views (see the changes to the useFetchIndex hook in this PR) so that our code can now rely on a single type of saved object to interface with when fetching data.

This PR is another step towards replacing sourcerer with the data view picker provided by kibana platform (which benefits users by maintaining consistency around data source selection UX) and additionally provides benefits to developers in the security solution by allowing us to reduce state-management complexity in components that rely on old indexPattern types or data view types.

What does this PR do, exactly?

This PR removes references to the search strategies that were implemented before the data views plugin became available, and replaces them with their corresponding apis available in the kibana data view plugin. These search strategies provided access to the data view saved object as well as the fields associated with the data view or array of index name strings.

I have also marked the server-side search strategies as deprecated (rather than deleting them) as they are being used by other teams and might be used by software outside of elastic, and I have also marked the following properties as deprecated; browserFields, indexFields, runtimeMappings, indexPatterns, patternList, selectedPatterns, and activePatterns. Once we have removed all references to the above listed properties we can use the data view type in our code without having to extend from it.

Next steps

In a perfect world we would be able to remove all the references to the old memoized properties on the useSourcererDataView hook (like patternList) and replace them with their counterparts on the sourcererDataView object returned by that same hook (like title or w/e the non-deprecated accessor is). After that is complete, we would be able to remove the useSourcererDataView hook entirely and replace that with the data view stored in redux returned by the selector associated with sourcerer, and then finally remove the sourcerer UI component and replace it with the UI component provided by kibana.

The faster way is to update the UI and replace the sourcerer component with the kibana data view picker, and update the corresponding data view stored in redux and use that selector in the useSourcererDataView hook, much like how we are changing the backend systems in this pr and just mapping those properties from the data view SO to the expected memoized properties in the useSourcererDataView hook. Again, the end goal is really to just use the selector directly in our components and not rely on any hooks, like the following:

const dataView = useInternalStateSelector((state) => state.dataView!);

Bonus round

Additionally, I have written up an issue for next steps to be made to the useFetchIndex hook which will allow it to take either an array of index names (as it currently does, returning an ad-hoc data view) or a data view id (and returning a data view). By making this change, components can remove extraneous logic relying on updating state with effects like this

const [dataView, setDataView] = useState<DataView | null>(null);
const { startTransaction } = useStartTransaction();
const { indexFieldsSearch } = useDataView();
const {
dataViewFieldEditor,
data: { dataViews },
} = useKibana().services;
const scopeIdSelector = useMemo(() => sourcererSelectors.scopeIdSelector(), []);
const { missingPatterns, selectedDataViewId } = useDeepEqualSelector((state) =>
scopeIdSelector(state, sourcererScope)
);
useEffect(() => {
if (selectedDataViewId != null && !missingPatterns.length) {
dataViews.get(selectedDataViewId).then((dataViewResponse) => {
setDataView(dataViewResponse);
});
}
}, [selectedDataViewId, missingPatterns, dataViews]);

And like this:

const [isIndexPatternLoading, { browserFields, indexPatterns: initIndexPattern }] = useFetchIndex(
index,
false
);
const [indexPattern, setIndexPattern] = useState<DataViewBase>(initIndexPattern);
const { data } = useKibana().services;
// Why do we need this? to ensure the query bar auto-suggest gets the latest updates
// when the index pattern changes
// when we select new dataView
// when we choose some other dataSourceType
useEffect(() => {
if (dataSourceType === DataSourceType.IndexPatterns) {
if (!isIndexPatternLoading) {
setIndexPattern(initIndexPattern);
}
}
if (dataSourceType === DataSourceType.DataView) {
const fetchDataView = async () => {
if (dataViewId != null) {
const dv = await data.dataViews.get(dataViewId);
setDataViewTitle(dv.title);
setIndexPattern(dv);
}
};
fetchDataView();
}
}, [dataSourceType, isIndexPatternLoading, data, dataViewId, initIndexPattern]);

And again..

const memoDataViewId = useMemo(
() =>
rules != null && isSingleRule
? rules[0].data_view_id || null
: `security-solution-${activeSpaceId}`,
[isSingleRule, rules, activeSpaceId]
);
const memoNonDataViewIndexPatterns = useMemo(
() =>
!memoDataViewId && rules != null && isSingleRule && rules[0].index != null
? rules[0].index
: [],
[memoDataViewId, isSingleRule, rules]
);
// Index pattern logic for ML
const memoMlJobIds = useMemo(
() => (isMLRule && isSingleRule && rules != null ? rules[0].machine_learning_job_id ?? [] : []),
[isMLRule, isSingleRule, rules]
);
const { loading: mlJobLoading, jobs } = useGetInstalledJob(memoMlJobIds);
// We only want to provide a non empty array if it's an ML rule and we were able to fetch
// the index patterns, or if it's a rule not using data views. Otherwise, return an empty
// empty array to avoid making the `useFetchIndex` call
const memoRuleIndices = useMemo(() => {
if (isMLRule && jobs.length > 0) {
return jobs[0].results_index_name ? [`.ml-anomalies-${jobs[0].results_index_name}`] : [];
} else if (memoDataViewId != null) {
return [];
} else {
return memoNonDataViewIndexPatterns;
}
}, [jobs, isMLRule, memoDataViewId, memoNonDataViewIndexPatterns]);
const [isIndexPatternLoading, { indexPatterns: indexIndexPatterns }] = useFetchIndex(
memoRuleIndices,
false,
'indexFields',
true
);
// Data view logic
const [dataViewIndexPatterns, setDataViewIndexPatterns] = useState<DataViewBase | null>(null);
useEffect(() => {
const fetchSingleDataView = async () => {
// ensure the memoized data view includes a space id, otherwise
// we could be trying to fetch a data view that does not exist, which would
// throw an error here.
if (activeSpaceId !== '' && memoDataViewId) {
setDataViewLoading(true);
const dv = await data.dataViews.get(memoDataViewId);
const fieldsWithUnmappedInfo = await data.dataViews.getFieldsForIndexPattern(dv, {
pattern: '',
includeUnmapped: true,
});
setDataViewLoading(false);
setDataViewIndexPatterns({
...dv,
fields: fieldsWithUnmappedInfo,
});
}
};
fetchSingleDataView();
}, [memoDataViewId, data.dataViews, setDataViewIndexPatterns, activeSpaceId]);
// Determine whether to use index patterns or data views
const indexPatternsToUse = useMemo(
(): DataViewBase =>
memoDataViewId && dataViewIndexPatterns != null ? dataViewIndexPatterns : indexIndexPatterns,
[memoDataViewId, dataViewIndexPatterns, indexIndexPatterns]
);

Checklist

Delete any items that are not applicable to this PR.

Risk Matrix

Delete this section if it is not applicable to this PR.

Before closing this PR, invite QA, stakeholders, and other developers to identify risks that should be tested prior to the change/feature release.

When forming the risk matrix, consider some of the following examples and how they may potentially impact the change:

Risk Probability Severity Mitigation/Notes
Multiple Spaces—unexpected behavior in non-default Kibana Space. Low High Integration tests will verify that all features are still supported in non-default Kibana Space and when user switches between spaces.
Multiple nodes—Elasticsearch polling might have race conditions when multiple Kibana nodes are polling for the same tasks. High Low Tasks are idempotent, so executing them multiple times will not result in logical error, but will degrade performance. To test for this case we add plenty of unit tests around this logic and document manual testing procedure.
Code should gracefully handle cases when feature X or plugin Y are disabled. Medium High Unit tests will verify that any feature flag or plugin combination still results in our service operational.
See more potential risk examples

For maintainers

dhurley14 and others added 2 commits January 23, 2023 16:32
…h does not. I think the missing link between the two is the loading states and the fields / title (stringified vs array of strings). Check out the differences and try to tie them into the ad-hoc-dv-sourcerer branch
@dhurley14 dhurley14 self-assigned this Jan 24, 2023
@dhurley14 dhurley14 changed the title this works with the rules page, whereas the ad-hoc-dv-sourcerer branc… [Security Solution] [Sourcerer] replace indices bsearch api with ad-hoc data views Mar 10, 2023
@dhurley14 dhurley14 force-pushed the working-ad-hoc-dv-sourcerer branch from 820f813 to b6bfca2 Compare March 31, 2023 17:49
Copy link
Contributor

@paul-tavares paul-tavares left a comment

Choose a reason for hiding this comment

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

Changes to the management page looks good 👍

fetchDataViews();
}, [dataServices.dataViews]);
fetchDV();
// eslint-disable-next-line react-hooks/exhaustive-deps
Copy link
Contributor

Choose a reason for hiding this comment

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

can we avoid disabling this eslint rule?
It may lead to stale data in hook and relying business logic on eslint rule would increase cost of maintenance and will be prone to issues in future

@@ -196,39 +196,39 @@ describe('Bulk editing index patterns of rules with index patterns and rules wit
waitForRulesTableToBeLoaded();
});

it('Add index patterns to custom rules: one rule is updated, one rule is skipped', () => {
it('Add index patterns to custom rules when overwrite data view checkbox is checked: all rules are updated', () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: were these tests just swap in places? I don't see any changes in them. If it so, maybe swapping it back would remove this file from PR diff

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yup just a swap. In their original format they were failing but switching them around made it pass.

Copy link
Contributor

Choose a reason for hiding this comment

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

Do you know why they failed?

Do they have some dependant data?

Copy link
Contributor

Choose a reason for hiding this comment

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

Was this a timeout?
I wrote this tests and can't figure out why switching the order would help.
I could revisit them later if you still have some of the logs for these

};
}, [indexNames, indexFieldsSearch, previousIndexesName]);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [indexNames]);
Copy link
Contributor

Choose a reason for hiding this comment

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

can we avoid disabling this eslint rule?
It may lead to stale data in hook and relying business logic on eslint rule would increase cost of maintenance and will be prone to issues in future


setState({
dataView: dv,
browserFields,
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use dataView.fields here since browserFields is deprecated?

Copy link
Contributor Author

@dhurley14 dhurley14 Apr 24, 2023

Choose a reason for hiding this comment

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

The shape of the data is different between the two so unfortunately until all references to browserFields in the codebase are removed or have been updated to use the fields property on the dataview, we won't be able to remove it here.

addError(exc?.message, {
title: i18n.ERROR_INDEX_FIELDS_SEARCH,
});
}
Copy link
Contributor

Choose a reason for hiding this comment

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

should loading be set to false if error happens?

loading: indexPatternsLoading,
patternList: fetchIndexReturn.indexes,
indexFields: fetchIndexReturn.indexPatterns
.fields as SelectedDataView['indexPattern']['fields'],
fields: fetchIndexReturn.indexPatterns.fields as DataViewFieldBase[],
Copy link
Contributor

Choose a reason for hiding this comment

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

this casting looks unnecessary and can be removed

// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => fetchDataViews(), [fetchDataViews]);
Copy link
Contributor

Choose a reason for hiding this comment

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

Hey, what is the reason to use use callback here?

The idea here, is to just fetch data view once, when the component mount.

Why not do:

useEffect(() => {
    const fetchDV = async () => {...}
    fetchDV();
    };, []);

@@ -328,22 +329,24 @@ const RuleDetailsPageComponent: React.FC<DetectionEngineComponentProps> = ({
const [indicesConfig] = useUiSetting$<string[]>(DEFAULT_INDEX_KEY);
const [threatIndicesConfig] = useUiSetting$<string[]>(DEFAULT_THREAT_INDEX_KEY);

useEffect(() => {
const fetchDataViews = async () => {
const fetchDataViews = useCallback(() => {
Copy link
Contributor

Choose a reason for hiding this comment

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

The same question as above

@nkhristinin
Copy link
Contributor

Also found that we use deprecated browserFields here

Should it be fixed? and if yes in this PR, on next steps?

Copy link
Contributor

@nkhristinin nkhristinin left a comment

Choose a reason for hiding this comment

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

I left some comments, but it LGTM!
Great work @dhurley14, and really nice description of the story which provides a lot of context!

Copy link
Contributor

@kevinlog kevinlog left a comment

Choose a reason for hiding this comment

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

Thanks for the discussion offline @dhurley14

After discussion with @peluja1012 and @caitlinbetz we are OK with this change since we have required logs-* read permissions in Elastic Security for some time. Docs: https://www.elastic.co/guide/en/security/current/sec-requirements.html#_kibana_space_and_index_privileges

Copy link
Contributor

@jpdjere jpdjere left a comment

Choose a reason for hiding this comment

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

Detection Rules LGTM 👍

Wondering about the flakiness of those Cypress tests, and why switching them help. Of course, not a blocker, but would like to revisit later.

dataView={kibanaDataProvider}
maxDepth={2}
/>
{sourcererDataView ? (
Copy link
Contributor

Choose a reason for hiding this comment

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

Is there ever a situation where this won't be set? Fyi @kqualters-elastic

Copy link
Contributor

@michaelolo24 michaelolo24 left a comment

Choose a reason for hiding this comment

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

Investigations codeowner changes. Nice work, LGTM!

Copy link
Contributor

@vitaliidm vitaliidm left a comment

Choose a reason for hiding this comment

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

alerts area LGTM. thanks for addressing comments

Copy link
Member

@cnasikas cnasikas left a comment

Choose a reason for hiding this comment

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

ResponseOps changes LTGM.

@kibana-ci
Copy link
Collaborator

💛 Build succeeded, but was flaky

Failed CI Steps

Test Failures

  • [job] [logs] FTR Configs #45 / console app console context menu should copy as curl and show toast when copy as curl button is clicked

Metrics [docs]

Public APIs missing comments

Total count of every public API that lacks a comment. Target amount is 0. Run node scripts/build_api_docs --plugin [yourplugin] --stats comments for more detailed information.

id before after diff
triggersActionsUi 520 522 +2

Async chunks

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

id before after diff
securitySolution 9.1MB 9.1MB -6.6KB
triggersActionsUi 1.4MB 1.4MB -1.3KB
total -7.9KB

Page load bundle

Size of the bundles that are downloaded on every page load. Target size is below 100kb

id before after diff
securitySolution 60.3KB 60.4KB +147.0B
triggersActionsUi 84.8KB 86.9KB +2.1KB
total +2.2KB
Unknown metric groups

API count

id before after diff
triggersActionsUi 549 551 +2

ESLint disabled line counts

id before after diff
enterpriseSearch 17 19 +2
securitySolution 397 402 +5
total +7

References to deprecated APIs

id before after diff
@kbn/securitysolution-data-table 0 15 +15
securitySolution 380 546 +166
total +181

Total ESLint disabled count

id before after diff
enterpriseSearch 18 20 +2
securitySolution 477 482 +5
total +7

History

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

cc @dhurley14

@peluja1012
Copy link
Contributor

Hi @vitaliidm, we will address the comments related to the disabled es-lint rules when we refactor the Rule Creation page components. We were running into unexpected behavior with hooks otherwise.

@dhurley14 dhurley14 merged commit 9c4f99d into elastic:main Apr 25, 2023
@kibanamachine kibanamachine added the backport:skip This commit does not require backporting label Apr 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport:skip This commit does not require backporting release_note:skip Skip the PR/issue when compiling release notes review Team:Security Solution Platform Security Solution Platform Team v8.8.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.