Search in sources :

Example 1 with TOpenSessionResp

use of org.apache.hive.service.rpc.thrift.TOpenSessionResp in project hive by apache.

the class TestThriftHttpCLIServiceFeatures method verifyForwardedHeaders.

private void verifyForwardedHeaders(ArrayList<String> headerIPs, String cmd) throws Exception {
    TTransport transport;
    DefaultHttpClient hClient = new DefaultHttpClient();
    String httpUrl = getHttpUrl();
    // add an interceptor that adds the X-Forwarded-For header with given ips
    if (!headerIPs.isEmpty()) {
        Header xForwardHeader = new BasicHeader("X-Forwarded-For", Joiner.on(",").join(headerIPs));
        RequestDefaultHeaders headerInterceptor = new RequestDefaultHeaders(Arrays.asList(xForwardHeader));
        hClient.addRequestInterceptor(headerInterceptor);
    }
    // interceptor for adding username, pwd
    HttpBasicAuthInterceptor authInt = new HttpBasicAuthInterceptor(ThriftCLIServiceTest.USERNAME, ThriftCLIServiceTest.PASSWORD, null, null, false, null, null);
    hClient.addRequestInterceptor(authInt);
    transport = new THttpClient(httpUrl, hClient);
    TCLIService.Client httpClient = getClient(transport);
    // Create a new open session request object
    TOpenSessionReq openReq = new TOpenSessionReq();
    TOpenSessionResp openResp = httpClient.OpenSession(openReq);
    // execute a query
    TExecuteStatementReq execReq = new TExecuteStatementReq(openResp.getSessionHandle(), "show tables");
    httpClient.ExecuteStatement(execReq);
    // capture arguments to authorizer impl call and verify ip addresses passed
    ArgumentCaptor<HiveAuthzContext> contextCapturer = ArgumentCaptor.forClass(HiveAuthzContext.class);
    verify(mockedAuthorizer).checkPrivileges(any(HiveOperationType.class), Matchers.anyListOf(HivePrivilegeObject.class), Matchers.anyListOf(HivePrivilegeObject.class), contextCapturer.capture());
    HiveAuthzContext context = contextCapturer.getValue();
    System.err.println("Forwarded IP Addresses " + context.getForwardedAddresses());
    List<String> auditIPAddresses = new ArrayList<String>(context.getForwardedAddresses());
    Collections.sort(auditIPAddresses);
    Collections.sort(headerIPs);
    Assert.assertEquals("Checking forwarded IP Address", headerIPs, auditIPAddresses);
}
Also used : RequestDefaultHeaders(org.apache.http.client.protocol.RequestDefaultHeaders) HiveAuthzContext(org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAuthzContext) ArrayList(java.util.ArrayList) THttpClient(org.apache.thrift.transport.THttpClient) TExecuteStatementReq(org.apache.hive.service.rpc.thrift.TExecuteStatementReq) HiveOperationType(org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType) HivePrivilegeObject(org.apache.hadoop.hive.ql.security.authorization.plugin.HivePrivilegeObject) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) TCLIService(org.apache.hive.service.rpc.thrift.TCLIService) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) TTransport(org.apache.thrift.transport.TTransport) TOpenSessionReq(org.apache.hive.service.rpc.thrift.TOpenSessionReq) TOpenSessionResp(org.apache.hive.service.rpc.thrift.TOpenSessionResp) BasicHeader(org.apache.http.message.BasicHeader) HttpBasicAuthInterceptor(org.apache.hive.jdbc.HttpBasicAuthInterceptor)

Example 2 with TOpenSessionResp

use of org.apache.hive.service.rpc.thrift.TOpenSessionResp in project hive by apache.

the class TestHs2ConnectionMetricsHttp method testOpenConnectionMetrics.

