33from aiohttp import ClientResponseError , ClientError
44from lxml .etree import LxmlError
55from gettext import gettext as _
6- from os import environ
6+ from os import environ , path
77
88from rest_framework import serializers
99
2626from bandersnatch .master import Master
2727from bandersnatch .configuration import BandersnatchConfig
2828from packaging .requirements import Requirement
29- from urllib .parse import urljoin
29+ from urllib .parse import urljoin , urlsplit , urlunsplit
3030
3131logger = logging .getLogger (__name__ )
3232
@@ -111,14 +111,22 @@ async def run(self):
111111 """
112112 If includes is specified, then only sync those,else try to sync all other packages
113113 """
114+ # Prevent bandersnatch from reading actual .netrc file, set to nonexistent file
115+ # See discussion on https://github.com/pulp/pulp_python/issues/581
116+ environ ["NETRC" ] = f"{ path .curdir } /.fake-netrc"
114117 # TODO Change Bandersnatch internal API to take proxy settings in from config parameters
115- if self .remote .proxy_url :
116- environ ['http_proxy' ] = self .remote .proxy_url
117- environ ['https_proxy' ] = self .remote .proxy_url
118+ if proxy_url := self .remote .proxy_url :
119+ if self .remote .proxy_username or self .remote .proxy_password :
120+ parsed_proxy = urlsplit (proxy_url )
121+ creds = f"{ self .remote .proxy_username } :{ self .remote .proxy_password } "
122+ netloc = f"{ creds } @{ parsed_proxy .netloc } "
123+ proxy_url = urlunsplit ((parsed_proxy .scheme , netloc , "" , "" , "" ))
124+ environ ['http_proxy' ] = proxy_url
125+ environ ['https_proxy' ] = proxy_url
118126 # Bandersnatch includes leading slash when forming API urls
119127 url = self .remote .url .rstrip ("/" )
120128 # local & global timeouts defaults to 10secs and 5 hours
121- async with PulpMaster (url ) as master :
129+ async with Master (url ) as master :
122130 deferred_download = self .remote .policy != Remote .IMMEDIATE
123131 workers = self .remote .download_concurrency or self .remote .DEFAULT_DOWNLOAD_CONCURRENCY
124132 async with ProgressReport (
@@ -140,18 +148,6 @@ async def run(self):
140148 await pmirror .synchronize (packages_to_sync )
141149
142150
143- class PulpMaster (Master ):
144- """
145- Temporary subclass of bandersnatch.Master until features are in bandersnatch.
146- """
147-
148- async def __aenter__ (self ) -> "Master" :
149- """Ensure Pulp does not try to read the .netrc file."""
150- await super ().__aenter__ ()
151- self .session ._trust_env = False
152- return self
153-
154-
155151class PulpMirror (Mirror ):
156152 """
157153 Pulp Mirror Class to perform syncing using Bandersnatch
0 commit comments