Search in sources :

Example 11 with AuthenticationClient

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

the class CLIConfig method getAuthenticationClient.

private AuthenticationClient getAuthenticationClient(ConnectionConfig connectionInfo) {
    AuthenticationClient authenticationClient = new BasicAuthenticationClient();
    authenticationClient.setConnectionInfo(connectionInfo.getHostname(), connectionInfo.getPort(), connectionInfo.isSSLEnabled());
    return authenticationClient;
}
Also used : BasicAuthenticationClient(co.cask.cdap.security.authentication.client.basic.BasicAuthenticationClient) AuthenticationClient(co.cask.cdap.security.authentication.client.AuthenticationClient) BasicAuthenticationClient(co.cask.cdap.security.authentication.client.basic.BasicAuthenticationClient)

Example 12 with AuthenticationClient

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

the class CLIConfig method getNewAccessToken.

private UserAccessToken getNewAccessToken(ConnectionConfig connectionInfo, PrintStream output, boolean debug) throws IOException {
    AuthenticationClient authenticationClient = getAuthenticationClient(connectionInfo);
    Properties properties = new Properties();
    properties.put(BasicAuthenticationClient.VERIFY_SSL_CERT_PROP_NAME, String.valueOf(clientConfig.isVerifySSLCert()));
    String username = "";
    // obtain new access token via manual user input
    output.printf("Authentication is enabled in the CDAP instance: %s.\n", connectionInfo.getHostname());
    ConsoleReader reader = new ConsoleReader();
    for (Credential credential : authenticationClient.getRequiredCredentials()) {
        String prompt = "Please, specify " + credential.getDescription() + "> ";
        String credentialValue;
        if (credential.isSecret()) {
            credentialValue = reader.readLine(prompt, '*');
        } else {
            credentialValue = reader.readLine(prompt);
        }
        properties.put(credential.getName(), credentialValue);
        if (credential.getName().contains("username")) {
            username = credentialValue;
        }
    }
    authenticationClient.configure(properties);
    AccessToken accessToken = authenticationClient.getAccessToken();
    UserAccessToken userToken = new UserAccessToken(accessToken, username);
    if (accessToken != null) {
        if (saveAccessToken(userToken, connectionInfo.getHostname()) && debug) {
            output.printf("Saved access token to %s\n", getAccessTokenFile(connectionInfo.getHostname()).getAbsolutePath());
        }
    }
    return userToken;
}
Also used : Credential(co.cask.cdap.security.authentication.client.Credential) ConsoleReader(jline.console.ConsoleReader) AccessToken(co.cask.cdap.security.authentication.client.AccessToken) AuthenticationClient(co.cask.cdap.security.authentication.client.AuthenticationClient) BasicAuthenticationClient(co.cask.cdap.security.authentication.client.basic.BasicAuthenticationClient) Properties(java.util.Properties)

Example 13 with AuthenticationClient

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

the class CdapFlumeIT method createStream.

private void createStream() throws Exception {
    RestStreamClient.Builder builder = RestStreamClient.builder(streamReader.getCdapHost(), streamReader.getCdapPort()).ssl(streamReader.getSsl());
    if (streamReader.getAuthClientPropertiesPath() != null) {
        AuthenticationClient authClient = streamReader.createAuthClient();
        builder.authClient(authClient);
    }
    StreamClient streamClient = builder.build();
    streamClient.create(streamName);
}
Also used : StreamClient(co.cask.cdap.client.StreamClient) RestStreamClient(co.cask.cdap.client.rest.RestStreamClient) RestStreamClient(co.cask.cdap.client.rest.RestStreamClient) AuthenticationClient(co.cask.cdap.security.authentication.client.AuthenticationClient)

Example 14 with AuthenticationClient

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

the class RestStreamClientTest method testNotAuthorizedUnknownTokenSetTTL.

@Test
public void testNotAuthorizedUnknownTokenSetTTL() 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("test");
    Mockito.when(accessToken.getTokenType()).thenReturn("Bearer");
    createClient(authClient);
    try {
        streamClient.setTTL(TestUtils.AUTH_STREAM_NAME, STREAM_TTL);
        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 15 with AuthenticationClient

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

the class RestStreamClientTest method testNotAuthorizedEmptyTokenTruncate.

@Test
public void testNotAuthorizedEmptyTokenTruncate() 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.truncate(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)

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