Skip to content

Commit

Permalink
Fix #12464: Only set username, password for HTTP/s (#12482)
Browse files Browse the repository at this point in the history
  • Loading branch information
tobiasge authored May 4, 2023
1 parent 2c75687 commit 4df517e
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions netbox/core/data_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def register_backend(name):
"""
Decorator for registering a DataBackend class.
"""

def _wrapper(cls):
registry['data_backends'][name] = cls
return cls
Expand All @@ -56,7 +57,6 @@ def fetch(self):

@register_backend(DataSourceTypeChoices.LOCAL)
class LocalBackend(DataBackend):

@contextmanager
def fetch(self):
logger.debug(f"Data source type is local; skipping fetch")
Expand All @@ -71,12 +71,14 @@ class GitBackend(DataBackend):
'username': forms.CharField(
required=False,
label=_('Username'),
widget=forms.TextInput(attrs={'class': 'form-control'})
widget=forms.TextInput(attrs={'class': 'form-control'}),
help_text=_("Only used for cloning with HTTP / HTTPS"),
),
'password': forms.CharField(
required=False,
label=_('Password'),
widget=forms.TextInput(attrs={'class': 'form-control'})
widget=forms.TextInput(attrs={'class': 'form-control'}),
help_text=_("Only used for cloning with HTTP / HTTPS"),
),
'branch': forms.CharField(
required=False,
Expand All @@ -89,21 +91,30 @@ class GitBackend(DataBackend):
def fetch(self):
local_path = tempfile.TemporaryDirectory()

username = self.params.get('username')
password = self.params.get('password')
branch = self.params.get('branch')
config = StackedConfig.default()
clone_args = {
"branch": self.params.get('branch'),
"config": config,
"depth": 1,
"errstream": porcelain.NoneStream(),
"quiet": True,
}

if self.url_scheme in ('http', 'https'):
clone_args.update(
{
"username": self.params.get('username'),
"password": self.params.get('password'),
}
)

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

logger.debug(f"Cloning git repo: {self.url}")
try:
porcelain.clone(
self.url, local_path.name, depth=1, branch=branch, username=username, password=password,
config=config, quiet=True, errstream=porcelain.NoneStream()
)
porcelain.clone(self.url, local_path.name, **clone_args)
except BaseException as e:
raise SyncError(f"Fetching remote data failed ({type(e).__name__}): {e}")

Expand Down

0 comments on commit 4df517e

Please sign in to comment.