ACL.java 8.4 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
 * 
 * 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.
 */
24 25
package hudson.security;

26
import javax.annotation.Nonnull;
K
Kohsuke Kawaguchi 已提交
27
import hudson.remoting.Callable;
28 29
import hudson.model.ItemGroup;
import hudson.model.TopLevelItemDescriptor;
30
import jenkins.security.NonSerializableSecurityContext;
31
import jenkins.model.Jenkins;
32
import jenkins.security.NotReallyRoleSensitiveCallable;
33 34
import org.acegisecurity.AccessDeniedException;
import org.acegisecurity.Authentication;
35 36
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
K
kohsuke 已提交
37
import org.acegisecurity.providers.UsernamePasswordAuthenticationToken;
38 39
import org.acegisecurity.acls.sid.PrincipalSid;
import org.acegisecurity.acls.sid.Sid;
40 41
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
42 43

/**
K
kohsuke 已提交
44 45
 * Gate-keeper that controls access to Hudson's model objects.
 *
46 47 48 49 50 51
 * @author Kohsuke Kawaguchi
 */
public abstract class ACL {
    /**
     * Checks if the current security principal has this permission.
     *
K
kohsuke 已提交
52 53 54
     * <p>
     * This is just a convenience function.
     *
55 56 57
     * @throws AccessDeniedException
     *      if the user doesn't have the permission.
     */
58
    public final void checkPermission(@Nonnull Permission p) {
59
        Authentication a = Jenkins.getAuthentication();
K
kohsuke 已提交
60
        if(!hasPermission(a,p))
61
            throw new AccessDeniedException2(a,p);
K
kohsuke 已提交
62
    }
63

64 65 66 67 68 69
    /**
     * Checks if the current security principal has this permission.
     *
     * @return false
     *      if the user doesn't have the permission.
     */
70
    public final boolean hasPermission(@Nonnull Permission p) {
71
        return hasPermission(Jenkins.getAuthentication(),p);
72 73
    }

K
kohsuke 已提交
74 75
    /**
     * Checks if the given principle has the given permission.
K
kohsuke 已提交
76 77 78 79
     *
     * <p>
     * Note that {@link #SYSTEM} can be passed in as the authentication parameter,
     * in which case you should probably just assume it has every permission.
K
kohsuke 已提交
80
     */
81
    public abstract boolean hasPermission(@Nonnull Authentication a, @Nonnull Permission permission);
82

83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
    /**
     * Checks if the current security principal has the permission to create top level items within the specified item group.
     * <p>
     * Note that {@link #SYSTEM} can be passed in as the authentication parameter,
     * in which case you should probably just assume it has can create anything.
     * <p>
     * This is just a convenience function.
     * @param c the container of the item.
     * @param d the descriptor of the item to be created.
     * @throws AccessDeniedException
     *      if the user doesn't have the permission.
     * @since 1.582
     */
    public final void checkCreatePermission(@Nonnull ItemGroup c,
                                            @Nonnull TopLevelItemDescriptor d) {
        Authentication a = Jenkins.getAuthentication();
        if (!hasCreatePermission(a, c, d)) {
            throw new AccessDeniedException(Messages.AccessDeniedException2_MissingPermission(a.getName(),
                    "Item/CREATE/" + d.getDisplayName()));
        }
    }
    /**
S
Stephen Connolly 已提交
105
     * Checks if the given principal has the permission to create top level items within the specified item group.
106 107 108
     * <p>
     * Note that {@link #SYSTEM} can be passed in as the authentication parameter,
     * in which case you should probably just assume it has can create anything.
S
Stephen Connolly 已提交
109
     * @param a the principal.
110 111 112 113 114 115 116 117 118 119 120
     * @param c the container of the item.
     * @param d the descriptor of the item to be created.
     * @return false
     *      if the user doesn't have the permission.
     * @since 1.582
     */
    public boolean hasCreatePermission(@Nonnull Authentication a, @Nonnull ItemGroup c,
                                       @Nonnull TopLevelItemDescriptor d) {
        return true;
    }

121 122 123 124 125 126 127 128 129 130 131 132
    //
    // Sid constants
    //

