<commit_before>/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.integration.ldap;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.junit.annotations.Network;
import org.junit.BeforeClass;

/**
 * This tests that configurations that contain two AD realms work correctly.
 * The required behaviour is that users from both realms (directory servers) can be authenticated using
 * just their userid (the AuthenticationService tries them in order)
 */
@Network
public class MultipleAdRealmTests extends AbstractAdLdapRealmTestCase {

    private static RealmConfig secondaryRealmConfig;

    @BeforeClass
    public static void setupSecondaryRealm() {
        // Pick a secondary realm that has the inverse value for 'loginWithCommonName' compare with the primary realm
        final List<RealmConfig> configs = Arrays.stream(RealmConfig.values())
                .filter(config -> config.loginWithCommonName != AbstractAdLdapRealmTestCase.realmConfig.loginWithCommonName)
                .collect(Collectors.toList());
        secondaryRealmConfig = randomFrom(configs);
        ESLoggerFactory.getLogger("test")
                .info("running test with secondary realm configuration [{}], with direct group to role mapping [{}]. Settings [{}]",
                        secondaryRealmConfig, secondaryRealmConfig.mapGroupsAsRoles, secondaryRealmConfig.settings.getAsMap());

        // It's easier to test 2 realms when using file based role mapping, and for the purposes of
        // this test, there's no need to test native mappings.
        AbstractAdLdapRealmTestCase.roleMappings = Stream.concat(
                realmConfig.selectRoleMappings(() -> true).stream(),
                secondaryRealmConfig.selectRoleMappings(() -> true).stream()
        ).distinct().collect(Collectors.toList());
    }

    @Override
    protected Settings nodeSettings(int nodeOrdinal) {
        Settings.Builder builder = Settings.builder();
        builder.put(super.nodeSettings(nodeOrdinal));

        Path store = getDataPath(TESTNODE_KEYSTORE);
        final Settings secondarySettings = super.buildRealmSettings(secondaryRealmConfig, store);
        secondarySettings.getAsMap().forEach((name, value) -> {
            name = name.replace(XPACK_SECURITY_AUTHC_REALMS_EXTERNAL, XPACK_SECURITY_AUTHC_REALMS_EXTERNAL + "2");
            builder.put(name, value);
        });

        return builder.build();
    }<commit_msg>[TESTS] Really fix MultipleAdRealmTests (I think)<commit_after>/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License;
 * you may not use this file except in compliance with the Elastic License.
 */
package org.elasticsearch.integration.ldap;

import java.io.IOException;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.elasticsearch.common.logging.ESLoggerFactory;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.test.junit.annotations.Network;
import org.junit.BeforeClass;

/**
 * This tests that configurations that contain two AD realms work correctly.
 * The required behaviour is that users from both realms (directory servers) can be authenticated using
 * just their userid (the AuthenticationService tries them in order)
 */
@Network
public class MultipleAdRealmTests extends AbstractAdLdapRealmTestCase {

    private static RealmConfig secondaryRealmConfig;

    @BeforeClass
    public static void setupSecondaryRealm() {
        // Pick a secondary realm that has the inverse value for 'loginWithCommonName' compare with the primary realm
        final List<RealmConfig> configs = Arrays.stream(RealmConfig.values())
                .filter(config -> config.loginWithCommonName != AbstractAdLdapRealmTestCase.realmConfig.loginWithCommonName)
                .filter(config -> config.name().startsWith("AD"))
                .collect(Collectors.toList());
        secondaryRealmConfig = randomFrom(configs);
        ESLoggerFactory.getLogger("test")
                .info("running test with secondary realm configuration [{}], with direct group to role mapping [{}]. Settings [{}]",
                        secondaryRealmConfig, secondaryRealmConfig.mapGroupsAsRoles, secondaryRealmConfig.settings.getAsMap());

        // It's easier to test 2 realms when using file based role mapping, and for the purposes of
        // this test, there's no need to test native mappings.
        AbstractAdLdapRealmTestCase.roleMappings = Stream.concat(
                realmConfig.selectRoleMappings(() -> true).stream(),
                secondaryRealmConfig.selectRoleMappings(() -> true).stream()
        ).distinct().collect(Collectors.toList());
    }

    @Override
    protected Settings nodeSettings(int nodeOrdinal) {
        Settings.Builder builder = Settings.builder();
        builder.put(super.nodeSettings(nodeOrdinal));

        Path store = getDataPath(TESTNODE_KEYSTORE);
        final Settings secondarySettings = super.buildRealmSettings(secondaryRealmConfig, store);
        secondarySettings.getAsMap().forEach((name, value) -> {
            name = name.replace(XPACK_SECURITY_AUTHC_REALMS_EXTERNAL, XPACK_SECURITY_AUTHC_REALMS_EXTERNAL + "2");
            builder.put(name, value);
        });

        return builder.build();
    }
{'fraction_non_alphanumeric': 0.08964009418096199, 'fraction_numerical': 0.0006727211570803902, 'mean_word_length': 3.6208236208236206, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '262485', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1522, 'n_words': 429}