Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add timeout to HTTP calls to prevent application hangs. #51

Merged
merged 1 commit into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add timeout to HTTP calls to prevent application hangs.
  • Loading branch information
nzlosh committed Oct 10, 2024
commit bb0c1dfd528688ca8bcd280bc8459ba524ab0d53
46 changes: 27 additions & 19 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ SOURCE_PATH=src/err-stackstorm
LIB_PATH=${SOURCE_PATH}/errst2lib
TESTS_PATH=tests

# ANSI colour formats for help output.
FMT_TITLE=\e[34;40;1m
FMT_TARGET=\e[37;0;1m
FMT_NONE=\e[0m

.PHONY: all
all: auto_format lint_test unit_test security_scan
all: auto_format format_test lint_test unit_test security_scan

.PHONY: clean
.PHONY: clean # Remove tmp files and previous build artifacst. (Not Implemented)
clean:
@echo Cleaning - N/A

.PHONY: setup
.PHONY: setup # Install errbot and dependencies into virtual environment.
setup:
test -n "${CI}" || ( test -n "${VIRTUAL_ENV}" || (echo "Not running in virtualenv/CI - abort setup"; exit 1 ) ) && echo "Running in virtual environment or CI pipeline"
pip install --upgrade pip
Expand All @@ -21,53 +26,56 @@ setup:
pip install -r requirements-test.txt
pip install -r requirements-build.txt

.PHONY: build_python_package
.PHONY: build_python_package # Build python deployment packages.
build_python_package:
echo "Build python package"
python -m build

.PHONY: publish_pypi
.PHONY: publish_pypi # Push python deployment packages to pypi. (Not Implemented)
publish_pypi:
echo "Publish python packages to pypi"
echo TO DO: python -m twine upload err-stackstorm dist/*

.PHONY: documentation
.PHONY: build_documentation # Generate readthedocs documentation. (Not Implemented)
documentation:
echo "Build documentation"
echo TO DO - trigger readthedocs.

.PHONY: format_test
.PHONY: format_test # Run black formatting check over source files.
format_test:
echo "Formatting code\n"
black --check --line-length=${MAX_LINE_LEN} ${SOURCE_PATH}/st2.py ${LIB_PATH}/*.py ${TESTS_PATH}/*.py

.PHONY: auto_format
.PHONY: auto_format # Apply black format against python source files.
auto_format:
echo "Formatting code\n"
black --line-length=${MAX_LINE_LEN} ${SOURCE_PATH}/st2.py ${LIB_PATH}/*.py ${TESTS_PATH}/*.py

.PHONY: security_scan
.PHONY: security_scan # Check python source code for security issues.
security_scan:
echo "Scanning for potential security issues\n"
bandit ${SOURCE_PATH}/*.py ${LIB_PATH}/*.py

.PHONY: unit_test
.PHONY: unit_test # Run Unit tests using pytest.
unit_test:
echo "Running Python unit tests\n"
python -m pytest

.PHONY: lint_test
.PHONY: lint_test # Run flake and pycodestyle tests on source files.
lint_test:
echo -n "Running LINT tests\n"
pycodestyle --max-line-length=${MAX_LINE_LEN} ${SOURCE_PATH}/st2.py ${LIB_PATH}/*.py

.PHONY: help
help:
echo "lint_test: Run flake and pycodestyle tests on source files."
echo "unit_test: Run pytest."
echo "format_test: Run black formatting check over source files."
echo "auto_format: Apply black formatting over source files."
echo "setup: Install errbot and dependencies into virtual environment."
echo "build_python_package: Build a python package of err-stackstorm."
echo "publish_pypi: Upload python package in dist/ to pypi."
echo "documentation: Trigger build on the readthedocs site."
echo "${FMT_TITLE}TARGET${FMT_NONE} ${FMT_TITLE}DESCRIPTION${FMT_NONE}"
echo "${FMT_TARGET}clean${FMT_NONE} Remove tmp files and previous build artifacst. (Not Implemented)"
echo "${FMT_TARGET}setup${FMT_NONE} Install errbot and dependencies into virtual environment."
echo "${FMT_TARGET}build_python_package${FMT_NONE} Build python deployment packages."
echo "${FMT_TARGET}publish_pypi${FMT_NONE} Push python deployment packages to pypi. (Not Implemented)"
echo "${FMT_TARGET}build_documentation${FMT_NONE} Generate readthedocs documentation. (Not Implemented)"
echo "${FMT_TARGET}format_test${FMT_NONE} Run black formatting check over source files."
echo "${FMT_TARGET}auto_format${FMT_NONE} Apply black format against python source files."
echo "${FMT_TARGET}security_scan${FMT_NONE} Check python source code for security issues."
echo "${FMT_TARGET}unit_test${FMT_NONE} Run Unit tests using pytest."
echo "${FMT_TARGET}lint_test${FMT_NONE} Run flake and pycodestyle tests on source files."
37 changes: 32 additions & 5 deletions src/err-stackstorm/errst2lib/stackstorm_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ def error(self, return_code, message):
class StackStormAPI(object):
stream_backoff = 10
authenticate_backoff = 10
http_timeout = 10

def __init__(self, cfg, accessctl):
self.cfg = cfg
Expand All @@ -55,7 +56,13 @@ def enquiry_list(self, st2_creds=None):
url = f"{self.cfg.api_url}/inquiries/"
params = {}
headers = st2_creds.requests()
return requests.get(url, headers=headers, params=params, verify=self.cfg.verify_cert)
return requests.get(
url,
headers=headers,
params=params,
timeout=StackStormAPI.http_timeout,
verify=self.cfg.verify_cert,
)

def enquiry_get(self, enquiry_id, st2_creds=None):
"""
Expand Down Expand Up @@ -100,7 +107,13 @@ def enquiry_get(self, enquiry_id, st2_creds=None):
url = f"{self.cfg.api_url}/inquiries/{enquiry_id}"
params = {}
headers = st2_creds.requests()
response = requests.get(url, headers=headers, params=params, verify=self.cfg.verify_cert)
response = requests.get(
url,
headers=headers,
params=params,
timeout=StackStormAPI.http_timeout,
verify=self.cfg.verify_cert,
)

if response.status_code == requests.codes.ok:
return response.json()
Expand Down Expand Up @@ -132,7 +145,13 @@ def actionalias_help(self, pack=None, filter=None, limit=None, offset=None, st2_
params["offset"] = offset

headers = st2_creds.requests()
response = requests.get(url, headers=headers, params=params, verify=self.cfg.verify_cert)
response = requests.get(
url,
headers=headers,
params=params,
timeout=StackStormAPI.http_timeout,
verify=self.cfg.verify_cert,
)
if response.status_code == requests.codes.ok:
return response.json().get("helpstrings", [])
elif response.status_code == 401:
Expand All @@ -150,7 +169,11 @@ def match(self, text, st2token):
result = Result()
try:
response = requests.post(
url, headers=headers, data=payload, verify=self.cfg.verify_cert
url,
headers=headers,
data=payload,
timeout=StackStormAPI.http_timeout,
verify=self.cfg.verify_cert,
)
if response.status_code == 200:
result.OK(response.json())
Expand Down Expand Up @@ -195,7 +218,11 @@ def execute_actionalias(self, msg, chat_user, st2token):
msg = ""
try:
response = requests.post(
url, headers=headers, json=payload, verify=self.cfg.verify_cert
url,
headers=headers,
json=payload,
timeout=StackStormAPI.http_timeout,
verify=self.cfg.verify_cert,
)

if response.status_code in [201, 400]:
Expand Down
Loading