Search in sources :

Example 6 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig 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 7 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig 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);
    properties.setProperty("security.auth.client.verify.ssl.cert", Boolean.toString(getClientConfig().isVerifySSLCert()));
    final AuthenticationClient authClient = new BasicAuthenticationClient();
    authClient.configure(properties);
    ConnectionConfig connectionConfig = getClientConfig().getConnectionConfig();
    authClient.setConnectionInfo(connectionConfig.getHostname(), connectionConfig.getPort(), connectionConfig.isSSLEnabled());
    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(io.cdap.cdap.security.authentication.client.basic.BasicAuthenticationClient) Properties(java.util.Properties) BasicAuthenticationClient(io.cdap.cdap.security.authentication.client.basic.BasicAuthenticationClient) AuthenticationClient(io.cdap.cdap.security.authentication.client.AuthenticationClient) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) AccessException(io.cdap.cdap.api.security.AccessException) TimeoutException(java.util.concurrent.TimeoutException) UnauthorizedException(io.cdap.cdap.security.spi.authorization.UnauthorizedException) IOException(java.io.IOException) UnauthenticatedException(io.cdap.cdap.security.spi.authentication.UnauthenticatedException)

Example 8 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by cdapio.

the class ArtifactHttpHandlerTestBase method setup.

@BeforeClass
public static void setup() {
    artifactRepository = getInjector().getInstance(ArtifactRepository.class);
    systemArtifactsDir = getInjector().getInstance(CConfiguration.class).get(Constants.AppFabric.SYSTEM_ARTIFACTS_DIR);
    DiscoveryServiceClient discoveryClient = getInjector().getInstance(DiscoveryServiceClient.class);
    EndpointStrategy endpointStrategy = new RandomEndpointStrategy(() -> discoveryClient.discover(Constants.Service.METADATA_SERVICE));
    Discoverable discoverable = endpointStrategy.pick(1, TimeUnit.SECONDS);
    Assert.assertNotNull(discoverable);
    String host = discoverable.getSocketAddress().getHostName();
    int port = discoverable.getSocketAddress().getPort();
    ConnectionConfig connectionConfig = ConnectionConfig.builder().setHostname(host).setPort(port).setSSLEnabled(URIScheme.HTTPS.isMatch(discoverable)).build();
    ClientConfig clientConfig = ClientConfig.builder().setVerifySSLCert(false).setConnectionConfig(connectionConfig).build();
}
Also used : Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) EndpointStrategy(io.cdap.cdap.common.discovery.EndpointStrategy) ArtifactRepository(io.cdap.cdap.internal.app.runtime.artifact.ArtifactRepository) ClientConfig(io.cdap.cdap.client.config.ClientConfig) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) BeforeClass(org.junit.BeforeClass)

Example 9 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by cdapio.

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 10 with ConnectionConfig

use of io.cdap.cdap.client.config.ConnectionConfig in project cdap by cdapio.

the class MetadataServiceMainTest method testMetadataService.

