Skip to content

Commit 74d4184

Browse files
authored
proxy support (#387)
* proxy support * doc note and allow lower-case env var * style
1 parent b0a8322 commit 74d4184

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
The intended audience of this file is for py42 consumers -- as such, changes that don't affect
99
how a consumer would use the library (e.g. adding unit tests, updating documentation, etc) are not captured here.
1010

11+
## Unreleased
12+
13+
### Added
14+
15+
- Proxy support via `HTTPS_PROXY` environment variable.
16+
1117
## 1.15.0 - 2022-08-23
1218

1319
### Added

docs/userguides/gettingstarted.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,13 +85,21 @@ Password (TOTP) must be provided at every invocation of the CLI, either via the
8585
The Code42 CLI currently does **not** support SSO login providers or any other identity providers such as Active
8686
Directory or Okta.
8787

88+
## Proxy Support
89+
90+
```{eval-rst}
91+
.. note:: Proxy support was added in code42cli version 1.16.0
92+
```
93+
94+
The Code42 CLI will attempt to connect through a proxy if the `https_proxy`/`HTTPS_PROXY` environment variable is set.
95+
8896
### Windows and Mac
8997

9098
For Windows and Mac systems, the CLI uses Keyring when storing passwords.
9199

92100
### Red Hat Enterprise Linux
93101

94-
To use Keyring to store the credentials you enter in the Code42 CLI, enter the following commands before installing.
102+
To use Keyring to store the credentials you 2enter in the Code42 CLI, enter the following commands before installing.
95103
```bash
96104
yum -y install python-pip python3 dbus-python gnome-keyring libsecret dbus-x11
97105
pip3 install code42cli

src/code42cli/sdk_client.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from os import environ
2+
13
import py42.sdk
24
import py42.settings
35
import py42.settings.debug as debug
@@ -19,6 +21,9 @@
1921

2022

2123
def create_sdk(profile, is_debug_mode, password=None, totp=None):
24+
proxy = environ.get("HTTPS_PROXY") or environ.get("https_proxy")
25+
if proxy:
26+
py42.settings.proxies = {"https": proxy}
2227
if is_debug_mode:
2328
py42.settings.debug.level = debug.DEBUG
2429
if profile.ignore_ssl_errors == "True":
@@ -46,6 +51,10 @@ def _validate_connection(authority_url, username, password, totp=None):
4651
)
4752
except ConnectionError as err:
4853
logger.log_error(err)
54+
if "ProxyError" in str(err):
55+
raise LoggedCLIError(
56+
f"Unable to connect to proxy! Proxy configuration set by environment variable: HTTPS_PROXY={environ.get('HTTPS_PROXY')}"
57+
)
4958
raise LoggedCLIError(f"Problem connecting to {authority_url}.")
5059
except Py42UnauthorizedError as err:
5160
logger.log_error(err)

tests/test_sdk_client.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,18 @@ def test_create_sdk_uses_given_credentials(
116116
)
117117

118118

119+
@pytest.mark.parametrize("proxy_env", ["HTTPS_PROXY", "https_proxy"])
120+
def test_create_sdk_uses_proxy_when_env_var_set(
121+
mock_profile_with_password, monkeypatch, proxy_env
122+
):
123+
monkeypatch.setenv(proxy_env, "http://test.domain")
124+
with pytest.raises(LoggedCLIError) as err:
125+
create_sdk(mock_profile_with_password, False)
126+
127+
assert "Unable to connect to proxy!" in str(err.value)
128+
assert py42.settings.proxies["https"] == "http://test.domain"
129+
130+
119131
def test_create_sdk_connection_when_2FA_login_config_detected_prompts_for_totp(
120132
mocker, monkeypatch, mock_sdk_factory, capsys, mock_profile_with_password
121133
):

0 commit comments

Comments
 (0)