Site icon iDelta

OAuth with the Splunk Add-on Builder

in this article we’ll show how you a step-by-step approach to performing an OAuth authentication when developing in the Splunk Add-on Builder.

In a previous post we introduced the GlowStick Zigbee CAD, a device that collects your smart meter data and makes it available to you via an API.

Understand the Journey

The API you are trying to code against should of course have documentation, perhaps even “curl” examples of how to perform the OAuth journey. Read this and try out the “curl” examples with your credentials., make sure you can see that a token is returned in the response.

Use Postman

Postman is an API platform for building and using APIs. One key feature is that it will generate the code required to call the API that you are coding against. Through the user interface you can add the URI of the API, any headers required and the payload.

Setup your OAuth call in Postman

As indicated above, we can use Postman to provide code that makes the API call. There are various language and library options available, for the purposes of Splunk Add-on development we will use the Python – Requests library. Clicking on the “Code” link as shown in the screenshot above provides the code, as shown below.

Postman Code Screen showing how to call the API using Python – Requests

Use Add-on Builder to Create the App

Use the Splunk Add-on Builder to create your app. To get started click “New Add-on”:

Splunk Add-on Builder – Create New Add-on

Create Data Input

The next step is to start the setup of a data input. You will need to know the parameters required to complete the OAuth authentication along with any other parameters required to fetch the data once the authentiation is complete. This may be as straightforward as a a client_id and secret but some scenarios may require more. By completing step 1 “Understand the Journey” you should already have an understanding of this.

Supply the Code

Replace the supplied python code with the following:

import os
import sys
import time
import datetime
import json
import utils.auth as rauth
import utils.utils as utils
     
def validate_input(helper, definition):
	pass
	
def collect_events(helper, ew):
	pass

Now we need to add some code into the correct directory within the Add-on:

auth.py

The file auth.py is where we will perform the OAuth authentication and retreive the token to be used on the subsequent call to collect the data.

# encoding = utf-8
import sys
import json
import requests

def get_access_token(username, password, application_id):
    url = "https://api.glowmarkt.com/api/v0-1/auth"
    payload="{\"username\": \"%s\", \n\"password\": \"%s\"\n}"%(username,password)
    headers = {
        'Content-Type': 'application/json',
        'applicationId': application_id
        }
    try:
        response = requests.post(url, headers=headers,data=payload).json()
        return response['token']
    except Exception as e:
            raise e

utils.py

Now we create another file, utils.py. The code in this file will call the API to retrieve the data. You could use Postman again to help ensure you have the correct headers and to check out what the response looks like.

# encoding = utf-8
import sys
import json
import requests

def get_items(helper, access_token, url, applicationId):
    header = {'Content-Type': 'application/json',
    'token': access_token,
    'applicationId': applicationId}

    try:
        r = requests.get(url, headers=header)
        r.raise_for_status()
        response_json = json.loads(r.content)
        
    except Exception as e:
        raise e

    return response_json

Back to the Builder

Switch back to the Add-on Builder and click on Add-On Setup Parameters.

Now we need to supply the code for the collect_events method we can see in the Add-on builder Code Editor:

import os
import sys
import time
import datetime
import json
import utils.auth as rauth
import utils.utils as utils
     
def validate_input(helper, definition):
	pass
	
def collect_events(helper, ew):
    global_account = helper.get_arg("global_account")
    username = global_account["username"]
    password = global_account["password"]
    applicationid = helper.get_arg("applicationid")
    resourceid = helper.get_arg("resourceid")
    
    access_token = rauth.get_access_token(username, password, applicationid)
    
    if(access_token):
        
        helper.log_debug("Calling glowmarkt API")
        url = "https://api.glowmarkt.com/api/v0-1/resource/%s/current" % (resourceid)
        electricity_reading = utils.get_items(helper, access_token, url, applicationid)
        
        event = helper.new_event(
            data=json.dumps(electricity_reading),
            source=helper.get_input_type(), 
            index=helper.get_output_index(),
            sourcetype=helper.get_sourcetype())
        ew.write_event(event)

    else:
        raise RuntimeError("Unable to obtain access token. Please check the username, password and applicationId")

Hit Save then Test. If everything is in place then you should see the JSON output of the API call in the right hand side. Congratulations on coding an OAuth journey with the Splunk Add-on Builder!

Validate your Add-on

There are more inputs to add to this add-on but we are going to skip to the end for now. Prior to uploading to Splunkbase you should validate your add-on. Even if you don’t plan to upload to Splunkbase it’s a good time to check that everything is in order.

As you can see below the Add-on passed validation with one single warning about their being no events. We haven’t setup any inputs in the actual add-on, the reason for this is so that the downloaded package doesn’t contain any user config.

Add-on Builder Validate Package Screen

Finally you can click on the “Download Package” button to get your newly created app and share it with your friends!

Credits and Thanks

In 2019 I was fortunate to be invited to and attend the Splunk Partner Technical Symposium in Berlin. Splunk had arranged for a number of great speakers to give proper deep dive technical talks on a variety of subjects. One of these talks explained how to perform an OAuth authentication while using the add-on builder to generate a new add-on. Many thanks to Jason Conger for this session. You can access his notes and code on github here.


For 2021 we’ve committed to posting a new Splunk tip every week!

If you want to keep up to date on tips like the one above then sign up below:

Subscribe to our newsletter to receive regular updates from iDelta, including news and updates, information on upcoming events, and Splunk tips and tricks from our team of experts. You can also find us on Twitter and LinkedIn.

Subscribe

* indicates required
Exit mobile version