Skip to content

Commit f7968d1

Browse files
committed
Put remove password in the utils and use it also in cmd.execute
1 parent f7180d5 commit f7968d1

File tree

3 files changed

+35
-14
lines changed

3 files changed

+35
-14
lines changed

‎git/cmd.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
is_win,
2929
)
3030
fromgit.excimportCommandError
31-
fromgit.utilimportis_cygwin_git, cygpath, expand_path
31+
fromgit.utilimportis_cygwin_git, cygpath, expand_path, remove_password_if_present
3232

3333
from .excimport (
3434
GitCommandError,
@@ -682,8 +682,10 @@ def execute(self, command,
682682
:note:
683683
If you add additional keyword arguments to the signature of this method,
684684
you must update the execute_kwargs tuple housed in this module."""
685+
# Remove password for the command if present
686+
redacted_command=remove_password_if_present(command)
685687
ifself.GIT_PYTHON_TRACEand (self.GIT_PYTHON_TRACE!='full'oras_process):
686-
log.info(' '.join(command))
688+
log.info(' '.join(redacted_command))
687689

688690
# Allow the user to have the command executed in their working dir.
689691
cwd=self._working_diroros.getcwd()

‎git/repo/base.py‎

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
importos
1010
importre
1111
importwarnings
12-
fromurllib.parseimporturlsplit, urlunsplit
1312

1413
fromgit.cmdimport (
1514
Git,
@@ -27,7 +26,7 @@
2726
fromgit.objectsimportSubmodule, RootModule, Commit
2827
fromgit.refsimportHEAD, Head, Reference, TagReference
2928
fromgit.remoteimportRemote, add_progress, to_progress_instance
30-
fromgit.utilimportActor, finalize_process, decygpath, hex_to_bin, expand_path
29+
fromgit.utilimportActor, finalize_process, decygpath, hex_to_bin, expand_path, remove_password_if_present
3130
importos.pathasosp
3231

3332
from .funimportrev_parse, is_git_dir, find_submodule_git_dir, touch, find_worktree_git_dir
@@ -971,16 +970,8 @@ def _clone(cls, git, url, path, odb_default_type, progress, multi_options=None,
971970
else:
972971
(stdout, stderr) =proc.communicate()
973972
cmdline=getattr(proc, 'args', '')
974-
uri=cmdline[-2]
975-
try:
976-
url=urlsplit(uri)
977-
# Remove password from the URL if present
978-
ifurl.password:
979-
edited_url=url._replace(
980-
netloc=url.netloc.replace(url.password, "****"))
981-
cmdline[-2] =urlunsplit(edited_url)
982-
exceptValueError:
983-
log.debug("Unable to parse the URL %s", url)
973+
cmdline=remove_password_if_present(cmdline)
974+
984975
log.debug("Cmd(%s)'s unused stdout: %s", cmdline, stdout)
985976
finalize_process(proc, stderr=stderr)
986977

‎git/util.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
fromsysimportmaxsize
1717
importtime
1818
fromunittestimportSkipTest
19+
fromurllib.parseimporturlsplit, urlunsplit
1920

2021
fromgitdb.utilimport (# NOQA @IgnorePep8
2122
make_sha,
@@ -338,6 +339,33 @@ def expand_path(p, expand_vars=True):
338339
exceptException:
339340
returnNone
340341

342+
343+
defremove_password_if_present(cmdline):
344+
"""
345+
Parse any command line argument and if on of the element is an URL with a
346+
password, replace it by stars. If nothing found just returns a copy of the
347+
command line as-is.
348+
349+
This should be used for every log line that print a command line.
350+
"""
351+
redacted_cmdline= []
352+
forto_parseincmdline:
353+
try:
354+
url=urlsplit(to_parse)
355+
# Remove password from the URL if present
356+
ifurl.passwordisNone:
357+
raiseValueError()
358+
359+
edited_url=url._replace(
360+
netloc=url.netloc.replace(url.password, "*****"))
361+
redacted_cmdline.append(urlunsplit(edited_url))
362+
exceptValueError:
363+
redacted_cmdline.append(to_parse)
364+
# This is not a valid URL
365+
pass
366+
returnredacted_cmdline
367+
368+
341369
#} END utilities
342370

343371
#{Classes

0 commit comments

Comments
(0)