diff --git a/core/src/main/java/hudson/security/ACL.java b/core/src/main/java/hudson/security/ACL.java new file mode 100644 index 0000000000..33ab4f6f8d --- /dev/null +++ b/core/src/main/java/hudson/security/ACL.java @@ -0,0 +1,19 @@ +package hudson.security; + +import org.acegisecurity.AccessDeniedException; +import org.acegisecurity.Authentication; + +/** + * @author Kohsuke Kawaguchi + */ +public abstract class ACL { + /** + * Checks if the current security principal has this permission. + * + * @throws AccessDeniedException + * if the user doesn't have the permission. + */ + public abstract void checkPermission(Permission p); + + public abstract boolean hasPermission(Authentication a, Permission permission); +} diff --git a/core/src/main/java/hudson/security/AuthenticationManagerProxy.java b/core/src/main/java/hudson/security/AuthenticationManagerProxy.java new file mode 100644 index 0000000000..b9b9614a33 --- /dev/null +++ b/core/src/main/java/hudson/security/AuthenticationManagerProxy.java @@ -0,0 +1,33 @@ +package hudson.security; + +import org.acegisecurity.AuthenticationManager; +import org.acegisecurity.Authentication; +import org.acegisecurity.AuthenticationException; +import org.acegisecurity.DisabledException; + +/** + * {@link AuthenticationManager} proxy that delegates to another instance. + * + *
+ * This is used so that we can set up servlet filters first (which requires a reference + * to {@link AuthenticationManager}), then later change the actual authentication manager + * (and its set up) at runtime. + * + * @author Kohsuke Kawaguchi + */ +public class AuthenticationManagerProxy implements AuthenticationManager { + private volatile AuthenticationManager manager; + + public Authentication authenticate(Authentication authentication) throws AuthenticationException { + AuthenticationManager m = manager; // fix the reference we are working with + + if(m ==null) + throw new DisabledException("Authentication service is still not ready yet"); + else + return m.authenticate(authentication); + } + + public void setManager(AuthenticationManager manager) { + this.manager = manager; + } +} diff --git a/core/src/main/java/hudson/security/ChainedServletFilter.java b/core/src/main/java/hudson/security/ChainedServletFilter.java new file mode 100644 index 0000000000..6fbf5c8887 --- /dev/null +++ b/core/src/main/java/hudson/security/ChainedServletFilter.java @@ -0,0 +1,60 @@ +package hudson.security; + +import javax.servlet.Filter; +import javax.servlet.FilterConfig; +import javax.servlet.ServletException; +import javax.servlet.ServletRequest; +import javax.servlet.ServletResponse; +import javax.servlet.FilterChain; +import java.util.Collection; +import java.io.IOException; + +/** + * Servlet {@link Filter} that chains multiple {@link Filter}s. + * + * @author Kohsuke Kawaguchi + */ +public class ChainedServletFilter implements Filter { + // array is assumed to be immutable once set + protected volatile Filter[] filters; + + public ChainedServletFilter() { + filters = new Filter[0]; + } + + public ChainedServletFilter(Collection extends Filter> filters) { + setFilters(filters); + } + + public void setFilters(Collection extends Filter> filters) { + this.filters = filters.toArray(new Filter[filters.size()]); + } + + public void init(FilterConfig filterConfig) throws ServletException { + for (Filter f : filters) + f.init(filterConfig); + } + + public void doFilter(ServletRequest request, ServletResponse response, final FilterChain chain) throws IOException, ServletException { + new FilterChain() { + private int position=0; + // capture the array for thread-safety + private final Filter[] filters = ChainedServletFilter.this.filters; + + public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException { + if(position==filters.length) { + // reached to the end + chain.doFilter(request,response); + } else { + // call next + filters[position++].doFilter(request,response,this); + } + } + }.doFilter(request,response); + } + + public void destroy() { + for (Filter f : filters) + f.destroy(); + } +} diff --git a/core/src/main/java/hudson/security/Permission.java b/core/src/main/java/hudson/security/Permission.java new file mode 100644 index 0000000000..54dfe2b212 --- /dev/null +++ b/core/src/main/java/hudson/security/Permission.java @@ -0,0 +1,72 @@ +package hudson.security; + +import java.util.List; +import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; + +/** + * Permission, which represents activity that requires a security privilege. + * + *
+ * Each permission is represented by a specific instance of {@link Permission}. + * + * @author Kohsuke Kawaguchi + */ +public class Permission { + public final Class owner; + + public final String name; + /** + * Bundled {@link Permission} that also implies this permission. + * + *
+ * This allows us to organize permissions in a hierarchy, so that + * for example we can say "view workspace" permission is implied by + * the (broader) "read" permission. + * + *
+ * The idea here is that for most people, access control based on
+ * such broad permission bundle is good enough, and those few
+ * that need finer control can do so.
+ */
+ public final Permission impliedBy;
+
+ public Permission(Class owner, String name, Permission impliedBy) {
+ this.owner = owner;
+ this.name = name;
+ this.impliedBy = impliedBy;
+
+ synchronized (PERMISSIONS) {
+ List