<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.phoenix.jdbc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.CommonConfigurationKeys;
import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.minikdc.MiniKdc;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.authentication.util.KerberosName;
import org.apache.hadoop.security.authentication.util.KerberosUtil;
import org.apache.phoenix.jdbc.PhoenixEmbeddedDriver.ConnectionInfo;
import org.apache.phoenix.query.ConfigurationFactory;
import org.apache.phoenix.util.InstanceResolver;
import org.apache.phoenix.util.PhoenixRuntime;
import org.apache.phoenix.util.ReadOnlyProps;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Tests ConnectionQueryServices caching when Kerberos authentication is enabled. It's not
 * trivial to directly test this, so we exploit the knowledge that the caching is driven by
 * a ConcurrentHashMap. We can use a HashSet to determine when instances of ConnectionInfo
 * collide and when they do not.
 */
public class SecureUserConnectionsTest {
    private static final File TEMP_DIR = new File(getClassTempDir());
    private static final File KEYTAB_DIR = new File(TEMP_DIR, "keytabs");
    private static final File KDC_DIR = new File(TEMP_DIR, "kdc");
    private static final List<File> USER_KEYTAB_FILES = new ArrayList<>();
    private static final int NUM_USERS = 3;
    private static final Properties EMPTY_PROPERTIES = new Properties();
    private static final String BASE_URL = PhoenixRuntime.JDBC_PROTOCOL + ":localhost:2181";

    private static MiniKdc KDC;

    @BeforeClass
    public static void setupKdc() throws Exception {
        ensureIsEmptyDirectory(KDC_DIR);
        ensureIsEmptyDirectory(KEYTAB_DIR);
        // Create and start the KDC
        Properties kdcConf = MiniKdc.createConf();
        kdcConf.put(MiniKdc.DEBUG, true);
        KDC = new MiniKdc(kdcConf, KDC_DIR);
        KDC.start();<commit_msg>PHOENIX-3287 Retry starting KDC on BindException<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to you under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.phoenix.jdbc;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.security.PrivilegedExceptionAction;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.CommonConfigurationKeys;
import org.apache.hadoop.hbase.security.User;
import org.apache.hadoop.minikdc.MiniKdc;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.security.authentication.util.KerberosName;
import org.apache.phoenix.jdbc.PhoenixEmbeddedDriver.ConnectionInfo;
import org.apache.phoenix.query.ConfigurationFactory;
import org.apache.phoenix.util.InstanceResolver;
import org.apache.phoenix.util.PhoenixRuntime;
import org.apache.phoenix.util.ReadOnlyProps;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * Tests ConnectionQueryServices caching when Kerberos authentication is enabled. It's not
 * trivial to directly test this, so we exploit the knowledge that the caching is driven by
 * a ConcurrentHashMap. We can use a HashSet to determine when instances of ConnectionInfo
 * collide and when they do not.
 */
public class SecureUserConnectionsTest {
    private static final Log LOG = LogFactory.getLog(SecureUserConnectionsTest.class); 
    private static final int KDC_START_ATTEMPTS = 10;

    private static final File TEMP_DIR = new File(getClassTempDir());
    private static final File KEYTAB_DIR = new File(TEMP_DIR, "keytabs");
    private static final File KDC_DIR = new File(TEMP_DIR, "kdc");
    private static final List<File> USER_KEYTAB_FILES = new ArrayList<>();
    private static final int NUM_USERS = 3;
    private static final Properties EMPTY_PROPERTIES = new Properties();
    private static final String BASE_URL = PhoenixRuntime.JDBC_PROTOCOL + ":localhost:2181";

    private static MiniKdc KDC;

    @BeforeClass
    public static void setupKdc() throws Exception {
        ensureIsEmptyDirectory(KDC_DIR);
        ensureIsEmptyDirectory(KEYTAB_DIR);
        // Create and start the KDC. MiniKDC appears to have a race condition in how it does
        // port allocation (with apache-ds). See PHOENIX-3287.
        boolean started = false;
        for (int i = 0; !started && i < KDC_START_ATTEMPTS; i++) {
            Properties kdcConf = MiniKdc.createConf();
            kdcConf.put(MiniKdc.DEBUG, true);
            KDC = new MiniKdc(kdcConf, KDC_DIR);
            try {
                KDC.start();
                started = true;
            } catch (Exception e) {
                LOG.warn("PHOENIX-3287: Failed to start KDC, retrying..", e);
            }
        }
        assertTrue("The embedded KDC failed to start successfully after " + KDC_START_ATTEMPTS
                + " attempts.", started);
{'fraction_non_alphanumeric': 0.08145604395604396, 'fraction_numerical': 0.004532967032967033, 'mean_word_length': 4.123856439127375, 'pattern_counts': {'":': 2, '<': 8, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, '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': '6038052', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1998, 'n_words': 670}