-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
497 additions
and
337 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
195 changes: 195 additions & 0 deletions
195
...y_solution/public/detections/components/rules/related_integrations/integration_details.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,195 @@ | ||
/* | ||
* 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 { capitalize } from 'lodash'; | ||
import semver from 'semver'; | ||
import { | ||
InstalledIntegration, | ||
InstalledIntegrationArray, | ||
RelatedIntegration, | ||
RelatedIntegrationArray, | ||
} from '../../../../../common/detection_engine/schemas/common'; | ||
import { IntegrationPrivileges } from './integration_privileges'; | ||
|
||
export interface IntegrationDetails { | ||
packageName: string; | ||
integrationName: string | null; | ||
integrationTitle: string; | ||
|
||
requiredVersion: string; | ||
targetVersion: string; | ||
targetUrl: string; | ||
|
||
installationStatus: KnownInstallationStatus | UnknownInstallationStatus; | ||
} | ||
|
||
export interface KnownInstallationStatus { | ||
isKnown: true; | ||
isInstalled: boolean; | ||
isEnabled: boolean; | ||
isVersionMismatch: boolean; | ||
installedVersion: string; | ||
} | ||
|
||
export interface UnknownInstallationStatus { | ||
isKnown: false; | ||
} | ||
|
||
/** | ||
* Given an array of integrations and an array of installed integrations this will return an | ||
* array of integrations augmented with install details like targetVersion, and `version_satisfied` | ||
* has. | ||
*/ | ||
export const calculateIntegrationDetails = ( | ||
privileges: IntegrationPrivileges, | ||
relatedIntegrations: RelatedIntegrationArray, | ||
installedIntegrations: InstalledIntegrationArray | undefined | ||
): IntegrationDetails[] => { | ||
const integrationMatches = findIntegrationMatches(relatedIntegrations, installedIntegrations); | ||
const integrationDetails = integrationMatches.map((integration) => { | ||
return createIntegrationDetails(integration, privileges); | ||
}); | ||
|
||
return integrationDetails.sort((a, b) => { | ||
return a.integrationTitle.localeCompare(b.integrationTitle); | ||
}); | ||
}; | ||
|
||
interface IntegrationMatch { | ||
related: RelatedIntegration; | ||
installed: InstalledIntegration | null; | ||
isLoaded: boolean; | ||
} | ||
|
||
const findIntegrationMatches = ( | ||
relatedIntegrations: RelatedIntegrationArray, | ||
installedIntegrations: InstalledIntegrationArray | undefined | ||
): IntegrationMatch[] => { | ||
return relatedIntegrations.map((ri: RelatedIntegration) => { | ||
if (installedIntegrations == null) { | ||
return { | ||
related: ri, | ||
installed: null, | ||
isLoaded: false, | ||
}; | ||
} else { | ||
const match = installedIntegrations.find( | ||
(ii: InstalledIntegration) => | ||
ii.package_name === ri.package && ii?.integration_name === ri?.integration | ||
); | ||
return { | ||
related: ri, | ||
installed: match ?? null, | ||
isLoaded: true, | ||
}; | ||
} | ||
}); | ||
}; | ||
|
||
const createIntegrationDetails = ( | ||
integration: IntegrationMatch, | ||
privileges: IntegrationPrivileges | ||
): IntegrationDetails => { | ||
const { related, installed, isLoaded } = integration; | ||
const { canReadInstalledIntegrations } = privileges; | ||
|
||
const packageName = related.package; | ||
const integrationName = related.integration ?? null; | ||
const requiredVersion = related.version; | ||
|
||
// We don't know whether the integration is installed or not. | ||
if (!canReadInstalledIntegrations || !isLoaded) { | ||
const integrationTitle = getCapitalizedTitle(packageName, integrationName); | ||
const targetVersion = getMinimumConcreteVersionMatchingSemver(requiredVersion); | ||
const targetUrl = buildTargetUrl(packageName, integrationName, targetVersion); | ||
|
||
return { | ||
packageName, | ||
integrationName, | ||
integrationTitle, | ||
requiredVersion, | ||
targetVersion, | ||
targetUrl, | ||
installationStatus: { | ||
isKnown: false, | ||
}, | ||
}; | ||
} | ||
|
||
// We know that the integration is not installed | ||
if (installed == null) { | ||
const integrationTitle = getCapitalizedTitle(packageName, integrationName); | ||
const targetVersion = getMinimumConcreteVersionMatchingSemver(requiredVersion); | ||
const targetUrl = buildTargetUrl(packageName, integrationName, targetVersion); | ||
|
||
return { | ||
packageName, | ||
integrationName, | ||
integrationTitle, | ||
requiredVersion, | ||
targetVersion, | ||
targetUrl, | ||
installationStatus: { | ||
isKnown: true, | ||
isInstalled: false, | ||
isEnabled: false, | ||
isVersionMismatch: false, | ||
installedVersion: '', | ||
}, | ||
}; | ||
} | ||
|
||
// We know that the integration is installed | ||
{ | ||
const integrationTitle = installed.integration_title ?? installed.package_title; | ||
|
||
// Version check e.g. installed version `1.2.3` satisfies required version `~1.2.1` | ||
const installedVersion = installed.package_version; | ||
const isVersionSatisfied = semver.satisfies(installedVersion, requiredVersion); | ||
const targetVersion = isVersionSatisfied | ||
? installedVersion | ||
: getMinimumConcreteVersionMatchingSemver(requiredVersion); | ||
|
||
const targetUrl = buildTargetUrl(packageName, integrationName, targetVersion); | ||
|
||
return { | ||
packageName, | ||
integrationName, | ||
integrationTitle, | ||
requiredVersion, | ||
targetVersion, | ||
targetUrl, | ||
installationStatus: { | ||
isKnown: true, | ||
isInstalled: true, | ||
isEnabled: installed.is_enabled, | ||
isVersionMismatch: !isVersionSatisfied, | ||
installedVersion, | ||
}, | ||
}; | ||
} | ||
}; | ||
|
||
const getCapitalizedTitle = (packageName: string, integrationName: string | null): string => { | ||
return integrationName == null | ||
? `${capitalize(packageName)}` | ||
: `${capitalize(packageName)} ${capitalize(integrationName)}`; | ||
}; | ||
|
||
const getMinimumConcreteVersionMatchingSemver = (semverString: string): string => { | ||
return semver.valid(semver.coerce(semverString)) ?? ''; | ||
}; | ||
|
||
const buildTargetUrl = ( | ||
packageName: string, | ||
integrationName: string | null, | ||
targetVersion: string | ||
): string => { | ||
const packageSegment = targetVersion ? `${packageName}-${targetVersion}` : packageName; | ||
const query = integrationName ? `?integration=${integrationName}` : ''; | ||
return `app/integrations/detail/${packageSegment}/overview${query}`; | ||
}; |
10 changes: 10 additions & 0 deletions
10
...olution/public/detections/components/rules/related_integrations/integration_privileges.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,10 @@ | ||
/* | ||
* 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 interface IntegrationPrivileges { | ||
canReadInstalledIntegrations: boolean; | ||
} |
Oops, something went wrong.