1414# KIND, either express or implied. See the License for the
1515# specific language governing permissions and limitations
1616# under the License.
17+ import base64
1718import os
1819from urllib import parse
1920
@@ -26,11 +27,19 @@ def __init__(
2627 self ,
2728 remote_server_addr : str ,
2829 keep_alive : bool = True ,
29- proxy = None ,
30+ proxy : Proxy = Proxy (raw = {"proxyType" : ProxyType .SYSTEM }),
31+ username : str = None ,
32+ password : str = None ,
33+ auth_type : str = "Basic" ,
34+ token : str = None ,
3035 ) -> None :
3136 self .remote_server_addr = remote_server_addr
3237 self .keep_alive = keep_alive
3338 self .proxy = proxy
39+ self .username = username
40+ self .password = password
41+ self .auth_type = auth_type
42+ self .token = token
3443
3544 @property
3645 def remote_server_addr (self ) -> str :
@@ -57,8 +66,6 @@ def keep_alive(self, value: bool) -> None:
5766 @property
5867 def proxy (self ) -> Proxy :
5968 """:Returns: The proxy used for communicating to the driver/server."""
60-
61- self ._proxy = self ._proxy or Proxy (raw = {"proxyType" : ProxyType .SYSTEM })
6269 return self ._proxy
6370
6471 @proxy .setter
@@ -71,17 +78,49 @@ def proxy(self, proxy: Proxy) -> None:
7178 """
7279 self ._proxy = proxy
7380
74- def get_proxy_url (self ):
81+ @property
82+ def username (self ) -> str :
83+ return self ._username
84+
85+ @username .setter
86+ def username (self , value : str ) -> None :
87+ self ._username = value
88+
89+ @property
90+ def password (self ) -> str :
91+ return self ._password
92+
93+ @password .setter
94+ def password (self , value : str ) -> None :
95+ self ._password = value
96+
97+ @property
98+ def auth_type (self ) -> str :
99+ return self ._auth_type
100+
101+ @auth_type .setter
102+ def auth_type (self , value : str ) -> None :
103+ self ._auth_type = value
104+
105+ @property
106+ def token (self ) -> str :
107+ return self ._token
108+
109+ @token .setter
110+ def token (self , value : str ) -> None :
111+ self ._token = value
112+
113+ def get_proxy_url (self ) -> str :
75114 if self .proxy .proxy_type == ProxyType .DIRECT :
76115 return None
77116 elif self .proxy .proxy_type == ProxyType .SYSTEM :
78117 _no_proxy = os .environ .get ("no_proxy" , os .environ .get ("NO_PROXY" ))
79118 if _no_proxy :
80- for npu in _no_proxy .split ("," ):
81- npu = npu .strip ()
82- if npu == "*" :
119+ for entry in _no_proxy .split ("," ):
120+ entry = entry .strip ()
121+ if entry == "*" :
83122 return None
84- n_url = parse .urlparse (npu )
123+ n_url = parse .urlparse (entry )
85124 remote_add = parse .urlparse (self .remote_server_addr )
86125 if n_url .netloc :
87126 if remote_add .netloc == n_url .netloc :
@@ -102,3 +141,15 @@ def get_proxy_url(self):
102141 return None
103142 else :
104143 return None
144+
145+ def get_auth_header (self ):
146+ auth_type = self .auth_type .lower ()
147+ if auth_type == "basic" and self .username and self .password :
148+ credentials = f"{ self .username } :{ self .password } "
149+ encoded_credentials = base64 .b64encode (credentials .encode ()).decode ()
150+ return {"Authorization" : f"Basic { encoded_credentials } " }
151+ elif auth_type == "bearer" and self .token :
152+ return {"Authorization" : f"Bearer { self .token } " }
153+ elif auth_type == "oauth" and self .token :
154+ return {"Authorization" : f"OAuth { self .token } " }
155+ return None
0 commit comments