def _read_rhn_proxy_settings(self): if not rhn_config: return # Read and store rhn-setup's proxy settings, as they have been set # on the prior screen (which is owned by rhn-setup) up2date_cfg = rhn_config.initUp2dateConfig() cfg = rhsm.config.initConfig() if up2date_cfg["enableProxy"]: proxy = up2date_cfg["httpProxy"] if proxy: # Remove any URI scheme provided proxy = remove_scheme(proxy) try: host, port = proxy.split(":") cfg.set("server", "proxy_hostname", host) cfg.set("server", "proxy_port", port) except ValueError: cfg.set("server", "proxy_hostname", proxy) cfg.set("server", "proxy_port", rhsm.config.DEFAULT_PROXY_PORT) if up2date_cfg["enableProxyAuth"]: cfg.set("server", "proxy_user", up2date_cfg["proxyUser"]) cfg.set("server", "proxy_password", up2date_cfg["proxyPassword"]) else: cfg.set("server", "proxy_hostname", "") cfg.set("server", "proxy_port", "") cfg.set("server", "proxy_user", "") cfg.set("server", "proxy_password", "") cfg.save() self.backend.cp_provider.set_connection_info()
def _read_rhn_proxy_settings(self): if not rhn_config: return # Read and store rhn-setup's proxy settings, as they have been set # on the prior screen (which is owned by rhn-setup) up2date_cfg = rhn_config.initUp2dateConfig() cfg = rhsm.config.initConfig() if up2date_cfg['enableProxy']: proxy = up2date_cfg['httpProxy'] if proxy: # Remove any URI scheme provided proxy = remove_scheme(proxy) try: host, port = proxy.split(':') cfg.set('server', 'proxy_hostname', host) cfg.set('server', 'proxy_port', port) except ValueError: cfg.set('server', 'proxy_hostname', proxy) cfg.set('server', 'proxy_port', rhsm.config.DEFAULT_PROXY_PORT) if up2date_cfg['enableProxyAuth']: cfg.set('server', 'proxy_user', up2date_cfg['proxyUser']) cfg.set('server', 'proxy_password', up2date_cfg['proxyPassword']) else: cfg.set('server', 'proxy_hostname', '') cfg.set('server', 'proxy_port', '') cfg.set('server', 'proxy_user', '') cfg.set('server', 'proxy_password', '') cfg.save() self.backend.cp_provider.set_connection_info()
def display_connection_status(self, button): connection_label = self.xml.get_widget("connectionStatusLabel") if not len(remove_scheme(self.cfg.get("server", "proxy_hostname"))): connection_label.set_label(_("Proxy location cannot be empty")) elif self.test_connection(): connection_label.set_label(_("Proxy connection succeeded")) else: connection_label.set_label(_("Proxy connection failed"))
def test_connection(self): proxy_host = remove_scheme(self.cfg.get("server", "proxy_hostname")) proxy_port = self.cfg.get_int("server", "proxy_port") cp = self.cp_provider.get_no_auth_cp() try: cp.getStatus() except connection.RemoteServerException, e: log.debug("Reporting proxy connection as good despite %s" % e.code) return True
def write_values(self, widget=None, dummy=None): proxy = self.proxyEntry.get_text() or "" # don't save these values if they are disabled in the gui # settings of HTTP Proxy if proxy and self.enableProxyButton.get_active(): # Remove any URI scheme provided proxy = remove_scheme(proxy) # Update the proxy entry field to show we removed any scheme self.proxyEntry.set_text(proxy) try: proxy_hostname, proxy_port = proxy.split(':') self.cfg.set("server", "proxy_hostname", proxy_hostname) self.cfg.set("server", "proxy_port", proxy_port) except ValueError: # no port? just write out the hostname and assume default self.cfg.set("server", "proxy_hostname", proxy) self.cfg.set("server", "proxy_port", rhsm.config.DEFAULT_PROXY_PORT) else: # delete config options if we disable it in the ui self.cfg.set("server", "proxy_hostname", "") self.cfg.set("server", "proxy_port", "") # settings of HTTP proxy authentication if self.enableProxyAuthButton.get_active(): if self.proxyUserEntry.get_text() is not None: self.cfg.set("server", "proxy_user", str(self.proxyUserEntry.get_text())) if self.proxyPasswordEntry.get_text() is not None: self.cfg.set("server", "proxy_password", str(self.proxyPasswordEntry.get_text())) else: self.cfg.set("server", "proxy_user", "") self.cfg.set("server", "proxy_password", "") # settings of bypass the HTTP proxy for specific host/domain if self.enableProxyBypassButton.get_active(): if self.noProxyEntry.get_text() is not None: self.cfg.set("server", "no_proxy", str(self.noProxyEntry.get_text())) else: self.cfg.set("server", "no_proxy", "") try: self.cfg.save() self.cp_provider.set_connection_info() except Exception: show_error_window( _("There was an error saving your configuration.") + _("Make sure that you own %s.") % self.cfg.fileName, parent=self.networkConfigDialog)
def parse_proxy_entry(self, proxy_url): proxy_url = remove_scheme(proxy_url) proxy_host = None proxy_port = None try: proxy_info = parse_url(proxy_url, default_port=rhsm.config.DEFAULT_PROXY_PORT) proxy_host = proxy_info[2] proxy_port = proxy_info[3] except rhsm.utils.ServerUrlParseErrorPort, e: proxy_host = proxy_url.split(':')[0] proxy_port = rhsm.config.DEFAULT_PROXY_PORT
def write_values(self, widget=None, dummy=None): proxy = self.proxyEntry.get_text() or "" # don't save these values if they are disabled in the gui # settings of HTTP Proxy if proxy and self.enableProxyButton.get_active(): # Remove any URI scheme provided proxy = remove_scheme(proxy) # Update the proxy entry field to show we removed any scheme self.proxyEntry.set_text(proxy) try: proxy_hostname, proxy_port = proxy.split(':') self.cfg.set("server", "proxy_hostname", proxy_hostname) self.cfg.set("server", "proxy_port", proxy_port) except ValueError: # no port? just write out the hostname and assume default self.cfg.set("server", "proxy_hostname", proxy) self.cfg.set("server", "proxy_port", rhsm.config.DEFAULT_PROXY_PORT) else: # delete config options if we disable it in the ui self.cfg.set("server", "proxy_hostname", "") self.cfg.set("server", "proxy_port", "") # settings of HTTP proxy authentication if self.enableProxyAuthButton.get_active(): if self.proxyUserEntry.get_text() is not None: self.cfg.set("server", "proxy_user", str(self.proxyUserEntry.get_text())) if self.proxyPasswordEntry.get_text() is not None: self.cfg.set("server", "proxy_password", str(self.proxyPasswordEntry.get_text())) else: self.cfg.set("server", "proxy_user", "") self.cfg.set("server", "proxy_password", "") # settings of bypass the HTTP proxy for specific host/domain if self.enableProxyBypassButton.get_active(): if self.noProxyEntry.get_text() is not None: self.cfg.set("server", "no_proxy", str(self.noProxyEntry.get_text())) else: self.cfg.set("server", "no_proxy", "") try: self.cfg.save() self.cp_provider.set_connection_info() except Exception: show_error_window(_("There was an error saving your configuration.") + _("Make sure that you own %s.") % self.cfg.fileName, parent=self.networkConfigDialog)
def _read_rhn_proxy_settings(self): if not rhn_config: return # Read and store rhn-setup's proxy settings, as they have been set # on the prior screen (which is owned by rhn-setup) up2date_cfg = rhn_config.initUp2dateConfig() cfg = rhsm.config.initConfig() # Track if we have changed this in the gui proxy dialog, if # we have changed it to disabled, then we apply "null", otherwise # if the version off the fs was disabled, we ignore the up2date proxy settings. # # Don't do anything if proxies aren't enabled in rhn config. if not up2date_cfg['enableProxy']: if self.proxies_were_enabled_from_gui: cfg.set('server', 'proxy_hostname', '') cfg.set('server', 'proxy_port', '') self.backend.cp_provider.set_connection_info() return # If we get here, we think we are enabling or updating proxy info # based on changes from the gui proxy settings dialog, so take that # to mean that enabledProxy=0 means to unset proxy info, not just to # not override it. self.proxies_were_enabled_from_gui = up2date_cfg['enableProxy'] proxy = up2date_cfg['httpProxy'] if proxy: # Remove any URI scheme provided proxy = remove_scheme(proxy) try: host, port = proxy.split(':') # the rhn proxy value is unicode, assume we can # cast to ascii ints port = str(int(port)) cfg.set('server', 'proxy_hostname', host) cfg.set('server', 'proxy_port', port) except ValueError: cfg.set('server', 'proxy_hostname', proxy) cfg.set('server', 'proxy_port', rhsm.config.DEFAULT_PROXY_PORT) if up2date_cfg['enableProxyAuth']: cfg.set('server', 'proxy_user', up2date_cfg['proxyUser']) cfg.set('server', 'proxy_password', up2date_cfg['proxyPassword']) self.backend.cp_provider.set_connection_info()
def main(self, args=None): # TODO: For now, we disable the CLI entirely. We may want to allow some commands in the future. if rhsm.config.in_container(): system_exit( os.EX_CONFIG, _("subscription-manager is disabled when running inside a container. Please refer to your host system for subscription management.\n" )) config_changed = False # In testing we sometimes specify args, otherwise use the default: if not args: args = sys.argv[1:] (self.options, self.args) = self.parser.parse_known_args(args) # we dont need argv[0] in this list... self.args = self.args[1:] # check for unparsed arguments if self.args: for arg in self.args: print(_("cannot parse argument: {}").format(arg)) system_exit(os.EX_USAGE) if hasattr(self.options, "insecure") and self.options.insecure: conf["server"]["insecure"] = "1" config_changed = True if hasattr(self.options, "server_url") and self.options.server_url: try: (self.server_hostname, self.server_port, self.server_prefix) = parse_server_info( self.options.server_url, conf) except ServerUrlParseError as e: print(_("Error parsing serverurl:")) handle_exception("Error parsing serverurl:", e) conf["server"]["hostname"] = self.server_hostname conf["server"]["port"] = self.server_port conf["server"]["prefix"] = self.server_prefix if self.server_port: self.server_port = int(self.server_port) config_changed = True if hasattr(self.options, "base_url") and self.options.base_url: try: (baseurl_server_hostname, baseurl_server_port, baseurl_server_prefix) = parse_baseurl_info( self.options.base_url) except ServerUrlParseError as e: print(_("Error parsing baseurl:")) handle_exception("Error parsing baseurl:", e) conf["rhsm"]["baseurl"] = format_baseurl(baseurl_server_hostname, baseurl_server_port, baseurl_server_prefix) config_changed = True # support foo.example.com:3128 format if hasattr(self.options, "proxy_url") and self.options.proxy_url: parts = remove_scheme(self.options.proxy_url).split(':') self.proxy_hostname = parts[0] # no ':' if len(parts) > 1: self.proxy_port = int(parts[1]) else: # if no port specified, use the one from the config, or fallback to the default self.proxy_port = conf['server'].get_int( 'proxy_port') or rhsm.config.DEFAULT_PROXY_PORT config_changed = True if hasattr(self.options, "proxy_user") and self.options.proxy_user: self.proxy_user = self.options.proxy_user if hasattr(self.options, "proxy_password") and self.options.proxy_password: self.proxy_password = self.options.proxy_password if hasattr(self.options, "no_proxy") and self.options.no_proxy: self.no_proxy = self.options.no_proxy # Proxy information isn't written to the config, so we have to make sure # the sorter gets it connection_info = {} if self.proxy_hostname: connection_info['proxy_hostname_arg'] = self.proxy_hostname if self.proxy_port: connection_info['proxy_port_arg'] = self.proxy_port if self.proxy_user: connection_info['proxy_user_arg'] = self.proxy_user if self.proxy_password: connection_info['proxy_password_arg'] = self.proxy_password if self.server_hostname: connection_info['host'] = self.server_hostname if self.server_port: connection_info['ssl_port'] = self.server_port if self.server_prefix: connection_info['handler'] = self.server_prefix if self.no_proxy: connection_info['no_proxy_arg'] = self.no_proxy self.cp_provider = inj.require(inj.CP_PROVIDER) self.cp_provider.set_connection_info(**connection_info) self.log.debug("X-Correlation-ID: {id}".format(id=self.correlation_id)) self.cp_provider.set_correlation_id(self.correlation_id) self.log_client_version() if self.require_connection(): # make sure we pass in the new server info, otherwise we # we use the defaults from connection module init # we've set self.proxy* here, so we'll use them if they # are set self.cp = self.cp_provider.get_consumer_auth_cp() # no auth cp for get / (resources) and # get /status (status and versions) self.no_auth_cp = self.cp_provider.get_no_auth_cp() self.entcertlib = EntCertActionInvoker() if config_changed: try: # catch host/port issues; does not catch auth issues if not self.test_proxy_connection(): system_exit( os.EX_UNAVAILABLE, _("Proxy connection failed, please check your settings." )) # this tries to actually connect to the server and ping it if not is_valid_server_info(self.no_auth_cp): system_exit( os.EX_UNAVAILABLE, _("Unable to reach the server at {host}:{port}{handler}" ).format(host=self.no_auth_cp.host, port=self.no_auth_cp.ssl_port, handler=self.no_auth_cp.handler)) except MissingCaCertException: system_exit( os.EX_CONFIG, _("Error: CA certificate for subscription service has not been installed." )) except ProxyException: system_exit( os.EX_UNAVAILABLE, _("Proxy connection failed, please check your settings." )) else: self.cp = None # do the work, catch most common errors here: try: return_code = self._do_command() # Only persist the config changes if there was no exception if config_changed and self.persist_server_options(): conf.persist() if return_code is not None: return return_code except (CertificateException, ssl.SSLError) as e: log.error(e) system_exit(os.EX_SOFTWARE, _('System certificates corrupted. Please reregister.')) except connection.GoneException as ge: if ge.deleted_id == self.identity.uuid: log.critical( "Consumer profile \"{uuid}\" has been deleted from the server." .format(uuid=self.identity.uuid)) system_exit( os.EX_UNAVAILABLE, _("Consumer profile \"{uuid}\" has been deleted from the server. You can use command clean or unregister to remove local profile." ).format(uuid=self.identity.uuid)) else: raise ge except InvalidCLIOptionError as err: # This exception is handled in cli module raise err except Exception as err: handle_exception("exception caught in subscription-manager", err)
def test_no_port(self): proxy_url = "proxy.example.com" res = remove_scheme(proxy_url) self.assertEqual(res, proxy_url)
def test_https_scheme(self): proxy_url = "https://example.com:3128" res = remove_scheme(proxy_url) self.assertEquals(res, "example.com:3128")
def test_https_scheme(self): proxy_url = "https://example.com:3128" res = remove_scheme(proxy_url) self.assertEqual(res, "example.com:3128")
def test_no_port(self): proxy_url = "proxy.example.com" res = remove_scheme(proxy_url) self.assertEquals(res, proxy_url)
def test_colon_port(self): proxy_url = "proxy.example.com:3128" res = remove_scheme(proxy_url) self.assertEquals(res, proxy_url)
def test_colon_port(self): proxy_url = "proxy.example.com:3128" res = remove_scheme(proxy_url) self.assertEqual(res, proxy_url)