Skip to content

Commit 8f40231

Browse files
Merge branch 'centralized-permissions' into cloud-maintenance
Merging the permission manager changes to the working branch and also reverting the UUID logic.
2 parents 1b8ba9d + b6e1ace commit 8f40231

File tree

7 files changed

+213
-144
lines changed

7 files changed

+213
-144
lines changed

src/main/java/com/sap/prd/jenkins/plugins/agent_maintenance/MaintenanceAction.java

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import java.io.IOException;
1515
import java.time.LocalDateTime;
1616
import java.util.ArrayList;
17-
import java.util.Arrays;
1817
import java.util.Collections;
1918
import java.util.HashMap;
2019
import java.util.List;
@@ -64,33 +63,12 @@ public boolean isVisible() {
6463
if (!MaintenanceHelper.getInstance().isValidTarget(target.toKey())) {
6564
return false;
6665
}
67-
68-
if (isAgent()) {
69-
Computer computer = Jenkins.get().getComputer(target.getName());
70-
return computer != null
71-
&& (computer.hasPermission(Computer.DISCONNECT)
72-
|| computer.hasPermission(Computer.CONFIGURE)
73-
|| computer.hasPermission(Computer.EXTENDED_READ))
74-
&& computer.getNode() != null;
75-
}
76-
return Jenkins.get().hasPermission(Jenkins.ADMINISTER);
66+
return PermissionManager.canView(target);
7767
} catch (IOException e) {
7868
return false;
7969
}
8070
}
8171