@Test
public void testMetadataService() throws Exception {
    Injector injector = getServiceMainInstance(MetadataServiceMain.class).getInjector();
    DatasetId datasetId = NamespaceId.DEFAULT.dataset("testds");
    // Create a dataset, a metadata should get published.
    DatasetFramework datasetFramework = injector.getInstance(DatasetFramework.class);
    long beforeCreation = System.currentTimeMillis();
    datasetFramework.addInstance(KeyValueTable.class.getName(), datasetId, DatasetProperties.EMPTY);
    // Query the metadata
    DiscoveryServiceClient discoveryServiceClient = injector.getInstance(DiscoveryServiceClient.class);
    Discoverable metadataEndpoint = new RandomEndpointStrategy(() -> discoveryServiceClient.discover(Constants.Service.METADATA_SERVICE)).pick(5, TimeUnit.SECONDS);
    Assert.assertNotNull(metadataEndpoint);
    // Try to query the metadata
    InetSocketAddress metadataAddr = metadataEndpoint.getSocketAddress();
    ConnectionConfig connConfig = ConnectionConfig.builder().setSSLEnabled(URIScheme.HTTPS.isMatch(metadataEndpoint)).setHostname(metadataAddr.getHostName()).setPort(metadataAddr.getPort()).build();
    MetadataClient metadataClient = new MetadataClient(ClientConfig.builder().setVerifySSLCert(false).setConnectionConfig(connConfig).build());
    MetadataSearchResponse response = metadataClient.searchMetadata(datasetId.getNamespaceId(), "*", (String) null);
    Set<MetadataSearchResultRecord> results = response.getResults();
    Assert.assertFalse(results.isEmpty());
    long creationTime = results.stream().filter(r -> datasetId.equals(r.getEntityId())).map(MetadataSearchResultRecord::getMetadata).map(metadata -> metadata.get(MetadataScope.SYSTEM).getProperties().get(MetadataConstants.CREATION_TIME_KEY)).map(Long::parseLong).findFirst().orElse(-1L);
    // The creation time should be between the beforeCreation time and the current time
    Assert.assertTrue(creationTime >= beforeCreation);
    Assert.assertTrue(creationTime <= System.currentTimeMillis());
}
Also used : MetadataSearchResponse(io.cdap.cdap.proto.metadata.MetadataSearchResponse) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) URIScheme(io.cdap.cdap.common.discovery.URIScheme) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) DatasetFramework(io.cdap.cdap.data2.dataset2.DatasetFramework) Set(java.util.Set) Test(org.junit.Test) InetSocketAddress(java.net.InetSocketAddress) Injector(com.google.inject.Injector) DatasetProperties(io.cdap.cdap.api.dataset.DatasetProperties) TimeUnit(java.util.concurrent.TimeUnit) MetadataClient(io.cdap.cdap.client.MetadataClient) Discoverable(org.apache.twill.discovery.Discoverable) MetadataSearchResultRecord(io.cdap.cdap.proto.metadata.MetadataSearchResultRecord) DatasetId(io.cdap.cdap.proto.id.DatasetId) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) ClientConfig(io.cdap.cdap.client.config.ClientConfig) MetadataScope(io.cdap.cdap.api.metadata.MetadataScope) MetadataConstants(io.cdap.cdap.spi.metadata.MetadataConstants) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) Constants(io.cdap.cdap.common.conf.Constants) Assert(org.junit.Assert) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) Discoverable(org.apache.twill.discovery.Discoverable) DiscoveryServiceClient(org.apache.twill.discovery.DiscoveryServiceClient) InetSocketAddress(java.net.InetSocketAddress) MetadataSearchResponse(io.cdap.cdap.proto.metadata.MetadataSearchResponse) DatasetId(io.cdap.cdap.proto.id.DatasetId) DatasetFramework(io.cdap.cdap.data2.dataset2.DatasetFramework) MetadataClient(io.cdap.cdap.client.MetadataClient) MetadataSearchResultRecord(io.cdap.cdap.proto.metadata.MetadataSearchResultRecord) Injector(com.google.inject.Injector) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) ConnectionConfig(io.cdap.cdap.client.config.ConnectionConfig) RandomEndpointStrategy(io.cdap.cdap.common.discovery.RandomEndpointStrategy) Test(org.junit.Test)

Aggregations

ConnectionConfig (io.cdap.cdap.client.config.ConnectionConfig)36 ClientConfig (io.cdap.cdap.client.config.ClientConfig)14 RandomEndpointStrategy (io.cdap.cdap.common.discovery.RandomEndpointStrategy)10 Discoverable (org.apache.twill.discovery.Discoverable)10 IOException (java.io.IOException)8 NotFoundException (io.cdap.cdap.common.NotFoundException)6 EndpointStrategy (io.cdap.cdap.common.discovery.EndpointStrategy)6 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)6 InetSocketAddress (java.net.InetSocketAddress)6 URI (java.net.URI)6 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)6 Injector (com.google.inject.Injector)4 DatasetProperties (io.cdap.cdap.api.dataset.DatasetProperties)4 CLIConnectionConfig (io.cdap.cdap.cli.CLIConnectionConfig)4 CsvTableRenderer (io.cdap.cdap.cli.util.table.CsvTableRenderer)4 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)4 URIScheme (io.cdap.cdap.common.discovery.URIScheme)4 RetryStrategy (io.cdap.cdap.common.service.RetryStrategy)4 AccessToken (io.cdap.cdap.security.authentication.client.AccessToken)4 UnauthenticatedException (io.cdap.cdap.security.spi.authentication.UnauthenticatedException)4