forked from jenkinsci/matrix-auth-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAuthorizationProperty.java
More file actions
270 lines (243 loc) · 10.1 KB
/
AuthorizationProperty.java
File metadata and controls
270 lines (243 loc) · 10.1 KB
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
/*
* The MIT License
*
* Copyright (c) 2004-2017 Sun Microsystems, Inc., Kohsuke Kawaguchi, Yahoo! Inc., Peter Hayes, Tom Huybrechts, Daniel Beck
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.matrixauth;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.model.Describable;
import hudson.model.Descriptor;
import hudson.security.Permission;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.stream.Collectors;
import jenkins.model.Jenkins;
import org.jenkinsci.Symbol;
import org.jenkinsci.plugins.matrixauth.inheritance.InheritGlobalStrategy;
import org.jenkinsci.plugins.matrixauth.inheritance.InheritanceStrategy;
import org.jenkinsci.plugins.matrixauth.inheritance.NonInheritingStrategy;
import org.jenkinsci.plugins.matrixauth.integrations.PermissionFinder;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
@Restricted(NoExternalUse.class)
public interface AuthorizationProperty<T extends Describable<T>> extends AuthorizationContainer<T> {
void setInheritanceStrategy(InheritanceStrategy inheritanceStrategy);
InheritanceStrategy getInheritanceStrategy();
/**
* Sets the flag to block inheritance.
*
* Since the introduction of inheritance strategies, set the inheritance
* strategy roughly matching the previous behavior, i.e. {@code false} will
* set the {@link NonInheritingStrategy}, {@code true} will set the
* {@link InheritGlobalStrategy}.
*
* Note that for items nested inside folders, this will change behavior significantly.
*
* @since 2.0
* @deprecated Use {@link InheritanceStrategy} instead.
*/
@Deprecated
default void setBlocksInheritance(boolean blocksInheritance) {
if (blocksInheritance) {
setInheritanceStrategy(new NonInheritingStrategy());
} else {
setInheritanceStrategy(new InheritGlobalStrategy());
}
}
/**
* Returns true if the authorization matrix is configured to block
* inheritance from the parent.
*
* Since the introduction of inheritance strategies, returns {@code true}
* if and only if the selected inheritance strategy is {@link NonInheritingStrategy}.
*
* @since 2.0
* @deprecated Use {@link #getInheritanceStrategy()} instead.
*/
@Deprecated
default boolean isBlocksInheritance() {
return getInheritanceStrategy() instanceof NonInheritingStrategy;
}
/**
* Set entries from DSL in Job DSL or Pipeline plugins.
*
* @param entries list of entries to use for permission assignment
*/
@Restricted(DoNotUse.class)
default void setEntries(List<DslEntry> entries) {
for (DslEntry entry : entries) {
entry.addPermission(this);
}
}
/**
* Getter supporting nicer DSL syntax for Job DSL and Pipeline job property definitions.
* @return a list of {@link DslEntry}
*/
default List<DslEntry> getEntries() {
final Map<PermissionEntry, SortedSet<String>> mapping = new HashMap<>();
getGrantedPermissionEntries()
.forEach((permission, value) ->
value.forEach(sid -> mapping.computeIfAbsent(sid, unused -> new TreeSet<>())
.add(permission.group.getId() + "/" + permission.name)));
return mapping.entrySet().stream()
.map(e -> {
final PermissionEntry key = e.getKey();
if (key.getType() == AuthorizationType.USER) {
return new DslUser(key.getSid(), new ArrayList<>(e.getValue()));
}
if (key.getType() == AuthorizationType.GROUP) {
return new DslGroup(key.getSid(), new ArrayList<>(e.getValue()));
}
if (key.getType() == AuthorizationType.EITHER) {
return new DslUserOrGroup(key.getSid(), new ArrayList<>(e.getValue()));
}
throw new IllegalStateException("Got unexpected key type in: " + key);
})
.distinct()
.sorted()
.collect(Collectors.toList());
}
/**
* Common superclass for {@link DslUser}, {@link DslGroup}, and {@link DslUserOrGroup}, supporting nicer DSLs
* for Job DSL and Pipeline Job definitions/reconfigurations.
* Job DSL and Pipeline use this for {@link hudson.security.AuthorizationMatrixProperty}.
* Job DSL additionally uses this for {@link com.cloudbees.hudson.plugins.folder.properties.AuthorizationMatrixProperty}.
*/
@Restricted(NoExternalUse.class)
abstract class DslEntry implements Describable<DslEntry>, Comparable<DslEntry> {
private final String name;
private final List<String> permissions;
/**
*
* @param name the sid of the DSL entity
* @param permissions the list of string-typed permissions
*/
public DslEntry(String name, List<String> permissions) {
this.name = name;
this.permissions = permissions;
}
public String getName() {
return name;
}
public List<String> getPermissions() {
return permissions;
}
@Override
public Descriptor<DslEntry> getDescriptor() {
return Jenkins.get().getDescriptorOrDie(getClass());
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DslEntry that = (DslEntry) o;
return Objects.equals(name, that.name) && Objects.equals(permissions, that.permissions);
}
@Override
public int hashCode() {
return Objects.hash(name, permissions, getClass());
}
@Override
public int compareTo(@NonNull DslEntry that) {
if (this.getClass() != that.getClass()) {
return this.getClass().getName().compareTo(that.getClass().getName());
}
if (!this.name.equals(that.name)) {
return this.name.compareTo(that.name);
}
return this.permissions.size() - that.permissions.size();
}
public abstract void addPermission(AuthorizationProperty authorizationProperty);
protected static Permission findPermission(String value) {
final Permission permission = Permission.fromId(value);
if (permission != null) {
return permission;
}
return PermissionFinder.findPermission(value);
}
}
/**
* Represents a user being assigned permissions.
*/
@Restricted(NoExternalUse.class)
class DslUser extends DslEntry {
@DataBoundConstructor
public DslUser(String name, List<String> permissions) {
super(name, permissions);
}
@Override
public void addPermission(AuthorizationProperty authorizationProperty) {
getPermissions()
.forEach(permission ->
authorizationProperty.add(findPermission(permission), PermissionEntry.user(getName())));
}
@Extension
@Symbol("user")
public static class DescriptorImpl extends Descriptor<DslEntry> {}
}
/**
* Represents a group being assigned permissions.
*/
@Restricted(NoExternalUse.class)
class DslGroup extends DslEntry {
@DataBoundConstructor
public DslGroup(String name, List<String> permissions) {
super(name, permissions);
}
@Override
public void addPermission(AuthorizationProperty authorizationProperty) {
getPermissions()
.forEach(permission ->
authorizationProperty.add(findPermission(permission), PermissionEntry.group(getName())));
}
@Extension
@Symbol("group")
public static class DescriptorImpl extends Descriptor<DslEntry> {}
}
/**
* Represents a user or group being assigned permissions.
* Generally discouraged "ambiguous" permission assignments.
*/
@Restricted(NoExternalUse.class)
class DslUserOrGroup extends DslEntry {
@DataBoundConstructor
public DslUserOrGroup(String name, List<String> permissions) {
super(name, permissions);
}
@Override
public void addPermission(AuthorizationProperty authorizationProperty) {
getPermissions()
.forEach(permission -> authorizationProperty.add(
findPermission(permission), new PermissionEntry(AuthorizationType.EITHER, getName())));
}
@Extension
@Symbol("userOrGroup")
public static class DescriptorImpl extends Descriptor<DslEntry> {}
}
}