|
| 1 | +/* |
| 2 | + * The MIT License |
| 3 | + * |
| 4 | + * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in |
| 14 | + * all copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 | + * THE SOFTWARE. |
| 23 | + */ |
| 24 | + |
| 25 | +package hudson.model; |
| 26 | + |
| 27 | +import hudson.Extension; |
| 28 | +import hudson.ExtensionList; |
| 29 | +import hudson.ExtensionPoint; |
| 30 | +import hudson.security.Permission; |
| 31 | +import hudson.triggers.SCMTrigger; |
| 32 | +import hudson.triggers.TimerTrigger; |
| 33 | +import java.io.IOException; |
| 34 | +import java.util.Set; |
| 35 | +import jenkins.model.Jenkins; |
| 36 | +import org.kohsuke.accmod.Restricted; |
| 37 | +import org.kohsuke.accmod.restrictions.NoExternalUse; |
| 38 | +import org.kohsuke.stapler.StaplerProxy; |
| 39 | +import org.kohsuke.stapler.StaplerRequest2; |
| 40 | +import org.kohsuke.stapler.StaplerResponse2; |
| 41 | +import org.kohsuke.stapler.interceptor.RequirePOST; |
| 42 | + |
| 43 | +/** |
| 44 | + * Checks the health of a subsystem of Jenkins and if there's something |
| 45 | + * that requires administrator's attention, notify the administrator. |
| 46 | + * |
| 47 | + * <h2>How to implement?</h2> |
| 48 | + * <p> |
| 49 | + * Plugins who wish to contribute such notifications can implement this |
| 50 | + * class and put {@link Extension} on it to register it to Jenkins. |
| 51 | + * |
| 52 | + * <p> |
| 53 | + * Once installed, it's the implementer's responsibility to perform |
| 54 | + * monitoring and activate/deactivate the monitor accordingly. Sometimes |
| 55 | + * this can be done by updating a flag from code (see {@link SCMTrigger} |
| 56 | + * for one such example), while other times it's more convenient to do |
| 57 | + * so by running some code periodically (for this, use {@link TimerTrigger#timer}) |
| 58 | + * |
| 59 | + * <p> |
| 60 | + * {@link AdministrativeMonitor}s are bound to URL by {@link Jenkins#getAdministrativeMonitor(String)}. |
| 61 | + * See {@link #getUrl()}. |
| 62 | + * |
| 63 | + * <h3>Views</h3> |
| 64 | + * <dl> |
| 65 | + * <dt>message.jelly</dt> |
| 66 | + * <dd> |
| 67 | + * If {@link #isActivated()} returns true, Jenkins will use the {@code message.jelly} |
| 68 | + * view of this object to render the warning text. This happens in the |
| 69 | + * {@code http://SERVER/jenkins/manage} page. This view should typically render |
| 70 | + * a DIV box with class='alert alert-danger' or class='alert alert-warning' with a human-readable text |
| 71 | + * inside it. It often also contains a link to a page that provides more details |
| 72 | + * about the problem.<br> |
| 73 | + * Additionally 2 numbers are shown in the Jenkins header of administrators, one with the number or active |
| 74 | + * non-security relevant monitors and one with the number of active security relevant monitors. |
| 75 | + * </dd> |
| 76 | + * </dl> |
| 77 | + * |
| 78 | + * <h3>Use with System Read permission</h3> |
| 79 | + * <p> |
| 80 | + * By default administrative monitors are visible only to users with Administer permission. |
| 81 | + * Users with {@link Jenkins#SYSTEM_READ} permission can access administrative monitors that override {@link #getRequiredPermission()}. |
| 82 | + * Care needs to be taken to ensure users with that permission don't have access to actions modifying system state. |
| 83 | + * For more details, see {@link #getRequiredPermission()}. |
| 84 | + * </p> |
| 85 | + * |
| 86 | + * @author Kohsuke Kawaguchi |
| 87 | + * @since 1.273 |
| 88 | + * @see Jenkins#administrativeMonitors |
| 89 | + */ |
| 90 | +public abstract class AdministrativeMonitor extends AbstractModelObject implements ExtensionPoint, StaplerProxy { |
| 91 | + /** |
| 92 | + * Human-readable ID of this monitor, which needs to be unique within the system. |
| 93 | + * |
| 94 | + * <p> |
| 95 | + * This ID is used to remember persisted setting for this monitor, |
| 96 | + * so the ID should remain consistent beyond the Hudson JVM lifespan. |
| 97 | + */ |
| 98 | + public final String id; |
| 99 | + |
| 100 | + protected AdministrativeMonitor(String id) { |
| 101 | + this.id = id; |
| 102 | + } |
| 103 | + |
| 104 | + protected AdministrativeMonitor() { |
| 105 | + this.id = this.getClass().getName(); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns the URL of this monitor, relative to the context path, like "administrativeMonitor/foobar". |
| 110 | + */ |
| 111 | + public String getUrl() { |
| 112 | + return "administrativeMonitor/" + id; |
| 113 | + } |
| 114 | + |
| 115 | + @Override |
| 116 | + public String getDisplayName() { |
| 117 | + return id; |
| 118 | + } |
| 119 | + |
| 120 | + @Override |
| 121 | + public final String getSearchUrl() { |
| 122 | + return getUrl(); |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Mark this monitor as disabled, to prevent this from showing up in the UI. |
| 127 | + */ |
| 128 | + public void disable(boolean value) throws IOException { |
| 129 | + AbstractCIBase jenkins = Jenkins.get(); |
| 130 | + Set<String> set = jenkins.getDisabledAdministrativeMonitors(); |
| 131 | + if (value) { |
| 132 | + set.add(id); |
| 133 | + } else { |
| 134 | + set.remove(id); |
| 135 | + } |
| 136 | + jenkins.setDisabledAdministrativeMonitors(set); |
| 137 | + jenkins.save(); |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Returns true if this monitor {@link #disable(boolean) isn't disabled} earlier. |
| 142 | + * |
| 143 | + * <p> |
| 144 | + * This flag implements the ability for the admin to say "no thank you" to the monitor that |
| 145 | + * he wants to ignore. |
| 146 | + */ |
| 147 | + public boolean isEnabled() { |
| 148 | + return !Jenkins.get().getDisabledAdministrativeMonitors().contains(id); |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Returns true if this monitor is activated and |
| 153 | + * wants to produce a warning message. |
| 154 | + * |
| 155 | + * <p> |
| 156 | + * This method is called from the HTML rendering thread, |
| 157 | + * so it should run efficiently. |
| 158 | + */ |
| 159 | + public abstract boolean isActivated(); |
| 160 | + |
| 161 | + @Restricted(NoExternalUse.class) |
| 162 | + public boolean isActivationFake() { |
| 163 | + return false; |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * Returns true if this monitor is security related. |
| 168 | + * |
| 169 | + * This will be used to determine which icon will be used in the navigation bar. |
| 170 | + * |
| 171 | + * @since 2.267 |
| 172 | + */ |
| 173 | + public boolean isSecurity() { |
| 174 | + return false; |
| 175 | + } |
| 176 | + |
| 177 | + /** |
| 178 | + * URL binding to disable this monitor. |
| 179 | + */ |
| 180 | + @RequirePOST |
| 181 | + public void doDisable(StaplerRequest2 req, StaplerResponse2 rsp) throws IOException { |
| 182 | + Jenkins.get().checkPermission(Jenkins.ADMINISTER); |
| 183 | + disable(true); |
| 184 | + rsp.sendRedirect2(req.getContextPath() + "/manage"); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Required permission to view this admin monitor. |
| 189 | + * By default {@link Jenkins#ADMINISTER}, but {@link Jenkins#SYSTEM_READ} or {@link Jenkins#MANAGE} are also supported. |
| 190 | + * <p> |
| 191 | + * Changing this permission check to return {@link Jenkins#SYSTEM_READ} will make the active |
| 192 | + * administrative monitor appear on {@link ManageJenkinsAction} to users without Administer permission. |
| 193 | + * {@link #doDisable(StaplerRequest2, StaplerResponse2)} will still always require Administer permission. |
| 194 | + * </p> |
| 195 | + * <p> |
| 196 | + * This method only allows for a single permission to be returned. If more complex permission checks are required, |
| 197 | + * override {@link #checkRequiredPermission()} and {@link #hasRequiredPermission()} instead. |
| 198 | + * </p> |
| 199 | + * <p> |
| 200 | + * Implementers need to ensure that {@code doAct} and other web methods perform necessary permission checks: |
| 201 | + * Users with System Read permissions are expected to be limited to read-only access. |
| 202 | + * Form UI elements that change system state, e.g. toggling a feature on or off, need to be hidden from users |
| 203 | + * lacking Administer permission. |
| 204 | + * </p> |
| 205 | + * @since 2.233 |
| 206 | + * @deprecated Callers should use {@link #checkRequiredPermission()} or {@link #hasRequiredPermission()}. |
| 207 | + */ |
| 208 | + @Deprecated |
| 209 | + public Permission getRequiredPermission() { |
| 210 | + return Jenkins.ADMINISTER; |
| 211 | + } |
| 212 | + |
| 213 | + /** |
| 214 | + * Checks if the current user has the minimum required permission to view this administrative monitor. |
| 215 | + * <p> |
| 216 | + * Subclasses may override this method and {@link #hasRequiredPermission()} instead of {@link #getRequiredPermission()} to perform more complex permission checks, |
| 217 | + * for example, checking either {@link Jenkins#MANAGE} or {@link Jenkins#SYSTEM_READ}. |
| 218 | + * </p> |
| 219 | + * @see #getRequiredPermission() |
| 220 | + * @see #hasRequiredPermission() |
| 221 | + * @since 2.468 |
| 222 | + */ |
| 223 | + public void checkRequiredPermission() { |
| 224 | + Jenkins.get().checkPermission(getRequiredPermission()); |
| 225 | + } |
| 226 | + |
| 227 | + /** |
| 228 | + * Checks if the current user has the minimum required permission to view this administrative monitor. |
| 229 | + * <p> |
| 230 | + * Subclasses may override this method and {@link #checkRequiredPermission} instead of {@link #getRequiredPermission()} to perform more complex permission checks, |
| 231 | + * for example, checking either {@link Jenkins#MANAGE} or {@link Jenkins#SYSTEM_READ}. |
| 232 | + * </p> |
| 233 | + * @see #getRequiredPermission() |
| 234 | + * @see #checkRequiredPermission() |
| 235 | + * @since 2.468 |
| 236 | + */ |
| 237 | + public boolean hasRequiredPermission() { |
| 238 | + return Jenkins.get().hasPermission(getRequiredPermission()); |
| 239 | + } |
| 240 | + |
| 241 | + /** |
| 242 | + * Checks if the current user has the minimum required permission to view any administrative monitor. |
| 243 | + * |
| 244 | + * @return true if the current user has the minimum required permission to view any administrative monitor. |
| 245 | + * |
| 246 | + * @since 2.468 |
| 247 | + */ |
| 248 | + public static boolean hasPermissionToDisplay() { |
| 249 | + return Jenkins.get().hasAnyPermission(Jenkins.SYSTEM_READ, Jenkins.MANAGE); |
| 250 | + } |
| 251 | + |
| 252 | + /** |
| 253 | + * Ensure that URLs in this administrative monitor are only accessible to users with {@link #getRequiredPermission()}. |
| 254 | + */ |
| 255 | + @Override |
| 256 | + @Restricted(NoExternalUse.class) |
| 257 | + public Object getTarget() { |
| 258 | + checkRequiredPermission(); |
| 259 | + return this; |
| 260 | + } |
| 261 | + |
| 262 | + /** |
| 263 | + * All registered {@link AdministrativeMonitor} instances. |
| 264 | + */ |
| 265 | + public static ExtensionList<AdministrativeMonitor> all() { |
| 266 | + return ExtensionList.lookup(AdministrativeMonitor.class); |
| 267 | + } |
| 268 | +} |
0 commit comments