Search in sources :

Example 11 with JerseyRestClient

use of com.thinkbiganalytics.rest.JerseyRestClient in project kylo by Teradata.

the class KyloRestLoginModuleTest method test.

/**
 * Verify logging in by querying a REST API.
 */
@Test
public void test() throws Exception {
    // Mock REST client
    final User user = new User();
    user.setEnabled(true);
    user.setGroups(ImmutableSet.of("designers", "operators"));
    user.setSystemName("dladmin");
    final JerseyRestClient client = Mockito.mock(JerseyRestClient.class);
    Mockito.when(client.get(any(Invocation.Builder.class), eq(User.class))).thenReturn(user);
    // Mock callback handler
    final CallbackHandler callbackHandler = callbacks -> Arrays.stream(callbacks).forEach(callback -> {
        if (callback instanceof NameCallback) {
            ((NameCallback) callback).setName("dladmin");
        } else if (callback instanceof PasswordCallback) {
            ((PasswordCallback) callback).setPassword("thinkbig".toCharArray());
        }
    });
    // Mock login module
    final KyloRestLoginModule module = new KyloRestLoginModule() {

        @Nonnull
        @Override
        JerseyRestClient getClient(@Nonnull LoginJerseyClientConfig config) {
            return client;
        }
    };
    // Test login
    final Subject subject = new Subject();
    Map<String, Object> options = new HashMap<>();
    options.put(KyloRestLoginModule.REST_CLIENT_CONFIG, new LoginJerseyClientConfig());
    options.put(KyloRestLoginModule.SERVICES_LOGOUT, false);
    module.initialize(subject, callbackHandler, Collections.emptyMap(), options);
    Assert.assertTrue(module.login());
    Assert.assertTrue(module.commit());
    // Verify subject
    final Principal[] principals = subject.getPrincipals().toArray(new Principal[0]);
    Assert.assertEquals(3, principals.length);
    Arrays.sort(principals, Comparator.comparing(Principal::getName));
    Assert.assertEquals("designers", principals[0].getName());
    Assert.assertEquals("dladmin", principals[1].getName());
    Assert.assertEquals("operators", principals[2].getName());
}
Also used : Arrays(java.util.Arrays) PasswordCallback(javax.security.auth.callback.PasswordCallback) ImmutableSet(com.google.common.collect.ImmutableSet) Test(org.junit.Test) HashMap(java.util.HashMap) Invocation(javax.ws.rs.client.Invocation) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient) Subject(javax.security.auth.Subject) Matchers.any(org.mockito.Matchers.any) Mockito(org.mockito.Mockito) Principal(java.security.Principal) CallbackHandler(javax.security.auth.callback.CallbackHandler) NameCallback(javax.security.auth.callback.NameCallback) Matchers.eq(org.mockito.Matchers.eq) Map(java.util.Map) User(com.thinkbiganalytics.security.rest.model.User) Assert(org.junit.Assert) Comparator(java.util.Comparator) Collections(java.util.Collections) Nonnull(javax.annotation.Nonnull) CallbackHandler(javax.security.auth.callback.CallbackHandler) User(com.thinkbiganalytics.security.rest.model.User) Nonnull(javax.annotation.Nonnull) HashMap(java.util.HashMap) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient) Subject(javax.security.auth.Subject) NameCallback(javax.security.auth.callback.NameCallback) PasswordCallback(javax.security.auth.callback.PasswordCallback) Principal(java.security.Principal) Test(org.junit.Test)

Example 12 with JerseyRestClient

use of com.thinkbiganalytics.rest.JerseyRestClient in project kylo by Teradata.

the class TestSparkLivyRestClient method testGetLivySessions.

/**
 * This test assumes: 1) you have configured a Livy Server for Kerberos and that it is running at 8998 2) that server has the library 'kylo-spark-shell-client-v1-0.10.0-SNAPSHOT.jar' installed
 */
@Test
// ignore, for now, because this is an integration test that requires livy configured on the test system
@Ignore
public void testGetLivySessions() throws JsonProcessingException, InterruptedException {
    System.setProperty("sun.security.krb5.debug", "true");
    System.setProperty("sun.security.jgss.debug", "true");
    SessionsGet sg = new SessionsGet.Builder().from(1).size(2).build();
    String sgJson = new ObjectMapper().writeValueAsString(sg);
    logger.debug("{}", sgJson);
    assertThat(sgJson).isEqualToIgnoringCase("{\"from\":1,\"size\":2}");
    logger.debug("kerberosSparkProperties={}", kerberosSparkProperties);
    SparkShellProcess sparkProcess = sparkLivyProcessManager.getProcessForUser("kylo");
    // ((SparkLivyProcess) sparkProcess).setPort(8999);
    JerseyRestClient client = sparkLivyProcessManager.getClient(sparkProcess);
    sparkLivyProcessManager.start("kylo");
    Integer stmtId = 0;
    Statement statement = livyRestProvider.pollStatement(client, sparkProcess, stmtId);
    logger.debug("statement={}", statement);
    assertThat(statement.getState()).isEqualTo(StatementState.available);
    assertThat(statement.getOutput().getStatus()).isEqualTo(StatementOutputStatus.ok);
}
Also used : SparkShellProcess(com.thinkbiganalytics.spark.shell.SparkShellProcess) Statement(com.thinkbiganalytics.kylo.spark.model.Statement) SessionsGet(com.thinkbiganalytics.kylo.spark.model.SessionsGet) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 13 with JerseyRestClient

use of com.thinkbiganalytics.rest.JerseyRestClient in project kylo by Teradata.

the class SparkLivyConfig method livyHeartbeatMonitor.

