aboutsummaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/TenantController.java
blob: 78099fac34e13f0692e1c9a31551b9e5db79e2a9 (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
// 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;

import com.yahoo.config.provision.TenantName;
import com.yahoo.vespa.athenz.api.AthenzDomain;
import com.yahoo.vespa.athenz.api.AthenzService;
import com.yahoo.vespa.athenz.api.AthenzUser;
import com.yahoo.vespa.athenz.api.OktaAccessToken;
import com.yahoo.vespa.athenz.client.zts.ZtsClient;
import com.yahoo.vespa.curator.Lock;
import com.yahoo.vespa.hosted.controller.api.identifiers.UserId;
import com.yahoo.vespa.hosted.controller.api.integration.athenz.AthenzClientFactory;
import com.yahoo.vespa.hosted.controller.athenz.impl.ZmsClientFacade;
import com.yahoo.vespa.hosted.controller.concurrent.Once;
import com.yahoo.vespa.hosted.controller.persistence.CuratorDb;
import com.yahoo.vespa.hosted.controller.tenant.AthenzTenant;
import com.yahoo.vespa.hosted.controller.tenant.Tenant;
import com.yahoo.vespa.hosted.controller.tenant.UserTenant;

import java.time.Duration;
import java.time.Instant;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * A singleton owned by the Controller which contains the methods and state for controlling tenants.
 *
 * @author bratseth
 * @author mpolden
 */
public class TenantController {

    private static final Logger log = Logger.getLogger(TenantController.class.getName());

    private final Controller controller;
    private final CuratorDb curator;
    private final ZmsClientFacade zmsClient;
    private final ZtsClient ztsClient;
    private final AthenzService controllerIdentity;

    public TenantController(Controller controller, CuratorDb curator, AthenzClientFactory athenzClientFactory) {
        this.controller = Objects.requireNonNull(controller, "controller must be non-null");
        this.curator = Objects.requireNonNull(curator, "curator must be non-null");
        this.controllerIdentity = athenzClientFactory.getControllerIdentity();
        this.zmsClient = new ZmsClientFacade(athenzClientFactory.createZmsClient(), controllerIdentity);
        this.ztsClient = athenzClientFactory.createZtsClient();

        // Update serialization format of all tenants
        Once.after(Duration.ofMinutes(1), () -> {
            Instant start = controller.clock().instant();
            int count = 0;
            for (TenantName name : curator.readTenantNames()) {
                try (Lock lock = lock(name)) {
                    // Get while holding lock so that we know we're operating on a current version
                    Optional<Tenant> optionalTenant = tenant(name);
                    if (!optionalTenant.isPresent()) continue; // Deleted while updating, skip

                    Tenant tenant = optionalTenant.get();
                    if (tenant instanceof AthenzTenant) {
                        curator.writeTenant((AthenzTenant) tenant);
                    } else if (tenant instanceof UserTenant) {
                        curator.writeTenant((UserTenant) tenant);
                    } else {
                        throw new IllegalArgumentException("Unknown tenant type: " + tenant.getClass().getSimpleName());
                    }
                }
                count++;
            }
            log.log(Level.INFO, String.format("Wrote %d tenants in %s", count,
                                              Duration.between(start, controller.clock().instant())));
        });
    }

    /** Returns a list of all known tenants sorted by name */
    public List<Tenant> asList() {
        return curator.readTenants().stream()
                      .sorted(Comparator.comparing(Tenant::name))
                      .collect(Collectors.toList());
    }

    /** Returns a list of all tenants accessible by the given user */
    public List<Tenant> asList(UserId user) {
        AthenzUser athenzUser = AthenzUser.fromUserId(user.id());
            Set<AthenzDomain> userDomains = new HashSet<>(ztsClient.getTenantDomains(controllerIdentity, athenzUser, "admin"));
            return asList().stream()
                           .filter(tenant -> isUser(tenant, user) ||
                                             userDomains.stream().anyMatch(domain -> inDomain(tenant, domain)))
                           .collect(Collectors.toList());
    }

    /**
     * Lock a tenant for modification and apply action. Only valid for Athenz tenants as it's the only type that
     * accepts modification.
     */
    public void lockIfPresent(TenantName name, Consumer<LockedTenant> action) {
        try (Lock lock = lock(name)) {
            tenant(name).map(tenant -> {
                tenant = tenant instanceof AthenzTenant ? (AthenzTenant) tenant  : (UserTenant) tenant;
                if (tenant instanceof AthenzTenant) return new LockedTenant((AthenzTenant) tenant, lock);
                else return new LockedTenant((UserTenant) tenant, lock);
            }).ifPresent(action);
        }
    }

    /** Lock a tenant for modification and apply action. Throws if the tenant does not exist */
    public void lockOrThrow(TenantName name, Consumer<LockedTenant> action) {
        try (Lock lock = lock(name)) {
            action.accept(new LockedTenant(requireAthenzTenant(name), lock));
        }
    }

    /** Replace and store any previous version of given tenant */
    public void store(LockedTenant tenant) {
        curator.writeTenant(tenant.get());
    }

    /** Create an user tenant with given username */
    public void create(UserTenant tenant) {
        try (Lock lock = lock(tenant.name())) {
            requireNonExistent(tenant.name());
            curator.writeTenant(tenant);
        }
    }

    /** Create an Athenz tenant */
    public void create(AthenzTenant tenant, OktaAccessToken token) {
        try (Lock lock = lock(tenant.name())) {
            requireNonExistent(tenant.name());
            AthenzDomain domain = tenant.domain();
            Optional<Tenant> existingTenantWithDomain = tenantIn(domain);
            if (existingTenantWithDomain.isPresent()) {
                throw new IllegalArgumentException("Could not create tenant '" + tenant.name().value() +
                                                   "': The Athens domain '" +
                                                   domain.getName() + "' is already connected to tenant '" +
                                                   existingTenantWithDomain.get().name().value() +
                                                   "'");
            }
            zmsClient.createTenant(domain, token);
            curator.writeTenant(tenant);
        }
    }

    /** Returns the tenant in the given Athenz domain, or empty if none */
    private Optional<Tenant> tenantIn(AthenzDomain domain) {
        return asList().stream()
                       .filter(tenant -> inDomain(tenant, domain))
                       .findFirst();
    }

    /** Find tenant by name */
    public Optional<Tenant> tenant(TenantName name) {
        return curator.readTenant(name);
    }

    /** Find tenant by name */
    public Optional<Tenant> tenant(String name) {
        return tenant(TenantName.from(name));
    }

    /** Find Athenz tenant by name */
    public Optional<AthenzTenant> athenzTenant(TenantName name) {
        return curator.readAthenzTenant(name);
    }

    /** Returns Athenz tenant with name or throws if no such tenant exists */
    public AthenzTenant requireAthenzTenant(TenantName name) {
        return athenzTenant(name).orElseThrow(() -> new IllegalArgumentException("Tenant '" + name + "' not found"));
    }

    /** Update Athenz domain for tenant. Returns the updated tenant which must be explicitly stored */
    public LockedTenant withDomain(LockedTenant tenant, AthenzDomain newDomain, OktaAccessToken token) {
        AthenzTenant athenzTenant = (AthenzTenant) tenant.get();
        AthenzDomain existingDomain = athenzTenant.domain();
        if (existingDomain.equals(newDomain)) return tenant;
        Optional<Tenant> existingTenantWithNewDomain = tenantIn(newDomain);
        if (existingTenantWithNewDomain.isPresent())
            throw new IllegalArgumentException("Could not set domain of " + tenant + " to '" + newDomain +
                                               "':" + existingTenantWithNewDomain.get() + " already has this domain");

        zmsClient.createTenant(newDomain, token);
        List<Application> applications = controller.applications().asList(tenant.get().name());
        applications.forEach(a -> zmsClient.addApplication(newDomain, new com.yahoo.vespa.hosted.controller.api.identifiers.ApplicationId(a.id().application().value()), token));
        applications.forEach(a -> zmsClient.deleteApplication(existingDomain, new com.yahoo.vespa.hosted.controller.api.identifiers.ApplicationId(a.id().application().value()), token));
        zmsClient.deleteTenant(existingDomain, token);
        log.info("Set Athenz domain for '" + tenant + "' from '" + existingDomain + "' to '" + newDomain + "'");

        return tenant.with(newDomain);
    }

    /** Delete an user tenant */
    public void deleteTenant(UserTenant tenant) {
        try (Lock lock = lock(tenant.name())) {
            deleteTenant(tenant.name());
        }
    }

    /** Delete an Athenz tenant */
    public void deleteTenant(AthenzTenant tenant, OktaAccessToken token) {
        try (Lock lock = lock(tenant.name())) {
            deleteTenant(tenant.name());
            zmsClient.deleteTenant(tenant.domain(), token);
        }
    }

    private void deleteTenant(TenantName name) {
        if (!controller.applications().asList(name).isEmpty()) {
            throw new IllegalArgumentException("Could not delete tenant '" + name.value()
                                               + "': This tenant has active applications");
        }
        curator.removeTenant(name);
    }

    private void requireNonExistent(TenantName name) {
        if (tenant(name).isPresent() ||
            // Underscores are allowed in existing Athenz tenant names, but tenants with - and _ cannot co-exist. E.g.
            // my-tenant cannot be created if my_tenant exists.
            tenant(dashToUnderscore(name.value())).isPresent()) {
            throw new IllegalArgumentException("Tenant '" + name + "' already exists");
        }
    }

    /**
     * Returns a lock which provides exclusive rights to changing this tenant.
     * Any operation which stores a tenant need to first acquire this lock, then read, modify
     * and store the tenant, and finally release (close) the lock.
     */
    private Lock lock(TenantName tenant) {
        return curator.lock(tenant);
    }

    private static boolean inDomain(Tenant tenant, AthenzDomain domain) {
        return tenant instanceof AthenzTenant && ((AthenzTenant) tenant).in(domain);
    }

    private static boolean isUser(Tenant tenant, UserId userId) {
        return tenant instanceof UserTenant && ((UserTenant) tenant).is(userId.id());
    }

    private static String dashToUnderscore(String s) {
        return s.replace('-', '_');
    }

}