aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/application/validation/EndpointCertificateSecretsValidatorTest.java
blob: 991606a8e32c1b236c1d61c00b80551de25d6c2b (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.application.validation;

import com.yahoo.config.application.api.ApplicationPackage;
import com.yahoo.config.model.NullConfigModelRegistry;
import com.yahoo.config.model.api.EndpointCertificateSecrets;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.model.deploy.TestProperties;
import com.yahoo.config.model.test.MockApplicationPackage;
import com.yahoo.config.provision.CertificateNotReadyException;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.Zone;
import com.yahoo.vespa.model.VespaModel;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.util.Optional;

import static com.yahoo.config.model.test.TestUtil.joinLines;
import static org.junit.Assert.assertTrue;

/**
 * @author andreer
 */
public class EndpointCertificateSecretsValidatorTest {
    @SuppressWarnings("deprecation")
    @Rule
    public final ExpectedException exceptionRule = ExpectedException.none();

    private static String servicesXml() {
        return joinLines("<services version='1.0'>",
                "  <container id='default' version='1.0'>",
                "  </container>",
                "</services>");
    }

    private static String deploymentXml() {
        return joinLines("<deployment version='1.0' >",
                "  <prod />",
                "</deployment>");
    }

    @Test
    public void missing_certificate_fails_validation() throws Exception {
        DeployState deployState = deployState(servicesXml(), deploymentXml(), Optional.of(EndpointCertificateSecrets.missing(1)));
        VespaModel model = new VespaModel(new NullConfigModelRegistry(), deployState);

        exceptionRule.expect(CertificateNotReadyException.class);
        exceptionRule.expectMessage("TLS enabled, but could not yet retrieve certificate version 1 for application default:default:default");

        new EndpointCertificateSecretsValidator().validate(model, deployState);
    }

    @Test
    public void validation_succeeds_with_certificate() throws Exception {
        DeployState deployState = deployState(servicesXml(), deploymentXml(), Optional.of(new EndpointCertificateSecrets("cert", "key")));
        VespaModel model = new VespaModel(new NullConfigModelRegistry(), deployState);

        new EndpointCertificateSecretsValidator().validate(model, deployState);
    }

    @Test
    public void validation_succeeds_without_certificate() throws Exception {
        DeployState deployState = deployState(servicesXml(), deploymentXml(), Optional.empty());
        VespaModel model = new VespaModel(new NullConfigModelRegistry(), deployState);

        new EndpointCertificateSecretsValidator().validate(model, deployState);
    }

    private static DeployState deployState(String servicesXml, String deploymentXml, Optional<EndpointCertificateSecrets> endpointCertificateSecretsSecrets) {
        ApplicationPackage app = new MockApplicationPackage.Builder()
                .withServices(servicesXml)
                .withDeploymentSpec(deploymentXml)
                .build();
        DeployState.Builder builder = new DeployState.Builder()
                .applicationPackage(app)
                .zone(new Zone(Environment.prod, RegionName.from("foo")))
                .properties(
                        new TestProperties()
                                .setHostedVespa(true)
                                .setEndpointCertificateSecrets(endpointCertificateSecretsSecrets));
        final DeployState deployState = builder.build();

        assertTrue("Test must emulate a hosted deployment.", deployState.isHosted());
        return deployState;
    }
}