aboutsummaryrefslogtreecommitdiffstats
path: root/controller-api/src/main/java/com/yahoo/vespa/hosted/controller/api/role/Policy.java
blob: 15d8d8dfdbeeee80d5810055d34fbc214e293be8 (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// 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.ApplicationName;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.TenantName;

import java.net.URI;
import java.util.Set;

/**
 * Policies for REST APIs in the controller. A policy is only considered when defined in a {@link Role}.
 *
 * - A policy describes a set of {@link Privilege}s, which are valid for a set of {@link SystemName}s.
 * - A policy is evaluated by an {@link Enforcer}, which holds the {@link SystemName} the evaluation is done in.
 * - A policy is evaluated in a {@link Context}, which may limit it to a specific {@link TenantName} or
 *   {@link ApplicationName}.
 *
 * @author mpolden
 */
enum Policy {

    /** Full access to everything. */
    operator(Privilege.grant(Action.all())
                      .on(PathGroup.allExcept(PathGroup.operatorRestrictedPaths()))
                      .in(SystemName.all()),
             Privilege.grant(Action.read)
                      .on(PathGroup.billingPathsNoToken())
                      .in(SystemName.all()),
             Privilege.grant(Action.read)
                      .on(PathGroup.billingToken)
                      .in(SystemName.PublicCd)),

    /** Full access to everything. */
    supporter(Privilege.grant(Action.read)
                       .on(PathGroup.allExcept(PathGroup.classifiedOperator, PathGroup.applicationRouting))
                       .in(SystemName.all()),
              Privilege.grant(Action.all())
                       .on(PathGroup.classifiedOperator, PathGroup.applicationRouting)
                       .in(SystemName.all())),

    /** Full access to user management for a tenant in select systems. */
    tenantManager(Privilege.grant(Action.all())
                           .on(PathGroup.tenantUsers)
                           .in(SystemName.all())),

    /** Full access to user management for an application in select systems. */
    applicationManager(Privilege.grant(Action.all())
                                .on(PathGroup.applicationUsers)
                                .in(SystemName.all())),

    /** Access to create a user tenant in select systems. */
    user(Privilege.grant(Action.create, Action.update)
                  .on(PathGroup.user)
                  .in(SystemName.main, SystemName.cd)),

    /** Access to create a tenant. */
    tenantCreate(Privilege.grant(Action.create)
                          .on(PathGroup.tenant)
                          .in(SystemName.all())),

    /** Full access to tenant information and settings. */
    tenantDelete(Privilege.grant(Action.delete)
                          .on(PathGroup.tenant)
                          .in(SystemName.all())),

    /** Full access to tenant information and settings. */
    tenantUpdate(Privilege.grant(Action.update)
                          .on(PathGroup.tenantInfo)
                          .on(PathGroup.tenant)
                          .in(SystemName.all())),

    /** Read access to tenant information and settings. */
    tenantRead(Privilege.grant(Action.read)
                        .on(PathGroup.tenant, PathGroup.tenantInfo, PathGroup.tenantUsers, PathGroup.applicationUsers)
                        .in(SystemName.all())),

    /** Access to set and unset archive access role under a tenant. */
    tenantArchiveAccessManagement(Privilege.grant(Action.update, Action.delete)
                                           .on(PathGroup.tenantArchiveAccess)
                                           .in(SystemName.all())),

    /** Access to create application under a certain tenant. */
    applicationCreate(Privilege.grant(Action.create)
                               .on(PathGroup.application)
                               .in(SystemName.all())),

    /** Read access to application information and settings. */
    applicationRead(Privilege.grant(Action.read)
                             .on(PathGroup.application, PathGroup.applicationInfo, PathGroup.applicationRouting, PathGroup.reindexing, PathGroup.serviceDump, PathGroup.dropDocuments)
                             .in(SystemName.all())),

    /** Update access to application information and settings. */
    applicationUpdate(Privilege.grant(Action.update)
                               .on(PathGroup.application, PathGroup.applicationInfo, PathGroup.applicationRouting)
                               .in(SystemName.all())),

    /** Access to delete a certain application. */
    applicationDelete(Privilege.grant(Action.delete)
                               .on(PathGroup.application)
                               .in(SystemName.all())),

    /** Full access to application information and settings. */
    applicationOperations(Privilege.grant(Action.write())
                                   .on(PathGroup.applicationInfo, PathGroup.applicationRouting, PathGroup.productionRestart, PathGroup.reindexing, PathGroup.serviceDump, PathGroup.dropDocuments)
                                   .in(SystemName.all())),

    /** Access to create and delete developer and deploy keys under a tenant. */
    keyManagement(Privilege.grant(Action.write())
                           .on(PathGroup.tenantKeys, PathGroup.applicationKeys)
                           .in(SystemName.all())),

    /** Access to revoke keys from the tenant */
    keyRevokal(Privilege.grant(Action.delete)
                        .on(PathGroup.tenantKeys, PathGroup.applicationKeys)
                        .in(SystemName.all())),

    /** Full access to application development deployments. */
    developmentDeployment(Privilege.grant(Action.all())
                                   .on(PathGroup.developmentDeployment, PathGroup.developmentRestart)
                                   .in(SystemName.all())),

    /** Read access to all application deployments. */
    deploymentRead(Privilege.grant(Action.read)
                            .on(PathGroup.developmentDeployment, PathGroup.productionDeployment)
                            .in(SystemName.all())),

    /** Full access to submissions for continuous deployment. */
    submission(Privilege.grant(Action.all())
                        .on(PathGroup.submission)
                        .in(SystemName.all())),

    /** Read access to all information in select systems. */
    classifiedRead(Privilege.grant(Action.read)
                            .on(PathGroup.allExcept(PathGroup.classifiedOperator))
                            .in(SystemName.main, SystemName.cd)),

    /** Read access to public info. */
    publicRead(Privilege.grant(Action.read)
                        .on(PathGroup.publicInfo)
                        .in(SystemName.all())),

    /** Access to /system-flags/v1/deploy. */
    systemFlagsDeploy(Privilege.grant(Action.update)
                                  .on(PathGroup.systemFlagsDeploy)
                                  .in(SystemName.all())),

    /** Access to /system-flags/v1/dryrun. */
    systemFlagsDryrun(Privilege.grant(Action.update)
                                  .on(PathGroup.systemFlagsDryrun)
                                  .in(SystemName.all())),

    /** Access to /payment/notification */
    paymentProcessor(Privilege.grant(Action.create)
                                .on(PathGroup.paymentProcessor)
                                .in(SystemName.PublicCd)),

    /** Read your own instrument information */
    paymentInstrumentRead(Privilege.grant(Action.read)
                                   .on(PathGroup.billingInstrument)
                                   .in(SystemName.PublicCd, SystemName.Public)),

    /** Ability to update tenant payment instrument */
    paymentInstrumentUpdate(Privilege.grant(Action.update)
                                     .on(PathGroup.billingInstrument)
                                     .in(SystemName.PublicCd, SystemName.Public)),

    /** Ability to remove your own payment instrument */
    paymentInstrumentDelete(Privilege.grant(Action.delete)
                                     .on(PathGroup.billingInstrument)
                                     .in(SystemName.PublicCd, SystemName.Public)),

    /** Get the token to view instrument form */
    paymentInstrumentCreate(Privilege.grant(Action.read)
                                    .on(PathGroup.billingToken)
                                    .in(SystemName.PublicCd, SystemName.Public)),

    /** Ability to update tenant payment instrument */
    planUpdate(Privilege.grant(Action.update)
            .on(PathGroup.billingPlan, PathGroup.billing)
            .in(SystemName.PublicCd, SystemName.Public)),

    /** Ability to update tenant collection method */
    collectionMethodUpdate(Privilege.grant(Action.update)
            .on(PathGroup.billingCollection)
            .in(SystemName.PublicCd, SystemName.Public)),


    /** Read the generated bills */
    billingInformationRead(Privilege.grant(Action.read)
                                    .on(PathGroup.billingList, PathGroup.billing)
                                    .in(SystemName.PublicCd, SystemName.Public)),

    accessRequests(Privilege.grant(Action.all())
            .on(PathGroup.accessRequests, PathGroup.accessRequestApproval)
            .in(SystemName.PublicCd, SystemName.Public)),

    /** Invoice management */
    hostedAccountant(Privilege.grant(Action.all())
                                    .on(PathGroup.hostedAccountant, PathGroup.accountant, PathGroup.userSearch)
                                    .in(SystemName.PublicCd, SystemName.Public)),

    /** Listing endpoint certificates and re-requesting certificates */
    endpointCertificateApi(Privilege.grant(Action.all())
            .on(PathGroup.endpointCertificates)
            .in(SystemName.all())),

    /** Secret store operations */
    secretStoreOperations(Privilege.grant(Action.all())
                                .on(PathGroup.secretStore)
                                .in(SystemName.PublicCd, SystemName.Public)),

    horizonProxyOperations(Privilege.grant(Action.all())
            .on(PathGroup.horizonProxy)
            .in(SystemName.PublicCd, SystemName.Public)),

    emailVerification(Privilege.grant(Action.create)
                        .on(PathGroup.emailVerification)
                        .in(SystemName.PublicCd, SystemName.Public)),

    dataplaneToken(Privilege.grant(Action.all())
                           .on(PathGroup.dataplaneToken)
                           .in(SystemName.PublicCd, SystemName.Public));

    private final Set<Privilege> privileges;

    Policy(Privilege... privileges) {
        this.privileges = Set.of(privileges);
    }

    /** Returns whether action is allowed on path in given context */
    boolean evaluate(Action action, URI uri, Context context, SystemName system) {
        return privileges.stream().anyMatch(privilege -> privilege.actions().contains(action) &&
                                                         privilege.systems().contains(system) &&
                                                         privilege.pathGroups().stream()
                                                                  .anyMatch(pg -> pg.matches(uri, context)));
    }

}