summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/application/ApplicationPackage.java
blob: d0e8a0f2816df421e9b520b0c7793262927d69f5 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.application;

import com.google.common.collect.ImmutableMap;
import com.google.common.hash.Hashing;
import com.yahoo.component.Version;
import com.yahoo.config.application.api.DeploymentSpec;
import com.yahoo.config.application.api.ValidationId;
import com.yahoo.config.application.api.ValidationOverrides;
import com.yahoo.security.X509CertificateUtils;
import com.yahoo.slime.Inspector;
import com.yahoo.slime.Slime;
import com.yahoo.slime.SlimeUtils;
import com.yahoo.vespa.hosted.controller.deployment.ZipBuilder;
import com.yahoo.yolean.Exceptions;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.security.cert.X509Certificate;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;

import static java.nio.charset.StandardCharsets.UTF_8;

/**
 * A representation of the content of an application package.
 * Only the deployment.xml content can be accessed as anything other than compressed data.
 * A package is identified by a hash of the content.
 * 
 * This is immutable.
 * 
 * @author bratseth
 */
public class ApplicationPackage {

    private static final String trustedCertificatesFile = "security/clients.pem";

    private final String contentHash;
    private final byte[] zippedContent;
    private final DeploymentSpec deploymentSpec;
    private final ValidationOverrides validationOverrides;
    private final Optional<Version> compileVersion;
    private final Optional<Instant> buildTime;
    private final List<X509Certificate> trustedCertificates;

    /**
     * Creates an application package from its zipped content.
     * This <b>assigns ownership</b> of the given byte array to this class;
     * it must not be further changed by the caller.
     */
    public ApplicationPackage(byte[] zippedContent) {
        this(zippedContent, false);
    }

    /**
     * Creates an application package from its zipped content.
     * This <b>assigns ownership</b> of the given byte array to this class;
     * it must not be further changed by the caller.
     * If 'requireFiles' is true, files needed by deployment orchestration must be present.
     */
    public ApplicationPackage(byte[] zippedContent, boolean requireFiles) {
        this.zippedContent = Objects.requireNonNull(zippedContent, "The application package content cannot be null");
        this.contentHash = Hashing.sha1().hashBytes(zippedContent).toString();
        Files files = Files.extract(Set.of("deployment.xml", "validation-overrides.xml", "build-meta.json", trustedCertificatesFile), zippedContent);

        Optional<DeploymentSpec> deploymentSpec = files.getAsReader("deployment.xml").map(DeploymentSpec::fromXml);
        if (requireFiles && deploymentSpec.isEmpty())
            throw new IllegalArgumentException("Missing required file 'deployment.xml'");
        this.deploymentSpec = deploymentSpec.orElse(DeploymentSpec.empty);

        this.validationOverrides = files.getAsReader("validation-overrides.xml").map(ValidationOverrides::fromXml).orElse(ValidationOverrides.empty);

        Optional<Inspector> buildMetaObject = files.get("build-meta.json").map(SlimeUtils::jsonToSlime).map(Slime::get);
        if (requireFiles && buildMetaObject.isEmpty())
            throw new IllegalArgumentException("Missing required file 'build-meta.json'");
        this.compileVersion = buildMetaObject.flatMap(object -> parse(object, "compileVersion", field -> Version.fromString(field.asString())));
        this.buildTime = buildMetaObject.flatMap(object -> parse(object, "buildTime", field -> Instant.ofEpochMilli(field.asLong())));

        this.trustedCertificates = files.get(trustedCertificatesFile).map(bytes -> X509CertificateUtils.certificateListFromPem(new String(bytes, UTF_8))).orElse(List.of());
    }

    /** Returns a copy of this with the given certificate appended. */
    public ApplicationPackage withTrustedCertificate(X509Certificate certificate) {
        List<X509Certificate> trustedCertificates = new ArrayList<>(this.trustedCertificates);
        trustedCertificates.add(certificate);
        byte[] certificatesBytes = X509CertificateUtils.toPem(trustedCertificates).getBytes(UTF_8);

        ByteArrayOutputStream modified = new ByteArrayOutputStream(zippedContent.length + certificatesBytes.length);
        ZipStreamReader.transferAndWrite(modified, new ByteArrayInputStream(zippedContent), trustedCertificatesFile, certificatesBytes);
        return new ApplicationPackage(modified.toByteArray());
    }

    /** Returns a hash of the content of this package */
    public String hash() { return contentHash; }
    
    /** Returns the content of this package. The content <b>must not</b> be modified. */
    public byte[] zippedContent() { return zippedContent; }

