Search in sources :

Example 11 with AccessToken

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

the class UpgradeTool method getClientConfig.

private static ClientConfig getClientConfig(CommandLine commandLine) throws IOException {
    String uriStr = commandLine.hasOption("u") ? commandLine.getOptionValue("u") : "localhost:11015";
    if (!uriStr.contains("://")) {
        uriStr = "http://" + uriStr;
    }
    URI uri = URI.create(uriStr);
    String hostname = uri.getHost();
    int port = uri.getPort();
    boolean sslEnabled = "https".equals(uri.getScheme());
    ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(hostname).setPort(port).setSSLEnabled(sslEnabled).build();
    int readTimeout = commandLine.hasOption("t") ? Integer.parseInt(commandLine.getOptionValue("t")) : DEFAULT_READ_TIMEOUT_MILLIS;
    ClientConfig.Builder clientConfigBuilder = ClientConfig.builder().setDefaultReadTimeout(readTimeout).setConnectionConfig(connectionConfig);
    if (commandLine.hasOption("a")) {
        String tokenFilePath = commandLine.getOptionValue("a");
        File tokenFile = new File(tokenFilePath);
        if (!tokenFile.exists()) {
            throw new IllegalArgumentException("Access token file " + tokenFilePath + " does not exist.");
        }
        if (!tokenFile.isFile()) {
            throw new IllegalArgumentException("Access token file " + tokenFilePath + " is not a file.");
        }
        String tokenValue = new String(Files.readAllBytes(tokenFile.toPath()), StandardCharsets.UTF_8).trim();
        AccessToken accessToken = new AccessToken(tokenValue, 82000L, "Bearer");
        clientConfigBuilder.setAccessToken(accessToken);
    }
    return clientConfigBuilder.build();
}
Also used : AccessToken(io.cdap.cdap.security.authentication.client.AccessToken) ClientConfig(io.cdap.cdap.client.config.ClientConfig) URI(java.net.URI) File(java.io.File) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig)

Example 12 with AccessToken

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

the class IntegrationTestManager method getQueryClient.

@Override
public Connection getQueryClient(NamespaceId namespace) throws Exception {
    Map<String, String> connParams = new HashMap<>();
    connParams.put(ExploreConnectionParams.Info.NAMESPACE.getName(), namespace.getNamespace());
    AccessToken accessToken = clientConfig.getAccessToken();
    if (accessToken != null) {
        connParams.put(ExploreConnectionParams.Info.EXPLORE_AUTH_TOKEN.getName(), accessToken.getValue());
    }
    connParams.put(ExploreConnectionParams.Info.SSL_ENABLED.getName(), Boolean.toString(clientConfig.getConnectionConfig().isSSLEnabled()));
    connParams.put(ExploreConnectionParams.Info.VERIFY_SSL_CERT.getName(), Boolean.toString(clientConfig.isVerifySSLCert()));
    ConnectionConfig connConfig = clientConfig.getConnectionConfig();
    String url = String.format("%s%s:%d?%s", Constants.Explore.Jdbc.URL_PREFIX, connConfig.getHostname(), connConfig.getPort(), Joiner.on("&").withKeyValueSeparator("=").join(connParams));
    return new ExploreDriver().connect(url, new Properties());
}
Also used : ExploreDriver(io.cdap.cdap.explore.jdbc.ExploreDriver) HashMap(java.util.HashMap) AccessToken(io.cdap.cdap.security.authentication.client.AccessToken) DatasetProperties(io.cdap.cdap.api.dataset.DatasetProperties) Properties(java.util.Properties) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig)

Example 13 with AccessToken

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

the class RESTClientTest method testPostUnauthorizedWithAccessToken.

@Test(expected = UnauthenticatedException.class)
public void testPostUnauthorizedWithAccessToken() throws Exception {
    URL url = getBaseURI().resolve("/api/testPostAuth").toURL();
    HttpRequest request = HttpRequest.post(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 14 with AccessToken

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

the class RESTClientTest method testGetSuccessWithAccessToken.

@Test
public void testGetSuccessWithAccessToken() throws Exception {
    URL url = getBaseURI().resolve("/api/testGetAuth").toURL();
    HttpRequest request = HttpRequest.get(url).build();
    HttpResponse response = restClient.execute(request, new AccessToken(ACCESS_TOKEN, 82000L, "Bearer"));
    verifyResponse(response, only(200), any(), only("Access token received: " + ACCESS_TOKEN));
}
Also used : FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) HttpRequest(io.cdap.common.http.HttpRequest) AccessToken(io.cdap.cdap.security.authentication.client.AccessToken) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) Test(org.junit.Test)

Example 15 with AccessToken

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

the class RESTClientTest method testBody.

@Test
public void testBody() throws Exception {
    URL url = getBaseURI().resolve("/api/testCount").toURL();
    HttpResponse response = restClient.execute(HttpMethod.POST, url, "increment", null, new AccessToken(ACCESS_TOKEN, 82000L, "Bearer"), 200);
    Assert.assertEquals("1", response.getResponseBodyAsString());
    response = restClient.execute(HttpMethod.POST, url, "increment", null, new AccessToken(ACCESS_TOKEN, 82000L, "Bearer"), 200);
    Assert.assertEquals("2", response.getResponseBodyAsString());
    response = restClient.execute(HttpMethod.POST, url, "decrement", null, new AccessToken(ACCESS_TOKEN, 82000L, "Bearer"), 200);
    Assert.assertEquals("1", response.getResponseBodyAsString());
    response = restClient.execute(HttpMethod.POST, url, "decrement", null, new AccessToken(ACCESS_TOKEN, 82000L, "Bearer"), 200);
    Assert.assertEquals("0", response.getResponseBodyAsString());
}
Also used : AccessToken(io.cdap.cdap.security.authentication.client.AccessToken) HttpResponse(io.cdap.common.http.HttpResponse) 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