    /**
     * Special {@link Sid} that represents "everyone", even including anonymous users.
     *
     * <p>
     * This doesn't need to be included in {@link Authentication#getAuthorities()},
     * but {@link ACL} is responsible for checking it nontheless, as if it was the
     * last entry in the granted authority.
     */
K
kohsuke 已提交
133 134 135 136 137 138
    public static final Sid EVERYONE = new Sid() {
        @Override
        public String toString() {
            return "EVERYONE";
        }
    };
139

140 141 142 143 144
    /**
     * The username for the anonymous user
     */
    @Restricted(NoExternalUse.class)
    public static final String ANONYMOUS_USERNAME = "anonymous";
145 146 147 148 149 150
    /**
     * {@link Sid} that represents the anonymous unauthenticated users.
     * <p>
     * {@link HudsonFilter} sets this up, so this sid remains the same
     * regardless of the current {@link SecurityRealm} in use.
     */
151
    public static final Sid ANONYMOUS = new PrincipalSid(ANONYMOUS_USERNAME);
K
kohsuke 已提交
152

K
kohsuke 已提交
153 154
    protected static final Sid[] AUTOMATIC_SIDS = new Sid[]{EVERYONE,ANONYMOUS};

155 156 157 158 159 160
    /**
     * The username for the system user
     */
    @Restricted(NoExternalUse.class)
    public static final String SYSTEM_USERNAME = "SYSTEM";

K
kohsuke 已提交
161 162 163 164 165 166
    /**
     * {@link Sid} that represents the Hudson itself.
     * <p>
     * This is used when Hudson is performing computation for itself, instead
     * of acting on behalf of an user, such as doing builds.
     */
167
    public static final Authentication SYSTEM = new UsernamePasswordAuthenticationToken(SYSTEM_USERNAME,"SYSTEM");
168 169 170 171 172 173 174

    /**
     * Changes the {@link Authentication} associated with the current thread
     * to the specified one, and returns  the previous security context.
     * 
     * <p>
     * When the impersonation is over, be sure to restore the previous authentication
175 176
     * via {@code SecurityContextHolder.setContext(returnValueFromThisMethod)};
     * or just use {@link #impersonate(Authentication,Runnable)}.
177 178 179 180
     * 
     * <p>
     * We need to create a new {@link SecurityContext} instead of {@link SecurityContext#setAuthentication(Authentication)}
     * because the same {@link SecurityContext} object is reused for all the concurrent requests from the same session.
J
Jesse Glick 已提交
181
     * @since 1.462
182
     */
183
    public static @Nonnull SecurityContext impersonate(@Nonnull Authentication auth) {
184
        SecurityContext old = SecurityContextHolder.getContext();
185
        SecurityContextHolder.setContext(new NonSerializableSecurityContext(auth));
186 187
        return old;
    }
188 189 190 191 192

    /**
     * Safer variant of {@link #impersonate(Authentication)} that does not require a finally-block.
     * @param auth authentication, such as {@link #SYSTEM}
     * @param body an action to run with this alternate authentication in effect
K
Kohsuke Kawaguchi 已提交
193
     * @since 1.509
194
     */
195
    public static void impersonate(@Nonnull Authentication auth, @Nonnull Runnable body) {
196 197 198 199 200 201 202 203
        SecurityContext old = impersonate(auth);
        try {
            body.run();
        } finally {
            SecurityContextHolder.setContext(old);
        }
    }

K
Kohsuke Kawaguchi 已提交
204 205 206
    /**
     * Safer variant of {@link #impersonate(Authentication)} that does not require a finally-block.
     * @param auth authentication, such as {@link #SYSTEM}
207 208
     * @param body an action to run with this alternate authentication in effect (try {@link NotReallyRoleSensitiveCallable})
     * @since 1.587
K
Kohsuke Kawaguchi 已提交
209 210 211 212 213 214 215 216 217 218
     */
    public static <V,T extends Exception> V impersonate(Authentication auth, Callable<V,T> body) throws T {
        SecurityContext old = impersonate(auth);
        try {
            return body.call();
        } finally {
            SecurityContextHolder.setContext(old);
        }
    }

219
}