/
ProjectQueueItemAuthenticator.java
182 lines (161 loc) · 6.62 KB
/
ProjectQueueItemAuthenticator.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
/*
* The MIT License
*
* Copyright (c) 2013 IKEDA Yasuyuki
*
* 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.authorizeproject;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.Queue;
import javax.annotation.CheckForNull;
import net.sf.json.JSONObject;
import org.acegisecurity.Authentication;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
import jenkins.model.Jenkins;
import jenkins.security.QueueItemAuthenticatorConfiguration;
import jenkins.security.QueueItemAuthenticatorDescriptor;
import jenkins.security.QueueItemAuthenticator;
/**
* Authorize builds of projects configured with {@link AuthorizeProjectProperty}.
*/
public class ProjectQueueItemAuthenticator extends QueueItemAuthenticator {
private final Map<String,Boolean> strategyEnabledMap;
/**
*
*/
@Deprecated
public ProjectQueueItemAuthenticator() {
this(Collections.<String, Boolean>emptyMap());
}
public ProjectQueueItemAuthenticator(Map<String,Boolean> strategyEnabledMap) {
this.strategyEnabledMap = strategyEnabledMap;
}
public Object readResolve() {
if(strategyEnabledMap == null) {
return new ProjectQueueItemAuthenticator(Collections.<String, Boolean>emptyMap());
}
return this;
}
/**
* @param item
* @return
* @see jenkins.security.QueueItemAuthenticator#authenticate(hudson.model.Queue.Item)
*/
@Override
@CheckForNull
public Authentication authenticate(Queue.Item item) {
if (!(item.task instanceof Job)) {
return null;
}
Job<?, ?> project = (Job<?,?>)item.task;
if (project instanceof AbstractProject) {
project = ((AbstractProject<?,?>)project).getRootProject();
}
AuthorizeProjectProperty prop = project.getProperty(AuthorizeProjectProperty.class);
if (prop == null) {
return null;
}
return prop.authenticate(item);
}
public Map<String, Boolean> getStrategyEnabledMap() {
return strategyEnabledMap;
}
public boolean isStrategyEnabled(Descriptor<?> d)
{
Boolean b = getStrategyEnabledMap().get(d.getId());
if(b != null) {
return b.booleanValue();
}
if(!(d instanceof AuthorizeProjectStrategyDescriptor)) {
return true;
}
return ((AuthorizeProjectStrategyDescriptor)d).isEnabledByDefault();
}
/**
*
*/
@Extension
public static class DescriptorImpl extends QueueItemAuthenticatorDescriptor {
/**
* @return the name shown in the security configuration page.
* @see hudson.model.Descriptor#getDisplayName()
*/
@Override
public String getDisplayName() {
return Messages.ProjectQueueItemAuthenticator_DisplayName();
}
public List<AuthorizeProjectStrategyDescriptor> getDescriptorsForGlobalSecurityConfigPage() {
return AuthorizeProjectStrategyDescriptor.getDescriptorsForGlobalSecurityConfigPage();
}
public List<Descriptor<AuthorizeProjectStrategy>> getAvailableDescriptorList() {
return Jenkins.getInstance().getDescriptorList(AuthorizeProjectStrategy.class);
}
/**
* Creates new {@link ProjectQueueItemAuthenticator} from inputs.
* Additional to that, configure global configurations of {@link AuthorizeProjectStrategy}.
*
* @param req
* @param formData
* @return
* @throws hudson.model.Descriptor.FormException
* @see hudson.model.Descriptor#newInstance(org.kohsuke.stapler.StaplerRequest, net.sf.json.JSONObject)
*/
@Override
public ProjectQueueItemAuthenticator newInstance(StaplerRequest req, JSONObject formData)
throws FormException
{
Map<String,Boolean> strategyEnabledMap = new HashMap<String, Boolean>();
for (Descriptor<AuthorizeProjectStrategy> d : getAvailableDescriptorList()) {
String name = d.getJsonSafeClassName();
if (formData.has(name)) {
strategyEnabledMap.put(d.getId(), true);
if (
d instanceof AuthorizeProjectStrategyDescriptor
&& ((AuthorizeProjectStrategyDescriptor)d).getGlobalSecurityConfigPage() != null
) {
((AuthorizeProjectStrategyDescriptor)d).configureFromGlobalSecurity(req, formData.getJSONObject(name));
}
} else {
strategyEnabledMap.put(d.getId(), false);
}
}
return new ProjectQueueItemAuthenticator(strategyEnabledMap);
}
}
/**
* @return whether Jenkins is configured to use {@link ProjectQueueItemAuthenticator}.
*/
public static boolean isConfigured() {
for (QueueItemAuthenticator authenticator: QueueItemAuthenticatorConfiguration.get().getAuthenticators()) {
if (authenticator instanceof ProjectQueueItemAuthenticator) {
return true;
}
}
return false;
}
}