    /** 
     * Returns the deployment spec from the deployment.xml file of the package content.
     * This is the DeploymentSpec.empty instance if this package does not contain a deployment.xml file.
     */
    public DeploymentSpec deploymentSpec() { return deploymentSpec; }

    /**
     * Returns the validation overrides from the validation-overrides.xml file of the package content.
     * This is the ValidationOverrides.empty instance if this package does not contain a validation-overrides.xml file.
     */
    public ValidationOverrides validationOverrides() { return validationOverrides; }

    /** Returns the platform version which package was compiled against, if known. */
    public Optional<Version> compileVersion() { return compileVersion; }

    /** Returns the time this package was built, if known. */
    public Optional<Instant> buildTime() { return buildTime; }

    /** Returns the list of certificates trusted by this application, or an empty list if no trust configured. */
    public List<X509Certificate> trustedCertificates() {
        return trustedCertificates;
    }

    private static <Type> Optional<Type> parse(Inspector buildMetaObject, String fieldName, Function<Inspector, Type> mapper) {
        if ( ! buildMetaObject.field(fieldName).valid())
            throw new IllegalArgumentException("Missing value '" + fieldName + "' in 'build-meta.json'");
        try {
            return Optional.of(mapper.apply(buildMetaObject.field(fieldName)));
        }
        catch (RuntimeException e) {
            throw new IllegalArgumentException("Failed parsing \"" + fieldName + "\" in 'build-meta.json': " + Exceptions.toMessageString(e));
        }
    }

    private static class Files {

        /** Max size of each extracted file */
        private static final int maxSize = 10 * 1024 * 1024; // 10 MiB

        // TODO: Vespa 8: Remove application/ directory support
        private static final String applicationDir = "application/";

        private final ImmutableMap<String, byte[]> files;

        private Files(ImmutableMap<String, byte[]> files) {
            this.files = files;
        }

        public static Files extract(Set<String> filesToExtract, byte[] zippedContent) {
            ImmutableMap.Builder<String, byte[]> builder = ImmutableMap.builder();
            try (ByteArrayInputStream stream = new ByteArrayInputStream(zippedContent)) {
                ZipStreamReader reader = new ZipStreamReader(stream,
                                                             (name) -> filesToExtract.contains(withoutLegacyDir(name)),
                                                             maxSize);
                for (ZipStreamReader.ZipEntryWithContent entry : reader.entries()) {
                    builder.put(withoutLegacyDir(entry.zipEntry().getName()), entry.content());
                }
            } catch (IOException e) {
                throw new UncheckedIOException("Exception reading application package", e);
            }
            return new Files(builder.build());
        }


        /** Get content of given file name */
        public Optional<byte[]> get(String name) {
            return Optional.ofNullable(files.get(name));
        }

        /** Get reader for the content of given file name */
        public Optional<Reader> getAsReader(String name) {
            return get(name).map(ByteArrayInputStream::new).map(InputStreamReader::new);
        }

        private static String withoutLegacyDir(String name) {
            if (name.startsWith(applicationDir)) return name.substring(applicationDir.length());
            return name;
        }

    }

    /** Creates a valid application package that will remove all application's deployments */
    public static ApplicationPackage deploymentRemoval() {
        DeploymentSpec deploymentSpec = DeploymentSpec.empty;
        ValidationOverrides validationOverrides = allValidationOverrides();
        try (ZipBuilder zipBuilder = new ZipBuilder(deploymentSpec.xmlForm().length() + validationOverrides.xmlForm().length() + 500)) {
            zipBuilder.add("validation-overrides.xml", validationOverrides.xmlForm().getBytes(UTF_8));
            zipBuilder.add("deployment.xml", deploymentSpec.xmlForm().getBytes(UTF_8));

            zipBuilder.close();
            return new ApplicationPackage(zipBuilder.toByteArray());
        }
    }

    private static ValidationOverrides allValidationOverrides() {
        String until = DateTimeFormatter.ISO_LOCAL_DATE.format(Instant.now().plus(Duration.ofDays(25)).atZone(ZoneOffset.UTC));
        StringBuilder validationOverridesContents = new StringBuilder(1000);
        validationOverridesContents.append("<validation-overrides version=\"1.0\">\n");
        for (ValidationId validationId: ValidationId.values())
            validationOverridesContents.append("\t<allow until=\"").append(until).append("\">").append(validationId.value()).append("</allow>\n");
        validationOverridesContents.append("</validation-overrides>\n");

        return ValidationOverrides.fromXml(validationOverridesContents.toString());
    }
}