Search in sources :

Example 6 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 7 with JerseyRestClient

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

the class JerseySparkShellRestClient method getClient.

/**
 * Gets or creates a Jersey REST client for the specified Spark Shell process.
 *
 * @param process the Spark Shell process
 * @return the Jersey REST client
 */
@Nonnull
private JerseyRestClient getClient(@Nonnull final SparkShellProcess process) {
    return clients.computeIfAbsent(process, target -> {
        final JerseyClientConfig config = new JerseyClientConfig();
        config.setHost(target.getHostname());
        config.setPort(target.getPort());
        return new JerseyRestClient(config);
    });
}
Also used : JerseyClientConfig(com.thinkbiganalytics.rest.JerseyClientConfig) JerseyRestClient(com.thinkbiganalytics.rest.JerseyRestClient) Nonnull(javax.annotation.Nonnull)

Aggregations

JerseyRestClient (com.thinkbiganalytics.rest.JerseyRestClient)7 JerseyClientConfig (com.thinkbiganalytics.rest.JerseyClientConfig)4 Arrays (java.util.Arrays)2 Collections (java.util.Collections)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Nonnull (javax.annotation.Nonnull)2 Invocation (javax.ws.rs.client.Invocation)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 SPRING_PROFILES_INCLUDE (com.thinkbiganalytics.install.inspector.inspection.Configuration.SPRING_PROFILES_INCLUDE)1 User (com.thinkbiganalytics.security.rest.model.User)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Principal (java.security.Principal)1 Comparator (java.util.Comparator)1 List (java.util.List)1 Collectors (java.util.stream.Collectors)1 Inject (javax.inject.Inject)1 Subject (javax.security.auth.Subject)1 CallbackHandler (javax.security.auth.callback.CallbackHandler)1