@Bean
public LivyHeartbeatMonitor livyHeartbeatMonitor() {
    // LivyHeartbeatMonitor gets it's own jersey client
    LivyProperties livyProperties = livyProperties();
    final JerseyClientConfig config = new JerseyClientConfig();
    config.setHost(livyProperties.getHostname());
    config.setPort(livyProperties.getPort());
    if (livyProperties().getTruststorePassword() != null) {
        config.setHttps(true);
        config.setTruststorePath(livyProperties.getTruststorePath());
        config.setTruststorePassword(livyProperties.getTruststorePassword());
        config.setTrustStoreType(livyProperties.getTruststoreType());
    }
    // end if
    // all clients will have kerberos
    LivyRestClient.setKerberosSparkProperties(kerberosSparkProperties());
    JerseyRestClient livyRestClient = new LivyRestClient(config);
    return new LivyHeartbeatMonitor(livyClient(), livyRestClient, livyServer(livyProperties()), livyProperties());
}
Also used : SparkLivyRestClient(com.thinkbiganalytics.kylo.spark.livy.SparkLivyRestClient) LivyRestClient(com.thinkbiganalytics.kylo.spark.client.jersey.LivyRestClient) JerseyClientConfig(com.thinkbiganalytics.rest.JerseyClientConfig) LivyHeartbeatMonitor(com.thinkbiganalytics.kylo.spark.client.livy.LivyHeartbeatMonitor) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient) Bean(org.springframework.context.annotation.Bean)

Example 14 with JerseyRestClient

use of com.thinkbiganalytics.rest.JerseyRestClient in project kylo by Teradata.

the class SparkLivyRestClient method kyloCatalogTransform.

@Nonnull
public TransformResponse kyloCatalogTransform(@Nonnull final SparkShellProcess process, @Nonnull final KyloCatalogReadRequest request) {
    logger.entry(process, request);
    String script = scriptGenerator.wrappedScript("kyloCatalogTransform", "", "\n", ScalaScriptUtils.toJsonInScalaString(request));
    logger.debug("scala str\n{}", script);
    JerseyRestClient client = sparkLivyProcessManager.getClient(process);
    Statement statement = submitCode(client, script, process);
    if (statement.getState() == StatementState.running || statement.getState() == StatementState.waiting) {
        statement = pollStatement(client, process, statement.getId());
    } else {
        throw logger.throwing(new LivyUserException("livy.unexpected_error"));
    }
    // call with null so a transformId will be generated for this query
    return logger.exit(LivyRestModelTransformer.toTransformResponse(statement, null));
}
Also used : Statement(com.thinkbiganalytics.kylo.spark.model.Statement) LivyUserException(com.thinkbiganalytics.kylo.spark.exceptions.LivyUserException) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient) Nonnull(javax.annotation.Nonnull)

Example 15 with JerseyRestClient

use of com.thinkbiganalytics.rest.JerseyRestClient in project kylo by Teradata.

the class SparkLivyRestClient method getTransformSave.

@Nonnull
@Override
public Optional<SaveResponse> getTransformSave(@Nonnull SparkShellProcess process, @Nonnull String transformId, @Nonnull String saveId) {
    logger.entry(process, transformId, saveId);
    Validate.isInstanceOf(SparkLivyProcess.class, process, "SparkLivyRestClient.getTransformSave called on non Livy Process");
    SparkLivyProcess sparkLivyProcess = (SparkLivyProcess) process;
    JerseyRestClient client = sparkLivyProcessManager.getClient(process);
    SaveResponse response;
    Integer stmtId = transformIdsToLivyId.getIfPresent(transformId);
    if (stmtId != null) {
        Statement statement = livyClient.getStatement(client, sparkLivyProcess, stmtId);
        response = LivyRestModelTransformer.toSaveResponse(statement);
        if (statement.getState() == StatementState.available) {
            transformIdsToLivyId.invalidate(transformId);
        }
    } else {
        // saveId is not an integer.  We have already processed Livy Response
        String script = scriptGenerator.script("getSave", saveId);
        Statement statement = submitCode(client, script, process);
        response = LivyRestModelTransformer.toSaveResponse(statement);
        response.setId(saveId);
        // remember the true id.  how? a cache.. it's not gonna get communicated back to us from UI..
        transformIdsToLivyId.put(transformId, statement.getId());
    }
    return logger.exit(Optional.of(response));
}
Also used : SaveResponse(com.thinkbiganalytics.spark.rest.model.SaveResponse) Statement(com.thinkbiganalytics.kylo.spark.model.Statement) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient) Nonnull(javax.annotation.Nonnull)

Aggregations

JerseyRestClient (com.thinkbiganalytics.rest.JerseyRestClient)23 Statement (com.thinkbiganalytics.kylo.spark.model.Statement)11 Nonnull (javax.annotation.Nonnull)10 LivyUserException (com.thinkbiganalytics.kylo.spark.exceptions.LivyUserException)7 JerseyClientConfig (com.thinkbiganalytics.rest.JerseyClientConfig)5 Session (com.thinkbiganalytics.kylo.spark.model.Session)4 SparkJobResponse (com.thinkbiganalytics.kylo.spark.rest.model.job.SparkJobResponse)3 SaveResponse (com.thinkbiganalytics.spark.rest.model.SaveResponse)3 LivyServerNotReachableException (com.thinkbiganalytics.kylo.spark.exceptions.LivyServerNotReachableException)2 TransformResponse (com.thinkbiganalytics.spark.rest.model.TransformResponse)2 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Invocation (javax.ws.rs.client.Invocation)2 Response (javax.ws.rs.core.Response)2 Test (org.junit.Test)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 SPRING_PROFILES_INCLUDE (com.thinkbiganalytics.install.inspector.inspection.Configuration.SPRING_PROFILES_INCLUDE)1