aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/tls/SecureContainerTest.java
blob: 05a0aedb18bb7dd34032dec9960542e3824e9e81 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.tls;

import com.yahoo.application.Networking;
import com.yahoo.application.container.JDisc;
import com.yahoo.application.container.handler.Request;
import com.yahoo.application.container.handler.Response;
import com.yahoo.component.ComponentId;
import com.yahoo.security.KeyStoreBuilder;
import com.yahoo.security.KeyStoreType;
import com.yahoo.security.KeyStoreUtils;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Path;
import java.security.KeyStore;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

/**
 * @author mpolden
 */
public class SecureContainerTest {

    private JDisc container;

    @TempDir
    public File folder;

    @BeforeEach
    public void startContainer() {
        container = JDisc.fromServicesXml(servicesXml(writeKeyStore()), Networking.enable);
    }

    @AfterEach
    public void stopContainer() {
        container.close();
    }

    @Test
    void test_https_request() {
        assertNotNull(sslContextFactoryProvider(), "SslContextFactoryProvider is created");
        assertResponse(Request.Method.GET, "/", 200);
    }

    private void assertResponse(Request.Method method, String path, int expectedStatusCode) {
        Response response = container.handleRequest(new Request("https://localhost:9999" + path, new byte[0], method));
        assertEquals(expectedStatusCode, response.getStatus(), "Status code");
    }

    private ControllerSslContextFactoryProvider sslContextFactoryProvider() {
        return (ControllerSslContextFactoryProvider) container.components().getComponent(ComponentId.fromString("ssl-provider@default"));
    }

    private String servicesXml(Path trustStore) {
        return "<container version='1.0'>\n" +
               "  <config name=\"container.handler.threadpool\">\n" +
               "    <maxthreads>10</maxthreads>\n" +
               "  </config> \n" +
               "  <config name='vespa.hosted.controller.tls.config.tls'>\n" +
               "    <caTrustStore>" + trustStore.toString() + "</caTrustStore>\n" +
               "    <certificateSecret>controller.cert</certificateSecret>\n" +
               "    <privateKeySecret>controller.key</privateKeySecret>\n" +
               "  </config>\n" +
               "  <component id='com.yahoo.vespa.hosted.controller.tls.SecretStoreMock'/>\n" +
               "  <http>\n" +
               "    <server id='default' port='9999'>\n" +
               "      <ssl-provider class='com.yahoo.vespa.hosted.controller.tls.ControllerSslContextFactoryProvider' bundle='controller-server'/>\n" +
               "    </server>\n" +
               "  </http>\n" +
               "</container>";
    }

    private Path writeKeyStore()  {
        KeyStore keyStore = KeyStoreBuilder.withType(KeyStoreType.JKS)
                                           .withKeyEntry(getClass().getSimpleName(),
                                                         Keys.keyPair.getPrivate(), new char[0], Keys.certificate)
                                           .build();
        try {
            Path path = File.createTempFile("junit", null, folder).toPath();
            KeyStoreUtils.writeKeyStoreToFile(keyStore, path);
            return path;
        } catch (IOException e) {
            throw new UncheckedIOException(e);
        }
    }

}