Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions tsconfig.base.json
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,8 @@
"@kbn/alerting-plugin/*": ["x-pack/plugins/alerting/*"],
"@kbn/apm-plugin": ["x-pack/plugins/apm"],
"@kbn/apm-plugin/*": ["x-pack/plugins/apm/*"],
"@kbn/asset-inventory-plugin": ["x-pack/plugins/asset_inventory"],
"@kbn/asset-inventory-plugin/*": ["x-pack/plugins/asset_inventory/*"],
"@kbn/banners-plugin": ["x-pack/plugins/banners"],
"@kbn/banners-plugin/*": ["x-pack/plugins/banners/*"],
"@kbn/canvas-plugin": ["x-pack/plugins/canvas"],
Expand Down
14 changes: 14 additions & 0 deletions x-pack/plugins/asset_inventory/common/debug_log.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/*
* 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 function debug(...args: any[]) {
if (process.env.NODE_ENV === 'production') {
return;
}
// eslint-disable-next-line no-console
console.log('[DEBUG LOG]', ...args);
}
109 changes: 109 additions & 0 deletions x-pack/plugins/asset_inventory/common/types_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/*
* 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 type AssetKind = 'unknown' | 'node';
export type AssetType = 'k8s.pod' | 'k8s.cluster' | 'k8s.node';

export interface ECSDocument {
'@timestamp': string;
kubernetes?: EcsKubernetesFieldset;
orchestrator?: EcsOrchestratorFieldset;
cloud?: EcsCloudFieldset;
}

export type AssetStatus = 'CREATING' | 'ACTIVE' | 'DELETING' | 'FAILED' | 'UPDATING' | 'PENDING';

export interface Asset extends ECSDocument {
'asset.collection_version'?: string;
'asset.ean': string;
'asset.id': string;
'asset.kind': AssetKind;
'asset.name'?: string;
'asset.type': AssetType;
'asset.status'?: AssetStatus;
'asset.parents'?: string | string[];
'asset.children'?: string | string[];
'asset.namespace'?: string;
}

export interface K8sPod extends ECSDocument {
id: string;
name: string;
ean: string;
node?: string;
}

export interface K8sNode extends ECSDocument {
id: string;
name: string;
ean: string;
pods?: K8sPod[];
cluster?: string;
}

export interface K8sCluster extends ECSDocument {
name: string;
nodes: K8sNode[];
status: string;
version: string;
}

export interface EcsKubernetesFieldset {
namespace?: string;
pod?: {
name: string;
uid: string;
start_time?: Date;
};
node?: {
name: string;
start_time?: Date;
};
}

// See: https://www.elastic.co/guide/en/ecs/current/ecs-orchestrator.html
export interface EcsOrchestratorFieldset {
api_version?: string;
namespace?: string;
organization?: string;
type?: string;
cluster?: {
id?: string;
name?: string;
url?: string;
version?: string;
};
resource?: {
id?: string;
ip?: string;
name?: string;
type?: string;
parent?: {
type?: string;
};
};
}

export type CloudProviderName = 'aws' | 'gcp' | 'azure' | 'other' | 'unknown' | 'none';

export interface EcsCloudFieldset {
provider: CloudProviderName;
region?: string;
service?: {
name?: string;
};
}

export interface AssetFilters {
type?: AssetType;
kind?: AssetKind;
ean?: string;
id?: string;
typeLike?: string;
eanLike?: string;
collectionVersion?: number | 'latest' | 'all';
}
30 changes: 30 additions & 0 deletions x-pack/plugins/asset_inventory/kibana.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"id": "assetInventory",
"owner": {
"name": "TBD"
},
"version": "8.0.0",
"kibanaVersion": "kibana",
"configPath": [
"xpack",
"assetInventory"
],
"optionalPlugins": [
],
"requiredPlugins": [
"alerting",
"cases",
"data",
"dataViews",
"features",
"inspector",
"unifiedSearch",
"usageCollection"
],
"ui": true,
"server": true,
"requiredBundles": [
"kibanaReact",
"kibanaUtils"
]
}
90 changes: 90 additions & 0 deletions x-pack/plugins/asset_inventory/public/app/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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 { i18n } from '@kbn/i18n';
import React from 'react';
import ReactDOM from 'react-dom';
import { Route, Router, Switch } from 'react-router-dom';
import { Storage } from '@kbn/kibana-utils-plugin/public';

import { AppMountParameters, APP_WRAPPER_CLASS, CoreStart } from '@kbn/core/public';
import { KibanaContextProvider, KibanaThemeProvider } from '@kbn/kibana-react-plugin/public';
import { EuiThemeProvider } from '@kbn/kibana-react-plugin/common';

import { UsageCollectionSetup } from '@kbn/usage-collection-plugin/public';
import { AssetInventoryPublicPluginsStart } from '../plugin';
import { routes } from './routes';
import { AssetFilterContextProvider } from '../hooks/asset_filters';

function App() {
return (
<>
<Switch>
{routes.map((routeProps) => {
return <Route key={String(routeProps.path)} {...routeProps} />;
})}
</Switch>
</>
);
}

export function renderApp({
core,
config,
plugins,
appMountParameters,
usageCollection,
isDev,
}: {
core: CoreStart;
config: {};
plugins: AssetInventoryPublicPluginsStart;
appMountParameters: AppMountParameters;
usageCollection: UsageCollectionSetup;
isDev?: boolean;
}) {
const { element, history, theme$ } = appMountParameters;
const i18nCore = core.i18n;
const isDarkMode = core.uiSettings.get('theme:darkMode');

core.chrome.setHelpExtension({
appName: i18n.translate('xpack.observability.feedbackMenu.appName', {
defaultMessage: 'Observability',
}),
links: [{ linkType: 'discuss', href: 'https://ela.st/observability-discuss' }],
});

// ensure all divs are .kbnAppWrappers
element.classList.add(APP_WRAPPER_CLASS);

const ApplicationUsageTrackingProvider =
usageCollection?.components.ApplicationUsageTrackingProvider ?? React.Fragment;

ReactDOM.render(
<ApplicationUsageTrackingProvider>
<KibanaThemeProvider theme$={theme$}>
<KibanaContextProvider
services={{ ...core, ...plugins, storage: new Storage(localStorage), isDev }}
>
<AssetFilterContextProvider>
<Router history={history}>
<EuiThemeProvider darkMode={isDarkMode}>
<i18nCore.Context>
<App />
</i18nCore.Context>
</EuiThemeProvider>
</Router>
</AssetFilterContextProvider>
</KibanaContextProvider>
</KibanaThemeProvider>
</ApplicationUsageTrackingProvider>,
element
);
return () => {
ReactDOM.unmountComponentAtNode(element);
};
}
29 changes: 29 additions & 0 deletions x-pack/plugins/asset_inventory/public/app/routes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* 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 { RouteProps } from 'react-router-dom';
import { AssetInventoryListPage } from '../pages/asset_inventory_list_page';
import { K8sClustersListPage } from '../pages/k8s/clusters_list_page';
import { K8sClusterPage } from '../pages/k8s/cluster_page';

export const routes: RouteProps[] = [
{
path: '/',
exact: true,
component: AssetInventoryListPage,
},
{
path: '/k8s/clusters',
exact: true,
component: K8sClustersListPage,
},
{
path: '/k8s/clusters/:name',
exact: true,
component: K8sClusterPage,
},
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*
* 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 { EuiComboBox, EuiComboBoxOptionOption, EuiFormRow } from '@elastic/eui';
import axios from 'axios';
import React, { useEffect, useState } from 'react';
import { AssetFilters } from '../../common/types_api';
import { useAssetFilters } from '../hooks/asset_filters';

interface AutocompleteOptions {
field: string;
label: string;
filtersKey: keyof AssetFilters;
allowMultipleValues?: boolean;
placeholder?: string;
fullWidth?: boolean;
}

interface FieldValueResult {
key: string;
doc_count: number;
}

export function AssetFilterAutocomplete({
field,
label,
filtersKey,
allowMultipleValues = false,
placeholder,
fullWidth = false,
}: AutocompleteOptions) {
const [options, setOptions] = useState<EuiComboBoxOptionOption[]>([]);
const [selected, setSelected] = useState<EuiComboBoxOptionOption[]>([]);
const [isLoading, setIsLoading] = useState<boolean>(false);
const { filters, setFilters } = useAssetFilters();

const { collectionVersion } = filters;

useEffect(() => {
async function retrieve() {
setIsLoading(true);
const response = await axios.get<{ results: FieldValueResult[] }>(
`/local/api/asset-inventory/field-values?field=${field}&version=${
collectionVersion || 'all'
}`
);

if (response.data.results) {
const newOptions: EuiComboBoxOptionOption[] = response.data.results.map((result) => ({
label: `${result.key} (${result.doc_count})`,
value: result.key,
}));
setOptions(newOptions);
}
setIsLoading(false);
}

retrieve();
}, [field, collectionVersion]);

useEffect(() => {
const selectedValues = selected.map((option) => option.value);
const values = allowMultipleValues ? selectedValues : selectedValues[0];
setFilters((prevFilters) => ({ ...prevFilters, [filtersKey]: values }));
}, [selected, allowMultipleValues, filtersKey, setFilters]);

return (
<EuiFormRow label={label} fullWidth={fullWidth}>
<EuiComboBox
options={options}
selectedOptions={selected}
isLoading={isLoading}
singleSelection={allowMultipleValues ? false : { asPlainText: true }}
onChange={(newOptions) => setSelected(newOptions)}
placeholder={placeholder}
fullWidth={fullWidth}
/>
</EuiFormRow>
);
}
Loading