Skip to content

Commit 566f1ba

Browse files
committed
Use maxsplit=1, update sample config, add test.
1 parent cfd51d9 commit 566f1ba

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

conf/st2rc.sample.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ password = testpassword
2424
# Optional additional http basic auth credentials which are sent with each HTTP
2525
# request except the auth request to /v1/auth/tokens endpoint.
2626
# Available in StackStorm >= v3.4.0
27+
# NOTE: Username can't contain colon (:) character.
2728
# basic_auth = username:password
2829

2930
[api]

st2client/st2client/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,14 @@ def __init__(
131131
self.api_key = api_key
132132

133133
if basic_auth:
134-
if len(basic_auth.split(":")) != 2:
134+
# NOTE: We assume username can't contain colons
135+
if len(basic_auth.split(":", 1)) != 2:
135136
raise ValueError(
136137
"basic_auth config options needs to be in the "
137138
"username:password notation"
138139
)
139140

140-
self.basic_auth = tuple(basic_auth.split(":"))
141+
self.basic_auth = tuple(basic_auth.split(":", 1))
141142
else:
142143
self.basic_auth = None
143144

st2client/tests/unit/test_client.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,25 @@ def test_basic_auth_option_success(self):
8989
"http://127.0.0.1:9101/v1/actions", auth=("username", "password"), params={}
9090
)
9191

92+
@mock.patch.object(
93+
requests,
94+
"get",
95+
mock.MagicMock(return_value=base.FakeResponse(json.dumps({}), 200, "OK")),
96+
)
97+
def test_basic_auth_option_success_password_with_colon(self):
98+
client = Client(basic_auth="username:password:with:colon")
99+
self.assertEqual(client.basic_auth, ("username", "password:with:colon"))
100+
101+
self.assertEqual(requests.get.call_count, 0)
102+
client.actions.get_all()
103+
self.assertEqual(requests.get.call_count, 1)
104+
105+
requests.get.assert_called_with(
106+
"http://127.0.0.1:9101/v1/actions",
107+
auth=("username", "password:with:colon"),
108+
params={},
109+
)
110+
92111
def test_basic_auth_option_invalid_notation(self):
93112
self.assertRaisesRegex(
94113
ValueError,

0 commit comments

Comments
 (0)