Skip to content

Commit 89b2064

Browse files
authored
Merge pull request #444 from Tari-dev/main
feat: add support for password extractor
2 parents 2e858de + 06d5739 commit 89b2064

File tree

5 files changed

+71
-3
lines changed

5 files changed

+71
-3
lines changed

passgithelper.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,12 @@ def get_value(
314314
),
315315
"entry_name": EntryNameExtractor(option_suffix="_username"),
316316
}
317+
_password_extractors = {
318+
_line_extractor_name: SpecificLineExtractor(0, 0, option_suffix="_password"),
319+
"regex_search": RegexSearchExtractor(
320+
r"^password: +(.*)$", option_suffix="_password"
321+
),
322+
}
317323

318324

319325
def find_mapping_section(
@@ -386,7 +392,17 @@ def get_password(
386392

387393
pass_target = define_pass_target(section, request)
388394

389-
password_extractor = SpecificLineExtractor(0, 0, option_suffix="_password")
395+
password_extractor_name: str = section.get("password_extractor") # type: ignore
396+
397+
if password_extractor_name:
398+
password_extractor = _password_extractors.get(password_extractor_name)
399+
else:
400+
password_extractor = SpecificLineExtractor(0, 0, option_suffix="_password")
401+
402+
if password_extractor is None:
403+
raise ValueError(
404+
f"A password_extractor of type '{password_extractor_name}' does not exist"
405+
)
390406
password_extractor.configure(section)
391407

392408
username_extractor_name: str = section.get( # type: ignore
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[mytest.com]
2+
username_extractor=regex_search
3+
regex_username=^myuser: (.*)$
4+
password_extractor=regex_search
5+
regex_password=^myauth: (.*)$
6+
target=dev/mytest
File renamed without changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[DEFAULT]
2+
password_extractor=doesntexist
3+
4+
[mytest.com]
5+
target=dev/mytest

test_passgithelper.py

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ def test_prefix_skipping(self, capsys: Any) -> None:
373373
indirect=True,
374374
)
375375
@pytest.mark.usefixtures("helper_config")
376-
def test_select_unknown_extractor(self, capsys: Any) -> None:
376+
def test_select_unknown_username_extractor(self, capsys: Any) -> None:
377377
with pytest.raises(SystemExit):
378378
passgithelper.main(["get"])
379379
_, err = capsys.readouterr()
@@ -383,7 +383,7 @@ def test_select_unknown_extractor(self, capsys: Any) -> None:
383383
"helper_config",
384384
[
385385
HelperConfig(
386-
"test_data/regex-extraction",
386+
"test_data/regex-username-extraction",
387387
"""
388388
protocol=https
389389
host=mytest.com""",
@@ -421,6 +421,47 @@ def test_entry_name_is_user(self, capsys: Any) -> None:
421421
out, _ = capsys.readouterr()
422422
assert out == "password=xyz\nusername=myuser\n"
423423

424+
@pytest.mark.parametrize(
425+
"helper_config",
426+
[
427+
HelperConfig(
428+
"test_data/unknown-password-extractor",
429+
"""
430+
protocol=https
431+
host=mytest.com""",
432+
b"ignored",
433+
),
434+
],
435+
indirect=True,
436+
)
437+
@pytest.mark.usefixtures("helper_config")
438+
def test_select_unknown_password_extractor(self, capsys: Any) -> None:
439+
with pytest.raises(SystemExit):
440+
passgithelper.main(["get"])
441+
_, err = capsys.readouterr()
442+
assert "password_extractor of type 'doesntexist' does not exist" in err
443+
444+
@pytest.mark.parametrize(
445+
"helper_config",
446+
[
447+
HelperConfig(
448+
"test_data/regex-password-extraction",
449+
"""
450+
protocol=https
451+
host=mytest.com""",
452+
b"xyz\nsomeline\nmyauth: mygreattoken\nmorestuff\nmyuser: tester\n",
453+
"dev/mytest",
454+
),
455+
],
456+
indirect=True,
457+
)
458+
@pytest.mark.usefixtures("helper_config")
459+
def test_regex_password_selection(self, capsys: Any) -> None:
460+
passgithelper.main(["get"])
461+
462+
out, _ = capsys.readouterr()
463+
assert out == "password=mygreattoken\nusername=tester\n"
464+
424465
@pytest.mark.parametrize(
425466
"helper_config",
426467
[

0 commit comments

Comments
 (0)