Search in sources :

Example 1 with AccessToken

use of io.cdap.cdap.security.authentication.client.AccessToken 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(io.cdap.cdap.security.authentication.client.Credential) ConsoleReader(jline.console.ConsoleReader) AccessToken(io.cdap.cdap.security.authentication.client.AccessToken) BasicAuthenticationClient(io.cdap.cdap.security.authentication.client.basic.BasicAuthenticationClient) AuthenticationClient(io.cdap.cdap.security.authentication.client.AuthenticationClient) Properties(java.util.Properties)

Example 2 with AccessToken

use of io.cdap.cdap.security.authentication.client.AccessToken in project cdap by caskdata.

the class CLIConfig method tryConnect.

public void tryConnect(CLIConnectionConfig connectionConfig, boolean verifySSLCert, PrintStream output, boolean debug) throws Exception {
    try {
        clientConfig.setVerifySSLCert(verifySSLCert);
        UserAccessToken userToken = acquireAccessToken(clientConfig, connectionConfig, output, debug);
        AccessToken accessToken = null;
        if (userToken != null) {
            accessToken = userToken.getAccessToken();
            connectionConfig = new CLIConnectionConfig(connectionConfig, connectionConfig.getNamespace(), userToken.getUsername());
        }
        checkConnection(clientConfig, connectionConfig, accessToken);
        setConnectionConfig(connectionConfig);
        clientConfig.setAccessToken(accessToken);
        output.printf("Successfully connected to CDAP instance at %s", connectionConfig.getURI().toString());
        output.println();
    } catch (IOException e) {
        throw new IOException(String.format("CDAP instance at '%s' could not be reached: %s", connectionConfig.getURI().toString(), e.getMessage()), e);
    }
}
Also used : AccessToken(io.cdap.cdap.security.authentication.client.AccessToken) IOException(java.io.IOException)

Example 3 with AccessToken

use of io.cdap.cdap.security.authentication.client.AccessToken in project cdap by caskdata.

the class RESTClientTest method testDeleteForbidden.

@Test(expected = UnauthorizedException.class)
public void testDeleteForbidden() throws Exception {
    URL url = getBaseURI().resolve("/api/testDeleteForbidden").toURL();
    HttpRequest request = HttpRequest.delete(url).build();
    restClient.execute(request, new AccessToken("Unknown", 82000L, "Bearer"));
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequest(io.cdap.common.http.HttpRequest) AccessToken(io.cdap.cdap.security.authentication.client.AccessToken) URL(java.net.URL) Test(org.junit.Test)

Example 4 with AccessToken

use of io.cdap.cdap.security.authentication.client.AccessToken in project cdap by caskdata.

the class RESTClientTest method testPutForbidden.

@Test(expected = UnauthorizedException.class)
public void testPutForbidden() throws Exception {
    URL url = getBaseURI().resolve("/api/testPutForbidden").toURL();
    HttpRequest request = HttpRequest.put(url).build();
    restClient.execute(request, new AccessToken("Unknown", 82000L, "Bearer"));
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequest(io.cdap.common.http.HttpRequest) AccessToken(io.cdap.cdap.security.authentication.client.AccessToken) URL(java.net.URL) Test(org.junit.Test)

Example 5 with AccessToken

use of io.cdap.cdap.security.authentication.client.AccessToken in project cdap by caskdata.

the class RESTClientTest method testPutUnauthorizedWithAccessToken.

@Test(expected = UnauthenticatedException.class)
public void testPutUnauthorizedWithAccessToken() throws Exception {
    URL url = getBaseURI().resolve("/api/testPutAuth").toURL();
    HttpRequest request = HttpRequest.put(url).build();
    restClient.execute(request, new AccessToken("Unknown", 82000L, "Bearer"));
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequest(io.cdap.common.http.HttpRequest) AccessToken(io.cdap.cdap.security.authentication.client.AccessToken) URL(java.net.URL) Test(org.junit.Test)

Aggregations

AccessToken (io.cdap.cdap.security.authentication.client.AccessToken)18 URL (java.net.URL)14 Test (org.junit.Test)14 HttpRequest (io.cdap.common.http.HttpRequest)13 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)13 HttpResponse (io.cdap.common.http.HttpResponse)6 ConnectionConfig (io.cdap.cdap.client.config.ConnectionConfig)2 Properties (java.util.Properties)2 DatasetProperties (io.cdap.cdap.api.dataset.DatasetProperties)1 ClientConfig (io.cdap.cdap.client.config.ClientConfig)1 ExploreDriver (io.cdap.cdap.explore.jdbc.ExploreDriver)1 AuthenticationClient (io.cdap.cdap.security.authentication.client.AuthenticationClient)1 Credential (io.cdap.cdap.security.authentication.client.Credential)1 BasicAuthenticationClient (io.cdap.cdap.security.authentication.client.basic.BasicAuthenticationClient)1 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 HashMap (java.util.HashMap)1 ConsoleReader (jline.console.ConsoleReader)1