Skip to content

Commit

Permalink
Handle the HTTP errors coming from scripts and show a different messa…
Browse files Browse the repository at this point in the history
…ge if initial configuration for Splunk Enterprise
  • Loading branch information
Marc-Antoine Hinse committed Jan 15, 2025
1 parent 5728ccd commit 128bd95
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
gap: 0.5rem;
align-items: center;
color: var(--error);
text-align: start;
}

.error-container[hidden] {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ const ConfigurationInitialStep: FC<{
setIsLoading(false);
onApiKeyValidated();
})
.catch((error: any) => {
setErrorMessage(error.data);
.catch((error: Error) => {
setErrorMessage(error.message);
setIsLoading(false);
toastManager.show({
id: ToastKeys.ERROR,
isError: true,
content: 'Something went wrong. Please review your form.',
content: `Something went wrong. ${error.message}`,
});
});
}
Expand Down
1 change: 1 addition & 0 deletions packages/react-components/src/components/Toast.css
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
.toast-text {
color: var(--text-color);
overflow-wrap: anywhere;
text-align: start;
}

@keyframes toast-animation-in {
Expand Down
1 change: 1 addition & 0 deletions packages/react-components/src/models/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const SEVERITY_SAVED_SEARCH_NAME = 'Severity';
export const KV_COLLECTION_NAME = 'event_ingestion_collection';
export const KV_COLLECTION_KEY = '_key';
export const KV_COLLECTION_VALUE = 'value';
export const SPLUNK_CLOUD_TYPE = 'cloud';

export enum PasswordKeys {
API_KEY = 'api_key',
Expand Down
1 change: 1 addition & 0 deletions packages/react-components/src/models/splunk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export interface SplunkService {
storagePasswords: () => SplunkStoragePasswordAccessors;
indexes: () => SplunkIndexesAccessor;
savedSearches: () => SplunkSavedSearchAccessor;
serverInfo: () => any;
get: (splunkUrlPath: string, data: any) => SplunkRequestResponse;
post: (splunkUrlPath: string, data: any) => SplunkRequestResponse;
}
57 changes: 41 additions & 16 deletions packages/react-components/src/utils/setupConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
KV_COLLECTION_VALUE,
PasswordKeys,
SEVERITY_SAVED_SEARCH_NAME,
SPLUNK_CLOUD_TYPE,
STORAGE_REALM,
} from '../models/constants';
import { Severity, SourceType, SourceTypeCategory, Tenant } from '../models/flare';
Expand Down Expand Up @@ -58,44 +59,68 @@ function createService(): SplunkService {
return service;
}

function handleHTTPScriptError(error: any): Promise<boolean> {
if (error.status === 400) {
return Promise.all([fetchIsFirstConfiguration(), fetchIsSplunkCloud()]).then(
([isFirstConfiguration, isSplunkCloud]) => {
if (isFirstConfiguration && !isSplunkCloud) {
throw Error(
'The Splunk instance needs to be restarted to recognize the scripts of this application.'
);
} else if (error.data.messages !== undefined && error.data.messages.length > 0) {
throw Error(error.data.messages[0].text);
}
throw Error(error.data);
}
);
}
throw Error(error.data);
}

function fetchApiKeyValidation(apiKey: string): Promise<boolean> {
const service = createService();
const data = { apiKey };
return promisify(service.post)('/services/fetch_api_key_validation', data).then(
(response: SplunkRequestResponse) => {
return promisify(service.post)('/services/fetch_api_key_validation', data)
.then((response: SplunkRequestResponse) => {
return response.status === 200;
}
);
})
.catch(handleHTTPScriptError);
}

function fetchUserTenants(apiKey: string): Promise<Array<Tenant>> {
const service = createService();
const data = { apiKey };
return promisify(service.post)('/services/fetch_user_tenants', data).then(
(response: SplunkRequestResponse) => {
return promisify(service.post)('/services/fetch_user_tenants', data)
.then((response: SplunkRequestResponse) => {
return response.data.tenants;
}
);
})
.catch(handleHTTPScriptError);
}

function fetchSeverityFilters(apiKey: string): Promise<Array<Severity>> {
const service = createService();
const data = { apiKey };
return promisify(service.post)('/services/fetch_severity_filters', data).then(
(response: SplunkRequestResponse) => {
return promisify(service.post)('/services/fetch_severity_filters', data)
.then((response: SplunkRequestResponse) => {
return response.data.severities;
}
);
})
.catch(handleHTTPScriptError);
}

function fetchSourceTypeFilters(apiKey: string): Promise<Array<SourceTypeCategory>> {
const service = createService();
const data = { apiKey };
return promisify(service.post)('/services/fetch_source_type_filters', data).then(
(response: SplunkRequestResponse) => {
return promisify(service.post)('/services/fetch_source_type_filters', data)
.then((response: SplunkRequestResponse) => {
return response.data.categories;
}
);
})
.catch(handleHTTPScriptError);
}

async function fetchIsSplunkCloud(): Promise<boolean> {
const service = createService();
const info = await service.serverInfo();
return info?.entry?.[0]?.content?.instance_type === SPLUNK_CLOUD_TYPE;
}

function doesPasswordExist(storage: SplunkStoragePasswordAccessors, key: string): boolean {
Expand Down

0 comments on commit 128bd95

Please sign in to comment.