Search in sources :

Example 16 with AuthenticationClient

use of co.cask.cdap.security.authentication.client.AuthenticationClient in project cdap-ingest by caskdata.

the class RestStreamWriterTest method testSuccessAuthorizedStringWrite.

@Test
public void testSuccessAuthorizedStringWrite() throws IOException, InterruptedException, ExecutionException {
    AuthenticationClient authClient = Mockito.mock(AuthenticationClient.class);
    AccessToken accessToken = Mockito.mock(AccessToken.class);
    Mockito.when(authClient.getAccessToken()).thenReturn(accessToken);
    Mockito.when(authClient.isAuthEnabled()).thenReturn(true);
    Mockito.when(accessToken.getValue()).thenReturn(RestTest.AUTH_TOKEN);
    Mockito.when(accessToken.getTokenType()).thenReturn("Bearer");
    createClient(authClient);
    streamWriter = streamClient.createWriter(TestUtils.AUTH_STREAM_NAME + TestUtils.WRITER_TEST_STREAM_NAME_POSTFIX);
    streamWriter.write(RestTest.EXPECTED_WRITER_CONTENT, Charsets.UTF_8).get();
}
Also used : AccessToken(co.cask.cdap.security.authentication.client.AccessToken) AuthenticationClient(co.cask.cdap.security.authentication.client.AuthenticationClient) Test(org.junit.Test)

Example 17 with AuthenticationClient

use of co.cask.cdap.security.authentication.client.AuthenticationClient in project cdap-ingest by caskdata.

the class RestStreamClientTest method testSuccessAuthSetTTL.

@Test
public void testSuccessAuthSetTTL() throws IOException {
    AuthenticationClient authClient = Mockito.mock(AuthenticationClient.class);
    AccessToken accessToken = Mockito.mock(AccessToken.class);
    Mockito.when(authClient.getAccessToken()).thenReturn(accessToken);
    Mockito.when(accessToken.getValue()).thenReturn(AUTH_TOKEN);
    Mockito.when(accessToken.getTokenType()).thenReturn("Bearer");
    createClient(authClient);
    streamClient.setTTL(TestUtils.SUCCESS_STREAM_NAME, STREAM_TTL);
}
Also used : AccessToken(co.cask.cdap.security.authentication.client.AccessToken) AuthenticationClient(co.cask.cdap.security.authentication.client.AuthenticationClient) Test(org.junit.Test)

Example 18 with AuthenticationClient

use of co.cask.cdap.security.authentication.client.AuthenticationClient in project cdap-ingest by caskdata.

the class RestStreamClientTest method testNotAuthorizedEmptyTokenCreate.

@Test
public void testNotAuthorizedEmptyTokenCreate() throws IOException {
    AuthenticationClient authClient = Mockito.mock(AuthenticationClient.class);
    AccessToken accessToken = Mockito.mock(AccessToken.class);
    Mockito.when(authClient.getAccessToken()).thenReturn(accessToken);
    Mockito.when(accessToken.getValue()).thenReturn(StringUtils.EMPTY);
    Mockito.when(accessToken.getTokenType()).thenReturn("Bearer");
    createClient(authClient);
    try {
        streamClient.create(TestUtils.AUTH_STREAM_NAME);
        Assert.fail("Expected HttpFailureException");
    } catch (HttpFailureException e) {
        Assert.assertEquals(HttpURLConnection.HTTP_UNAUTHORIZED, e.getStatusCode());
    }
}
Also used : HttpFailureException(co.cask.common.http.exception.HttpFailureException) AccessToken(co.cask.cdap.security.authentication.client.AccessToken) AuthenticationClient(co.cask.cdap.security.authentication.client.AuthenticationClient) Test(org.junit.Test)

Example 19 with AuthenticationClient

use of co.cask.cdap.security.authentication.client.AuthenticationClient in project cdap-ingest by caskdata.

the class RestStreamClientTest method testSuccessAuthCreate.

@Test
public void testSuccessAuthCreate() throws IOException {
    AuthenticationClient authClient = Mockito.mock(AuthenticationClient.class);
    AccessToken accessToken = Mockito.mock(AccessToken.class);
    Mockito.when(authClient.getAccessToken()).thenReturn(accessToken);
    Mockito.when(accessToken.getValue()).thenReturn(AUTH_TOKEN);
    Mockito.when(accessToken.getTokenType()).thenReturn("Bearer");
    createClient(authClient);
    streamClient.create(TestUtils.SUCCESS_STREAM_NAME);
}
Also used : AccessToken(co.cask.cdap.security.authentication.client.AccessToken) AuthenticationClient(co.cask.cdap.security.authentication.client.AuthenticationClient) Test(org.junit.Test)

Example 20 with AuthenticationClient

use of co.cask.cdap.security.authentication.client.AuthenticationClient in project cdap by caskdata.

the class IntegrationTestBase method fetchAccessToken.

protected AccessToken fetchAccessToken(String username, String password) throws IOException, TimeoutException, InterruptedException {
    Properties properties = new Properties();
    properties.setProperty("security.auth.client.username", username);
    properties.setProperty("security.auth.client.password", password);
    final AuthenticationClient authClient = new BasicAuthenticationClient();
    authClient.configure(properties);
    ConnectionConfig connectionConfig = getClientConfig().getConnectionConfig();
    authClient.setConnectionInfo(connectionConfig.getHostname(), connectionConfig.getPort(), false);
    checkServicesWithRetry(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return authClient.getAccessToken() != null;
        }
    }, "Unable to connect to Authentication service to obtain access token, Connection info : " + connectionConfig);
    return authClient.getAccessToken();
}
Also used : BasicAuthenticationClient(co.cask.cdap.security.authentication.client.basic.BasicAuthenticationClient) Properties(java.util.Properties) AuthenticationClient(co.cask.cdap.security.authentication.client.AuthenticationClient) BasicAuthenticationClient(co.cask.cdap.security.authentication.client.basic.BasicAuthenticationClient) ConnectionConfig(co.cask.cdap.client.config.ConnectionConfig) TimeoutException(java.util.concurrent.TimeoutException) UnauthorizedException(co.cask.cdap.security.spi.authorization.UnauthorizedException) IOException(java.io.IOException) UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException)

Aggregations

AuthenticationClient (co.cask.cdap.security.authentication.client.AuthenticationClient)20 AccessToken (co.cask.cdap.security.authentication.client.AccessToken)16 Test (org.junit.Test)15 HttpFailureException (co.cask.common.http.exception.HttpFailureException)10 BasicAuthenticationClient (co.cask.cdap.security.authentication.client.basic.BasicAuthenticationClient)4 Properties (java.util.Properties)3 ExecutionException (java.util.concurrent.ExecutionException)2 StreamClient (co.cask.cdap.client.StreamClient)1 ConnectionConfig (co.cask.cdap.client.config.ConnectionConfig)1 RestStreamClient (co.cask.cdap.client.rest.RestStreamClient)1 UnauthenticatedException (co.cask.cdap.common.UnauthenticatedException)1 Credential (co.cask.cdap.security.authentication.client.Credential)1 UnauthorizedException (co.cask.cdap.security.spi.authorization.UnauthorizedException)1 IOException (java.io.IOException)1 TimeoutException (java.util.concurrent.TimeoutException)1 ConsoleReader (jline.console.ConsoleReader)1