Repository: SAP/cloud-pysec

Issue:
Timeout during IAS API requests
While creating a security context for IAS, the API requests made to IAS times out. The current timeout is 2s.

Endpoints that currently timeout-
1. `/.well-known/openid-configuration`
2. `verification_key_url`

This happens mostly in AP regions where IAS broker and main tenant are in different DC's.

Increasing the timeout(5s) or making it flexible to set it from outside will help to make this problem better.

Code - https://github.com/SAP/cloud-pysec/blob/de3cff1b5dd6930091bde36c7a691634a037cb52/sap/xssec/constants.py#L13


Discussion:

Patch:
diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml
index 9983d27..120dab6 100644
--- a/.github/workflows/main.yml
+++ b/.github/workflows/main.yml
@@ -10,7 +10,7 @@ on:
 
 jobs:
   build:
-    runs-on: ubuntu-latest
+    runs-on: ubuntu-22.04
 
     strategy:
       matrix:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index dc220eb..2da272d 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -6,6 +6,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).
 
 The format is based on [Keep a Changelog](http://keepachangelog.com/).
 
+## 4.2.1
+### Added
+- Allow overriding HTTP timeout using environment variable `XSSEC_HTTP_TIMEOUT_IN_SECONDS`
+
 ## 4.2.0
 ### Changed
 - Allow `issuer` field without `https://` prefix in IAS token
diff --git a/sap/xssec/constants.py b/sap/xssec/constants.py
index a2af048..e13ba4c 100644
--- a/sap/xssec/constants.py
+++ b/sap/xssec/constants.py
@@ -1,4 +1,6 @@
 """ constants """
+import os
+
 XSAPPNAMEPREFIX = '$XSAPPNAME.'
 SYSTEM = 'System'
 JOBSCHEDULER = 'JobScheduler'
@@ -10,7 +12,7 @@ GRANTTYPE_SAML2BEARER = 'urn:ietf:params:oauth:grant-type:saml2-bearer'
 GRANTTYPE_JWT_BEARER = 'urn:ietf:params:oauth:grant-type:jwt-bearer'
 KEYCACHE_DEFAULT_CACHE_SIZE = 100
 KEYCACHE_DEFAULT_CACHE_ENTRY_EXPIRATION_TIME_IN_MINUTES = 15
-HTTP_TIMEOUT_IN_SECONDS = 2
+HTTP_TIMEOUT_IN_SECONDS = int(os.getenv("XSSEC_HTTP_TIMEOUT_IN_SECONDS", 2))
 HTTP_RETRY_NUMBER_RETRIES = 3
 HTTP_RETRY_ON_ERROR_CODE = [502, 503, 504]
 HTTP_RETRY_BACKOFF_FACTOR = 0.5  # retry after 0s, 1s, 2s
diff --git a/version.txt b/version.txt
index 6aba2b2..fae6e3d 100644
--- a/version.txt
+++ b/version.txt
@@ -1,1 +1,1 @@
-4.2.0
+4.2.1


Test patch:
diff --git a/tests/test_config.py b/tests/test_config.py
new file mode 100644
index 0000000..c11334f
--- /dev/null
+++ b/tests/test_config.py
@@ -0,0 +1,15 @@
+from sap.xssec import constants
+from importlib import reload
+
+
+def test_http_timeout_overridden(monkeypatch):
+    assert constants.HTTP_TIMEOUT_IN_SECONDS == 2
+
+    monkeypatch.setenv("XSSEC_HTTP_TIMEOUT_IN_SECONDS", "10")
+    reload(constants)
+    assert constants.HTTP_TIMEOUT_IN_SECONDS == 10
+
+    monkeypatch.delenv("XSSEC_HTTP_TIMEOUT_IN_SECONDS")
+    reload(constants)
+    assert constants.HTTP_TIMEOUT_IN_SECONDS == 2
+
diff --git a/tests/test_xssec.py b/tests/test_xssec.py
index cfa7709..739d68f 100644
--- a/tests/test_xssec.py
+++ b/tests/test_xssec.py
@@ -4,7 +4,6 @@ import unittest
 import json
 from os import environ
 from datetime import datetime
-from parameterized import parameterized_class
 from sap import xssec
 from sap.xssec import constants, jwt_validation_facade, security_context_xsuaa
 from tests import uaa_configs