2023-01-27 09:47:59 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""Build Splunk app package"""
|
|
|
|
|
|
|
|
import tarfile
|
|
|
|
from configparser import ConfigParser
|
|
|
|
from os import environ, path
|
2023-10-01 10:03:49 +00:00
|
|
|
from pathlib import PurePath
|
|
|
|
from posixpath import join as posixjoin
|
|
|
|
from re import search
|
2023-01-27 09:47:59 +00:00
|
|
|
from subprocess import check_call
|
|
|
|
from sys import executable
|
|
|
|
|
|
|
|
|
|
|
|
def version():
|
|
|
|
"""
|
|
|
|
Return version number from app.conf or commit hash if in CI
|
|
|
|
"""
|
|
|
|
|
|
|
|
commit_sha = (
|
2023-01-27 11:05:34 +00:00
|
|
|
(
|
|
|
|
# gitlab
|
|
|
|
environ.get("CI_COMMIT_TAG")
|
|
|
|
or environ.get("CI_COMMIT_SHORT_SHA")
|
|
|
|
# github
|
|
|
|
or (
|
|
|
|
environ.get("GITHUB_REF_NAME")
|
|
|
|
if environ.get("GITHUB_REF_TYPE") == "tag"
|
|
|
|
else None
|
|
|
|
)
|
|
|
|
or environ.get("GITHUB_SHA", "")[0:8]
|
|
|
|
)
|
|
|
|
if environ.get("CI") == "true"
|
|
|
|
else None
|
2023-01-27 09:47:59 +00:00
|
|
|
)
|
|
|
|
if commit_sha:
|
|
|
|
return commit_sha
|
|
|
|
|
|
|
|
app_conf_path = path.join(
|
|
|
|
"default",
|
|
|
|
"app.conf",
|
|
|
|
)
|
|
|
|
app_conf = ConfigParser()
|
|
|
|
app_conf.read(app_conf_path)
|
2023-02-06 06:40:51 +00:00
|
|
|
launcher = app_conf["launcher"] if "launcher" in app_conf.sections() else {}
|
|
|
|
return launcher.get("version", "")
|
2023-01-27 09:47:59 +00:00
|
|
|
|
|
|
|
|
|
|
|
def exclusion(tarinfo):
|
|
|
|
"""Exclude dev files and cache, and reset file stats"""
|
|
|
|
|
|
|
|
# exclude certain folders/files
|
|
|
|
pathname = tarinfo.name
|
|
|
|
if search(
|
2023-10-01 10:03:49 +00:00
|
|
|
r"/\.|\\\.|__pycache__|pyproject\.toml|requirements|build\.py|\.tar\.gz|\.tgz",
|
|
|
|
pathname,
|
2023-01-27 09:47:59 +00:00
|
|
|
):
|
|
|
|
return None
|
|
|
|
|
2023-10-01 10:03:49 +00:00
|
|
|
app = PurePath(pathname).parts[0]
|
2023-01-27 09:47:59 +00:00
|
|
|
|
|
|
|
# reset file stats
|
2023-10-01 10:03:49 +00:00
|
|
|
tarinfo.uid = 0
|
|
|
|
tarinfo.gid = 0
|
2023-01-27 09:47:59 +00:00
|
|
|
tarinfo.uname = tarinfo.gname = ""
|
2023-10-01 10:03:49 +00:00
|
|
|
if tarinfo.isfile():
|
|
|
|
# remove execution permission
|
|
|
|
tarinfo.mode = 0o644
|
|
|
|
|
|
|
|
# except for scripts
|
|
|
|
# tarinfo uses posix (not nt)
|
|
|
|
if (
|
|
|
|
tarinfo.name.startswith(posixjoin(app, "bin"))
|
|
|
|
and path.splitext(tarinfo.name)[-1] == ".py"
|
|
|
|
):
|
|
|
|
tarinfo.mode = 0o744
|
|
|
|
if tarinfo.isdir():
|
|
|
|
# remove write permission from group & world
|
|
|
|
tarinfo.mode = 0o755
|
2023-01-27 09:47:59 +00:00
|
|
|
|
|
|
|
return tarinfo
|
|
|
|
|
|
|
|
|
|
|
|
print("Installing dependencies into './lib/'...")
|
|
|
|
check_call(
|
|
|
|
[
|
|
|
|
executable,
|
|
|
|
"-m",
|
|
|
|
"pip",
|
|
|
|
"install",
|
|
|
|
"--quiet",
|
2023-02-08 10:06:21 +00:00
|
|
|
"splunk-sdk == 1.*",
|
2023-01-27 09:47:59 +00:00
|
|
|
"-t",
|
|
|
|
"lib",
|
|
|
|
"--upgrade",
|
|
|
|
]
|
|
|
|
)
|
|
|
|
|
2023-07-14 10:12:39 +00:00
|
|
|
pkg_file = f"TA-malware-filter-{version()}.tar.gz"
|
2023-01-27 09:47:59 +00:00
|
|
|
print(f"Creating {pkg_file}...")
|
|
|
|
with tarfile.open(pkg_file, "w:gz") as tar:
|
2023-10-01 10:03:49 +00:00
|
|
|
tar.add(".", filter=exclusion, arcname="TA-malware-filter")
|