/*
 * Licensed to Crate under one or more contributor license agreements.
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.  Crate 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.
 *
 * However, if you have executed another commercial license agreement
 * with Crate these terms will supersede the license and you may use the
 * software solely pursuant to the terms of the relevant commercial
 * agreement.
 */

package io.crate.auth;

import io.crate.user.User;
import io.crate.protocols.postgres.ConnectionProperties;
import org.elasticsearch.test.ESTestCase;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import org.elasticsearch.common.network.InetAddresses;
import org.junit.Before;
import org.junit.Test;

import javax.net.ssl.SSLSession;
import java.security.cert.Certificate;
import java.util.Date;

import static org.hamcrest.Matchers.is;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class ClientCertAuthTest extends ESTestCase {

    private ConnectionProperties sslConnWithCert;
    // "example.com" is the CN used in SelfSignedCertificate
    private User exampleUser = User.of("example.com");
    private SSLSession sslSession;

    @Before
    public void setUpSsl() throws Exception {
        var notBefore = new Date(System.currentTimeMillis() - 86400000L * 365);
        var notAfter = new Date(253402300799000L);
        SelfSignedCertificate ssc = new SelfSignedCertificate(
            "example.com", notBefore, notAfter, "RSA", 2048
        );
        sslSession = mock(SSLSession.class);
        when(sslSession.getPeerCertificates()).thenReturn(new Certificate[] { ssc.cert() });

        sslConnWithCert = new ConnectionProperties(InetAddresses.forString("127.0.0.1"), Protocol.POSTGRES, sslSession);
    }

    @Test
    public void testLookupValidUserWithCert() throws Exception {
        ClientCertAuth clientCertAuth = new ClientCertAuth(userName -> exampleUser);

        User user = clientCertAuth.authenticate("example.com", null, sslConnWithCert);
        assertThat(user, is(exampleUser));
    }

    @Test
    public void testLookupValidUserWithCertWithDifferentCN() throws Exception {
        ClientCertAuth clientCertAuth = new ClientCertAuth(userName -> User.of("arthur"));

        expectedException.expectMessage("Common name \"example.com\" in client certificate doesn't match username \"arthur\"");
        clientCertAuth.authenticate("arthur", null, sslConnWithCert);
    }

    @Test
    public void testLookupUserWithMatchingCertThatDoesNotExist() throws Exception {
        ClientCertAuth clientCertAuth = new ClientCertAuth(userName -> null);

        expectedException.expectMessage("Client certificate authentication failed for user \"example.com\"");
        clientCertAuth.authenticate("example.com", null, sslConnWithCert);
    }

    @Test
    public void testMissingClientCert() throws Exception {
        SSLSession sslSession = mock(SSLSession.class);
        when(sslSession.getPeerCertificates()).thenReturn(new Certificate[0]);
        ConnectionProperties connectionProperties = new ConnectionProperties(
            InetAddresses.forString("127.0.0.1"), Protocol.POSTGRES, sslSession);
        ClientCertAuth clientCertAuth = new ClientCertAuth(userName -> exampleUser);

        expectedException.expectMessage("Client certificate authentication failed for user \"example.com\"");
        clientCertAuth.authenticate("example.com", null, connectionProperties);
    }

    @Test
    public void testHttpClientCertAuthFailsOnUserMissMatchWithCN() throws Exception {
        ClientCertAuth clientCertAuth = new ClientCertAuth(userName -> exampleUser);
        ConnectionProperties conn = new ConnectionProperties(InetAddresses.forString("127.0.0.1"), Protocol.HTTP, sslSession);

        expectedException.expectMessage("Common name \"example.com\" in client certificate doesn't match username \"arthur_is_wrong\"");
        clientCertAuth.authenticate("arthur_is_wrong", null, conn);
    }
}

@@ -1278,16 +1278,46 @@
 Before;%0A
+import org.junit.BeforeClass;%0A
 import o
@@ -1331,16 +1331,16 @@
 t.Test;%0A
-
 %0Aimport 
@@ -1426,16 +1426,41 @@
 il.Date;
+%0Aimport java.util.Locale;
 %0A%0Aimport
@@ -1790,24 +1790,24 @@
 mple.com%22);%0A
-
     private 
@@ -1830,16 +1830,456 @@
 ssion;%0A%0A
+    @BeforeClass%0A    public static void ensureEnglishLocale() %7B%0A        // BouncyCastle is parsing date objects with the system locale while creating self-signed SSL certs%0A        // This fails for certain locales, e.g. 'ks'.%0A        // Until this is fixed, we force the english locale.%0A        // See also https://github.com/bcgit/bc-java/issues/405 (different topic, but same root cause)%0A        Locale.setDefault(Locale.ENGLISH);%0A    %7D%0A%0A
     @Bef

Fix self-sign certificate generation at the ClientCertAuthTest