aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/model/container/xml/CloudTokenDataPlaneFilter.java
blob: bb24f96784e56e1fc182c321cbe316d3eb0eddee (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
// Copyright Vespa.ai. 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.component.ComponentSpecification;
import com.yahoo.component.chain.dependencies.Dependencies;
import com.yahoo.component.chain.model.ChainedComponentModel;
import com.yahoo.config.model.deploy.DeployState;
import com.yahoo.config.provision.DataplaneToken;
import com.yahoo.container.bundle.BundleInstantiationSpecification;
import com.yahoo.jdisc.http.filter.security.cloud.config.CloudTokenDataPlaneFilterConfig;
import com.yahoo.vespa.model.container.ApplicationContainerCluster;
import com.yahoo.vespa.model.container.http.Client;
import com.yahoo.vespa.model.container.http.Filter;

import java.time.Instant;
import java.util.Collection;
import java.util.List;

class CloudTokenDataPlaneFilter extends Filter implements CloudTokenDataPlaneFilterConfig.Producer {
    private final Collection<Client> clients;
    private final String tokenContext;

    CloudTokenDataPlaneFilter(ApplicationContainerCluster cluster, DeployState state) {
        super(model());
        this.clients = List.copyOf(cluster.getClients());
        // Token domain must be identical to the domain used for generating the tokens
        this.tokenContext = "Vespa Cloud tenant data plane:%s"
                .formatted(state.getProperties().applicationId().tenant().value());
    }

    private static ChainedComponentModel model() {
        return new ChainedComponentModel(
                new BundleInstantiationSpecification(
                        new ComponentSpecification("com.yahoo.jdisc.http.filter.security.cloud.CloudTokenDataPlaneFilter"),
                        null,
                        new ComponentSpecification("jdisc-security-filters")),
                Dependencies.emptyDependencies());
    }

    @Override
    public void getConfig(CloudTokenDataPlaneFilterConfig.Builder builder) {
        var clientsCfg = clients.stream()
                .filter(c -> !c.tokens().isEmpty())
                .map(x -> new CloudTokenDataPlaneFilterConfig.Clients.Builder()
                        .id(x.id())
                        .tokens(tokensConfig(x.tokens()))
                        .permissions(x.permissions()))
                .toList();
        builder.clients(clientsCfg).tokenContext(tokenContext);
    }

    private static List<CloudTokenDataPlaneFilterConfig.Clients.Tokens.Builder> tokensConfig(Collection<DataplaneToken> tokens) {
        return tokens.stream()
                .map(token -> new CloudTokenDataPlaneFilterConfig.Clients.Tokens.Builder()
                        .id(token.tokenId())
                        .fingerprints(token.versions().stream().map(DataplaneToken.Version::fingerprint).toList())
                        .checkAccessHashes(token.versions().stream().map(DataplaneToken.Version::checkAccessHash).toList())
                        .expirations(token.versions().stream().map(v -> v.expiration().map(Instant::toString).orElse("<none>")).toList()))
                .toList();
    }

}