Java Code Examples for com.datastax.oss.driver.api.core.CqlSessionBuilder#withAuthCredentials()

The following examples show how to use com.datastax.oss.driver.api.core.CqlSessionBuilder#withAuthCredentials() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CassandraManager.java    From dynamo-cassandra-proxy with Apache License 2.0 5 votes vote down vote up
public void start() {
    logger.info("Contact points {}", config.getContactPoints());

    CqlSessionBuilder builder = CqlSession.builder()
            .addContactPoints(Arrays.stream(config.getContactPoints().split(","))
                    .map(s -> new InetSocketAddress(s, config.getCqlPort()))
                    .collect(Collectors.toList()))
            .withLocalDatacenter(config.getLocalDC());

    if (config.getCqlUserName() != null)
        builder.withAuthCredentials(config.getCqlUserName(), config.getCqlPassword());

    if (config.isDockerCassandra()) {
        logger.info("Docker cassandra enabled in the yaml.");
        logger.info("Attempting to stand up container.");
        DockerHelper dh = new DockerHelper();
        dh.startDSE();

        //TODO
        try {
            Thread.sleep(60000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }

    session = builder.build();


    logger.info("Preparing statements for " + CassandraManager.class.getSimpleName());
    stmts = new CassandraStatements.Prepared(session, config.getKeyspaceName(), config.getReplicationStrategy());

    refreshSchema();
}
 
Example 2
Source File: CassandraDataStoreConnector.java    From egeria with Apache License 2.0 5 votes vote down vote up
/**
 * Set up the Cassandra Cluster Connection with schema change listener to track the metadata changes
 *
 * @param schemaChangeListener the schema change listener
 */
public void startCassandraConnection(SchemaChangeListener schemaChangeListener) {

    String actionDescription = "start Cassandra Data Store Connection";
    try {
        CqlSessionBuilder builder = CqlSession.builder();
        builder.addContactPoint(new InetSocketAddress(serverAddresses, Integer.valueOf(port)));
        builder.withSchemaChangeListener(schemaChangeListener);
        builder.withAuthCredentials(username, password);
        this.cqlSession = builder.build();

    } catch (ExceptionInInitializerError e) {
        auditLog = CassandraDataStoreAuditCode.CONNECTOR_SERVER_CONNECTION_ERROR;
        omrsAuditLog.logRecord(actionDescription,
                auditLog.getLogMessageId(),
                auditLog.getSeverity(),
                auditLog.getFormattedLogMessage(),
                null,
                auditLog.getSystemAction(),
                auditLog.getUserAction());
    }
    auditLog = CassandraDataStoreAuditCode.CONNECTOR_INITIALIZED;
    omrsAuditLog.logRecord(actionDescription,
            auditLog.getLogMessageId(),
            auditLog.getSeverity(),
            auditLog.getFormattedLogMessage(),
            null,
            auditLog.getSystemAction(),
            auditLog.getUserAction());
}
 
Example 3
Source File: CassandraInterpreter.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public void open() {
  final String[] addresses = getProperty(CASSANDRA_HOSTS, DEFAULT_HOST)
          .trim().split(",");
  final int port = parseInt(getProperty(CASSANDRA_PORT, DEFAULT_PORT));
  Collection<InetSocketAddress> hosts = new ArrayList<>();
  for (String address : addresses) {
    if (!StringUtils.isBlank(address)) {
      logger.debug("Adding contact point: {}", address);
      if (InetAddresses.isInetAddress(address)) {
        hosts.add(new InetSocketAddress(address, port));
      } else {
        hosts.add(InetSocketAddress.createUnresolved(address, port));
      }
    }
  }

  LOGGER.info("Bootstrapping Cassandra Java Driver to connect to {} on port {}",
          getProperty(CASSANDRA_HOSTS), port);

  DriverConfigLoader loader = createLoader();

  LOGGER.debug("Creating cluster builder");
  CqlSessionBuilder clusterBuilder = CqlSession.builder()
          .withApplicationName("Zeppelin")
          .withApplicationVersion("");
  if (!hosts.isEmpty()) {
    LOGGER.debug("Adding contact points");
    clusterBuilder.addContactPoints(hosts);
  }

  String username = getProperty(CASSANDRA_CREDENTIALS_USERNAME, NONE_VALUE).trim();
  String password = getProperty(CASSANDRA_CREDENTIALS_PASSWORD, NONE_VALUE).trim();
  if (StringUtils.isNotBlank(username) && StringUtils.isNotBlank(password) &&
          !NONE_VALUE.equalsIgnoreCase(username) && !NONE_VALUE.equalsIgnoreCase(password)) {
    LOGGER.debug("Adding credentials. Username = {}", username);
    clusterBuilder.withAuthCredentials(username, password);
  }

  String keyspace = getProperty(CASSANDRA_KEYSPACE_NAME, DEFAULT_KEYSPACE);
  if (StringUtils.isNotBlank(keyspace) && !DEFAULT_KEYSPACE.equalsIgnoreCase(keyspace)) {
    LOGGER.debug("Set default keyspace");
    clusterBuilder.withKeyspace(keyspace);
  }

  final String runWithSSL = getProperty(CASSANDRA_WITH_SSL, "false");
  if ("true".equalsIgnoreCase(runWithSSL)) {
    LOGGER.debug("Using SSL");
    try {
      final SSLContext sslContext;
      {
        final KeyStore trustStore = KeyStore.getInstance("JKS");
        final InputStream stream = Files.newInputStream(Paths.get(
                getProperty(CASSANDRA_TRUSTSTORE_PATH)));
        trustStore.load(stream, getProperty(CASSANDRA_TRUSTSTORE_PASSWORD).toCharArray());

        final TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(
                TrustManagerFactory.getDefaultAlgorithm());
        trustManagerFactory.init(trustStore);

        sslContext = SSLContext.getInstance("TLS");
        sslContext.init(null, trustManagerFactory.getTrustManagers(), null);
      }
      clusterBuilder = clusterBuilder.withSslContext(sslContext);
    } catch (Exception e) {
      LOGGER.error("Exception initializing SSL {}", e.toString());
    }
  } else {
    LOGGER.debug("Not using SSL");
  }

  LOGGER.debug("Creating CqlSession");
  session = clusterBuilder.withConfigLoader(loader).build();
  LOGGER.debug("Session configuration");
  for (Map.Entry<String, Object> entry:
          session.getContext().getConfig().getDefaultProfile().entrySet()) {
    logger.debug("{} = {}", entry.getKey(), entry.getValue().toString());
  }
  LOGGER.debug("Creating helper");
  helper = new InterpreterLogic(session, properties);
}