|
| 1 | +package hudson.plugins.active_directory; |
| 2 | + |
| 3 | +/* |
| 4 | + * The MIT License |
| 5 | + * |
| 6 | + * Copyright (c) 2017, Felix Belzunce Arcos, CloudBees, Inc., and contributors |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; |
| 28 | +import hudson.Extension; |
| 29 | +import hudson.model.ManagementLink; |
| 30 | +import hudson.security.SecurityRealm; |
| 31 | +import hudson.util.ListBoxModel; |
| 32 | +import jenkins.model.Jenkins; |
| 33 | +import jenkins.util.ProgressiveRendering; |
| 34 | +import net.sf.json.JSON; |
| 35 | +import net.sf.json.JSONArray; |
| 36 | +import net.sf.json.JSONObject; |
| 37 | +import org.acegisecurity.userdetails.UserDetails; |
| 38 | +import org.kohsuke.accmod.Restricted; |
| 39 | +import org.kohsuke.accmod.restrictions.NoExternalUse; |
| 40 | + |
| 41 | +import java.io.IOException; |
| 42 | +import java.util.Collections; |
| 43 | +import java.util.LinkedList; |
| 44 | +import java.util.List; |
| 45 | + |
| 46 | +/** |
| 47 | + * ManagementLink to provide an Active Directory health status |
| 48 | + * |
| 49 | + * Intend to report a health status of the Active Directory Domain through |
| 50 | + * a ManagementLink on Jenkins. |
| 51 | + * - Check if there is any broken Domain Controller on the farm |
| 52 | + * - Report the connection time |
| 53 | + * - Provides the User lookup time |
| 54 | + * |
| 55 | + * @since 2.1 |
| 56 | + */ |
| 57 | +@Extension |
| 58 | +public class ActiveDirectoryStatus extends ManagementLink { |
| 59 | + |
| 60 | + @Override |
| 61 | + public String getIconFileName() { |
| 62 | + return "/plugin/active-directory/images/icon.png"; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public String getDisplayName() { |
| 67 | + return Messages._ActiveDirectoryStatus_ActiveDirectoryHealthStatus().toString(); |
| 68 | + } |
| 69 | + |
| 70 | + @Override |
| 71 | + public String getUrlName() { |
| 72 | + return "ad-health"; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Get the list of domains configured on the Security Realm |
| 77 | + * |
| 78 | + * @return the Active Directory domains {@link ActiveDirectoryDomain}. |
| 79 | + */ |
| 80 | + @Restricted(NoExternalUse.class) |
| 81 | + public static List<ActiveDirectoryDomain> getDomains() { |
| 82 | + SecurityRealm securityRealm = Jenkins.getInstance().getSecurityRealm(); |
| 83 | + if (securityRealm instanceof ActiveDirectorySecurityRealm) { |
| 84 | + ActiveDirectorySecurityRealm activeDirectorySecurityRealm = (ActiveDirectorySecurityRealm) securityRealm; |
| 85 | + return activeDirectorySecurityRealm.getDomains(); |
| 86 | + } |
| 87 | + return Collections.emptyList(); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Start the Domain Controller Health checks against a specific domain |
| 92 | + * |
| 93 | + * @param domain to check the health |
| 94 | + * @return {@link ProgressiveRendering} |
| 95 | + */ |
| 96 | + @Restricted(NoExternalUse.class) |
| 97 | + public ProgressiveRendering startDomainHealthChecks(final String domain) { |
| 98 | + return new ProgressiveRendering() { |
| 99 | + final List<ServerHealth> domainHealth = new LinkedList<ServerHealth>(); |
| 100 | + @Override protected void compute() throws Exception { |
| 101 | + for (ActiveDirectoryDomain domainItem : getDomains()) { |
| 102 | + if (canceled()) { |
| 103 | + return; |
| 104 | + } |
| 105 | + if (domainItem.getName().equals(domain)) { |
| 106 | + SecurityRealm securityRealm = Jenkins.getInstance().getSecurityRealm(); |
| 107 | + if (securityRealm instanceof ActiveDirectorySecurityRealm) { |
| 108 | + ActiveDirectorySecurityRealm activeDirectorySecurityRealm = (ActiveDirectorySecurityRealm) securityRealm; |
| 109 | + List<SocketInfo> servers = activeDirectorySecurityRealm.getDescriptor().obtainLDAPServer(domainItem); |
| 110 | + for (SocketInfo socketInfo : servers) { |
| 111 | + ServerHealth serverHealth = new ServerHealth(socketInfo); |
| 112 | + domainHealth.add(serverHealth); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + @Override protected synchronized JSON data() { |
| 119 | + JSONArray r = new JSONArray(); |
| 120 | + for (ServerHealth serverHealth : domainHealth) { |
| 121 | + r.add(serverHealth); |
| 122 | + } |
| 123 | + domainHealth.clear(); |
| 124 | + return new JSONObject().accumulate("domainHealth", r); |
| 125 | + } |
| 126 | + }; |
| 127 | + } |
| 128 | + |
| 129 | + @Restricted(NoExternalUse.class) |
| 130 | + public ListBoxModel doFillDomainsItems() { |
| 131 | + ListBoxModel model = new ListBoxModel(); |
| 132 | + for (ActiveDirectoryDomain domain : getDomains()) { |
| 133 | + model.add(domain.getName()); |
| 134 | + } |
| 135 | + return model; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * ServerHealth of a SocketInfo |
| 140 | + */ |
| 141 | + @SuppressFBWarnings("UUF_UNUSED_FIELD") |
| 142 | + public static class ServerHealth extends SocketInfo { |
| 143 | + /** |
| 144 | + * true if able to retrieve the user details from Jenkins |
| 145 | + */ |
| 146 | + private boolean canLogin; |
| 147 | + |
| 148 | + /** |
| 149 | + * Time for a Socket to reach out the target server |
| 150 | + */ |
| 151 | + private long pingExecutionTime; |
| 152 | + |
| 153 | + /** |
| 154 | + * Total amount of time for Jenkins to perform SecurityRealm.loadUserByUsername |
| 155 | + */ |
| 156 | + private long loginExecutionTime; |
| 157 | + |
| 158 | + public ServerHealth(SocketInfo socketInfo) { |
| 159 | + super(socketInfo.getHost(), socketInfo.getPort()); |
| 160 | + this.pingExecutionTime = this.computePingExecutionTime(); |
| 161 | + this.loginExecutionTime = this.computeLoginExecutionTime(); |
| 162 | + } |
| 163 | + |
| 164 | + @Restricted(NoExternalUse.class) |
| 165 | + public boolean isCanLogin() { |
| 166 | + return true ? loginExecutionTime != -1 : false; |
| 167 | + } |
| 168 | + |
| 169 | + @Restricted(NoExternalUse.class) |
| 170 | + public long getPingExecutionTime() { |
| 171 | + return pingExecutionTime; |
| 172 | + } |
| 173 | + |
| 174 | + @Restricted(NoExternalUse.class) |
| 175 | + public long getLoginExecutionTime() { |
| 176 | + return loginExecutionTime; |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Retrieve the time for Jenkins to perform SecurityRealm.loadUserByUsername |
| 181 | + * |
| 182 | + * @return -1 in case the user could not be retrieved |
| 183 | + */ |
| 184 | + private long computeLoginExecutionTime() { |
| 185 | + String username = Jenkins.getAuthentication().getName(); |
| 186 | + long t0 = System.currentTimeMillis(); |
| 187 | + UserDetails userDetails = Jenkins.getInstance().getSecurityRealm().loadUserByUsername(username); |
| 188 | + long t1 = System.currentTimeMillis(); |
| 189 | + return (userDetails!=null) ? (t1 - t0) : -1; |
| 190 | + } |
| 191 | + |
| 192 | + /** |
| 193 | + * Retrieve the time to to establish a Socket connection with the AD server |
| 194 | + * |
| 195 | + * @return -1 in case the connection failed |
| 196 | + */ |
| 197 | + private long computePingExecutionTime() { |
| 198 | + try { |
| 199 | + long t0 = System.currentTimeMillis(); |
| 200 | + super.connect().close(); |
| 201 | + long t1 = System.currentTimeMillis(); |
| 202 | + return t1-t0; |
| 203 | + } catch (IOException e) { |
| 204 | + } |
| 205 | + return -1; |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | +} |
0 commit comments