|
| 1 | +package com.microsoft.jenkins.azuread; |
| 2 | + |
| 3 | +import edu.umd.cs.findbugs.annotations.NonNull; |
| 4 | +import hudson.Extension; |
| 5 | +import hudson.model.Computer; |
| 6 | +import hudson.model.Node; |
| 7 | +import hudson.model.User; |
| 8 | +import hudson.security.AuthorizationStrategy; |
| 9 | +import hudson.security.Permission; |
| 10 | +import hudson.security.PermissionScope; |
| 11 | +import hudson.slaves.NodePropertyDescriptor; |
| 12 | +import hudson.util.FormValidation; |
| 13 | +import jenkins.model.Jenkins; |
| 14 | +import jenkins.model.NodeListener; |
| 15 | +import net.sf.json.JSONObject; |
| 16 | +import org.jenkinsci.Symbol; |
| 17 | +import org.jenkinsci.plugins.matrixauth.AbstractAuthorizationPropertyConverter; |
| 18 | +import org.jenkinsci.plugins.matrixauth.AuthorizationMatrixNodeProperty; |
| 19 | +import org.jenkinsci.plugins.matrixauth.AuthorizationPropertyDescriptor; |
| 20 | +import org.kohsuke.accmod.Restricted; |
| 21 | +import org.kohsuke.accmod.restrictions.DoNotUse; |
| 22 | +import org.kohsuke.accmod.restrictions.NoExternalUse; |
| 23 | +import org.kohsuke.accmod.restrictions.suppressions.SuppressRestrictedWarnings; |
| 24 | +import org.kohsuke.stapler.AncestorInPath; |
| 25 | +import org.kohsuke.stapler.QueryParameter; |
| 26 | +import org.kohsuke.stapler.StaplerRequest; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.logging.Level; |
| 31 | +import java.util.logging.Logger; |
| 32 | + |
| 33 | +public class AzureAdAuthorizationMatrixNodeProperty extends AuthorizationMatrixNodeProperty { |
| 34 | + |
| 35 | + private final transient ObjId2FullSidMap objId2FullSidMap = new ObjId2FullSidMap(); |
| 36 | + |
| 37 | + public AzureAdAuthorizationMatrixNodeProperty() { |
| 38 | + super(Collections.emptyMap()); |
| 39 | + } |
| 40 | + |
| 41 | + void refreshMap() { |
| 42 | + for (String fullSid : this.getAllSIDs()) { |
| 43 | + objId2FullSidMap.putFullSid(fullSid); |
| 44 | + } |
| 45 | + new AzureAdAuthorizationMatrixNodeProperty(); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void add(Permission p, String sid) { |
| 50 | + super.add(p, sid); |
| 51 | + objId2FullSidMap.putFullSid(sid); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public boolean hasExplicitPermission(String sid, Permission p) { |
| 56 | + // Jenkins will pass in the object Id as sid |
| 57 | + final String objectId = sid; |
| 58 | + if (objectId == null) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + return super.hasExplicitPermission(objId2FullSidMap.getOrOriginal(objectId), p); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public boolean hasPermission(String sid, Permission p) { |
| 66 | + // Jenkins will pass in the object Id as sid |
| 67 | + final String objectId = sid; |
| 68 | + return super.hasPermission(objId2FullSidMap.getOrOriginal(objectId), p); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public boolean hasPermission(String sid, Permission p, boolean principal) { |
| 73 | + // Jenkins will pass in the object Id as sid |
| 74 | + final String objectId = sid; |
| 75 | + return super.hasPermission(objId2FullSidMap.getOrOriginal(objectId), p, principal); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Persist {@link AzureAdAuthorizationMatrixNodeProperty} as a list of IDs that |
| 80 | + * represent {@link AzureAdAuthorizationMatrixNodeProperty#getGrantedPermissions()}. |
| 81 | + */ |
| 82 | + @Restricted(NoExternalUse.class) |
| 83 | + @SuppressRestrictedWarnings(AbstractAuthorizationPropertyConverter.class) |
| 84 | + public static final class ConverterImpl extends |
| 85 | + AbstractAuthorizationPropertyConverter<AzureAdAuthorizationMatrixNodeProperty> { |
| 86 | + public boolean canConvert(Class type) { |
| 87 | + return type == AzureAdAuthorizationMatrixNodeProperty.class; |
| 88 | + } |
| 89 | + |
| 90 | + public AzureAdAuthorizationMatrixNodeProperty create() { |
| 91 | + return new AzureAdAuthorizationMatrixNodeProperty(); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + @Extension |
| 96 | + @Symbol("azureAdAuthorizationMatrix") |
| 97 | + @SuppressRestrictedWarnings(AuthorizationPropertyDescriptor.class) |
| 98 | + public static class DescriptorImpl extends NodePropertyDescriptor |
| 99 | + implements AuthorizationPropertyDescriptor<AzureAdAuthorizationMatrixNodeProperty> { |
| 100 | + |
| 101 | + @Override |
| 102 | + public AzureAdAuthorizationMatrixNodeProperty create() { |
| 103 | + return new AzureAdAuthorizationMatrixNodeProperty(); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public PermissionScope getPermissionScope() { |
| 108 | + return PermissionScope.COMPUTER; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public AzureAdAuthorizationMatrixNodeProperty newInstance( |
| 113 | + StaplerRequest req, |
| 114 | + @NonNull JSONObject formData |
| 115 | + ) throws FormException { |
| 116 | + return createNewInstance(req, formData, false); |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public boolean isApplicable() { |
| 121 | + return Jenkins.get().getAuthorizationStrategy() instanceof AzureAdMatrixAuthorizationStrategy; |
| 122 | + } |
| 123 | + |
| 124 | + @NonNull |
| 125 | + @Override |
| 126 | + public String getDisplayName() { |
| 127 | + return "Azure Active Directory Authorization Matrix"; |
| 128 | + } |
| 129 | + |
| 130 | + @SuppressWarnings("unused") // called by jelly |
| 131 | + public boolean isDisableGraphIntegration() { |
| 132 | + AzureSecurityRealm securityRealm = (AzureSecurityRealm) Jenkins.get().getSecurityRealm(); |
| 133 | + return securityRealm.isDisableGraphIntegration(); |
| 134 | + } |
| 135 | + |
| 136 | + @Restricted(DoNotUse.class) |
| 137 | + public FormValidation doCheckName(@AncestorInPath Computer computer, @QueryParameter String value) { |
| 138 | + if (isDisableGraphIntegration()) { |
| 139 | + return Utils.undecidableResponse(value); |
| 140 | + } |
| 141 | + |
| 142 | + // Computer isn't a DescriptorByNameOwner before Jenkins 2.78, and then @AncestorInPath doesn't work |
| 143 | + return doCheckName_(value, |
| 144 | + computer == null ? Jenkins.get() : computer, |
| 145 | + computer == null ? Jenkins.ADMINISTER : Computer.CONFIGURE); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + /** |
| 150 | + * Ensure that the user creating a node has Read and Configure permissions. |
| 151 | + */ |
| 152 | + @Extension |
| 153 | + @Restricted(NoExternalUse.class) |
| 154 | + public static class NodeListenerImpl extends NodeListener { |
| 155 | + @Override |
| 156 | + protected void onCreated(@NonNull Node node) { |
| 157 | + AuthorizationStrategy authorizationStrategy = Jenkins.get().getAuthorizationStrategy(); |
| 158 | + if (authorizationStrategy instanceof AzureAdMatrixAuthorizationStrategy) { |
| 159 | + AzureAdMatrixAuthorizationStrategy strategy = |
| 160 | + (AzureAdMatrixAuthorizationStrategy) authorizationStrategy; |
| 161 | + |
| 162 | + AuthorizationMatrixNodeProperty prop = node |
| 163 | + .getNodeProperty(AzureAdAuthorizationMatrixNodeProperty.class); |
| 164 | + if (prop == null) { |
| 165 | + prop = new AzureAdAuthorizationMatrixNodeProperty(); |
| 166 | + } |
| 167 | + |
| 168 | + User current = User.current(); |
| 169 | + String sid = current == null ? "anonymous" : current.getId(); |
| 170 | + |
| 171 | + if (!strategy.getACL(node).hasPermission2(Jenkins.getAuthentication2(), Computer.CONFIGURE)) { |
| 172 | + prop.add(Computer.CONFIGURE, sid); |
| 173 | + } |
| 174 | + if (!prop.getGrantedPermissions().isEmpty()) { |
| 175 | + try { |
| 176 | + node.getNodeProperties().replace(prop); |
| 177 | + } catch (IOException ex) { |
| 178 | + LOGGER.log(Level.WARNING, "Failed to grant creator permissions on node " |
| 179 | + + node.getDisplayName(), ex); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + private static final Logger LOGGER = Logger.getLogger(AzureAdAuthorizationMatrixNodeProperty.class.getName()); |
| 187 | +} |
0 commit comments