aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/role/Privilege.java
blob: 3006dce11abb2b6320e8a3eebb3f8e951cc4b71b (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
// Copyright Yahoo. 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.util.EnumSet;
import java.util.LinkedHashSet;
import java.util.Objects;
import java.util.Set;

/**
 * This describes a privilege in the controller. A privilege expresses the actions (e.g. create or read) granted
 * for a particular group of REST API paths. A privilege is valid in one or more systems.
 *
 * @author mpolden
 */
class Privilege {

    private final Set<SystemName> systems;
    private final Set<Action> actions;
    private final Set<PathGroup> pathGroups;

    private Privilege(Set<SystemName> systems, Set<Action> actions, Set<PathGroup> pathGroups) {
        this.systems = EnumSet.copyOf(Objects.requireNonNull(systems, "system must be non-null"));
        this.actions = EnumSet.copyOf(Objects.requireNonNull(actions, "actions must be non-null"));
        this.pathGroups = EnumSet.copyOf(Objects.requireNonNull(pathGroups, "pathGroups must be non-null"));
        if (systems.isEmpty()) {
            throw new IllegalArgumentException("systems must be non-empty");
        }
    }

    /** Systems where this applies */
    Set<SystemName> systems() {
        return systems;
    }

    /** Actions allowed by this */
    Set<Action> actions() {
        return actions;
    }

    /** Path groups where this applies */
    Set<PathGroup> pathGroups() {
        return pathGroups;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Privilege privilege = (Privilege) o;
        return systems.equals(privilege.systems) &&
               actions.equals(privilege.actions) &&
               pathGroups.equals(privilege.pathGroups);
    }

    @Override
    public int hashCode() {
        return Objects.hash(systems, actions, pathGroups);
    }

    static PrivilegeBuilder grant(Action... actions) {
        return grant(Set.of(actions));
    }

    static PrivilegeBuilder grant(Set<Action> actions) {
        return new PrivilegeBuilder(actions);
    }

    static class PrivilegeBuilder {

        private Set<Action> actions;
        private Set<PathGroup> pathGroups;

        private PrivilegeBuilder(Set<Action> actions) {
            this.actions = EnumSet.copyOf(actions);
            this.pathGroups = new LinkedHashSet<>();
        }

        PrivilegeBuilder on(PathGroup... pathGroups) {
            return on(Set.of(pathGroups));
        }

        PrivilegeBuilder on(Set<PathGroup> pathGroups) {
            this.pathGroups.addAll(pathGroups);
            return this;
        }

        Privilege in(SystemName... systems) {
            return in(Set.of(systems));
        }

        Privilege in(Set<SystemName> systems) {
            return new Privilege(systems, actions, pathGroups);
        }

    }

}