<commit_before>/**
 * Copyright (C) 2016 DataStax, Inc.
 *
 * Please see the license for details:
 * http://www.datastax.com/terms/datastax-dse-driver-license-terms
 */
'use strict';
const assert = require('assert');
const helper = require('../../../test-helper');
const DseGssapiAuthProvider = require('../../../../lib/auth/dse-gssapi-auth-provider');
const Client = require('../../../../lib/dse-client');
const ads = helper.ads;
const cDescribe = helper.conditionalDescribe(helper.requireOptional('kerberos'), 'kerberos required to run');

cDescribe('DseGssapiAuthProvider', function () {
  this.timeout(180000);
  before(function (done) {
    ads.start(function(err) {
      assert.ifError(err);
      ads.acquireTicket('cassandra', '<EMAIL>', done);
    });
  });
  after(ads.stop.bind(ads));
  it('should authenticate against DSE instance using Kerberos', function (done) {
    const v5 = helper.versionCompare(helper.getDseVersion(), '5.0');
    // Set authenticator based on DSE version.
    const authenticator = v5 ?
      'authenticator:com.datastax.bdp.cassandra.auth.DseAuthenticator' :
      'authenticator:com.datastax.bdp.cassandra.auth.KerberosAuthenticator';

    const yamlOptions = [
      'kerberos_options.keytab:' + ads.getKeytabPath('dse'),
      'kerberos_options.service_principal:dse/_HOST@DATASTAX.COM',
      'kerberos_options.http_principal:dse/_HOST@DATASTAX.COM',
      'kerberos_options.qop:auth'
    ];

    // add DSE 5.0 specific options required to enable kerberos.
    if(v5) {
      yamlOptions.concat(
        'authentication_options.enabled:true',
        'authentication_options.default_scheme:kerberos'
      );
    }

    const testClusterOptions = {
      yaml: authenticator,
      dseYaml: yamlOptions,
      jvmArgs: ['-Dcassandra.superuser_setup_delay_ms=0', '-Djava.security.krb5.conf=' + ads.getKrb5ConfigPath()]
    };

    helper.ccm.startAll(1, testClusterOptions, function (err) {
      assert.ifError(err);
      const authProvider = new DseGssapiAuthProvider();
      const clientOptions = helper.getOptions({ authProvider: authProvider });
      helper.connectAndQuery(new Client(clientOptions), done);
    });
  });
});<commit_msg>Test auth: fail when authProvider is not set and kerberos is enabled<commit_after>/**
 * Copyright (C) 2016 DataStax, Inc.
 *
 * Please see the license for details:
 * http://www.datastax.com/terms/datastax-dse-driver-license-terms
 */
'use strict';
const assert = require('assert');
const helper = require('../../../test-helper');
const DseGssapiAuthProvider = require('../../../../lib/auth/dse-gssapi-auth-provider');
const Client = require('../../../../lib/dse-client');
const errors = require('../../../../lib/errors');
const ads = helper.ads;
const cDescribe = helper.conditionalDescribe(helper.requireOptional('kerberos'), 'kerberos required to run');

cDescribe('DseGssapiAuthProvider', function () {
  this.timeout(180000);
  before(function (done) {
    ads.start(function(err) {
      assert.ifError(err);
      ads.acquireTicket('cassandra', '<EMAIL>', done);
    });
  });

  before(done => {

    const v5 = helper.versionCompare(helper.getDseVersion(), '5.0');
    // Set authenticator based on DSE version.
    const authenticator = v5 ?
      'authenticator:com.datastax.bdp.cassandra.auth.DseAuthenticator' :
      'authenticator:com.datastax.bdp.cassandra.auth.KerberosAuthenticator';

    const yamlOptions = [
      'kerberos_options.keytab:' + ads.getKeytabPath('dse'),
      'kerberos_options.service_principal:dse/_HOST@DATASTAX.COM',
      'kerberos_options.http_principal:dse/_HOST@DATASTAX.COM',
      'kerberos_options.qop:auth'
    ];

    // add DSE 5.0 specific options required to enable kerberos.
    if(v5) {
      yamlOptions.push(
        'authentication_options.enabled:true',
        'authentication_options.default_scheme:kerberos'
      );
    }

    const testClusterOptions = {
      yaml: authenticator,
      dseYaml: yamlOptions,
      jvmArgs: ['-Dcassandra.superuser_setup_delay_ms=0', '-Djava.security.krb5.conf=' + ads.getKrb5ConfigPath()]
    };

    helper.ccm.startAll(1, testClusterOptions, done);
  });

  after(ads.stop.bind(ads));

  it('should authenticate against DSE instance using Kerberos', done => {
    const authProvider = new DseGssapiAuthProvider();
    const clientOptions = helper.getOptions({ authProvider: authProvider });
    helper.connectAndQuery(new Client(clientOptions), done);
  });

  it('should fail with auth errors when authProvider is undefined', () => {
    const clientOptions = helper.getOptions({ authProvider: undefined });
    const client = new Client(clientOptions);
    let catchCalled = false;

    return client.connect()
      .catch(err => {
        catchCalled = true;
        helper.assertInstanceOf(err, errors.NoHostAvailableError);
        const addresses = Object.keys(err.innerErrors);
        assert.strictEqual(addresses.length, 1);
        helper.assertInstanceOf(err.innerErrors[addresses[0]], errors.AuthenticationError);
      })
      .then(() => assert.strictEqual(catchCalled, true));
  });
});
{'fraction_non_alphanumeric': 0.1476536422540742, 'fraction_numerical': 0.008639308855291577, 'mean_word_length': 3.6648351648351647, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7319683', 'n_tokens_mistral': 1641, 'n_tokens_neox': 1604, 'n_words': 288}