aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/application/pkg/ApplicationPackageTest.java
blob: 910d526a2edbeac3f8ab9fdceb60da6cc1611f51 (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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.application.pkg;

import com.yahoo.config.application.api.DeploymentSpec;
import com.yahoo.config.application.api.ValidationId;
import com.yahoo.io.LazyInputStream;
import org.junit.jupiter.api.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.SequenceInputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;

import static com.yahoo.vespa.hosted.controller.application.pkg.ApplicationPackage.filesZip;
import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

/**
 * @author valerijf
 * @author jonmv
 */
public class ApplicationPackageTest {

    static final String deploymentXml = """
                                        <?xml version="1.0" encoding="UTF-8"?>
                                        <deployment version="1.0">
                                            <test />
                                            <prod>
                                                <parallel>
                                                    <region active="true">us-central-1</region>
                                                </parallel>
                                            </prod>
                                        </deployment>
                                        """;

    static final String servicesXml = """
                                      <services version='1.0' xmlns:deploy="vespa" xmlns:preprocess="properties">
                                          <preprocess:include file='jdisc.xml' />
                                          <content version='1.0' if='foo' />
                                          <content version='1.0' id='foo' deploy:environment='staging prod' deploy:region='us-east-3 us-central-1'>
                                              <preprocess:include file='content/content.xml' />
                                          </content>
                                          <preprocess:include file='not_found.xml' required='false' />
                                      </services>
                                      """;

    private static final String jdiscXml = "<container id='stateless' version='1.0' />\n";

    private static final String contentXml = """
                                             <documents>
                                                 <document type="music.sd" mode="index" />
                                             </documents>
                                             <preprocess:include file="nodes.xml" />""";

    private static final String nodesXml = """
                                           <nodes>
                                               <node hostalias="node0" distribution-key="0" />
                                           </nodes>""";

    @Test
    void test_createEmptyForDeploymentRemoval() {
        ApplicationPackage app = ApplicationPackage.deploymentRemoval();
        assertEquals(DeploymentSpec.empty, app.deploymentSpec());

        for (ValidationId validationId : ValidationId.values()) {
            assertTrue(app.validationOverrides().allows(validationId, Instant.now()));
        }
    }

    @Test
    void testMetaData() {
        byte[] zip = filesZip(Map.of("services.xml", servicesXml.getBytes(UTF_8),
                                     "jdisc.xml", jdiscXml.getBytes(UTF_8),
                                     "content/content.xml", contentXml.getBytes(UTF_8),
                                     "content/nodes.xml", nodesXml.getBytes(UTF_8),
                                     "gurba", "gurba".getBytes(UTF_8)));

        assertEquals(Map.of("services.xml", servicesXml,
                            "jdisc.xml", jdiscXml,
                            "content/content.xml", contentXml,
                            "content/nodes.xml", nodesXml),
                     unzip(new ApplicationPackage(zip).metaDataZip()));
    }

    @Test
    void testMetaDataWithMissingFiles() {
        byte[] zip = filesZip(Map.of("services.xml", servicesXml.getBytes(UTF_8)));

        try {
            new ApplicationPackage(zip).metaDataZip();
            fail("Should fail on missing include file");
        }
        catch (RuntimeException e) {
            assertEquals("./jdisc.xml", e.getCause().getMessage());
        }
    }

    @Test
    void testAbsoluteInclude() throws Exception {
        try {
            getApplicationZip("include-absolute.zip");
            fail("Should fail on include file outside zip");
        }
        catch (RuntimeException e) {
            assertEquals(IllegalArgumentException.class, e.getClass());
        }
    }

    @Test
    void testParentInclude() throws Exception {
        try {
            getApplicationZip("include-parent.zip");
            fail("Should fail on include file outside zip");
        }
        catch (RuntimeException e) {
            assertEquals("./../not_found.xml is not a descendant of .", e.getMessage());
        }
    }

    @Test
    void testBundleHashesAreSameWithDifferentDeploymentXml() throws Exception {
        var originalPackage = getApplicationZip("original.zip");
        var changedServices = getApplicationZip("changed-services-xml.zip");
        var changedDeploymentXml = getApplicationZip("changed-deployment-xml.zip");
        var similarDeploymentXml = getApplicationZip("similar-deployment-xml.zip");

        // services.xml is changed -> different bundle hash
        assertNotEquals(originalPackage.bundleHash(), changedServices.bundleHash());

        // deployment.xml is changed, with real changes -> different bundle hash
        assertNotEquals(originalPackage.bundleHash(), changedDeploymentXml.bundleHash());

        // deployment.xml is changed, but only deployment orchestration settings -> same bundle hash
        assertEquals(originalPackage.bundleHash(), similarDeploymentXml.bundleHash());
    }

    @Test
    void testCertificateFileExists() throws Exception {
        getApplicationZip("with-certificate.zip", true);
    }

    @Test
    void testCertificateFileMissing() throws Exception {
        try {
            getApplicationZip("original.zip", true);
            fail("Should fail on missing certificate file file");
        } catch (RuntimeException e) {
            assertEquals("No client certificate found in security/ in application package, see https://cloud.vespa.ai/en/security/guide", e.getMessage());
        }
    }

    public static Map<String, String> unzip(byte[] zip) {
        return ZipEntries.from(zip, __ -> true, 1 << 24, true)
                         .asList().stream()
                         .collect(Collectors.toMap(ZipEntries.ZipEntryWithContent::name,
                                                   entry -> new String(entry.content().orElse(new byte[0]), UTF_8)));
    }

    private ApplicationPackage getApplicationZip(String path) throws IOException {
        return getApplicationZip(path, false);
    }

    private ApplicationPackage getApplicationZip(String path, boolean checkCertificateFile) throws IOException {
        return new ApplicationPackage(Files.readAllBytes(Path.of("src/test/resources/application-packages/" + path)), true, checkCertificateFile);
    }

    public static byte[] zip(Map<String, String> content) {
        return filesZip(content.entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey(),
                                                                             entry -> entry.getValue().getBytes(UTF_8))));
    }

    private static class AngryStreams {

        private final byte[] content;
        private final Map<ByteArrayInputStream, Throwable> streams = new LinkedHashMap<>();

        AngryStreams(byte[] content) {
            this.content = content;
        }

        InputStream stream() {
            ByteArrayInputStream stream = new ByteArrayInputStream(Arrays.copyOf(content, content.length)) {
                boolean closed = false;
                @Override public void close() { closed = true; }
                @Override public int read() { assertFalse(closed); return super.read(); }
                @Override public int read(byte[] b, int off, int len) { assertFalse(closed); return super.read(b, off, len); }
                @Override public long transferTo(OutputStream out) throws IOException { assertFalse(closed); return super.transferTo(out); }
                @Override public byte[] readAllBytes() { assertFalse(closed); return super.readAllBytes(); }
            };
            streams.put(stream, new Throwable());
            return stream;
        }

        void verifyAllRead() {
            streams.forEach((stream, stack) -> assertEquals(0, stream.available(),
                                                            "unconsumed content in stream created at " +
                                                            new ByteArrayOutputStream() {{ stack.printStackTrace(new PrintStream(this)); }}));
        }

    }

    @Test
    void testApplicationPackageStream() throws Exception {
        Map<String, String> content = Map.of("deployment.xml", deploymentXml,
                                             "services.xml", servicesXml,
                                             "jdisc.xml", jdiscXml,
                                             "unused1.xml", jdiscXml,
                                             "content/content.xml", contentXml,
                                             "content/nodes.xml", nodesXml,
                                             "gurba", "gurba");
        byte[] zip = zip(content);
        assertEquals(content, unzip(zip));
        AngryStreams angry = new AngryStreams(zip);

        ApplicationPackageStream identity = new ApplicationPackageStream(angry::stream, () -> __ -> true);
        InputStream lazy = new LazyInputStream(() -> new ByteArrayInputStream(identity.truncatedPackage().zippedContent()));
        assertEquals("must completely exhaust input before reading package",
                     assertThrows(IllegalStateException.class, identity::truncatedPackage).getMessage());

        // Verify no content has changed when passing through the stream.
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try (InputStream stream = identity.zipStream()) { stream.transferTo(out); }
        assertEquals(content, unzip(out.toByteArray()));
        assertEquals(content, unzip(identity.truncatedPackage().zippedContent()));
        assertEquals(content, unzip(lazy.readAllBytes()));
        ApplicationPackage original = new ApplicationPackage(zip);
        assertEquals(unzip(original.metaDataZip()), unzip(identity.truncatedPackage().metaDataZip()));
        assertEquals(original.bundleHash(), identity.truncatedPackage().bundleHash());

        // Change deployment.xml, remove unused1.xml and add unused2.xml
        Map<String, UnaryOperator<InputStream>> replacements = Map.of("deployment.xml", in -> new SequenceInputStream(in, new ByteArrayInputStream("\n\n".getBytes(UTF_8))),
                                                                      "unused1.xml", in -> null,
                                                                      "unused2.xml", __ -> new ByteArrayInputStream(jdiscXml.getBytes(UTF_8)));
        Predicate<String> truncation = name -> name.endsWith(".xml");
        ApplicationPackageStream modifier = new ApplicationPackageStream(angry::stream, () -> truncation, replacements);
        out.reset();

        InputStream partiallyRead = modifier.zipStream();
        assertEquals(15, partiallyRead.readNBytes(15).length);

        try (InputStream stream = modifier.zipStream()) { stream.transferTo(out); }

        assertEquals(Map.of("deployment.xml", deploymentXml + "\n\n",
                            "services.xml", servicesXml,
                            "jdisc.xml", jdiscXml,
                            "unused2.xml", jdiscXml,
                            "content/content.xml", contentXml,
                            "content/nodes.xml", nodesXml,
                            "gurba", "gurba"),
                     unzip(out.toByteArray()));

        assertEquals(Map.of("deployment.xml", deploymentXml + "\n\n",
                            "services.xml", servicesXml,
                            "jdisc.xml", jdiscXml,
                            "unused2.xml", jdiscXml,
                            "content/content.xml", contentXml,
                            "content/nodes.xml", nodesXml),
                     unzip(modifier.truncatedPackage().zippedContent()));

        // Compare retained metadata for an updated original package, and the truncated package of the modifier.
        assertEquals(unzip(new ApplicationPackage(zip(Map.of("deployment.xml", deploymentXml + "\n\n",  // Expected to change.
                                                             "services.xml", servicesXml,
                                                             "jdisc.xml", jdiscXml,
                                                             "unused1.xml", jdiscXml,                   // Irrelevant.
                                                             "content/content.xml", contentXml,
                                                             "content/nodes.xml", nodesXml,
                                                             "gurba", "gurba"))).metaDataZip()),
                     unzip(modifier.truncatedPackage().metaDataZip()));

        try (InputStream stream1 = modifier.zipStream();
             InputStream stream2 = modifier.zipStream()) {
            assertArrayEquals(stream1.readAllBytes(),
                              stream2.readAllBytes());
        }

        ByteArrayOutputStream byteAtATime = new ByteArrayOutputStream();
        try (InputStream stream1 = modifier.zipStream();
             InputStream stream2 = modifier.zipStream()) {
            for (int b; (b = stream1.read()) != -1; ) byteAtATime.write(b);
            assertArrayEquals(stream2.readAllBytes(),
                              byteAtATime.toByteArray());
        }

        assertEquals(byteAtATime.size(),
                     15 + partiallyRead.readAllBytes().length);
        partiallyRead.close();

        try (InputStream stream = modifier.zipStream()) { stream.readNBytes(12); }

        angry.verifyAllRead();
    }

}