From 958a05f79c2a41a532ec9aaee1f3d77fcdb89471 Mon Sep 17 00:00:00 2001 From: Joey Dreijer Date: Fri, 24 Jul 2020 01:48:45 +0200 Subject: [PATCH] Initial upload --- .dockerignore | 4 ++ .gitignore | 138 ++++++++++++++++++++++++++++++++++++++++++++++++++ Dockerfile | 33 ++++++++++++ upload.py | 64 +++++++++++++++++++++++ 4 files changed, 239 insertions(+) create mode 100644 .dockerignore create mode 100644 .gitignore create mode 100644 Dockerfile create mode 100644 upload.py diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..f74483a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,4 @@ +.git +.gitignore +.cache +azure-pipelines.yml diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..a81c8ee --- /dev/null +++ b/.gitignore @@ -0,0 +1,138 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..e0a4890 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,33 @@ +FROM python:3.7-slim + +# Install base dependencies +RUN apt-get update && apt-get install -y wget libjpeg-dev libxml2-dev libxslt-dev lib32z1-dev python-lxml +RUN pip install virtualenv + +# Set up non-root user and assign manpage access +# since the splunkpackagingtoolkit requires access to the global manpage (man1) dir +RUN useradd -ms /bin/bash splunkbuild +RUN mkdir /usr/share/man/man1 +RUN chown -R splunkbuild:splunkbuild /usr/share/man/man1 +USER splunkbuild +WORKDIR /home/splunkbuild + +# Set up virtualenvironment for appinspect +ENV VIRTUAL_ENV_APPINSPECT=/home/splunkbuild/venv_appinspect +RUN virtualenv -p python3 $VIRTUAL_ENV_APPINSPECT +ENV PATH="$VIRTUAL_ENV_APPINSPECT/bin:$PATH" + +# Install and download appinspect + dependencies +RUN pip install Pillow +RUN wget https://download.splunk.com/misc/appinspect/splunk-appinspect-latest.tar.gz +RUN pip install splunk-appinspect-latest.tar.gz + +# Set up virtualenvironment for packagingtoolkit +ENV VIRTUAL_ENV_TOOLKIT=/home/splunkbuild/venv_packagingtoolkit +RUN virtualenv -p python3 $VIRTUAL_ENV_TOOLKIT +ENV PATH="$VIRTUAL_ENV_TOOLKIT/bin:$PATH" + +# Install and download appinspect + packagingtoolkit +RUN pip install semantic_version +RUN wget https://download.splunk.com/misc/packaging-toolkit/splunk-packaging-toolkit-1.0.1.tar.gz +RUN pip install splunk-packaging-toolkit-1.0.1.tar.gz diff --git a/upload.py b/upload.py new file mode 100644 index 0000000..04e06d0 --- /dev/null +++ b/upload.py @@ -0,0 +1,64 @@ +from bs4 import BeautifulSoup +import argparse +import requests +import json +import os + +splunk_host = os.getenv('SPLUNK_HOST') +service_username = os.getenv('SPLUNK_USERNAME') +service_password = os.getenv('SPLUNK_PASSWORD') + +class Splunk: + def __init__(self, host, username, password): + self.host = host + self.username = username + self.password = password + self.session = requests.Session() + + def init_login(self, endpoint): + get_session = self.session.get(endpoint) + soup = BeautifulSoup(get_session.text, 'html.parser') + get_partials = soup.find(id='splunkd-partials', type='text/json').contents[0] + cval = json.loads(get_partials.strip())['/services/session']['entry'][0]['content']['cval'] + return cval + + def login(self): + endpoint = f'{self.host}/en-US/account/login' + header = {'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'} + login_form = { 'username': self.username, + 'password': self.password, 'cval': self.init_login(endpoint) } + self.session.post(endpoint, data=login_form, headers=header) + + def init_upload(self, endpoint): + get_upload = self.session.get(endpoint) + soup = BeautifulSoup(get_upload.text, 'html.parser') + form_key = soup.find('input', {'name': 'splunk_form_key'}).get('value') + form_state = soup.find('input', {'name': 'state'}).get('value') + return form_key, form_state + + def upload(self, app_path): + endpoint = f'{self.host}/en-US/manager/appinstall/_upload' + form_key, form_state = self.init_upload(endpoint) + base_filename = os.path.basename(app_path) + multipart_upload = { + 'state': (None, form_state), + 'splunk_form_key': (None, form_key), + 'appfile': (base_filename, open(app_path, 'rb'), 'application/x-gzip') + } + upload_app = self.session.post(endpoint, files=multipart_upload) + print(upload_app) + + def __enter__(self): + self.login() + return self + + def __exit__(self ,type, value, traceback): + self.session.close() + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description='Upload Splunk app') + parser.add_argument('--path', type=str, help='Path to compressed Splunk app') + args = parser.parse_args() + + with Splunk(splunk_host, service_username, service_password) as splunk: + splunk.upload(args.path)