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
This commit is contained in:
kohsuke committed 2007-11-26 05:12:12 +00:00
1 parent 65ef99fd45
commit 58146e4cdc
6 files changed
+290

No files matched your search

@@ -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);
}
@@ -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.
*
* <p>
* 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;
}
}
@@ -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();
}
}
@@ -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.
*
* <p>
* 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.
*
* <p>
* 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.
*
* <p>
* 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<Permission> ps = PERMISSIONS.get(owner);
if(ps==null) {
ps = new ArrayList<Permission>();
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<Permission> getAll() {
return ALL;
}
/**
* All the permissions in the system, keyed by their owners.
*/
private static final Map<Class, List<Permission>> PERMISSIONS = new HashMap<Class,List<Permission>>();
/**
* The same as {@link #PERMISSIONS} but in a single list.
*/
private static final List<Permission> ALL = new ArrayList<Permission>();
}
@@ -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<Entry> entries = new ArrayList<Entry>();
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");
}
@@ -0,0 +1,5 @@
<html>
<body>
Security-related code
</body>
</html>