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

[ML] Fix spaces job ID check #84404

Merged
merged 2 commits into from
Nov 30, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions x-pack/plugins/ml/server/lib/ml_client/ml_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,19 @@ export function getMlClient(
const jobIds =
jobType === 'anomaly-detector' ? getADJobIdsFromRequest(p) : getDFAJobIdsFromRequest(p);
if (jobIds.length) {
const filteredJobIds = await jobSavedObjectService.filterJobIdsForSpace(jobType, jobIds);
let missingIds = jobIds.filter((j) => filteredJobIds.indexOf(j) === -1);
if (allowWildcards === true && missingIds.join().match('\\*') !== null) {
// filter out wildcard ids from the error
missingIds = missingIds.filter((id) => id.match('\\*') === null);
}
if (missingIds.length) {
throw new MLJobNotFound(`No known job with id '${missingIds.join(',')}'`);
}
await checkIds(jobType, jobIds, allowWildcards);
}
}

async function checkIds(jobType: JobType, jobIds: string[], allowWildcards: boolean = false) {
const filteredJobIds = await jobSavedObjectService.filterJobIdsForSpace(jobType, jobIds);
let missingIds = jobIds.filter((j) => filteredJobIds.indexOf(j) === -1);
if (allowWildcards === true && missingIds.join().match('\\*') !== null) {
// filter out wildcard ids from the error
missingIds = missingIds.filter((id) => id.match('\\*') === null);
}
if (missingIds.length) {
throw new MLJobNotFound(`No known job with id '${missingIds.join(',')}'`);
}
}

Expand All @@ -59,8 +63,17 @@ export function getMlClient(
if (ids.length) {
// find all groups from unfiltered jobs
const responseGroupIds = [...new Set(allJobs.map((j) => j.groups ?? []).flat())];
// work out which ids requested are actually groups
const requestedGroupIds = ids.filter((id) => responseGroupIds.includes(id));

// work out which ids requested are actually groups and which are jobs
const requestedGroupIds: string[] = [];
const requestedJobIds: string[] = [];
ids.forEach((id) => {
if (responseGroupIds.includes(id)) {
requestedGroupIds.push(id);
} else {
requestedJobIds.push(id);
}
});

// find all groups from filtered jobs
const groupIdsFromFilteredJobs = [
Expand All @@ -77,10 +90,15 @@ export function getMlClient(
);

if (groupsIdsThatDidNotMatch.length) {
// is there are group ids which were requested but didn't
// if there are group ids which were requested but didn't
// exist in filtered jobs, list them in an error
throw new MLJobNotFound(`No known job with id '${groupsIdsThatDidNotMatch.join(',')}'`);
}

// check the remaining jobs ids
if (requestedJobIds.length) {
await checkIds('anomaly-detector', requestedJobIds, true);
}
}
}

Expand Down