@Test
public void testOpenConnectionMetrics() throws Exception {
    CodahaleMetrics metrics = (CodahaleMetrics) MetricsFactory.getInstance();
    TCLIService.Client httpClient = getHttpClient();
    TOpenSessionReq openSessionReq = new TOpenSessionReq();
    TOpenSessionResp tOpenSessionResp = httpClient.OpenSession(openSessionReq);
    // wait a couple of sec to make sure the connection is closed
    TimeUnit.SECONDS.sleep(3);
    verifyConnectionMetrics(metrics.dumpJson(), 0, 1);
    TSessionHandle sessionHandle = tOpenSessionResp.getSessionHandle();
    TCloseSessionReq closeSessionReq = new TCloseSessionReq(sessionHandle);
    httpClient.CloseSession(closeSessionReq);
    TimeUnit.SECONDS.sleep(3);
    verifyConnectionMetrics(metrics.dumpJson(), 0, 2);
    tOpenSessionResp = httpClient.OpenSession(openSessionReq);
    TimeUnit.SECONDS.sleep(3);
    verifyConnectionMetrics(metrics.dumpJson(), 0, 3);
    sessionHandle = tOpenSessionResp.getSessionHandle();
    closeSessionReq = new TCloseSessionReq(sessionHandle);
    httpClient.CloseSession(closeSessionReq);
    TimeUnit.SECONDS.sleep(3);
    verifyConnectionMetrics(metrics.dumpJson(), 0, 4);
}
Also used : CodahaleMetrics(org.apache.hadoop.hive.common.metrics.metrics2.CodahaleMetrics) TSessionHandle(org.apache.hive.service.rpc.thrift.TSessionHandle) TOpenSessionReq(org.apache.hive.service.rpc.thrift.TOpenSessionReq) TOpenSessionResp(org.apache.hive.service.rpc.thrift.TOpenSessionResp) TCloseSessionReq(org.apache.hive.service.rpc.thrift.TCloseSessionReq) TCLIService(org.apache.hive.service.rpc.thrift.TCLIService) Test(org.junit.Test)

Example 3 with TOpenSessionResp

use of org.apache.hive.service.rpc.thrift.TOpenSessionResp in project hive by apache.

the class ThriftCLIService method OpenSession.

@Override
public TOpenSessionResp OpenSession(TOpenSessionReq req) throws TException {
    LOG.info("Client protocol version: " + req.getClient_protocol());
    TOpenSessionResp resp = new TOpenSessionResp();
    String userName = null;
    try {
        userName = getUserName(req);
        final SessionHandle sessionHandle = getSessionHandle(req, resp, userName);
        final int fetchSize = hiveConf.getIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_RESULTSET_DEFAULT_FETCH_SIZE);
        Map<String, String> map = new HashMap<>();
        map.put(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_RESULTSET_DEFAULT_FETCH_SIZE.varname, Integer.toString(fetchSize));
        map.put(HiveConf.ConfVars.HIVE_DEFAULT_NULLS_LAST.varname, String.valueOf(hiveConf.getBoolVar(ConfVars.HIVE_DEFAULT_NULLS_LAST)));
        resp.setSessionHandle(sessionHandle.toTSessionHandle());
        resp.setConfiguration(map);
        resp.setStatus(OK_STATUS);
        ThriftCLIServerContext context = (ThriftCLIServerContext) currentServerContext.get();
        if (context != null) {
            context.setSessionHandle(sessionHandle);
        }
        LOG.info("Login attempt is successful for user : " + userName);
    } catch (Exception e) {
        // Do not log request as it contains password information
        LOG.error("Login attempt failed for user : {}", userName, e);
        resp.setStatus(HiveSQLException.toTStatus(e));
    }
    return resp;
}
Also used : HashMap(java.util.HashMap) SessionHandle(org.apache.hive.service.cli.SessionHandle) TOpenSessionResp(org.apache.hive.service.rpc.thrift.TOpenSessionResp) ServiceException(org.apache.hive.service.ServiceException) TException(org.apache.thrift.TException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) LoginException(javax.security.auth.login.LoginException) HiveSQLException(org.apache.hive.service.cli.HiveSQLException)

Example 4 with TOpenSessionResp

use of org.apache.hive.service.rpc.thrift.TOpenSessionResp in project hive by apache.

the class ThriftCLIServiceClient method openSession.

/* (non-Javadoc)
   * @see org.apache.hive.service.cli.ICLIService#openSession(java.lang.String, java.lang.String, java.util.Map)
   */
@Override
public SessionHandle openSession(String username, String password, Map<String, String> configuration) throws HiveSQLException {
    try {
        TOpenSessionReq req = new TOpenSessionReq();
        req.setUsername(username);
        req.setPassword(password);
        req.setConfiguration(configuration);
        TOpenSessionResp resp = cliService.OpenSession(req);
        checkStatus(resp.getStatus());
        return new SessionHandle(resp.getSessionHandle(), resp.getServerProtocolVersion());
    } catch (HiveSQLException e) {
        throw e;
    } catch (Exception e) {
        throw new HiveSQLException(e);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) TOpenSessionReq(org.apache.hive.service.rpc.thrift.TOpenSessionReq) TOpenSessionResp(org.apache.hive.service.rpc.thrift.TOpenSessionResp) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) TException(org.apache.thrift.TException)

Example 5 with TOpenSessionResp

use of org.apache.hive.service.rpc.thrift.TOpenSessionResp in project hive by apache.

the class TestThriftCliServiceWithInfoMessage method testExecuteReturnWithInfoMessage.

