From 58146e4cdc5c014a96ad03efec0360751b1f0949 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Mon, 26 Nov 2007 05:12:12 +0000 Subject: [PATCH] Started integrating acegi security into Hudson. Introducing basic security model objects like Permission and ACL. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@6061 71c3de6d-444a-0410-be80-ed276b4c234a --- core/src/main/java/hudson/security/ACL.java | 19 ++++ .../security/AuthenticationManagerProxy.java | 33 ++++++ .../hudson/security/ChainedServletFilter.java | 60 +++++++++++ .../main/java/hudson/security/Permission.java | 72 +++++++++++++ .../main/java/hudson/security/SparseACL.java | 101 ++++++++++++++++++ .../main/java/hudson/security/package.html | 5 + 6 files changed, 290 insertions(+) create mode 100644 core/src/main/java/hudson/security/ACL.java create mode 100644 core/src/main/java/hudson/security/AuthenticationManagerProxy.java create mode 100644 core/src/main/java/hudson/security/ChainedServletFilter.java create mode 100644 core/src/main/java/hudson/security/Permission.java create mode 100644 core/src/main/java/hudson/security/SparseACL.java create mode 100644 core/src/main/java/hudson/security/package.html 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 filters) { + setFilters(filters); + } + + public void setFilters(Collection 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 ps = PERMISSIONS.get(owner); + if(ps==null) { + ps = new ArrayList(); + PERMISSIONS.put(owner,ps); + } + ps.add(this); + } + ALL.add(this); + } + + public Permission(Class owner, String name) { + this(owner,name,null); + } + + public String toString() { + return "Permission["+owner+','+name+']'; + } + + public static List getAll() { + return ALL; + } + + /** + * All the permissions in the system, keyed by their owners. + */ + private static final Map> PERMISSIONS = new HashMap>(); + + /** + * The same as {@link #PERMISSIONS} but in a single list. + */ + private static final List ALL = new ArrayList(); +} diff --git a/core/src/main/java/hudson/security/SparseACL.java b/core/src/main/java/hudson/security/SparseACL.java new file mode 100644 index 0000000000..40095ffaea --- /dev/null +++ b/core/src/main/java/hudson/security/SparseACL.java @@ -0,0 +1,101 @@ +package hudson.security; + +import org.acegisecurity.AccessDeniedException; +import org.acegisecurity.Authentication; +import org.acegisecurity.GrantedAuthority; +import org.acegisecurity.acls.sid.GrantedAuthoritySid; +import org.acegisecurity.acls.sid.PrincipalSid; +import org.acegisecurity.acls.sid.Sid; +import org.acegisecurity.context.SecurityContextHolder; + +import java.util.ArrayList; +import java.util.List; + +/** + * Accses control list. + * + * @author Kohsuke Kawaguchi + */ +public class SparseACL extends ACL { + public static final class Entry { + // Sid has value-equality semantics + public final Sid sid; + public final Permission permission; + public final boolean allowed; + + public Entry(Sid sid, Permission permission, boolean allowed) { + this.sid = sid; + this.permission = permission; + this.allowed = allowed; + } + } + + private final List entries = new ArrayList(); + private ACL parent; + + public SparseACL(ACL parent) { + this.parent = parent; + } + + public void add(Entry e) { + entries.add(e); + } + + public void add(Sid sid, Permission permission, boolean allowed) { + add(new Entry(sid,permission,allowed)); + } + + /** + * Checks if the current security principal has this permission. + * + * @throws AccessDeniedException + * if the user doesn't have the permission. + */ + public void checkPermission(Permission p) { + Authentication a = SecurityContextHolder.getContext().getAuthentication(); + + if(!hasPermission(a,p)) + throw new AccessDeniedException(a.toString()+" is missing "+p.name); + } + + public boolean hasPermission(Authentication a, Permission permission) { + // ACL entries for this principal takes precedence + Boolean b = hasPermission(new PrincipalSid(a),permission); + if(b!=null) return b; + + // after that, we check if the groups this principal belongs to + // has any ACL entries. + // here we are using GrantedAuthority as a group + for(GrantedAuthority ga : a.getAuthorities()) { + b = hasPermission(new GrantedAuthoritySid(ga),permission); + if(b!=null) return b; + } + + // finally everyone + b = hasPermission(EVERYONE,permission); + if(b!=null) return b; + + if(parent!=null) + return parent.hasPermission(a,permission); + + return false; + } + + // sub-routine used by the above method + private Boolean hasPermission(Sid p, Permission permission) { + for( ; permission!=null; permission=permission.impliedBy ) { + for (Entry e : entries) { + if(e.permission==permission && e.sid.equals(p)) + return e.allowed; + } + } + return null; + } + + /** + * {@link Sid} that represents everyone, including anonymous users. + */ + public static final Sid EVERYONE = new Sid() {}; + + public static final Sid ANONYMOUS = new PrincipalSid("anonymous"); +} diff --git a/core/src/main/java/hudson/security/package.html b/core/src/main/java/hudson/security/package.html new file mode 100644 index 0000000000..ced59437c4 --- /dev/null +++ b/core/src/main/java/hudson/security/package.html @@ -0,0 +1,5 @@ + + +Security-related code + + \ No newline at end of file