82-
protected void checkPermission(Permission... permissions) {
83-
if (isAgent()) {
84-
Computer c = Jenkins.get().getComputer(target.getName());
85-
if (c == null) {
86-
throw new IllegalStateException("Agent '" + target.getName() + "' no longer exists");
87-
}
88-
c.checkAnyPermission(permissions);
89-
} else { // For cloud
90-
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
91-
}
92-
}
93-
9472
@Override
9573
public String getIconFileName() {
9674
if (isVisible()) {
@@ -103,7 +81,7 @@ public String getIconFileName() {
10381
@Override
10482
public String getDisplayName() {
10583
if (isVisible()) {
106-
if (hasPermissions()) {
84+
if (PermissionManager.canModify(target)) {
10785
return Messages.MaintenanceAction_maintenanceWindows();
10886
} else {
10987
return Messages.MaintenanceAction_view();
@@ -182,58 +160,30 @@ public Computer getAgentComputer() {
182160
}
183161

184162
/**
185-
* Checks if the user has permissions to access MaintenanceWindows.
163+
* Checks if the user can view maintenance windows for this target (called by jelly).
186164
*
187-
* @return true if they do.
165+
* @return true if they can view.
188166
*/
189167
public boolean hasPermissions() {
190-
if (isAgent()) {
191-
Computer c = Jenkins.get().getComputer(target.getName());
192-
return c != null
193-
&& (c.hasPermission(Computer.DISCONNECT)
194-
|| c.hasPermission(Computer.CONFIGURE)
195-
|| c.hasPermission(Computer.EXTENDED_READ));
196-
} else {
197-
return Jenkins.get().hasPermission(Jenkins.ADMINISTER);
198-
}
168+
return PermissionManager.canView(target);
199169
}
200170

201171
/**
202-
* Checks the given permissions.
172+
* Checks if the user can add or edit maintenance windows for this target (called by jelly).
203173
*
204-
* @param permissions A group of permissions to be checked.
205-
* @return true if all permissions are granted.
174+
* @return true if they can modify.
206175
*/
207-
public boolean hasPermissions(Permission... permissions) {
208-
if (isAgent()) {
209-
Computer c = Jenkins.get().getComputer(target.getName());
210-
return c != null && Arrays.stream(permissions).allMatch(c::hasPermission);
211-
} else {
212-
return Jenkins.get().hasPermission(Jenkins.ADMINISTER);
213-
}
176+
public boolean hasModifyPermission() {
177+
return PermissionManager.canModify(target);
214178
}
215179

216180
/**
217-
* Checks if the user has permissions to delete MaintenanceWindows.
181+
* Checks if the user can delete maintenance windows for this target.
218182
*
219-
* @return true if they do.
183+
* @return true if they can delete.
220184
*/
221185
public boolean hasDeletePermission() {
222-
if (isAgent()) {
223-
Computer c = Jenkins.get().getComputer(target.getName());
224-
return c != null && (c.hasPermission(Computer.DISCONNECT) || c.hasPermission(Computer.CONFIGURE));
225-
} else {
226-
return Jenkins.get().hasPermission(Jenkins.ADMINISTER);
227-
}
228-
}
229-
230-
/**
231-
* Checks if the user has permissions to delete Cloud windows.
232-
*
233-
* @return true if they do.
234-
*/
235-
public boolean hasCloudDeletePermission() {
236-
return Jenkins.get().hasPermission(Jenkins.ADMINISTER);
186+
return PermissionManager.canDelete(target);
237187
}
238188

239189
/**
@@ -321,7 +271,7 @@ public Set<RecurringMaintenanceWindow> getRecurringMaintenanceWindows() {
321271
*/
322272
@POST
323273
public HttpResponse doAdd(StaplerRequest2 req) throws IOException, ServletException {
324-
checkPermission(CONFIGURE_AND_DISCONNECT);
274+
PermissionManager.checkCanModify(target);
325275

326276
JSONObject src = req.getSubmittedForm();
327277
MaintenanceWindow mw = req.bindJSON(MaintenanceWindow.class, src);
@@ -339,7 +289,7 @@ public HttpResponse doAdd(StaplerRequest2 req) throws IOException, ServletExcept
339289
*/
340290
@POST
341291
public void doAddRecurring(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException, ServletException {
342-
checkPermission(CONFIGURE_AND_DISCONNECT);
292+
PermissionManager.checkCanModify(target);
343293

344294
JSONObject src = req.getSubmittedForm();
345295
RecurringMaintenanceWindow rmw = req.bindJSON(RecurringMaintenanceWindow.class, src);
@@ -354,7 +304,7 @@ public void doAddRecurring(StaplerRequest2 req, StaplerResponse2 rsp) throws IOE
354304
*/
355305
@JavaScriptMethod
356306
public String[] deleteMultiple(String[] ids) {
357-
checkPermission(CONFIGURE_AND_DISCONNECT);
307+
PermissionManager.checkCanDelete(target);
358308
List<String> deletedList = new ArrayList<>();
359309
for (String id : ids) {
360310
try {
@@ -375,7 +325,7 @@ public String[] deleteMultiple(String[] ids) {
375325
@JavaScriptMethod
376326
public Map<String, Boolean> getMaintenanceStatus() {
377327
Map<String, Boolean> statusList = new HashMap<>();
378-
if (hasPermissions()) {
328+
if (PermissionManager.canView(target)) {
379329
try {
380330
for (MaintenanceWindow mw : MaintenanceHelper.getInstance().getMaintenanceWindows(target.toKey())) {
381331
if (!mw.isMaintenanceOver()) {
@@ -396,7 +346,7 @@ public Map<String, Boolean> getMaintenanceStatus() {
396346
*/
397347
@JavaScriptMethod
398348
public String[] deleteMultipleRecurring(String[] ids) {
399-
checkPermission(CONFIGURE_AND_DISCONNECT);
349+
PermissionManager.checkCanDelete(target);
400350
List<String> deletedList = new ArrayList<>();
401351
for (String id : ids) {
402352
try {
@@ -417,7 +367,7 @@ public String[] deleteMultipleRecurring(String[] ids) {
417367
@JavaScriptMethod
418368
public boolean deleteMaintenance(String id) {
419369
try {
420-
checkPermission(CONFIGURE_AND_DISCONNECT);
370+
PermissionManager.checkCanDelete(target);
421371
if (Util.fixEmptyAndTrim(id) == null) {
422372
return false;
423373
}
@@ -442,7 +392,7 @@ public boolean deleteMaintenance(String id) {
442392
@JavaScriptMethod
443393
public boolean deleteRecurringMaintenance(String id) {
444394
try {
445-
checkPermission(CONFIGURE_AND_DISCONNECT);
395+
PermissionManager.checkCanDelete(target);
446396
if (Util.fixEmptyAndTrim(id) == null) {
447397
return false;
448398
}
@@ -469,7 +419,7 @@ public boolean deleteRecurringMaintenance(String id) {
469419
*/
470420
@POST
471421
public synchronized HttpResponse doConfigSubmit(StaplerRequest2 req) throws IOException, ServletException {
472-
checkPermission(Computer.CONFIGURE);
422+
PermissionManager.checkCanModify(target);
473423

474424
JSONObject src = req.getSubmittedForm();
475425

@@ -500,7 +450,7 @@ public synchronized HttpResponse doConfigSubmit(StaplerRequest2 req) throws IOEx
500450
public void doEnable(StaplerResponse2 rsp) throws IOException {
501451
Computer c = getAgentComputer();
502452
if (c != null) {
503-
c.checkPermission(Computer.CONFIGURE);
453+
PermissionManager.checkCanModify(target);
504454
MaintenanceHelper.getInstance().injectRetentionStrategy(c);
505455
}
506456
rsp.sendRedirect(".");
@@ -516,7 +466,7 @@ public void doEnable(StaplerResponse2 rsp) throws IOException {
516466
public void doDisable(StaplerResponse2 rsp) throws IOException {
517467
Computer c = getAgentComputer();
518468
if (c != null) {
519-
c.checkPermission(Computer.CONFIGURE);
469+
PermissionManager.checkCanModify(target);
520470
MaintenanceHelper.getInstance().removeRetentionStrategy(c);
521471
}
522472

@@ -536,15 +486,14 @@ public void doIndex(StaplerRequest2 req, StaplerResponse2 rsp)
536486
rsp.sendError(HttpServletResponse.SC_NOT_FOUND); // 404
537487
return;
538488
}
539-
c.checkAnyPermission(Computer.EXTENDED_READ, Computer.CONFIGURE, Computer.DISCONNECT);
540489
} else {
541490
Cloud cloud = getCloud();
542491
if (cloud == null) {
543492
rsp.sendError(HttpServletResponse.SC_NOT_FOUND);
544493
return;
545494
}
546-
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
547495
}
496+
PermissionManager.checkCanView(target);
548497

549498
req.getView(this, "index.jelly").forward(req, rsp);
550499
}

src/main/java/com/sap/prd/jenkins/plugins/agent_maintenance/MaintenanceLink.java

Lines changed: 19 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,18 @@ public String getDescription() {
5555

5656
@Override
5757
public String getDisplayName() {
58-
boolean hasClouds, hasAgents;
59-
try {
60-
hasClouds = !getCloudTargets().isEmpty();
61-
hasAgents = !getAgentTargets().isEmpty();
62-
} catch (IOException e) {
63-
LOGGER.log(Level.WARNING, "Error while reading cloud metadata", e);
64-
throw new RuntimeException(e);
65-
}
58+
List<MaintenanceAction> all = getTargets();
59+
boolean hasAgents = all.stream().anyMatch(MaintenanceAction::isAgent);
60+
boolean hasClouds = all.stream().anyMatch(MaintenanceAction::isCloud);
61+
6662
if (hasAgents && !hasClouds) {
6763
return Messages.MaintenanceLink_displayName_agent();
6864
}
65+
6966
if (hasClouds && !hasAgents) {
7067
return Messages.MaintenanceLink_displayName_cloud();
7168
}
69+
7270
return Messages.MaintenanceLink_displayName();
7371
}
7472

@@ -124,11 +122,13 @@ public List<MaintenanceAction> getTargets() throws IOException {
124122
}
125123
}
126124

127-
return targetList;
125+
return targetList.stream()
126+
.filter(action -> PermissionManager.canView(action.getTarget()))
127+
.toList();
128128
}
129129

130130
/**
131-
* Gets all Agents' maintenance actions.
131+
* Gets all Agents' maintenance actions (called by jelly).
132132
*
133133
* @return List of all Agent actions.
134134
*/
@@ -140,7 +140,7 @@ public List<MaintenanceAction> getAgentTargets() throws IOException {
140140
}
141141

142142
/**
143-
* Gets all Clouds' maintenance actions.
143+
* Gets all Clouds' maintenance actions (called by jelly).
144144
*
145145
* @return List of all Cloud actions.
146146
*/
@@ -242,7 +242,8 @@ private String getVerb(int count, String target) {
242242
*/
243243
@JavaScriptMethod
244244
public boolean deleteMaintenance(String id, String targetKey) {
245-
if (hasPermission(targetKey)) {
245+
MaintenanceTarget target = MaintenanceTarget.fromKey(targetKey);
246+
if (PermissionManager.canDelete(target)) {
246247
try {
247248
MaintenanceHelper.getInstance().deleteMaintenanceWindow(targetKey, id);
248249
return true;
@@ -265,7 +266,8 @@ public String[] deleteMultiple(JSONObject json) {
265266
List<String> deletedList = new ArrayList<>();
266267
for (Entry<String, String> entry : mwList.entrySet()) {
267268
String targetKey = entry.getValue();
268-
if (hasPermission(targetKey)) {
269+
MaintenanceTarget target = MaintenanceTarget.fromKey(targetKey);
270+
if (PermissionManager.canDelete(target)) {
269271
String id = entry.getKey();
270272
try {
271273
MaintenanceHelper.getInstance().deleteMaintenanceWindow(targetKey, id);
@@ -288,16 +290,14 @@ public Map<String, Boolean> getMaintenanceStatus() throws IOException {
288290
Map<String, Boolean> statusList = new HashMap<>();
289291
for (MaintenanceAction action : getTargets()) {
290292
try {
291-
if (!action.hasPermissions()) {
293+
if (!PermissionManager.canView(action.getTarget())) {
292294
continue;
293295
}
294296

295297
MaintenanceTarget target = action.getTarget();
296-
if (target != null) {
297-
for (MaintenanceWindow mw : MaintenanceHelper.getInstance().getMaintenanceWindows(target.toKey())) {
298-
if (!mw.isMaintenanceOver()) {
299-
statusList.put(mw.getId(), mw.isMaintenanceScheduled());
300-
}
298+
for (MaintenanceWindow mw : MaintenanceHelper.getInstance().getMaintenanceWindows(target.toKey())) {
299+
if (!mw.isMaintenanceOver()) {
300+
statusList.put(mw.getId(), mw.isMaintenanceScheduled());
301301
}
302302
}
303303
} catch (IOException ioe) {
@@ -307,34 +307,6 @@ public Map<String, Boolean> getMaintenanceStatus() throws IOException {
307307
return statusList;
308308
}
309309

310-
/**
311-
* Returns list of available clouds for multi select.
312-
*
313-
* @return List of available clouds
314-
*/
315-
public List<CloudOption> getAvailableClouds() {
316-
List<CloudOption> options = new ArrayList<>();
317-
Map<String, Boolean> duplicateMap = new HashMap<>();
318-
Jenkins j = Jenkins.get();
319-
320-
for (Cloud cloud : j.clouds) {
321-
duplicateMap.put(cloud.name, CLOUD_UUID_STORE.hasDuplicates(cloud.name));
322-
}
323-
324-
for (Cloud cloud : j.clouds) {
325-
String uuid = CLOUD_UUID_STORE.getUuidIfPresent(cloud);
326-
boolean hasDuplicate = duplicateMap.getOrDefault(cloud.name, false);
327-
String shortUuid = (uuid == null) ? null : uuid.substring(0, 8);
328-
options.add(new CloudOption(cloud.name, uuid, shortUuid, hasDuplicate));
329-
}
330-
return options;
331-
}
332-
333-
private boolean hasPermission(String targetKey) {
334-
MaintenanceAction action = new MaintenanceAction(MaintenanceTarget.fromKey(targetKey));
335-
return action.hasPermissions();
336-
}
337-
338310
@Restricted(NoExternalUse.class)
339311
public FormValidation doCheckLabel(@QueryParameter String value) {
340312
return LabelExpression.validate(value);

0 commit comments

Comments
 (0)