Skip to content

Commit

Permalink
Creates the flare index
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Kasaboski committed Nov 13, 2024
1 parent 5c8f6b1 commit 47cc9b3
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,9 @@ lint: setup-web venv-tools mypy format-check
mypy: venv-tools
venv-tools/bin/mypy --config-file mypy.ini packages/flare

.PHONY: sl
sl: splunk-local

.PHONY: splunk-local
splunk-local: venv setup-web
@echo "Create symlink from app to Splunk Enterprise and start watching files"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { FC } from 'react';
import React, { FC, useEffect, useState } from 'react';
import Button from './Button';
import { getFlareDataUrl } from '../utils/setupConfiguration';
import ArrowRightIcon from './icons/ArrowRightIcon';
Expand All @@ -10,6 +10,17 @@ const ConfigurationCompletedStep: FC<{
tenantName: string;
onBackClicked: () => void;
}> = ({ show, tenantName, onBackClicked }) => {
const [index, setIndex] = useState<string>();

useEffect(() => {
const getIndex = async () => {
const index: string = await getFlareDataUrl();
setIndex(index);
};

getIndex();
}, []);

return (
<div hidden={!show}>
<h5>
Expand All @@ -23,7 +34,7 @@ const ConfigurationCompletedStep: FC<{
Edit Configuration
</Button>
<div className="link">
<a href={getFlareDataUrl('main')}>View Flare Data</a>
<a href={index}>View Flare Data</a>
<ArrowRightIcon remSize={1} />
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions packages/configuration-screen/src/models/splunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface ConfigurationStanzaAccessor {
update: (properties: Record<string, string>) => void;
list: () => Array<{ name: string }>;
properties: () => Record<string, string>;
_properties: Record<string, string>;
}

export interface ConfigurationFileAccessor {
Expand Down
26 changes: 26 additions & 0 deletions packages/configuration-screen/src/utils/configurationFileHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,29 @@ export async function updateConfigurationFile(
// change the information of a property
await updateStanzaProperties(configurationStanzaAccessor, properties);
}

export async function getCurrentIndex(service: SplunkService): Promise<string> {
// Retrieve the accessor used to get a configuration file
let configurations = service.configurations({
// Name space information not provided
});
configurations = await promisify(configurations.fetch)();

// Retrieves the configuration file accessor
let configurationFileAccessor = getConfigurationFile(configurations, 'inputs');
configurationFileAccessor = await promisify(configurationFileAccessor.fetch)();

// Retrieves the configuration stanza accessor
let configurationStanzaAccessor = getConfigurationFileStanza(
configurationFileAccessor,
'script://$SPLUNK_HOME/etc/apps/flare/bin/cron_job_ingest_events.py'
);
configurationStanzaAccessor = await promisify(configurationStanzaAccessor.fetch)();

let currentIndex = 'main';
if (configurationStanzaAccessor._properties.hasOwnProperty('index')) {
currentIndex = configurationStanzaAccessor._properties['index'];
}

return currentIndex;
}
7 changes: 5 additions & 2 deletions packages/configuration-screen/src/utils/setupConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { updateConfigurationFile } from './configurationFileHelper';
import { updateConfigurationFile, getCurrentIndex } from './configurationFileHelper';
import { Tenant } from '../models/flare';
import {
PasswordKeys,
Expand Down Expand Up @@ -43,7 +43,10 @@ function getRedirectUrl(): string {
return `/app/${appName}`;
}

function getFlareDataUrl(indexName: string): string {
async function getFlareDataUrl(): Promise<string> {
const service = createService(applicationNameSpace);
const indexName = await getCurrentIndex(service);

return `/app/${appName}/search?q=search%20index%3D"${indexName}"%20source%3D"flare"`;
}

Expand Down
6 changes: 6 additions & 0 deletions packages/flare/bin/cron_job_ingest_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@


def main(logger: Logger, app: client.Application) -> None:
create_flare_index(app.service)
create_collection(app=app)

# To avoid cron jobs from doing the same work at the same time, exit new cron jobs if a cron job is already doing work
Expand Down Expand Up @@ -62,6 +63,11 @@ def main(logger: Logger, app: client.Application) -> None:
logger.info(f"Retrieved {events_retrieved_count} events")


def create_flare_index(service: Service) -> None:
if APP_NAME not in service.indexes:
service.indexes.create(APP_NAME)


def get_storage_password_value(
app: client.Application, password_key: str
) -> Optional[str]:
Expand Down
3 changes: 3 additions & 0 deletions packages/flare/tests/bin/test_flare_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest
import sys

from typing import Any
from unittest.mock import MagicMock
from unittest.mock import Mock
from unittest.mock import PropertyMock
Expand Down Expand Up @@ -53,7 +54,9 @@ def test_flare_full_data_without_metadata(

@patch("flare.FlareAPI._retrieve_full_event_from_uid")
@patch("flare.FlareAPI._retrieve_event_feed_metadata")
@patch("time.sleep", return_value=None)
def test_flare_full_data_with_metadata(
sleep: Any,
retrieve_event_feed_metadata_mock: MagicMock,
retrieve_full_event_from_uid_mock: MagicMock,
) -> None:
Expand Down
15 changes: 15 additions & 0 deletions packages/flare/tests/bin/test_ingest_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@


sys.path.insert(0, os.path.join(os.path.dirname(__file__), "../../bin"))
from constants import APP_NAME
from constants import CRON_JOB_THRESHOLD_SINCE_LAST_FETCH
from constants import KV_COLLECTION_NAME
from constants import CollectionKeys
from cron_job_ingest_events import create_flare_index
from cron_job_ingest_events import fetch_feed
from cron_job_ingest_events import get_api_key
from cron_job_ingest_events import get_collection_value
Expand All @@ -29,6 +31,19 @@
from cron_job_ingest_events import save_start_date


def test_create_flare_index_expect_none() -> None:
app = MagicMock()
app.service.indexes.__contains__.side_effect = lambda x: x == APP_NAME
create_flare_index(app.service)
app.service.indexes[APP_NAME].assert_not_called()


def test_create_flare_index_expect_create() -> None:
app = MagicMock()
create_flare_index(app.service)
app.service.indexes.create.assert_called_once_with(APP_NAME)


def test_get_collection_value_expect_none() -> None:
app = MagicMock()
assert get_collection_value(app=app, key="some_key") is None
Expand Down

0 comments on commit 47cc9b3

Please sign in to comment.