This script creates very basic roles with no extra capabilities and restricts the role to viewing only the indicated source category’s data.
################################################################################
# This script reads an Excel file containing role data, then uses the Sumo Logic
# API to create roles based on the data. It checks each row for a role name and
# uses the source category to set data filters. The script requires a config.py
# file with access credentials.
################################################################################
import pandas as pd
import requests
import json
from config import access_id, access_key # Import credentials from config.py
# Path to Excel file
excel_file_path = 'NewRoles.xlsx'
# Base URL for Sumo Logic API
base_url = 'https://api.sumologic.com/api/v1'
################################################################################
# Function to create a new role using the Sumo Logic API.
#
# Args:
# role_name (str): The name of the role to create.
# role_description (str): The description of the role.
# source_category (str): The source category to restrict the role to.
#
# Returns:
# None. Prints the status of the API call.
################################################################################
def create_role(role_name, role_description, source_category):
url = f'{base_url}/roles'
# Role payload
data_filter = f'_sourceCategory={source_category}'
payload = {
'name': role_name,
'description': role_description,
'logAnalyticsDataFilter': data_filter,
'auditDataFilter': data_filter,
'securityDataFilter': data_filter
}
# Headers for the request
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
# Debugging line
print(f"Attempting to create role: '{role_name}' with description: '{role_description}' and filter: '{data_filter}'")
# Make the POST request to create a new role
response = requests.post(url, auth=(access_id, access_key), headers=headers, data=json.dumps(payload))
# Check the response
if response.status_code == 201:
print(f'Role {role_name} created successfully.')
else:
print(f'Failed to create role {role_name}. Status Code: {response.status_code}')
print('Response:', response.json())
################################################################################
# Reads an Excel file and processes each row to extract role information and
# create roles using the Sumo Logic API.
#
# Args:
# file_path (str): The path to the Excel file containing role data.
#
# Returns:
# None. Processes the file and attempts to create roles based on the data.
################################################################################
def process_excel(file_path):
# Load the spreadsheet
df = pd.read_excel(file_path, engine='openpyxl')
# Print column names to help debug and find correct ones
print("Columns found in Excel:", df.columns)
# Iterate over each row in the DataFrame
for index, row in df.iterrows():
role_name = row['Role Name'] # Correct column name for role name
source_category = row['Source Category'] # Correct column name for source category to which role is restricted
# Only create a role if the role name is not null
if pd.notnull(role_name):
role_description = f'Provides access to source category {source_category}'
create_role(role_name, role_description, source_category)
# Process the Excel file
process_excel(excel_file_path)