Splunk Dev

Making REST API URL and Credentials Dynamic in Splunk React App

sanjai
Path Finder

Hi Splunker,

I’ve been developing a React app for Splunk that manages users via the REST API (create/update/delete). Initially, I hardcoded the REST API URL, username, and password for development purposes. Now that the development is nearly complete, I need to make the URL dynamic. It should retrieve the REST API server URL and the currently logged-in user’s information and use it in the Splunk React app.

How can I achieve this?

Here is the current hardcoded code:

const fetchAllUsers = async () => {
try {
const response = await axios.get('https://mymachine:8089/services/authentication/users', {
auth: { username: 'admin', password: 'admin123' },
headers: { 'Content-Type': 'application/xml' }
});
} catch (error) {
console.error('Error fetching users:', error);
}
};



#restapi  #createuser #react #reactapp


thanks in advance

0 Karma

PickleRick
SplunkTrust
SplunkTrust

I'm not sure I get the question right. Are you asking how to externalize config from the code in react application?

I'm not a react developer but there are several easily googleable links in that topic. For example https://stackoverflow.com/questions/30568796/how-to-store-configuration-file-and-read-it-using-react

0 Karma

sanjai
Path Finder

hi @PickleRick , thanks for your time,

Yes, that's correct! The goal is to externalize the configuration (like the REST API URL, username, and password) from the code so it’s not hardcoded. Specifically, I want to dynamically retrieve the REST API server URL and the currently logged-in user’s information and use it in the React app within Splunk. Do you have any suggestions on how to achieve this?

Is there a predefined token that can gives the server URL, username, and password (or something similar to a session key for the currently logged-in user) that I can use in my React code?

Thanks,

0 Karma

PickleRick
SplunkTrust
SplunkTrust

I'm not sure what you want here. You must have the server's address to connect to its API endpoint so it's not clear for me who would return that address to you. And you can't get user's password. It won't work that way.

Also - what do you mean by "currently logged in user"? Which user? Are you trying to piggyback on someone's already existing session (if so, I'd expect Splunk to have defenses against session hijacking and if possible, that should probably be explicitly configured)?

Or do you want to authenticate a user in Splunk so that you can use that session for your purposes? If so, use user's credentials to log in to Splunk, obtain a session token and use that token. But it has all the risks of a man-in-the-middle solution.

0 Karma
Career Survey
First 500 qualified respondents will receive a $20 gift card! Tell us about your professional Splunk journey.
Get Updates on the Splunk Community!

Data Persistence in the OpenTelemetry Collector

This blog post is part of an ongoing series on OpenTelemetry. What happens if the OpenTelemetry collector ...

Introducing Splunk 10.0: Smarter, Faster, and More Powerful Than Ever

Now On Demand Whether you're managing complex deployments or looking to future-proof your data ...

Community Content Calendar, September edition

Welcome to another insightful post from our Community Content Calendar! We're thrilled to continue bringing ...