def __init__(self, host, ssl_port=None, username=None, password=None, proxy_hostname=None, proxy_port=None, proxy_user=None, proxy_password=None, ca_dir=None, insecure=False, ssl_verify_depth=1): log.debug("ContectConnection") # FIXME self.ent_dir = "/etc/pki/entitlement" self.handler = "/" self.ssl_verify_depth = ssl_verify_depth self.host = host or config.get('server', 'hostname') self.ssl_port = ssl_port or safe_int(config.get('server', 'port')) self.ca_dir = ca_dir self.insecure = insecure self.username = username self.password = password self.ssl_verify_depth = ssl_verify_depth # get the proxy information from the environment variable # if available info = get_env_proxy_info() self.proxy_hostname = proxy_hostname or config.get('server', 'proxy_hostname') or info['proxy_hostname'] self.proxy_port = proxy_port or config.get('server', 'proxy_port') or info['proxy_port'] self.proxy_user = proxy_user or config.get('server', 'proxy_user') or info['proxy_username'] self.proxy_password = proxy_password or config.get('server', 'proxy_password') or info['proxy_password']
def test_http_proxy_info(self): with patch.dict('os.environ', self._gen_env({'http_proxy': 'http://*****:*****@host:2222'})): proxy_info = get_env_proxy_info() self.assertEquals("u", proxy_info["proxy_username"]) self.assertEquals("p", proxy_info["proxy_password"]) self.assertEquals("host", proxy_info["proxy_hostname"]) self.assertEquals(int("2222"), proxy_info["proxy_port"])
def test_https_proxy_info_allcaps(self): with patch.dict('os.environ', self._gen_env({'HTTPS_PROXY': 'https://*****:*****@host:4444'})): proxy_info = get_env_proxy_info() self.assertEquals("u", proxy_info["proxy_username"]) self.assertEquals("p", proxy_info["proxy_password"]) self.assertEquals("host", proxy_info["proxy_hostname"]) self.assertEquals(int("4444"), proxy_info["proxy_port"])
def test_no_port(self): with patch.dict('os.environ', self._gen_env({'HTTPS_PROXY': 'http://*****:*****@host'})): proxy_info = get_env_proxy_info() self.assertEquals("u", proxy_info["proxy_username"]) self.assertEquals("p", proxy_info["proxy_password"]) self.assertEquals("host", proxy_info["proxy_hostname"]) self.assertEquals(3128, proxy_info["proxy_port"])
def test_no_user_or_password(self): with patch.dict('os.environ', self._gen_env({'HTTPS_PROXY': 'http://host:1111'})): proxy_info = get_env_proxy_info() self.assertEquals(None, proxy_info["proxy_username"]) self.assertEquals(None, proxy_info["proxy_password"]) self.assertEquals("host", proxy_info["proxy_hostname"]) self.assertEquals(int("1111"), proxy_info["proxy_port"])
def test_http_proxy_info(self): with patch.dict('os.environ', self._gen_env({'http_proxy': 'http://*****:*****@host:2222'})): proxy_info = get_env_proxy_info() self.assertEqual("u", proxy_info["proxy_username"]) self.assertEqual("p", proxy_info["proxy_password"]) self.assertEqual("host", proxy_info["proxy_hostname"]) self.assertEqual(int("2222"), proxy_info["proxy_port"])
def test_http_proxy_info_allcaps(self): with patch.dict('os.environ', self._gen_env({'HTTP_PROXY': 'http://*****:*****@host:3333'})): proxy_info = get_env_proxy_info() self.assertEqual("u", proxy_info["proxy_username"]) self.assertEqual("p", proxy_info["proxy_password"]) self.assertEqual("host", proxy_info["proxy_hostname"]) self.assertEqual(int("3333"), proxy_info["proxy_port"])
def test_order(self): # should follow the order: HTTPS, https, HTTP, http with patch.dict('os.environ', self._gen_env({'HTTPS_PROXY': 'http://*****:*****@host:1111', 'http_proxy': 'http://*****:*****@host:2222'})): proxy_info = get_env_proxy_info() self.assertEquals("u", proxy_info["proxy_username"]) self.assertEquals("p", proxy_info["proxy_password"]) self.assertEquals("host", proxy_info["proxy_hostname"]) self.assertEquals(int("1111"), proxy_info["proxy_port"])
def test_https_proxy_info_allcaps(self): with patch.dict( "os.environ", self._gen_env({"HTTPS_PROXY": "https://*****:*****@host:4444"})): proxy_info = get_env_proxy_info() self.assertEqual("u", proxy_info["proxy_username"]) self.assertEqual("p", proxy_info["proxy_password"]) self.assertEqual("host", proxy_info["proxy_hostname"]) self.assertEqual(int("4444"), proxy_info["proxy_port"])
def test_https_proxy_info(self): with patch.dict( "os.environ", self._gen_env({"https_proxy": "https://*****:*****@host:1111"})): proxy_info = get_env_proxy_info() self.assertEqual("u", proxy_info["proxy_username"]) self.assertEqual("p", proxy_info["proxy_password"]) self.assertEqual("host", proxy_info["proxy_hostname"]) self.assertEqual(int("1111"), proxy_info["proxy_port"])
def test_order(self): # should follow the order: HTTPS, https, HTTP, http with patch.dict( 'os.environ', self._gen_env({ 'HTTPS_PROXY': 'http://*****:*****@host:1111', 'http_proxy': 'http://*****:*****@host:2222' })): proxy_info = get_env_proxy_info() self.assertEqual("u", proxy_info["proxy_username"]) self.assertEqual("p", proxy_info["proxy_password"]) self.assertEqual("host", proxy_info["proxy_hostname"]) self.assertEqual(int("1111"), proxy_info["proxy_port"])
def __init__(self, host, ssl_port=None, username=None, password=None, proxy_hostname=None, proxy_port=None, proxy_user=None, proxy_password=None, ca_dir=None, insecure=False, ssl_verify_depth=1): log.debug("ContectConnection") # FIXME self.ent_dir = "/etc/pki/entitlement" self.handler = "/" self.ssl_verify_depth = ssl_verify_depth self.host = host or config.get('server', 'hostname') self.ssl_port = ssl_port or safe_int(config.get('server', 'port')) self.ca_dir = ca_dir self.insecure = insecure self.username = username self.password = password self.ssl_verify_depth = ssl_verify_depth self.timeout_altered = False # get the proxy information from the environment variable # if available and host is not in no_proxy if urllib.proxy_bypass_environment(self.host): info = {'proxy_username': '', 'proxy_hostname': '', 'proxy_port': '', 'proxy_password': ''} else: info = utils.get_env_proxy_info() self.proxy_hostname = proxy_hostname or config.get('server', 'proxy_hostname') or info['proxy_hostname'] self.proxy_port = proxy_port or config.get('server', 'proxy_port') or info['proxy_port'] self.proxy_user = proxy_user or config.get('server', 'proxy_user') or info['proxy_username'] self.proxy_password = proxy_password or config.get('server', 'proxy_password') or info['proxy_password']
def __init__(self, host=None, ssl_port=None, handler=None, proxy_hostname=None, proxy_port=None, proxy_user=None, proxy_password=None, username=None, password=None, cert_file=None, key_file=None, insecure=None): """ Two ways to authenticate: - username/password for HTTP basic authentication. (owner admin role) - uuid/key_file/cert_file for identity cert authentication. (consumer role) Must specify one method of authentication or the other, not both. """ self.host = host or config.get('server', 'hostname') self.ssl_port = ssl_port or safe_int(config.get('server', 'port')) self.handler = handler or config.get('server', 'prefix') # remove trailing "/" from the prefix if it is there # BZ848836 self.handler = self.handler.rstrip("/") # get the proxy information from the environment variable # if available and host is not in no_proxy if urllib.proxy_bypass_environment(self.host): info = {'proxy_username': '', 'proxy_hostname': '', 'proxy_port': '', 'proxy_password': ''} else: info = get_env_proxy_info() self.proxy_hostname = proxy_hostname or config.get('server', 'proxy_hostname') or info['proxy_hostname'] self.proxy_port = proxy_port or config.get('server', 'proxy_port') or info['proxy_port'] self.proxy_user = proxy_user or config.get('server', 'proxy_user') or info['proxy_username'] self.proxy_password = proxy_password or config.get('server', 'proxy_password') or info['proxy_password'] self.cert_file = cert_file self.key_file = key_file self.username = username self.password = password self.ca_cert_dir = config.get('rhsm', 'ca_cert_dir') self.ssl_verify_depth = safe_int(config.get('server', 'ssl_verify_depth')) self.insecure = insecure if insecure is None: self.insecure = False config_insecure = safe_int(config.get('server', 'insecure')) if config_insecure: self.insecure = True using_basic_auth = False using_id_cert_auth = False if username and password: using_basic_auth = True elif cert_file and key_file: using_id_cert_auth = True if using_basic_auth and using_id_cert_auth: raise Exception("Cannot specify both username/password and " "cert_file/key_file") #if not (using_basic_auth or using_id_cert_auth): # raise Exception("Must specify either username/password or " # "cert_file/key_file") proxy_description = None if self.proxy_hostname and self.proxy_port: proxy_description = "http_proxy=%s:%s " % (self.proxy_hostname, self.proxy_port) auth_description = None # initialize connection if using_basic_auth: self.conn = Restlib(self.host, self.ssl_port, self.handler, username=self.username, password=self.password, proxy_hostname=self.proxy_hostname, proxy_port=self.proxy_port, proxy_user=self.proxy_user, proxy_password=self.proxy_password, ca_dir=self.ca_cert_dir, insecure=self.insecure, ssl_verify_depth=self.ssl_verify_depth) auth_description = "auth=basic username=%s" % username elif using_id_cert_auth: self.conn = Restlib(self.host, self.ssl_port, self.handler, cert_file=self.cert_file, key_file=self.key_file, proxy_hostname=self.proxy_hostname, proxy_port=self.proxy_port, proxy_user=self.proxy_user, proxy_password=self.proxy_password, ca_dir=self.ca_cert_dir, insecure=self.insecure, ssl_verify_depth=self.ssl_verify_depth) auth_description = "auth=identity_cert ca_dir=%s verify=%s" % (self.ca_cert_dir, self.insecure) else: self.conn = Restlib(self.host, self.ssl_port, self.handler, proxy_hostname=self.proxy_hostname, proxy_port=self.proxy_port, proxy_user=self.proxy_user, proxy_password=self.proxy_password, ca_dir=self.ca_cert_dir, insecure=self.insecure, ssl_verify_depth=self.ssl_verify_depth) auth_description = "auth=none" self.resources = None self.capabilities = None connection_description = "" if proxy_description: connection_description += proxy_description connection_description += "host=%s port=%s handler=%s %s" % (self.host, self.ssl_port, self.handler, auth_description) log.info("Connection built: %s", connection_description)
def __init__(self, host=None, ssl_port=None, handler=None, proxy_hostname=None, proxy_port=None, proxy_user=None, proxy_password=None, username=None, password=None, cert_file=None, key_file=None, insecure=None): """ Two ways to authenticate: - username/password for HTTP basic authentication. (owner admin role) - uuid/key_file/cert_file for identity cert authentication. (consumer role) Must specify one method of authentication or the other, not both. """ self.host = host or config.get('server', 'hostname') self.ssl_port = ssl_port or safe_int(config.get('server', 'port')) self.handler = handler or config.get('server', 'prefix') # remove trailing "/" from the prefix if it is there # BZ848836 self.handler = self.handler.rstrip("/") # get the proxy information from the environment variable # if available and host is not in no_proxy if urllib.proxy_bypass_environment(self.host): info = {'proxy_username': '', 'proxy_hostname': '', 'proxy_port': '', 'proxy_password': ''} else: info = utils.get_env_proxy_info() self.proxy_hostname = proxy_hostname or config.get('server', 'proxy_hostname') or info['proxy_hostname'] self.proxy_port = proxy_port or config.get('server', 'proxy_port') or info['proxy_port'] self.proxy_user = proxy_user or config.get('server', 'proxy_user') or info['proxy_username'] self.proxy_password = proxy_password or config.get('server', 'proxy_password') or info['proxy_password'] self.cert_file = cert_file self.key_file = key_file self.username = username self.password = password self.ca_cert_dir = config.get('rhsm', 'ca_cert_dir') self.ssl_verify_depth = safe_int(config.get('server', 'ssl_verify_depth')) self.insecure = insecure if insecure is None: self.insecure = False config_insecure = safe_int(config.get('server', 'insecure')) if config_insecure: self.insecure = True using_basic_auth = False using_id_cert_auth = False if username and password: using_basic_auth = True elif cert_file and key_file: using_id_cert_auth = True if using_basic_auth and using_id_cert_auth: raise Exception("Cannot specify both username/password and " "cert_file/key_file") #if not (using_basic_auth or using_id_cert_auth): # raise Exception("Must specify either username/password or " # "cert_file/key_file") proxy_description = None if self.proxy_hostname and self.proxy_port: proxy_description = "http_proxy=%s:%s " % (self.proxy_hostname, self.proxy_port) auth_description = None # initialize connection if using_basic_auth: self.conn = Restlib(self.host, self.ssl_port, self.handler, username=self.username, password=self.password, proxy_hostname=self.proxy_hostname, proxy_port=self.proxy_port, proxy_user=self.proxy_user, proxy_password=self.proxy_password, ca_dir=self.ca_cert_dir, insecure=self.insecure, ssl_verify_depth=self.ssl_verify_depth) auth_description = "auth=basic username=%s" % username elif using_id_cert_auth: self.conn = Restlib(self.host, self.ssl_port, self.handler, cert_file=self.cert_file, key_file=self.key_file, proxy_hostname=self.proxy_hostname, proxy_port=self.proxy_port, proxy_user=self.proxy_user, proxy_password=self.proxy_password, ca_dir=self.ca_cert_dir, insecure=self.insecure, ssl_verify_depth=self.ssl_verify_depth) auth_description = "auth=identity_cert ca_dir=%s verify=%s" % (self.ca_cert_dir, self.insecure) else: self.conn = Restlib(self.host, self.ssl_port, self.handler, proxy_hostname=self.proxy_hostname, proxy_port=self.proxy_port, proxy_user=self.proxy_user, proxy_password=self.proxy_password, ca_dir=self.ca_cert_dir, insecure=self.insecure, ssl_verify_depth=self.ssl_verify_depth) auth_description = "auth=none" self.conn.user_agent = "RHSM/1.0 (cmd=%s)" % utils.cmd_name(sys.argv) self.resources = None self.capabilities = None connection_description = "" if proxy_description: connection_description += proxy_description connection_description += "host=%s port=%s handler=%s %s" % (self.host, self.ssl_port, self.handler, auth_description) log.info("Connection built: %s", connection_description)
def __init__(self, host=None, ssl_port=None, handler=None, proxy_hostname=None, proxy_port=None, proxy_user=None, proxy_password=None, username=None, password=None, cert_file=None, key_file=None, insecure=None): """ Two ways to authenticate: - username/password for HTTP basic authentication. (owner admin role) - uuid/key_file/cert_file for identity cert authentication. (consumer role) Must specify one method of authentication or the other, not both. """ self.host = host or config.get('server', 'hostname') self.ssl_port = ssl_port or safe_int(config.get('server', 'port')) self.handler = handler or config.get('server', 'prefix') # remove trailing "/" from the prefix if it is there # BZ848836 self.handler = self.handler.rstrip("/") # get the proxy information from the environment variable # if available info = get_env_proxy_info() self.proxy_hostname = proxy_hostname or config.get('server', 'proxy_hostname') or info['proxy_hostname'] self.proxy_port = proxy_port or config.get('server', 'proxy_port') or info['proxy_port'] self.proxy_user = proxy_user or config.get('server', 'proxy_user') or info['proxy_username'] self.proxy_password = proxy_password or config.get('server', 'proxy_password') or info['proxy_password'] self.cert_file = cert_file self.key_file = key_file self.username = username self.password = password self.ca_cert_dir = config.get('rhsm', 'ca_cert_dir') self.ssl_verify_depth = safe_int(config.get('server', 'ssl_verify_depth')) self.insecure = insecure if insecure is None: self.insecure = False config_insecure = safe_int(config.get('server', 'insecure')) if config_insecure: self.insecure = True using_basic_auth = False using_id_cert_auth = False if username and password: using_basic_auth = True elif cert_file and key_file: using_id_cert_auth = True if using_basic_auth and using_id_cert_auth: raise Exception("Cannot specify both username/password and " "cert_file/key_file") #if not (using_basic_auth or using_id_cert_auth): # raise Exception("Must specify either username/password or " # "cert_file/key_file") # initialize connection if using_basic_auth: self.conn = Restlib(self.host, self.ssl_port, self.handler, username=self.username, password=self.password, proxy_hostname=self.proxy_hostname, proxy_port=self.proxy_port, proxy_user=self.proxy_user, proxy_password=self.proxy_password, ca_dir=self.ca_cert_dir, insecure=self.insecure, ssl_verify_depth=self.ssl_verify_depth) log.info("Using basic authentication as: %s" % username) elif using_id_cert_auth: self.conn = Restlib(self.host, self.ssl_port, self.handler, cert_file=self.cert_file, key_file=self.key_file, proxy_hostname=self.proxy_hostname, proxy_port=self.proxy_port, proxy_user=self.proxy_user, proxy_password=self.proxy_password, ca_dir=self.ca_cert_dir, insecure=self.insecure, ssl_verify_depth=self.ssl_verify_depth) log.info("Using certificate authentication: key = %s, cert = %s, " "ca = %s, insecure = %s" % (self.key_file, self.cert_file, self.ca_cert_dir, self.insecure)) else: self.conn = Restlib(self.host, self.ssl_port, self.handler, proxy_hostname=self.proxy_hostname, proxy_port=self.proxy_port, proxy_user=self.proxy_user, proxy_password=self.proxy_password, ca_dir=self.ca_cert_dir, insecure=self.insecure, ssl_verify_depth=self.ssl_verify_depth) log.info("Using no auth") self.resources = None log.info("Connection Built: host: %s, port: %s, handler: %s" % (self.host, self.ssl_port, self.handler))