@Test
public void testExecuteReturnWithInfoMessage() throws Exception {
    TTransport transport = HiveAuthUtils.getSocketTransport(host, cliPort, 0);
    try {
        transport.open();
        TCLIService.Iface client = new TCLIService.Client(new TBinaryProtocol(transport));
        TOpenSessionReq openReq = new TOpenSessionReq();
        openReq.setClient_protocol(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V11);
        TOpenSessionResp sessionResp = client.OpenSession(openReq);
        Map<String, String> serverHiveConf = sessionResp.getConfiguration();
        assertNotNull(serverHiveConf);
        assertTrue(Boolean.parseBoolean(serverHiveConf.get(ConfVars.HIVE_DEFAULT_NULLS_LAST.varname)));
        TSessionHandle sessHandle = sessionResp.getSessionHandle();
        TExecuteStatementReq execReq = new TExecuteStatementReq(sessHandle, "select 1");
        execReq.setRunAsync(true);
        TExecuteStatementResp execResp = client.ExecuteStatement(execReq);
        TStatus status = execResp.getStatus();
        assertTrue(status.getStatusCode() == TStatusCode.SUCCESS_WITH_INFO_STATUS);
        assertNotNull(status.getInfoMessages());
        assertTrue(status.getInfoMessages().size() > 0);
        Pattern pattern = Pattern.compile("http://.*:(\\d+)/query_page\\.html\\?operationId=(.+)$");
        Matcher matcher = pattern.matcher(status.getInfoMessages().get(0));
        assertTrue(matcher.find());
        assertEquals(webuiPort + "", matcher.group(1));
        OperationHandle operationHandle = new OperationHandle(execResp.getOperationHandle());
        assertEquals(operationHandle.getHandleIdentifier().toString(), matcher.group(2));
        // Cancel the operation
        TCancelOperationReq cancelReq = new TCancelOperationReq();
        cancelReq.setOperationHandle(execResp.getOperationHandle());
        client.CancelOperation(cancelReq);
        // Disable showing the link
        Map<String, String> overlayConf = new HashMap<>();
        overlayConf.put(ConfVars.HIVE_SERVER2_SHOW_OPERATION_DRILLDOWN_LINK.varname, "false");
        execReq = new TExecuteStatementReq(sessHandle, "select 1");
        execReq.setRunAsync(true);
        execReq.setConfOverlay(overlayConf);
        execResp = client.ExecuteStatement(execReq);
        status = execResp.getStatus();
        assertTrue(status.getStatusCode() == TStatusCode.SUCCESS_STATUS);
        assertTrue(status.getInfoMessages() == null);
    } finally {
        transport.close();
    }
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) TExecuteStatementReq(org.apache.hive.service.rpc.thrift.TExecuteStatementReq) OperationHandle(org.apache.hive.service.cli.OperationHandle) TCLIService(org.apache.hive.service.rpc.thrift.TCLIService) TSessionHandle(org.apache.hive.service.rpc.thrift.TSessionHandle) TCancelOperationReq(org.apache.hive.service.rpc.thrift.TCancelOperationReq) TBinaryProtocol(org.apache.thrift.protocol.TBinaryProtocol) TStatus(org.apache.hive.service.rpc.thrift.TStatus) TTransport(org.apache.thrift.transport.TTransport) TOpenSessionReq(org.apache.hive.service.rpc.thrift.TOpenSessionReq) TOpenSessionResp(org.apache.hive.service.rpc.thrift.TOpenSessionResp) TExecuteStatementResp(org.apache.hive.service.rpc.thrift.TExecuteStatementResp) Test(org.junit.Test)

Aggregations

TOpenSessionResp (org.apache.hive.service.rpc.thrift.TOpenSessionResp)6 TOpenSessionReq (org.apache.hive.service.rpc.thrift.TOpenSessionReq)4 TCLIService (org.apache.hive.service.rpc.thrift.TCLIService)3 TException (org.apache.thrift.TException)3 HashMap (java.util.HashMap)2 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)2 SessionHandle (org.apache.hive.service.cli.SessionHandle)2 TExecuteStatementReq (org.apache.hive.service.rpc.thrift.TExecuteStatementReq)2 TSessionHandle (org.apache.hive.service.rpc.thrift.TSessionHandle)2 TTransport (org.apache.thrift.transport.TTransport)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 LoginException (javax.security.auth.login.LoginException)1 CodahaleMetrics (org.apache.hadoop.hive.common.metrics.metrics2.CodahaleMetrics)1 HiveAuthzContext (org.apache.hadoop.hive.ql.security.authorization.plugin.HiveAuthzContext)1 HiveOperationType (org.apache.hadoop.hive.ql.security.authorization.plugin.HiveOperationType)1