-
Notifications
You must be signed in to change notification settings - Fork 234
/
CredentialsProviderManager.java
459 lines (430 loc) · 17.4 KB
/
CredentialsProviderManager.java
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc., Stephen Connolly.
*
* 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 com.cloudbees.plugins.credentials;
import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.XmlFile;
import hudson.model.Descriptor;
import hudson.model.DescriptorVisibilityFilter;
import hudson.model.Saveable;
import hudson.security.Permission;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
import jenkins.model.GlobalConfiguration;
import jenkins.model.GlobalConfigurationCategory;
import jenkins.model.Jenkins;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.StaplerRequest;
/**
* Manages the various {@link CredentialsProvider} implementations in a {@link Jenkins}
*
* @since 2.0
*/
@Extension
public class CredentialsProviderManager extends DescriptorVisibilityFilter implements Serializable, Saveable {
/**
* Our logger.
*/
private static final Logger LOGGER = Logger.getLogger(CredentialsProviderManager.class.getName());
/**
* Ensure standardized serialization.
*/
private static final long serialVersionUID = 1L;
/**
* Our {@link CredentialsProvider} filter.
*/
private CredentialsProviderFilter providerFilter;
/**
* Our {@link CredentialsDescriptor} filter.
*/
private CredentialsTypeFilter typeFilter;
/**
* Any additional restrictions to apply.
*/
private List<CredentialsProviderTypeRestriction> restrictions;
/**
* A cache of {@link #restrictions} grouped by {@link CredentialsProviderTypeRestriction#getDescriptor()}.
*/
private transient Map<CredentialsProviderTypeRestrictionDescriptor, List<CredentialsProviderTypeRestriction>>
restrictionGroups;
/**
* Our constructor.
*/
public CredentialsProviderManager() {
try {
XmlFile xml = getConfigFile();
if (xml.exists()) {
xml.unmarshal(this);
}
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to read the existing credentials", e);
}
}
/**
* Returns {@code true} if and only if the specified {@link CredentialsProvider} is enabled.
*
* @param provider the specified {@link CredentialsProvider} to check.
* @return {@code true} if and only if the specified {@link CredentialsProvider} is enabled.
*/
public static boolean isEnabled(CredentialsProvider provider) {
CredentialsProviderManager manager = getInstance();
return manager == null || manager.providerFilter == null || manager.providerFilter.filter(provider);
}
/**
* Returns our {@link CredentialsProviderManager} singleton.
*
* @return {@link CredentialsProviderManager} singleton or {@code null}
*/
@Nullable // should never be null under normal code paths
public static CredentialsProviderManager getInstance() {
return ExtensionList.lookup(DescriptorVisibilityFilter.class).get(CredentialsProviderManager.class);
}
/**
* Returns our {@link CredentialsProviderManager} singleton.
*
* @return {@link CredentialsProviderManager} singleton
*/
@NonNull
public static CredentialsProviderManager getInstanceOrDie() {
CredentialsProviderManager instance = getInstance();
if (instance == null) {
throw new IllegalStateException("CredentialsProviderManager is not registered with Jenkins");
}
return instance;
}
/**
* {@inheritDoc}
*/
@Override
public boolean filter(Object context, Descriptor descriptor) {
Map<CredentialsProviderTypeRestrictionDescriptor, List<CredentialsProviderTypeRestriction>> restrictions =
restrictions();
if (restrictions != null && descriptor instanceof CredentialsDescriptor) {
CredentialsProvider provider = null;
if (context instanceof CredentialsProvider) {
provider = (CredentialsProvider) context;
} else if (context instanceof CredentialsStore) {
provider = ((CredentialsStore) context).getProvider();
} else if (context instanceof CredentialsStoreAction.DomainWrapper) {
provider = ((CredentialsStoreAction.DomainWrapper) context).getStore().getProvider();
} else if (context instanceof CredentialsStoreAction.CredentialsWrapper) {
provider = ((CredentialsStoreAction.CredentialsWrapper) context).getStore().getProvider();
}
if (provider != null) {
CredentialsDescriptor type = (CredentialsDescriptor) descriptor;
for (Map.Entry<CredentialsProviderTypeRestrictionDescriptor, List<CredentialsProviderTypeRestriction>>
group : restrictions.entrySet()) {
if (!group.getKey().filter(group.getValue(), provider, type)) {
return false;
}
}
}
}
if (descriptor instanceof CredentialsDescriptor) {
return typeFilter == null || typeFilter.filter((CredentialsDescriptor) descriptor);
}
if (descriptor instanceof CredentialsProvider && context instanceof Jenkins) {
return providerFilter == null || providerFilter.filter((CredentialsProvider) descriptor);
}
return true;
}
/**
* Gets the configuration file that {@link CredentialsProviderManager} uses to store its credentials.
*
* @return the configuration file that {@link CredentialsProviderManager} uses to store its credentials.
*/
public static XmlFile getConfigFile() {
return new XmlFile(Jenkins.XSTREAM2, new File(Jenkins.get().getRootDir(), "credentials-configuration.xml"));
}
/**
* Short-cut method for {@link Jenkins#checkPermission(hudson.security.Permission)}
*
* @param p the permission to check.
*/
private void checkPermission(Permission p) {
Jenkins.get().checkPermission(p);
}
/**
* {@inheritDoc}
*/
@Override
public void save() throws IOException {
checkPermission(Jenkins.ADMINISTER);
getConfigFile().write(this);
}
/**
* Gets the current {@link CredentialsProviderFilter}.
*
* @return the current {@link CredentialsProviderFilter}.
*/
@NonNull
public CredentialsProviderFilter getProviderFilter() {
return providerFilter == null ? new CredentialsProviderFilter.None() : providerFilter;
}
/**
* Sets the {@link CredentialsProviderFilter}.
*
* @param providerFilter the new {@link CredentialsProviderFilter}.
*/
public void setProviderFilter(@CheckForNull CredentialsProviderFilter providerFilter) {
if (providerFilter == null) {
providerFilter = new CredentialsProviderFilter.None();
}
if (Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
if (!providerFilter.equals(this.providerFilter)) {
this.providerFilter = providerFilter;
try {
save();
} catch (IOException e) {
// ignore
}
}
}
}
/**
* Gets the current {@link CredentialsTypeFilter}.
*
* @return the current {@link CredentialsTypeFilter}.
*/
@NonNull
public CredentialsTypeFilter getTypeFilter() {
return typeFilter == null ? new CredentialsTypeFilter.None() : typeFilter;
}
/**
* Sets the {@link CredentialsTypeFilter}.
*
* @param typeFilter the new {@link CredentialsTypeFilter}.
*/
public void setTypeFilter(@CheckForNull CredentialsTypeFilter typeFilter) {
if (typeFilter == null) {
typeFilter = new CredentialsTypeFilter.None();
}
if (Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
if (!typeFilter.equals(this.typeFilter)) {
this.typeFilter = typeFilter;
try {
save();
} catch (IOException e) {
// ignore
}
}
}
}
/**
* Gets the current list of {@link CredentialsProviderTypeRestriction} instances.
*
* @return the current list of {@link CredentialsProviderTypeRestriction} instances.
*/
@NonNull
public List<CredentialsProviderTypeRestriction> getRestrictions() {
return restrictions == null
? Collections.<CredentialsProviderTypeRestriction>emptyList()
: Collections.unmodifiableList(restrictions);
}
/**
* Sets the list of {@link CredentialsProviderTypeRestriction} instances.
*
* @param restrictions the new list of {@link CredentialsProviderTypeRestriction} instances.
*/
public void setRestrictions(List<CredentialsProviderTypeRestriction> restrictions) {
if (Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
if (restrictions != null) {
// ensure they are sorted grouped so that it is easy to infer
Collections.sort(restrictions, new Comparator<CredentialsProviderTypeRestriction>() {
final ExtensionList<CredentialsProviderTypeRestrictionDescriptor> list =
ExtensionList.lookup(CredentialsProviderTypeRestrictionDescriptor.class);
/** {@inheritDoc} */
@Override
public int compare(CredentialsProviderTypeRestriction o1, CredentialsProviderTypeRestriction o2) {
int index1 = list.indexOf(o1.getDescriptor());
int index2 = list.indexOf(o2.getDescriptor());
if (index1 == -1) {
return index2 == -1 ? 0 : 1;
}
if (index2 == -1) {
return -1;
}
return Integer.compare(index1, index2);
}
});
}
if (!Objects.equals(restrictions, this.restrictions)) {
this.restrictions = restrictions;
this.restrictionGroups = null;
try {
save();
} catch (IOException e) {
// ignore
}
}
}
}
/**
* A repopulation-safe access for the {@link #restrictionGroups}
*
* @return the {@link #restrictionGroups} or {@code null} if empty.
*/
@CheckForNull
private Map<CredentialsProviderTypeRestrictionDescriptor, List<CredentialsProviderTypeRestriction>> restrictions() {
if (restrictionGroups == null && restrictions != null) {
Map<CredentialsProviderTypeRestrictionDescriptor, List<CredentialsProviderTypeRestriction>>
restrictionGroups = new HashMap<CredentialsProviderTypeRestrictionDescriptor,
List<CredentialsProviderTypeRestriction>>();
for (CredentialsProviderTypeRestriction restriction : restrictions) {
List<CredentialsProviderTypeRestriction> group = restrictionGroups.get(restriction.getDescriptor());
if (group == null) {
restrictionGroups.put(restriction.getDescriptor(),
group = new ArrayList<CredentialsProviderTypeRestriction>(1));
}
group.add(restriction);
}
this.restrictionGroups = restrictionGroups; // idempotent
}
return restrictionGroups;
}
/**
* Our global configuration.
*
* @since 2.0
*/
@Extension
public static class Configuration extends GlobalConfiguration {
/**
* A Jelly EL short-cut for {@link CredentialsProviderManager#getProviderFilter()}.
*
* @return the {@link CredentialsProviderFilter}.
*/
@SuppressWarnings("unused") // jelly EL helper
@Restricted(NoExternalUse.class)
public CredentialsProviderFilter getProviderFilter() {
CredentialsProviderManager manager = getInstance();
return manager == null ? new CredentialsProviderFilter.None() : manager.getProviderFilter();
}
/**
* A Jelly form-binding short-cut for
* {@link CredentialsProviderManager#setProviderFilter(CredentialsProviderFilter)}.
*
* @param providerFilter the {@link CredentialsProviderFilter}.
*/
@SuppressWarnings("unused") // jelly form binding
@Restricted(NoExternalUse.class)
public void setProviderFilter(CredentialsProviderFilter providerFilter) {
CredentialsProviderManager manager = getInstance();
if (manager != null) {
manager.setProviderFilter(providerFilter);
}
}
/**
* A Jelly EL short-cut for {@link CredentialsProviderManager#getTypeFilter()}.
*
* @return the {@link CredentialsTypeFilter}.
*/
@SuppressWarnings("unused") // jelly EL helper
@Restricted(NoExternalUse.class)
public CredentialsTypeFilter getTypeFilter() {
CredentialsProviderManager manager = getInstance();
return manager == null ? new CredentialsTypeFilter.None() : manager.getTypeFilter();
}
/**
* A Jelly form-binding short-cut for
* {@link CredentialsProviderManager#setTypeFilter(CredentialsTypeFilter)}.
*
* @param typeFilter the {@link CredentialsTypeFilter}.
*/
@SuppressWarnings("unused") // jelly form binding
@Restricted(NoExternalUse.class)
public void setTypeFilter(CredentialsTypeFilter typeFilter) {
CredentialsProviderManager manager = getInstance();
if (manager != null) {
manager.setTypeFilter(typeFilter);
}
}
/**
* A Jelly EL short-cut for {@link CredentialsProviderManager#getRestrictions()}.
*
* @return the {@link CredentialsProviderTypeRestriction} instances.
*/
@SuppressWarnings("unused") // jelly EL helper
@Restricted(NoExternalUse.class)
public List<CredentialsProviderTypeRestriction> getRestrictions() {
CredentialsProviderManager manager = getInstance();
return manager == null
? Collections.<CredentialsProviderTypeRestriction>emptyList()
: manager.getRestrictions();
}
/**
* A Jelly form-binding short-cut for
* {@link CredentialsProviderManager#setRestrictions(List)}.
*
* @param restrictions the {@link CredentialsProviderTypeRestriction} instances.
*/
@SuppressWarnings("unused") // jelly form binding
@Restricted(NoExternalUse.class)
public void setRestrictions(List<CredentialsProviderTypeRestriction> restrictions) {
CredentialsProviderManager manager = getInstance();
if (manager != null) {
manager.setRestrictions(restrictions);
}
}
/**
* {@inheritDoc}
*/
@Override
public boolean configure(StaplerRequest req, JSONObject json) throws FormException {
if (Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
if (!json.has("restrictions")) {
// JENKINS-36090 stapler "helpfully" does not submit the restrictions if there are none
// and hence you can never delete the last one
json.put("restrictions", new JSONArray());
}
req.bindJSON(this, json);
return super.configure(req, json);
}
return false;
}
/**
* {@inheritDoc}
*/
@Override
public GlobalConfigurationCategory getCategory() {
return GlobalConfigurationCategory.get(GlobalCredentialsConfiguration.Category.class);
}
}
}