Skip to content

Commit

Permalink
[ML] Fix spaces job ID check (elastic#84404)
Browse files Browse the repository at this point in the history
Co-authored-by: Kibana Machine <42973632+kibanamachine@users.noreply.github.com>
  • Loading branch information
2 people authored and peteharverson committed Nov 30, 2020
1 parent 5d4162b commit 8f4d15c
Showing 1 changed file with 30 additions and 12 deletions.
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

0 comments on commit 8f4d15c

Please sign in to comment.