aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/role/Enforcer.java
blob: 8b7d0d0b4fb33340c3693c212b8a0df6fd8df027 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.api.role;

import com.yahoo.config.provision.SystemName;

import java.net.URI;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

/**
 * Checks whether {@link Role}s have the required {@link Privilege}s to perform {@link Action}s on given {@link java.net.URI}s.
 *
 * @author jonmv
 */
public class Enforcer {

    private static final Logger logger = Logger.getLogger(Enforcer.class.getName());

    private final SystemName system;
    public Enforcer(SystemName system) {
        this.system = system;
    }

    /** Returns whether {@code role} has permission to perform {@code action} on {@code resource}, in this enforcer's system. */
    public boolean allows(Role role, Action action, URI resource) {
        List<Policy> matchingPolicies = role.definition().policies().stream()
                .filter(policy -> policy.evaluate(action, resource, role.context, system))
                .toList();
        logger.log(Level.FINE, "Matching policies for " +
                               "role: " + role.definition().name() + ", "+
                               "action " + action.name() + ", " +
                               resource.getPath() + " : " +
                               matchingPolicies.stream().map(Enum::name).collect(Collectors.joining()));
        return !matchingPolicies.isEmpty();
    }

}