Skip to content

Instantly share code, notes, and snippets.

@jayers99
Created December 5, 2023 22:41
Show Gist options
  • Save jayers99/2a43a91f791ff91372b9e97ff53e0d3e to your computer and use it in GitHub Desktop.
Save jayers99/2a43a91f791ff91372b9e97ff53e0d3e to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import requests
import pandas as pd
import os
import subprocess
from io import StringIO
def get_keychain_password(account, service):
"""
Fetch a password from the macOS Keychain.
:param account: The account name associated with the password.
:param service: The service name associated with the password.
:return: The password.
"""
command = ["security", "find-generic-password", "-a", account, "-s", service, "-w"]
try:
password = subprocess.check_output(command).strip().decode()
except subprocess.CalledProcessError:
raise ValueError("Could not retrieve password from Keychain")
return password
def main():
# Fetch Splunk server details
splunk_host = os.environ.get('SPLUNK_HOST')
splunk_api_endpoint = os.environ.get('SPLUNK_API_ENDPOINT')
splunk_username = os.environ.get('SPLUNK_USERNAME')
splunk_service_name = os.environ.get('SPLUNK_SERVICE_NAME') # The service name you used in Keychain
# Ensure all required environment variables are set
if not all([splunk_host, splunk_api_endpoint, splunk_username, splunk_service_name]):
raise EnvironmentError("One or more environment variables are missing.")
# Fetch password from Keychain
splunk_password = get_keychain_password(splunk_username, splunk_service_name)
# Fetch existing lookup table
response = requests.get(f"{splunk_host}{splunk_api_endpoint}", auth=(splunk_username, splunk_password))
current_data = pd.read_csv(StringIO(response.text))
# Append new data
new_data = {"column1": "value1", "column2": "value2"}
current_data = current_data.append(new_data, ignore_index=True)
# Convert DataFrame back to CSV
updated_data_csv = current_data.to_csv(index=False)
# Upload updated lookup table
requests.post(f"{splunk_host}{splunk_api_endpoint}", data=updated_data_csv, auth=(splunk_username, splunk_password), headers={"Content-Type": "text/csv"})
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment