<commit_before>/*
 * Copyright 2017 Red Hat Inc.
 *
 * Licensed 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 io.enmasse.systemtest.auth;

import io.enmasse.systemtest.*;
import io.enmasse.systemtest.amqp.AmqpClient;
import io.enmasse.systemtest.mqtt.MqttClient;
import org.apache.qpid.proton.message.Message;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class AuthenticationTest extends TestBase {

    private List<AddressSpace> addressSpaces = new ArrayList<>();
    private static final List<Destination> amqpAddressList = Arrays.asList(
            Destination.queue("auth-queue"),
            Destination.topic("auth-topic"),
            Destination.anycast("auth-anycast"),
            Destination.multicast("auth-multicast"));
    private static final String mqttAddress = "t1";
    private static final String anonymousUser = "anonymous";
    private static final String anonymousPswd = "anonymous";

    @Before
    public void setupSpaceList() throws Exception {
        addressSpaces = new ArrayList<>();
    }

    @After
    public void teardownSpaces() throws Exception {
        for (AddressSpace addressSpace : addressSpaces) {
            deleteAddressSpace(addressSpace);
            getKeycloakClient().deleteUser(addressSpace.getName(), "bob");
        }
        addressSpaces.clear();
    }

    @Override
    protected boolean createDefaultAddressSpace() {
        return false;
    }

    public String createAddressSpace(String name, String authService) throws Exception {
        AddressSpace addressSpace = new AddressSpace(name);
        addressSpaces.add(addressSpace);
        super.createAddressSpace(addressSpace, authService);
        setAddresses(addressSpace, amqpAddressList.toArray(new Destination[amqpAddressList.size()]));
        //        setAddresses(name, Destination.queue(amqpAddress)); //, Destination.topic(mqttAddress));
        return name;
    }

    public String createAddressSpace(String name, String authService, String type) throws Exception {
        AddressSpace addressSpace = new AddressSpace(name);
        addressSpaces.add(addressSpace);
        super.createAddressSpace(addressSpace, authService, type);
        if (type.equals(STANDARD_ADDRESS_SPACE_TYPE)) {
            setAddresses(addressSpace, amqpAddressList.toArray(new Destination[amqpAddressList.size()]));
        } else {
            List<Destination> brokeredAddressList = amqpAddressList.subList(0, 2);
            setAddresses(addressSpace, brokeredAddressList.toArray(new Destination[brokeredAddressList.size()]));
        }
        return addressSpace.getName();
    }

    @Test
    public void testStandardAuthenticationServiceBrokered() throws Exception {
        testStandardAuthenticationServiceGeneral("brokered");
    }

    @Test
    public void testNoneAuthenticationServiceBrokered() throws Exception {
        testNoneAuthenticationServiceGeneral("brokered", null, null);
    }

    @Test
    public void testStandardAuthenticationService() throws Exception {
        testStandardAuthenticationServiceGeneral("standard");
    }

    @Test
    public void testNoneAuthenticationService() throws Exception {
        testNoneAuthenticationServiceGeneral("standard", null, null);
    }

    public void testNoneAuthenticationServiceGeneral(String addressSpaceType, String emptyUser, String emptyPassword) throws Exception {
        String s3standard = createAddressSpace(addressSpaceType + "-s3", "none", addressSpaceType);
        assertCanConnect(s3standard, null, null);
        assertCanConnect(s3standard, "bob", "pass");

        String s4standard = createAddressSpace(addressSpaceType + " -s4", "standard", addressSpaceType);
        assertCanConnect(s3standard, null, null);
        assertCanConnect(s3standard, "bob", "pass");
        assertCannotConnect(s4standard, null, null);
        assertCannotConnect(s4standard, "bob", "pass");
    }

    public void testStandardAuthenticationServiceGeneral(String addressSpaceType) throws Exception {
        String s1brokered = createAddressSpace(addressSpaceType + "-s1", "standard", addressSpaceType);

        // Validate unsuccessful authentication with enmasse authentication service with no credentials
        assertCannotConnect(s1brokered, null, null);
        assertCannotConnect(s1brokered, "bob", "s1pass");

        KeycloakCredentials s1Bob = new KeycloakCredentials("bob", "<PASSWORD>");
        getKeycloakClient().createUser(s1brokered, s1Bob.getUsername(), s1Bob.getPassword());

        KeycloakCredentials s1Carol = new KeycloakCredentials("carol", "<PASSWORD>");
        getKeycloakClient().createUser(s1brokered, s1Carol.getUsername(), s1Carol.getPassword());

        assertCannotConnect(s1brokered, null, null);<commit_msg>UPD AuthenticationTest - white space removed from name of address space<commit_after>/*
 * Copyright 2017 Red Hat Inc.
 *
 * Licensed 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 io.enmasse.systemtest.auth;

import io.enmasse.systemtest.*;
import io.enmasse.systemtest.amqp.AmqpClient;
import io.enmasse.systemtest.mqtt.MqttClient;
import org.apache.qpid.proton.message.Message;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

public class AuthenticationTest extends TestBase {

    private List<AddressSpace> addressSpaces = new ArrayList<>();
    private static final List<Destination> amqpAddressList = Arrays.asList(
            Destination.queue("auth-queue"),
            Destination.topic("auth-topic"),
            Destination.anycast("auth-anycast"),
            Destination.multicast("auth-multicast"));
    private static final String mqttAddress = "t1";
    private static final String anonymousUser = "anonymous";
    private static final String anonymousPswd = "anonymous";

    @Before
    public void setupSpaceList() throws Exception {
        addressSpaces = new ArrayList<>();
    }

    @After
    public void teardownSpaces() throws Exception {
        for (AddressSpace addressSpace : addressSpaces) {
            deleteAddressSpace(addressSpace);
            getKeycloakClient().deleteUser(addressSpace.getName(), "bob");
        }
        addressSpaces.clear();
    }

    @Override
    protected boolean createDefaultAddressSpace() {
        return false;
    }

    public String createAddressSpace(String name, String authService) throws Exception {
        AddressSpace addressSpace = new AddressSpace(name);
        addressSpaces.add(addressSpace);
        super.createAddressSpace(addressSpace, authService);
        setAddresses(addressSpace, amqpAddressList.toArray(new Destination[amqpAddressList.size()]));
        //        setAddresses(name, Destination.queue(amqpAddress)); //, Destination.topic(mqttAddress));
        return name;
    }

    public String createAddressSpace(String name, String authService, String type) throws Exception {
        AddressSpace addressSpace = new AddressSpace(name);
        addressSpaces.add(addressSpace);
        super.createAddressSpace(addressSpace, authService, type);
        if (type.equals(STANDARD_ADDRESS_SPACE_TYPE)) {
            setAddresses(addressSpace, amqpAddressList.toArray(new Destination[amqpAddressList.size()]));
        } else {
            List<Destination> brokeredAddressList = amqpAddressList.subList(0, 2);
            setAddresses(addressSpace, brokeredAddressList.toArray(new Destination[brokeredAddressList.size()]));
        }
        return addressSpace.getName();
    }

    @Test
    public void testStandardAuthenticationServiceBrokered() throws Exception {
        testStandardAuthenticationServiceGeneral("brokered");
    }

    @Test
    public void testNoneAuthenticationServiceBrokered() throws Exception {
        testNoneAuthenticationServiceGeneral("brokered", null, null);
    }

    @Test
    public void testStandardAuthenticationService() throws Exception {
        testStandardAuthenticationServiceGeneral("standard");
    }

    @Test
    public void testNoneAuthenticationService() throws Exception {
        testNoneAuthenticationServiceGeneral("standard", null, null);
    }

    public void testNoneAuthenticationServiceGeneral(String addressSpaceType, String emptyUser, String emptyPassword) throws Exception {
        String s3standard = createAddressSpace(addressSpaceType + "-s3", "none", addressSpaceType);
        assertCanConnect(s3standard, null, null);
        assertCanConnect(s3standard, "bob", "pass");

        String s4standard = createAddressSpace(addressSpaceType + "-s4", "standard", addressSpaceType);
        assertCanConnect(s3standard, null, null);
        assertCanConnect(s3standard, "bob", "pass");
        assertCannotConnect(s4standard, null, null);
        assertCannotConnect(s4standard, "bob", "pass");
    }

    public void testStandardAuthenticationServiceGeneral(String addressSpaceType) throws Exception {
        String s1brokered = createAddressSpace(addressSpaceType + "-s1", "standard", addressSpaceType);

        // Validate unsuccessful authentication with enmasse authentication service with no credentials
        assertCannotConnect(s1brokered, null, null);
        assertCannotConnect(s1brokered, "bob", "s1pass");

        KeycloakCredentials s1Bob = new KeycloakCredentials("bob", "<PASSWORD>");
        getKeycloakClient().createUser(s1brokered, s1Bob.getUsername(), s1Bob.getPassword());

        KeycloakCredentials s1Carol = new KeycloakCredentials("carol", "<PASSWORD>");
        getKeycloakClient().createUser(s1brokered, s1Carol.getUsername(), s1Carol.getPassword());

        assertCannotConnect(s1brokered, null, null);
{'fraction_non_alphanumeric': 0.09393702836325787, 'fraction_numerical': 0.00624512099921936, 'mean_word_length': 4.00651324359531, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, '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': '4672697', 'n_tokens_mistral': 3059, 'n_tokens_neox': 2986, 'n_words': 746}