-
Notifications
You must be signed in to change notification settings - Fork 36
/
SystemAuthorizationStrategy.java
227 lines (205 loc) · 8.22 KB
/
SystemAuthorizationStrategy.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
/*
* The MIT License
*
* Copyright (c) 2013-2016 Stephen Connolly, 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.strategy;
import hudson.Extension;
import hudson.model.Descriptor;
import hudson.model.Job;
import hudson.model.Queue;
import hudson.security.ACL;
import hudson.util.FormValidation;
import java.io.IOException;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.acegisecurity.Authentication;
import org.jenkinsci.plugins.authorizeproject.AuthorizeProjectProperty;
import org.jenkinsci.plugins.authorizeproject.AuthorizeProjectStrategy;
import org.jenkinsci.plugins.authorizeproject.AuthorizeProjectStrategyDescriptor;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
/**
* Run builds as {@link ACL#SYSTEM}. Using this strategy becomes important when
* {@link org.jenkinsci.plugins.authorizeproject.GlobalQueueItemAuthenticator}
* is forcing jobs to a user other than {@link ACL#SYSTEM}.
*
* @since 1.1.1
*/
public class SystemAuthorizationStrategy extends AuthorizeProjectStrategy {
@DataBoundConstructor
public SystemAuthorizationStrategy() {
}
/**
* {@inheritDoc}
*/
@Override
public Authentication authenticate(Job<?, ?> job, Queue.Item item) {
return ACL.SYSTEM;
}
/**
* Return {@link SystemAuthorizationStrategy} configured in a job.
*
* @param job the {@link Job}
* @return the {@link SystemAuthorizationStrategy} or {@code null}
*/
protected static SystemAuthorizationStrategy getCurrentStrategy(Job<?, ?> job) {
if (job == null) {
return null;
}
AuthorizeProjectProperty prop = job.getProperty(AuthorizeProjectProperty.class);
if (prop == null) {
return null;
}
AuthorizeProjectStrategy strategy = prop.getStrategy();
if (!(strategy instanceof SystemAuthorizationStrategy)) {
return null;
}
return (SystemAuthorizationStrategy) strategy;
}
/**
* Called when XSTREAM2 instantiates this from XML configuration.
*
* When configured via REST/CLI, {@link Descriptor#newInstance(StaplerRequest, JSONObject)} is not called.
* Instead checks authentication here.
*
* @return return myself.
* @throws IOException authentication failed.
*/
private Object readResolve() throws IOException {
Jenkins instance = Jenkins.getInstance();
if (instance == null || !instance.hasPermission(Jenkins.RUN_SCRIPTS)) {
// This is called via REST/CLI.
// As REST/CLI interface saves configuration after successfully load object from the XML,
// this prevents the new configuration saved.
throw new IOException(Messages.SystemAuthorizationStrategy_readResolve());
}
return this;
}
/**
* For now we are an object with no configurable fields, so return a fixed value.
* If we add configurable fields we probably should consider removing the final.
*
* @return our hashCode.
*/
@Override
public final int hashCode() {
return SystemAuthorizationStrategy.class.hashCode();
}
/**
* For now we are an object with no configurable fields, so strict instanceof establishes equality.
* If we add configurable fields we probably should consider removing the final.
*
* @param obj the object to test equality with.
* @return {@code true} if and only if this is a equivalent {@link SystemAuthorizationStrategy} instance.
*/
@Override
public final boolean equals(Object obj) {
return obj != null && SystemAuthorizationStrategy.class == obj.getClass();
}
/**
* Our descriptor
*/
@Extension(ordinal = -100)
public static class DescriptorImpl extends AuthorizeProjectStrategyDescriptor {
/**
* Flag to mark where changing a job using this strategy requires administrator permissions.
*/
private boolean permitReconfiguration;
/**
* @return the name shown in project configuration pages.
* @see hudson.model.Descriptor#getDisplayName()
*/
@Override
public String getDisplayName() {
return Messages.SystemAuthorizationStrategy_DisplayName();
}
/**
* {@inheritDoc}
*/
@Override
public boolean isEnabledByDefault() {
return false;
}
/**
* Gets the flag to mark where changing a job using this strategy requires administrator permissions.
*
* @return {@code true} if non-admins are allowed to modify jobs that are using this strategy.
*/
public boolean isPermitReconfiguration() {
return permitReconfiguration;
}
/**
* Sets the flag to mark where changing a job using this strategy requires administrator permissions.
*
* @param permitReconfiguration {@code true} if non-admins are allowed to modify jobs that are using this strategy.
*/
public void setPermitReconfiguration(boolean permitReconfiguration) {
if (permitReconfiguration != this.permitReconfiguration) {
this.permitReconfiguration = permitReconfiguration;
save();
}
}
/**
* Tests if an object is a {@link Job}
*
* @param it the object.
* @return {@code true} if and only if the supplied object is a {@link Job}
*/
@Restricted(NoExternalUse.class) // helper method for Jelly EL
public boolean isJob(Object it) {
return it instanceof Job;
}
/**
* {@inheritDoc}
*/
@Override
public void configureFromGlobalSecurity(StaplerRequest req, JSONObject js) throws FormException {
setPermitReconfiguration(js.getBoolean("permitReconfiguration"));
}
/**
* {@inheritDoc}
*/
@Override
public SystemAuthorizationStrategy newInstance(StaplerRequest req, JSONObject formData)
throws FormException {
SystemAuthorizationStrategy result = (SystemAuthorizationStrategy) super.newInstance(req, formData);
Jenkins instance = Jenkins.getInstance();
if (instance == null || !instance.hasPermission(Jenkins.RUN_SCRIPTS)) {
Job job = req.findAncestorObject(Job.class);
if (job != null) {
if (!(permitReconfiguration && getCurrentStrategy(job) != null)) {
throw new FormException(Messages.SystemAuthorizationStrategy_administersOnly(), "strategy");
}
}
}
return result;
}
public FormValidation doCheckPermitReconfiguration(String value) {
if (!Boolean.parseBoolean(value)) {
return FormValidation.warning(Messages.SystemAuthorizationStrategy_administersOnly());
}
return FormValidation.warning(Messages.SystemAuthorizationStrategy_userConfigurable());
}
}
}