Skip to content

Commit 4df517e

Browse files
authored
Fix #12464: Only set username, password for HTTP/s (#12482)
1 parent 2c75687 commit 4df517e

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

netbox/core/data_backends.py

+21-10
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ def register_backend(name):
3131
"""
3232
Decorator for registering a DataBackend class.
3333
"""
34+
3435
def _wrapper(cls):
3536
registry['data_backends'][name] = cls
3637
return cls
@@ -56,7 +57,6 @@ def fetch(self):
5657

5758
@register_backend(DataSourceTypeChoices.LOCAL)
5859
class LocalBackend(DataBackend):
59-
6060
@contextmanager
6161
def fetch(self):
6262
logger.debug(f"Data source type is local; skipping fetch")
@@ -71,12 +71,14 @@ class GitBackend(DataBackend):
7171
'username': forms.CharField(
7272
required=False,
7373
label=_('Username'),
74-
widget=forms.TextInput(attrs={'class': 'form-control'})
74+
widget=forms.TextInput(attrs={'class': 'form-control'}),
75+
help_text=_("Only used for cloning with HTTP / HTTPS"),
7576
),
7677
'password': forms.CharField(
7778
required=False,
7879
label=_('Password'),
79-
widget=forms.TextInput(attrs={'class': 'form-control'})
80+
widget=forms.TextInput(attrs={'class': 'form-control'}),
81+
help_text=_("Only used for cloning with HTTP / HTTPS"),
8082
),
8183
'branch': forms.CharField(
8284
required=False,
@@ -89,21 +91,30 @@ class GitBackend(DataBackend):
8991
def fetch(self):
9092
local_path = tempfile.TemporaryDirectory()
9193

92-
username = self.params.get('username')
93-
password = self.params.get('password')
94-
branch = self.params.get('branch')
9594
config = StackedConfig.default()
95+
clone_args = {
96+
"branch": self.params.get('branch'),
97+
"config": config,
98+
"depth": 1,
99+
"errstream": porcelain.NoneStream(),
100+
"quiet": True,
101+
}
102+
103+
if self.url_scheme in ('http', 'https'):
104+
clone_args.update(
105+
{
106+
"username": self.params.get('username'),
107+
"password": self.params.get('password'),
108+
}
109+
)
96110

97111
if settings.HTTP_PROXIES and self.url_scheme in ('http', 'https'):
98112
if proxy := settings.HTTP_PROXIES.get(self.url_scheme):
99113
config.set("http", "proxy", proxy)
100114

101115
logger.debug(f"Cloning git repo: {self.url}")
102116
try:
103-
porcelain.clone(
104-
self.url, local_path.name, depth=1, branch=branch, username=username, password=password,
105-
config=config, quiet=True, errstream=porcelain.NoneStream()
106-
)
117+
porcelain.clone(self.url, local_path.name, **clone_args)
107118
except BaseException as e:
108119
raise SyncError(f"Fetching remote data failed ({type(e).__name__}): {e}")
109120

0 commit comments

Comments
 (0)