summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/athenz/impl/ZtsClientImpl.java
blob: 76c000936e310ce206ac80411fa723f3b194daa8 (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
// 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.zts.TenantDomains;
import com.yahoo.athenz.zts.ZTSClient;
import com.yahoo.athenz.zts.ZTSClientException;
import com.yahoo.log.LogLevel;
import com.yahoo.vespa.hosted.controller.api.identifiers.AthenzDomain;
import com.yahoo.vespa.hosted.controller.api.integration.athenz.AthenzIdentity;
import com.yahoo.vespa.hosted.controller.api.integration.athenz.AthenzService;
import com.yahoo.vespa.hosted.controller.api.integration.athenz.ZtsClient;
import com.yahoo.vespa.hosted.controller.api.integration.athenz.ZtsException;
import com.yahoo.vespa.hosted.controller.athenz.config.AthenzConfig;

import java.util.List;
import java.util.logging.Logger;

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

/**
 * @author bjorncs
 */
public class ZtsClientImpl implements ZtsClient {

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

    private final ZTSClient ztsClient;
    private final AthenzService service;

    public ZtsClientImpl(ZTSClient ztsClient, AthenzConfig config) {
        this.ztsClient = ztsClient;
        this.service = new AthenzService(config.domain(), config.service().name());
    }

    @Override
    public List<AthenzDomain> getTenantDomainsForUser(AthenzIdentity identity) {
        log.log(LogLevel.DEBUG, String.format(
                "getTenantDomains(domain=%s, identity=%s, rolename=admin, service=%s)",
                service.getDomain().id(), identity.getFullName(), service.getFullName()));
        try {
            TenantDomains domains = ztsClient.getTenantDomains(
                    service.getDomain().id(), identity.getFullName(), "admin", service.getName());
            return domains.getTenantDomainNames().stream()
                    .map(AthenzDomain::new)
                    .collect(toList());
        } catch (ZTSClientException e) {
            throw new ZtsException(e.getCode(), e);
        }
    }

}