summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/athenz/impl/ZmsClientImpl.java
blob: 6179d9891fdcb9d243f84ac87efdbe5c2d8dec7f (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
// 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.athenz.impl;

import com.yahoo.athenz.zms.DomainList;
import com.yahoo.athenz.zms.ProviderResourceGroupRoles;
import com.yahoo.athenz.zms.Tenancy;
import com.yahoo.athenz.zms.TenantRoleAction;
import com.yahoo.athenz.zms.ZMSClient;
import com.yahoo.athenz.zms.ZMSClientException;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.athenz.api.AthenzDomain;
import com.yahoo.vespa.athenz.api.AthenzIdentity;
import com.yahoo.vespa.athenz.api.AthenzService;
import com.yahoo.vespa.hosted.controller.api.identifiers.ApplicationId;
import com.yahoo.vespa.hosted.controller.api.integration.athenz.ApplicationAction;
import com.yahoo.vespa.hosted.controller.api.integration.athenz.ZmsClient;
import com.yahoo.vespa.hosted.controller.api.integration.athenz.ZmsException;
import com.yahoo.vespa.hosted.controller.athenz.config.AthenzConfig;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Supplier;
import java.util.logging.Logger;

import static java.util.stream.Collectors.toList;

/**
 * @author bjorncs
 */
public class ZmsClientImpl implements ZmsClient {

    private static final Logger log = Logger.getLogger(ZmsClientImpl.class.getName());
    private final ZMSClient zmsClient;
    private final AthenzService service;

    ZmsClientImpl(ZMSClient zmsClient, AthenzConfig config) {
        this.zmsClient = zmsClient;
        this.service = new AthenzService(config.domain(), config.service().name());
    }

    @Override
    public void createTenant(AthenzDomain tenantDomain) {
        log("putTenancy(tenantDomain=%s, service=%s)", tenantDomain, service);
        runOrThrow(() -> {
            Tenancy tenancy = new Tenancy()
                    .setDomain(tenantDomain.getName())
                    .setService(service.getFullName())
                    .setResourceGroups(Collections.emptyList());
            zmsClient.putTenancy(tenantDomain.getName(), service.getFullName(), /*auditref*/null, tenancy);
        });
    }

    @Override
    public void deleteTenant(AthenzDomain tenantDomain) {
        log("deleteTenancy(tenantDomain=%s, service=%s)", tenantDomain, service);
        runOrThrow(() -> zmsClient.deleteTenancy(tenantDomain.getName(), service.getFullName(), /*auditref*/null));
    }

    @Override
    public void addApplication(AthenzDomain tenantDomain, ApplicationId applicationName) {
        List<TenantRoleAction> tenantRoleActions = createTenantRoleActions();
        log("putProviderResourceGroupRoles(" +
                        "tenantDomain=%s, providerDomain=%s, service=%s, resourceGroup=%s, roleActions=%s)",
                tenantDomain, service.getDomain().getName(), service.getName(), applicationName, tenantRoleActions);
        runOrThrow(() -> {
            ProviderResourceGroupRoles resourceGroupRoles = new ProviderResourceGroupRoles()
                    .setDomain(service.getDomain().getName())
                    .setService(service.getName())
                    .setTenant(tenantDomain.getName())
                    .setResourceGroup(applicationName.id())
                    .setRoles(tenantRoleActions);
            zmsClient.putProviderResourceGroupRoles(
                    tenantDomain.getName(), service.getDomain().getName(), service.getName(),
                    applicationName.id(), /*auditref*/null, resourceGroupRoles);
        });
    }

    @Override
    public void deleteApplication(AthenzDomain tenantDomain, ApplicationId applicationName) {
        log("deleteProviderResourceGroupRoles(tenantDomain=%s, providerDomain=%s, service=%s, resourceGroup=%s)",
                tenantDomain, service.getDomain().getName(), service.getName(), applicationName);
        runOrThrow(() -> {
            zmsClient.deleteProviderResourceGroupRoles(
                    tenantDomain.getName(), service.getDomain().getName(), service.getName(), applicationName.id(), /*auditref*/null);
        });
    }

    @Override
    public boolean hasApplicationAccess(
            AthenzIdentity identity, ApplicationAction action, AthenzDomain tenantDomain, ApplicationId applicationName) {
        return hasAccess(
                action.name(), applicationResourceString(tenantDomain, applicationName), identity);
    }

    @Override
    public boolean hasTenantAdminAccess(AthenzIdentity identity, AthenzDomain tenantDomain) {
        return hasAccess(TenantAction._modify_.name(), tenantResourceString(tenantDomain), identity);
    }

    @Override
    public boolean hasHostedOperatorAccess(AthenzIdentity identity) {
        return getOrThrow(() -> hasAccess("modify", service.getDomain().getName() + ":hosted-vespa", identity));
    }

    /**
     * Used when creating tenancies. As there are no tenancy policies at this point,
     * we cannot use {@link #hasTenantAdminAccess(AthenzIdentity, AthenzDomain)}
     */
    @Override
    public boolean isDomainAdmin(AthenzIdentity identity, AthenzDomain domain) {
        log("getMembership(domain=%s, role=%s, principal=%s)", domain, "admin", identity);
        return getOrThrow(
                () -> zmsClient.getMembership(domain.getName(), "admin", identity.getFullName()).getIsMember());
    }

    @Override
    public List<AthenzDomain> getDomainList(String prefix) {
        log.log(LogLevel.DEBUG, String.format("getDomainList(prefix=%s)", prefix));
        return getOrThrow(
                () -> {
                    DomainList domainList = zmsClient.getDomainList(
                            /*limit*/null, /*skip*/null, prefix, /*depth*/null, /*domain*/null,
                            /*productId*/ null, /*modifiedSince*/null);
                    return toAthenzDomains(domainList.getNames());
                });
    }

    private static List<TenantRoleAction> createTenantRoleActions() {
        return Arrays.stream(ApplicationAction.values())
                .map(action -> new TenantRoleAction().setAction(action.name()).setRole(action.roleName))
                .collect(toList());
    }

    private static List<AthenzDomain> toAthenzDomains(List<String> domains) {
        return domains.stream().map(AthenzDomain::new).collect(toList());
    }

    private boolean hasAccess(String action, String resource, AthenzIdentity identity) {
        log("getAccess(action=%s, resource=%s, principal=%s)", action, resource, identity);
        return getOrThrow(
                () -> zmsClient.getAccess(action, resource, /*trustDomain*/null, identity.getFullName())
                        .getGranted());
    }

    private static void log(String format, Object... args) {
        log.log(LogLevel.DEBUG, String.format(format, args));
    }

    private static void runOrThrow(Runnable wrappedCode) {
        try {
            wrappedCode.run();
        } catch (ZMSClientException e) {
            logWarning(e);
            throw new ZmsException(e.getCode(), e);
        }
    }

    private static <T> T getOrThrow(Supplier<T> wrappedCode) {
        try {
            return wrappedCode.get();
        } catch (ZMSClientException e) {
            logWarning(e);
            throw new ZmsException(e.getCode(), e);
        }
    }

    private static void logWarning(ZMSClientException e) {
        log.warning("Error from Athenz: " + e.getMessage());
    }

    private String resourceStringPrefix(AthenzDomain tenantDomain) {
        return String.format("%s:service.%s.tenant.%s",
                             service.getDomain().getName(), service.getName(), tenantDomain.getName());
    }

    private String tenantResourceString(AthenzDomain tenantDomain) {
        return resourceStringPrefix(tenantDomain) + ".wildcard";
    }

    private String applicationResourceString(AthenzDomain tenantDomain, ApplicationId applicationName) {
        return resourceStringPrefix(tenantDomain) + "." + "res_group" + "." + applicationName.id() + ".wildcard";
    }

    private enum TenantAction {
        // This is meant to match only the '*' action of the 'admin' role.
        // If needed, we can replace it with 'create', 'delete' etc. later.
        _modify_
    }

}