/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import { BrowserServices } from '../../../../models/interfaces'; import { RuleEditorContainer } from '../../components/RuleEditor/RuleEditorContainer'; import React, { useCallback, useContext, useEffect, useState } from 'react'; import { EuiButton, EuiFilePicker, EuiFlexGroup, EuiFlexItem, EuiSpacer } from '@elastic/eui'; import { BREADCRUMBS, ROUTES } from '../../../../utils/constants'; import { Rule } from '../../../../../models/interfaces'; import { RouteComponentProps } from 'react-router-dom'; import { dump, load } from 'js-yaml'; import { ContentPanel } from '../../../../components/ContentPanel'; import { NotificationsStart } from 'opensearch-dashboards/public'; import { CoreServicesContext } from '../../../../components/core_services'; import { setBreadCrumb, validateRule } from '../../utils/helpers'; import { DataStore } from '../../../../store/DataStore'; export interface ImportRuleProps { services: BrowserServices; history: RouteComponentProps['history']; notifications?: NotificationsStart; } export const ImportRule: React.FC = ({ history, services, notifications }) => { const context = useContext(CoreServicesContext); const [fileError, setFileError] = useState(''); const onChange = useCallback((files: any) => { setFileError(''); if (files[0]?.type === 'application/x-yaml') { let reader = new FileReader(); reader.readAsText(files[0]); reader.onload = function () { try { const yamlContent: any = reader.result; if (!yamlContent) { setFileError('Invalid content in file'); return; } const jsonContent = load(yamlContent); if (!jsonContent) { setFileError('Invalid yaml content'); return; } let detectionYaml = ''; if (jsonContent.detection) { detectionYaml = dump(jsonContent.detection); } const rule: Rule = { id: '25b9c01c-350d-4b95-bed1-836d04a4f324', category: '', title: jsonContent.title || '', description: jsonContent.description || '', status: jsonContent.status || '', author: jsonContent.author || '', references: jsonContent.references?.map((reference: string) => ({ value: reference })) || [], tags: jsonContent.tags?.map((tag: string) => ({ value: tag })) || [], log_source: jsonContent.logsource || '', detection: detectionYaml, level: jsonContent.level || '', false_positives: jsonContent.falsepositives?.map((falsePositive: string) => ({ value: falsePositive, })) || [], }; setContent( ); } catch (error: any) { setFileError('Invalid file content'); } }; } else { setFileError(files.length > 0 ? 'Only yaml files are accepted' : ''); } }, []); const [content, setContent] = useState(null); useEffect(() => { setContent( <> {fileError &&
Error: {fileError}
}
history.replace(ROUTES.RULES)}>Cancel ); setBreadCrumb(BREADCRUMBS.RULES_IMPORT, context?.chrome.setBreadcrumbs); }, [fileError, onChange]); const footerActions: React.FC<{ rule: Rule }> = ({ rule }) => { const onCreate = async () => { if (!validateRule(rule, notifications!, 'create')) { return; } const response = await DataStore.rules.createRule(rule); if (response) { history.replace(ROUTES.RULES); } }; return ( history.replace(ROUTES.RULES)}>Cancel Create ); }; return content; };