def _test_authorized_proxy(self): """End-to-end impersonation + authorization test. Expects authorization to be configured before running this test""" # TODO: To reuse the HS2 utility code from the TestHS2 test suite we need to import # the module within this test function, rather than as a top-level import. This way # the tests in that module will not get pulled when executing this test suite. The fix # is to split the utility code out of the TestHS2 class and support HS2 as a first # class citizen in our test framework. from tests.hs2.test_hs2 import TestHS2 # Try to query a table we are not authorized to access. self.session_handle = self._open_hs2("hue", { "impala.doas.user": getuser() }).sessionHandle bad_resp = self._execute_hs2_stmt("describe tpch_seq.lineitem", False) assert "User '%s' does not have privileges to access" % getuser() in \ str(bad_resp) assert self._wait_for_audit_record(user=getuser(), impersonator="hue"), \ "No matching audit event recorded in time window" # Now try the same operation on a table we are authorized to access. good_resp = self._execute_hs2_stmt("describe tpch.lineitem") TestHS2.check_response(good_resp) # Verify the correct user information is in the runtime profile. query_id = operation_id_to_query_id( good_resp.operationHandle.operationId) profile_page = self.cluster.impalads[ 0].service.read_query_profile_page(query_id) self._verify_profile_user_fields(profile_page, effective_user=getuser(), delegated_user=getuser(), connected_user="******") # Try to delegate a user we are not authorized to delegate to. resp = self._open_hs2("hue", {"impala.doas.user": "******"}, False) assert "User 'hue' is not authorized to delegate to 'some_user'" in str( resp) # Create a new session which does not have a do_as_user and run a simple query. self.session_handle = self._open_hs2("hue", dict()).sessionHandle resp = self._execute_hs2_stmt("select 1") # Verify the correct user information is in the runtime profile. Since there is # no do_as_user the Delegated User field should be empty. query_id = operation_id_to_query_id(resp.operationHandle.operationId) profile_page = self.cluster.impalads[ 0].service.read_query_profile_page(query_id) self._verify_profile_user_fields(profile_page, effective_user="******", delegated_user="", connected_user="******")
def test_get_schemas(self): get_schemas_req = TCLIService.TGetSchemasReq() get_schemas_req.sessionHandle = self.session_handle get_schemas_resp = self.hs2_client.GetSchemas(get_schemas_req) TestHS2.check_response(get_schemas_resp) fetch_results_req = TCLIService.TFetchResultsReq() fetch_results_req.operationHandle = get_schemas_resp.operationHandle fetch_results_req.maxRows = 100 fetch_results_resp = self.hs2_client.FetchResults(fetch_results_req) TestHS2.check_response(fetch_results_resp) query_id = operation_id_to_query_id(get_schemas_resp.operationHandle.operationId) profile_page = self.impalad_test_service.read_query_profile_page(query_id) # Test fix for IMPALA-619 assert "Sql Statement: GET_SCHEMAS" in profile_page assert "Query Type: DDL" in profile_page
def test_disconnected_session_timeout(self): """Test that a session gets closed if it has no active connections for more than disconnected_session_timeout.""" conn = HS2TestSuite() conn.setup() open_session_req = TCLIService.TOpenSessionReq() open_session_resp = conn.hs2_client.OpenSession(open_session_req) HS2TestSuite.check_response(open_session_resp) conn.session_handle = open_session_resp.sessionHandle # Ren a query, which should succeed. conn.execute_statement("select 1") # Set up another connection and run a long-running query with the same session. conn2 = HS2TestSuite() conn2.setup() conn2.session_handle = open_session_resp.sessionHandle execute_resp = conn2.execute_statement("select sleep(10000)") # Close one connection and wait for longer than disconnected_session_timeout. The # session should still be available since there's still one active connection. conn2.teardown() sleep(5) conn.execute_statement("select 3") # Close the other connection and sleep again. THe session shuold now be closed. conn.teardown() sleep(5) conn.setup() # Run another query, which should fail since the session is closed. conn.execute_statement( "select 2", expected_error_prefix="Invalid session id", expected_status_code=TCLIService.TStatusCode.ERROR_STATUS) # Check that the query was cancelled correctly. query_id = operation_id_to_query_id( execute_resp.operationHandle.operationId) status = self.cluster.get_first_impalad().service.get_query_status( query_id) assert status == "Session closed because it has no active connections"
def __test_impersonation(self): """End-to-end impersonation + authorization test. Expects authorization to be configured before running this test""" # TODO: To reuse the HS2 utility code from the TestHS2 test suite we need to import # the module within this test function, rather than as a top-level import. This way # the tests in that module will not get pulled when executing this test suite. The fix # is to split the utility code out of the TestHS2 class and support HS2 as a first # class citizen in our test framework. from tests.hs2.test_hs2 import TestHS2 open_session_req = TCLIService.TOpenSessionReq() # Connected user is 'hue' open_session_req.username = '******' open_session_req.configuration = dict() # Delegated user is the current user open_session_req.configuration['impala.doas.user'] = getuser() resp = self.hs2_client.OpenSession(open_session_req) TestHS2.check_response(resp) # Try to query a table we are not authorized to access. self.session_handle = resp.sessionHandle execute_statement_req = TCLIService.TExecuteStatementReq() execute_statement_req.sessionHandle = self.session_handle execute_statement_req.statement = "describe tpch_seq.lineitem" execute_statement_resp = self.hs2_client.ExecuteStatement( execute_statement_req) assert 'User \'%s\' does not have privileges to access' % getuser() in\ str(execute_statement_resp) assert self.__wait_for_audit_record(user=getuser(), impersonator='hue'),\ 'No matching audit event recorded in time window' # Now try the same operation on a table we are authorized to access. execute_statement_req = TCLIService.TExecuteStatementReq() execute_statement_req.sessionHandle = self.session_handle execute_statement_req.statement = "describe tpch.lineitem" execute_statement_resp = self.hs2_client.ExecuteStatement( execute_statement_req) TestHS2.check_response(execute_statement_resp) # Verify the correct user information is in the runtime profile query_id = operation_id_to_query_id( execute_statement_resp.operationHandle.operationId) profile_page = self.cluster.impalads[ 0].service.read_query_profile_page(query_id) self.__verify_profile_user_fields(profile_page, effective_user=getuser(), delegated_user=getuser(), connected_user='******') # Try to user we are not authorized to delegate to. open_session_req.configuration['impala.doas.user'] = '******' resp = self.hs2_client.OpenSession(open_session_req) assert 'User \'hue\' is not authorized to delegate to \'some_user\'' in str( resp) # Create a new session which does not have a do_as_user. open_session_req.username = '******' open_session_req.configuration = dict() resp = self.hs2_client.OpenSession(open_session_req) TestHS2.check_response(resp) # Run a simple query, which should succeed. execute_statement_req = TCLIService.TExecuteStatementReq() execute_statement_req.sessionHandle = resp.sessionHandle execute_statement_req.statement = "select 1" execute_statement_resp = self.hs2_client.ExecuteStatement( execute_statement_req) TestHS2.check_response(execute_statement_resp) # Verify the correct user information is in the runtime profile. Since there is # no do_as_user the Delegated User field should be empty. query_id = operation_id_to_query_id( execute_statement_resp.operationHandle.operationId) profile_page = self.cluster.impalads[ 0].service.read_query_profile_page(query_id) self.__verify_profile_user_fields(profile_page, effective_user='******', delegated_user='', connected_user='******') self.socket.close() self.socket = None
def __test_impersonation(self, role): """End-to-end impersonation + authorization test. Expects authorization to be configured before running this test""" # TODO: To reuse the HS2 utility code from the TestHS2 test suite we need to import # the module within this test function, rather than as a top-level import. This way # the tests in that module will not get pulled when executing this test suite. The fix # is to split the utility code out of the TestHS2 class and support HS2 as a first # class citizen in our test framework. from tests.hs2.test_hs2 import TestHS2 try: self.session_handle = self.__open_hs2(getuser(), dict()).sessionHandle self.__execute_hs2_stmt("create role {0}".format(role)) self.__execute_hs2_stmt( "grant all on table tpch.lineitem to role {0}".format(role)) self.__execute_hs2_stmt("grant role {0} to group {1}".format( role, grp.getgrnam(getuser()).gr_name)) self.__execute_hs2_stmt("grant role {0} to group {1}".format( role, grp.getgrgid(os.getgid()).gr_name)) # Try to query a table we are not authorized to access self.session_handle = self.__open_hs2('hue', { 'impala.doas.user': getuser() }).sessionHandle bad_resp = self.__execute_hs2_stmt("describe tpch_seq.lineitem", False) assert 'User \'%s\' does not have privileges to access' % getuser() in\ str(bad_resp) assert self.__wait_for_audit_record(user=getuser(), impersonator='hue'),\ 'No matching audit event recorded in time window' # Now try the same operation on a table we are authorized to access. good_resp = self.__execute_hs2_stmt("describe tpch.lineitem") TestHS2.check_response(good_resp) # Verify the correct user information is in the runtime profile query_id = operation_id_to_query_id( good_resp.operationHandle.operationId) profile_page = self.cluster.impalads[ 0].service.read_query_profile_page(query_id) self.__verify_profile_user_fields(profile_page, effective_user=getuser(), delegated_user=getuser(), connected_user='******') # Try to user we are not authorized to delegate to. resp = self.__open_hs2('hue', {'impala.doas.user': '******'}, False) assert 'User \'hue\' is not authorized to delegate to \'some_user\'' in str( resp) # Create a new session which does not have a do_as_user and run a simple query. self.session_handle = self.__open_hs2('hue', dict()).sessionHandle resp = self.__execute_hs2_stmt("select 1") # Verify the correct user information is in the runtime profile. Since there is # no do_as_user the Delegated User field should be empty. query_id = operation_id_to_query_id( resp.operationHandle.operationId) profile_page = self.cluster.impalads[ 0].service.read_query_profile_page(query_id) self.__verify_profile_user_fields(profile_page, effective_user='******', delegated_user='', connected_user='******') finally: self.session_handle = self.__open_hs2(getuser(), dict()).sessionHandle self.__execute_hs2_stmt( "grant all on server to role {0}".format(role)) self.__execute_hs2_stmt("grant role {0} to group {1}".format( role, grp.getgrnam(getuser()).gr_name)) self.__execute_hs2_stmt("drop role {0}".format(role))
def test_impersonation(self): """End-to-end impersonation + authorization test. Expects authorization to be configured before running this test""" # TODO: To reuse the HS2 utility code from the TestHS2 test suite we need to import # the module within this test function, rather than as a top-level import. This way # the tests in that module will not get pulled when executing this test suite. The fix # is to split the utility code out of the TestHS2 class and support HS2 as a first # class citizen in our test framework. from tests.hs2.test_hs2 import TestHS2 open_session_req = TCLIService.TOpenSessionReq() # Connected user is 'hue' open_session_req.username = '******' open_session_req.configuration = dict() # Delegated user is the current user open_session_req.configuration['impala.doas.user'] = getuser() resp = self.hs2_client.OpenSession(open_session_req) TestHS2.check_response(resp) # Try to query a table we are not authorized to access. self.session_handle = resp.sessionHandle execute_statement_req = TCLIService.TExecuteStatementReq() execute_statement_req.sessionHandle = self.session_handle execute_statement_req.statement = "describe tpch_seq.lineitem" execute_statement_resp = self.hs2_client.ExecuteStatement(execute_statement_req) assert 'User \'%s\' does not have privileges to access' % getuser() in\ str(execute_statement_resp) assert self.__wait_for_audit_record(user=getuser(), impersonator='hue'),\ 'No matching audit event recorded in time window' # Now try the same operation on a table we are authorized to access. execute_statement_req = TCLIService.TExecuteStatementReq() execute_statement_req.sessionHandle = self.session_handle execute_statement_req.statement = "describe tpch.lineitem" execute_statement_resp = self.hs2_client.ExecuteStatement(execute_statement_req) TestHS2.check_response(execute_statement_resp) # Verify the correct user information is in the runtime profile query_id = operation_id_to_query_id( execute_statement_resp.operationHandle.operationId) profile_page = self.cluster.impalads[0].service.read_query_profile_page(query_id) self.__verify_profile_user_fields(profile_page, effective_user=getuser(), delegated_user=getuser(), connected_user='******') # Try to user we are not authorized to delegate to. open_session_req.configuration['impala.doas.user'] = '******' resp = self.hs2_client.OpenSession(open_session_req) assert 'User \'hue\' is not authorized to delegate to \'some_user\'' in str(resp) # Create a new session which does not have a do_as_user. open_session_req.username = '******' open_session_req.configuration = dict() resp = self.hs2_client.OpenSession(open_session_req) TestHS2.check_response(resp) # Run a simple query, which should succeed. execute_statement_req = TCLIService.TExecuteStatementReq() execute_statement_req.sessionHandle = resp.sessionHandle execute_statement_req.statement = "select 1" execute_statement_resp = self.hs2_client.ExecuteStatement(execute_statement_req) TestHS2.check_response(execute_statement_resp) # Verify the correct user information is in the runtime profile. Since there is # no do_as_user the Delegated User field should be empty. query_id = operation_id_to_query_id( execute_statement_resp.operationHandle.operationId) profile_page = self.cluster.impalads[0].service.read_query_profile_page(query_id) self.__verify_profile_user_fields(profile_page, effective_user='******', delegated_user='', connected_user='******') self.socket.close() self.socket = None
def __test_impersonation(self, role): """End-to-end impersonation + authorization test. Expects authorization to be configured before running this test""" # TODO: To reuse the HS2 utility code from the TestHS2 test suite we need to import # the module within this test function, rather than as a top-level import. This way # the tests in that module will not get pulled when executing this test suite. The fix # is to split the utility code out of the TestHS2 class and support HS2 as a first # class citizen in our test framework. from tests.hs2.test_hs2 import TestHS2 try: self.session_handle = self.__open_hs2(getuser(), dict()).sessionHandle self.__execute_hs2_stmt("create role {0}".format(role)) self.__execute_hs2_stmt("grant all on table tpch.lineitem to role {0}" .format(role)) self.__execute_hs2_stmt("grant role {0} to group {1}" .format(role, grp.getgrnam(getuser()).gr_name)) self.__execute_hs2_stmt("grant role {0} to group {1}" .format(role, grp.getgrgid(os.getgid()).gr_name)) # Try to query a table we are not authorized to access self.session_handle = self.__open_hs2('hue', {'impala.doas.user': getuser()}).sessionHandle bad_resp = self.__execute_hs2_stmt("describe tpch_seq.lineitem", False) assert 'User \'%s\' does not have privileges to access' % getuser() in\ str(bad_resp) assert self.__wait_for_audit_record(user=getuser(), impersonator='hue'),\ 'No matching audit event recorded in time window' # Now try the same operation on a table we are authorized to access. good_resp = self.__execute_hs2_stmt("describe tpch.lineitem") TestHS2.check_response(good_resp) # Verify the correct user information is in the runtime profile query_id = operation_id_to_query_id( good_resp.operationHandle.operationId) profile_page = self.cluster.impalads[0].service.read_query_profile_page(query_id) self.__verify_profile_user_fields(profile_page, effective_user=getuser(), delegated_user=getuser(), connected_user='******') # Try to user we are not authorized to delegate to. resp = self.__open_hs2('hue', {'impala.doas.user': '******'}, False) assert 'User \'hue\' is not authorized to delegate to \'some_user\'' in str(resp) # Create a new session which does not have a do_as_user and run a simple query. self.session_handle = self.__open_hs2('hue', dict()).sessionHandle resp = self.__execute_hs2_stmt("select 1") # Verify the correct user information is in the runtime profile. Since there is # no do_as_user the Delegated User field should be empty. query_id = operation_id_to_query_id(resp.operationHandle.operationId) profile_page = self.cluster.impalads[0].service.read_query_profile_page(query_id) self.__verify_profile_user_fields(profile_page, effective_user='******', delegated_user='', connected_user='******') finally: self.session_handle = self.__open_hs2(getuser(), dict()).sessionHandle self.__execute_hs2_stmt("grant all on server to role {0}".format(role)) self.__execute_hs2_stmt("grant role {0} to group {1}" .format(role, grp.getgrnam(getuser()).gr_name)) self.__execute_hs2_stmt("drop role {0}".format(role))