aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/test/java/com/yahoo/vespa/model/container/xml/CloudTokenDataPlaneFilterTest.java
blob: fba2ef891be96b486a1d083c6e22217e678b4468 (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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.model.container.xml;

import com.yahoo.cloud.config.DataplaneProxyConfig;
import com.yahoo.config.model.api.ApplicationClusterEndpoint;
import com.yahoo.config.model.api.ContainerEndpoint;
import com.yahoo.config.model.api.EndpointCertificateSecrets;
import com.yahoo.config.model.builder.xml.test.DomBuilderTest;
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.DataplaneToken;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.Zone;
import com.yahoo.jdisc.http.filter.security.cloud.config.CloudTokenDataPlaneFilterConfig;
import com.yahoo.processing.response.Data;
import com.yahoo.vespa.model.container.ContainerModel;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.w3c.dom.Element;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Duration;
import java.time.Instant;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set;

import static com.yahoo.vespa.model.container.xml.CloudDataPlaneFilterTest.createCertificate;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

public class CloudTokenDataPlaneFilterTest extends ContainerModelBuilderTestBase {

    private static final String servicesXmlTemplate = """
            <container version='1.0'>
              <clients>
                <client id="foo" permissions="read,write">
                    <certificate file="%s"/>
                </client>
                <client id="bar" permissions="read">
                    <token id="my-token"/>
                </client>
              </clients>
            </container>
            """;

    private static final List<DataplaneToken> defaultTokens = List.of(new DataplaneToken("my-token", List.of(
            new DataplaneToken.Version("myfingerprint1", "myaccesshash1", Optional.empty()),
            new DataplaneToken.Version("myfingerprint2", "myaccesshash2", Optional.of(Instant.EPOCH.plus(Duration.ofDays(100000)))))));
    private static final ContainerEndpoint tokenEndpoint = new ContainerEndpoint("cluster", ApplicationClusterEndpoint.Scope.zone, List.of("token"), OptionalInt.empty(), ApplicationClusterEndpoint.RoutingMethod.exclusive, ApplicationClusterEndpoint.AuthMethod.token);
    private static final ContainerEndpoint mtlsEndpoint = new ContainerEndpoint("cluster", ApplicationClusterEndpoint.Scope.zone, List.of("mtls"), OptionalInt.empty(), ApplicationClusterEndpoint.RoutingMethod.exclusive, ApplicationClusterEndpoint.AuthMethod.mtls);
    @TempDir
    public File applicationFolder;

    Path securityFolder;
    private static final String filterConfigId = "container/filters/chain/cloud-token-data-plane-secure/component/" +
            "com.yahoo.jdisc.http.filter.security.cloud.CloudTokenDataPlaneFilter";

    @BeforeEach
    public void setup() throws IOException {
        securityFolder = applicationFolder.toPath().resolve("security");
        Files.createDirectories(securityFolder);
    }

    @Test
    void generates_correct_config_for_tokens() throws IOException {
        var certFile = securityFolder.resolve("foo.pem");
        var clusterElem = DomBuilderTest.parse(servicesXmlTemplate.formatted(applicationFolder.toPath().relativize(certFile).toString()));
        createCertificate(certFile);
        buildModel(Set.of(tokenEndpoint, mtlsEndpoint), defaultTokens, clusterElem);

        var cfg = root.getConfig(CloudTokenDataPlaneFilterConfig.class, filterConfigId);
        var tokenClient = cfg.clients().stream().filter(c -> c.id().equals("bar")).findAny().orElse(null);
        assertNotNull(tokenClient);
        assertEquals(List.of("read"), tokenClient.permissions());
        var expectedTokenCfg = tokenConfig(
                "my-token", List.of("myfingerprint1", "myfingerprint2"), List.of("myaccesshash1", "myaccesshash2"),
                List.of("<none>", "2243-10-17T00:00:00Z"));
        assertEquals(List.of(expectedTokenCfg), tokenClient.tokens());
    }

    @Test
    void configures_dataplane_proxy_when_token_defined() throws IOException {
        var certFile = securityFolder.resolve("foo.pem");
        var clusterElem = DomBuilderTest.parse(servicesXmlTemplate.formatted(applicationFolder.toPath().relativize(certFile).toString()));
        createCertificate(certFile);
        buildModel(Set.of(tokenEndpoint, mtlsEndpoint), defaultTokens, clusterElem);

        var configId = "container/component/com.yahoo.container.jdisc.DataplaneProxyConfigurator";
        var cfg = root.getConfig(DataplaneProxyConfig.class, configId);
        assertEquals(8443, cfg.mtlsPort());
        assertEquals(8444, cfg.tokenPort());
    }

    @Test
    void configures_dataplane_proxy_when_token_defined_but_missing() throws IOException {
        var certFile = securityFolder.resolve("foo.pem");
        var clusterElem = DomBuilderTest.parse(servicesXmlTemplate.formatted(applicationFolder.toPath().relativize(certFile).toString()));
        createCertificate(certFile);
        buildModel(Set.of(tokenEndpoint, mtlsEndpoint), List.of(), clusterElem);

        var configId = "container/component/com.yahoo.container.jdisc.DataplaneProxyConfigurator";
        var cfg = root.getConfig(DataplaneProxyConfig.class, configId);
        assertNotNull(cfg);
        assertEquals(8443, cfg.mtlsPort());
        assertEquals(8444, cfg.tokenPort());
    }

    @Test
    void does_notconfigure_dataplane_proxy_when_token_endpoints_not_defined() throws IOException {
        var certFile = securityFolder.resolve("foo.pem");
        var clusterElem = DomBuilderTest.parse(servicesXmlTemplate.formatted(applicationFolder.toPath().relativize(certFile).toString()));
        createCertificate(certFile);
        buildModel(Set.of(mtlsEndpoint), List.of(), clusterElem);

        assertFalse(root.getConfigIds().stream().anyMatch(id -> id.contains("DataplaneProxyConfigurator")));
    }

    private static CloudTokenDataPlaneFilterConfig.Clients.Tokens tokenConfig(
            String id, Collection<String> fingerprints, Collection<String> accessCheckHashes, Collection<String> expirations) {
        return new CloudTokenDataPlaneFilterConfig.Clients.Tokens.Builder()
                .id(id).fingerprints(fingerprints).checkAccessHashes(accessCheckHashes).expirations(expirations).build();
    }

    public List<ContainerModel> buildModel(Set<ContainerEndpoint> endpoints, List<DataplaneToken> definedTokens, Element... clusterElem) {
        var applicationPackage = new MockApplicationPackage.Builder()
                .withRoot(applicationFolder)
                .build();

        DeployState state = new DeployState.Builder()
                .applicationPackage(applicationPackage)
                .properties(
                        new TestProperties()
                        .setEndpointCertificateSecrets(Optional.of(new EndpointCertificateSecrets("CERT", "KEY")))
                        .setDataplaneTokens(definedTokens)
                        .setHostedVespa(true))
                .zone(new Zone(SystemName.PublicCd, Environment.dev, RegionName.defaultName()))
                .endpoints(endpoints)
                .build();
        return createModel(root, state, null, clusterElem);
    }
}