summaryrefslogtreecommitdiffstats
path: root/controller-server/src/main/java/com/yahoo/vespa/hosted/controller/restapi/application/DeployAuthorizer.java
blob: 2e6276767662e643dc36b697f17868f20ef675d2 (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
// 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.restapi.application;

import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Environment;
import com.yahoo.vespa.hosted.controller.api.Tenant;
import com.yahoo.vespa.hosted.controller.api.identifiers.AthenzDomain;
import com.yahoo.vespa.hosted.controller.api.integration.zone.ZoneRegistry;
import com.yahoo.vespa.hosted.controller.application.ApplicationPackage;
import com.yahoo.vespa.hosted.controller.athenz.ApplicationAction;
import com.yahoo.vespa.hosted.controller.athenz.AthenzClientFactory;
import com.yahoo.vespa.hosted.controller.athenz.AthenzPrincipal;
import com.yahoo.vespa.hosted.controller.athenz.AthenzUtils;
import com.yahoo.vespa.hosted.controller.athenz.ZmsException;

import javax.ws.rs.ForbiddenException;
import javax.ws.rs.NotAuthorizedException;
import java.security.Principal;
import java.util.Objects;
import java.util.logging.Logger;

import static com.yahoo.vespa.hosted.controller.restapi.application.Authorizer.environmentRequiresAuthorization;

/**
 * @author bjorncs
 * @author gjoranv
 */
public class DeployAuthorizer {

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

    private final ZoneRegistry zoneRegistry;
    private final AthenzClientFactory athenzClientFactory;

    public DeployAuthorizer(ZoneRegistry zoneRegistry, AthenzClientFactory athenzClientFactory) {
        this.zoneRegistry = zoneRegistry;
        this.athenzClientFactory = athenzClientFactory;
    }

    public void throwIfUnauthorizedForDeploy(Principal principal,
                                             Environment environment,
                                             Tenant tenant,
                                             ApplicationId applicationId,
                                             ApplicationPackage applicationPackage) {
        // Validate that domain in identity configuration (deployment.xml) is same as tenant domain
        applicationPackage.deploymentSpec().athenzDomain().ifPresent(identityDomain -> {
            AthenzDomain tenantDomain = tenant.getAthensDomain().orElseThrow(() -> new IllegalArgumentException("Identity provider only available to Athenz onboarded tenants"));
            if (! Objects.equals(tenantDomain.id(), identityDomain.value())) {
                throw new ForbiddenException(
                        String.format(
                                "Athenz domain in deployment.xml: [%s] must match tenant domain: [%s]",
                                identityDomain.value(),
                                tenantDomain.id()
                        ));
            }
        });

        if (!environmentRequiresAuthorization(environment)) {
            return;
        }

        if (principal == null) {
            throw loggedUnauthorizedException("Principal not authenticated!");
        }

        if (!(principal instanceof AthenzPrincipal)) {
            throw loggedUnauthorizedException(
                    "Principal '%s' of type '%s' is not an Athenz principal, which is required for production deployments.",
                    principal.getName(), principal.getClass().getSimpleName());
        }

        AthenzPrincipal athenzPrincipal = (AthenzPrincipal) principal;
        AthenzDomain principalDomain = athenzPrincipal.getDomain();

        if (!principalDomain.equals(AthenzUtils.SCREWDRIVER_DOMAIN)) {
            throw loggedForbiddenException(
                    "Principal '%s' is not a Screwdriver principal. Excepted principal with Athenz domain '%s', got '%s'.",
                    principal.getName(), AthenzUtils.SCREWDRIVER_DOMAIN.id(), principalDomain.id());
        }

        // NOTE: no fine-grained deploy authorization for non-Athenz tenants
        if (tenant.isAthensTenant()) {
            AthenzDomain tenantDomain = tenant.getAthensDomain().get();
            if (!hasDeployAccessToAthenzApplication(athenzPrincipal, tenantDomain, applicationId)) {
                throw loggedForbiddenException(
                        "Screwdriver principal '%1$s' does not have deploy access to '%2$s'. " +
                        "Either the application has not been created at " + zoneRegistry.getDashboardUri() + " or " +
                        "'%1$s' is not added to the application's deployer role in Athenz domain '%3$s'.",
                        athenzPrincipal.toYRN(), applicationId, tenantDomain.id());
            }
        }
    }

    private static ForbiddenException loggedForbiddenException(String message, Object... args) {
        String formattedMessage = String.format(message, args);
        log.info(formattedMessage);
        return new ForbiddenException(formattedMessage);
    }

    private static NotAuthorizedException loggedUnauthorizedException(String message, Object... args) {
        String formattedMessage = String.format(message, args);
        log.info(formattedMessage);
        return new NotAuthorizedException(formattedMessage);
    }

    private boolean hasDeployAccessToAthenzApplication(AthenzPrincipal principal, AthenzDomain domain, ApplicationId applicationId) {
        try {
            return athenzClientFactory.createZmsClientWithServicePrincipal()
                    .hasApplicationAccess(
                            principal,
                            ApplicationAction.deploy,
                            domain,
                            new com.yahoo.vespa.hosted.controller.api.identifiers.ApplicationId(applicationId.application().value()));
        } catch (ZmsException e) {
            throw loggedForbiddenException(
                    "Failed to authorize deployment through Athenz. If this problem persists, " +
                            "please create ticket at yo/vespa-support. (" + e.getMessage() + ")");
        }
    }
}