summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/athenz/impl/ZmsClientImpl.java
blob: 6fc0558a5a1c61c45f3cadc6f49f469f206ce6c6 (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
// 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.auth.util.Crypto;
import com.yahoo.athenz.zms.DomainList;
import com.yahoo.athenz.zms.ProviderResourceGroupRoles;
import com.yahoo.athenz.zms.PublicKeyEntry;
import com.yahoo.athenz.zms.ServiceIdentity;
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.hosted.controller.api.identifiers.ApplicationId;
import com.yahoo.vespa.hosted.controller.api.identifiers.AthensDomain;
import com.yahoo.vespa.hosted.controller.athenz.ApplicationAction;
import com.yahoo.vespa.hosted.controller.athenz.AthenzPrincipal;
import com.yahoo.vespa.hosted.controller.athenz.AthenzPublicKey;
import com.yahoo.vespa.hosted.controller.athenz.AthenzService;
import com.yahoo.vespa.hosted.controller.athenz.ZmsClient;
import com.yahoo.vespa.hosted.controller.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(AthensDomain tenantDomain) {
        log("putTenancy(tenantDomain=%s, service=%s)", tenantDomain, service);
        runOrThrow(() -> {
            Tenancy tenancy = new Tenancy()
                    .setDomain(tenantDomain.id())
                    .setService(service.toFullServiceName())
                    .setResourceGroups(Collections.emptyList());
            zmsClient.putTenancy(tenantDomain.id(), service.toFullServiceName(), /*auditref*/null, tenancy);
        });
    }

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

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

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

    @Override
    public boolean hasApplicationAccess(
            AthenzPrincipal principal, ApplicationAction action, AthensDomain tenantDomain, ApplicationId applicationName) {
        return hasAccess(
                action.name(), applicationResourceString(tenantDomain, applicationName), principal);
    }

    @Override
    public boolean hasTenantAdminAccess(AthenzPrincipal principal, AthensDomain tenantDomain) {
        return hasAccess(TenantAction._modify_.name(), tenantResourceString(tenantDomain), principal);
    }

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

    @Override
    public List<AthensDomain> 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 toAthensDomains(domainList.getNames());
                });
    }

    @Override
    public AthenzPublicKey getPublicKey(AthenzService service, String keyId) {
        log("getPublicKeyEntry(domain=%s, service=%s, keyId=%s)", service.getDomain().id(), service.getServiceName(), keyId);
        return getOrThrow(() -> {
            PublicKeyEntry entry = zmsClient.getPublicKeyEntry(service.getDomain().id(), service.getServiceName(), keyId);
            return fromYbase64EncodedKey(entry.getKey(), keyId);
        });
    }

    @Override
    public List<AthenzPublicKey> getPublicKeys(AthenzService service) {
        log("getServiceIdentity(domain=%s, service=%s)", service.getDomain().id(), service.getServiceName());
        return getOrThrow(() -> {
            ServiceIdentity serviceIdentity = zmsClient.getServiceIdentity(service.getDomain().id(), service.getServiceName());
            return toAthensPublicKeys(serviceIdentity.getPublicKeys());
        });
    }

    private static AthenzPublicKey fromYbase64EncodedKey(String encodedKey, String keyId) {
        return new AthenzPublicKey(Crypto.loadPublicKey(Crypto.ybase64DecodeString(encodedKey)), keyId);
    }

    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<AthensDomain> toAthensDomains(List<String> domains) {
        return domains.stream().map(AthensDomain::new).collect(toList());
    }

    private static List<AthenzPublicKey> toAthensPublicKeys(List<PublicKeyEntry> publicKeys) {
        return publicKeys.stream()
                .map(entry -> fromYbase64EncodedKey(entry.getKey(), entry.getId()))
                .collect(toList());
    }

    private boolean hasAccess(String action, String resource, AthenzPrincipal principal) {
        log("getAccess(action=%s, resource=%s, principal=%s)", action, resource, principal);
        return getOrThrow(
                () -> zmsClient.getAccess(action, resource, /*trustDomain*/null, principal.toYRN()).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);
        }
    }

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

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

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

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

    private String applicationResourceString(AthensDomain 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_
    }

}