mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-18 06:22:29 +00:00
integrated the Acegi-based security system to modernize the security handling of Hudson.
So far I only implemented enough to emulate the old "either you are admin or not" model, with authentication by the container. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@6169 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
1 parent
434a978e1d
commit
eb6d3da111
5 files changed
+238
-27
No files matched your search
@@ -29,8 +29,15 @@ import hudson.scm.SCMDescriptor;
|
||||
import hudson.scm.SCMS;
|
||||
import hudson.search.CollectionSearchIndex;
|
||||
import hudson.search.SearchIndexBuilder;
|
||||
import hudson.security.ACL;
|
||||
import hudson.security.AuthorizationStrategy;
|
||||
import hudson.security.BasicAuthenticationFilter;
|
||||
import hudson.security.HudsonFilter;
|
||||
import hudson.security.LegacyAuthorizationStrategy;
|
||||
import hudson.security.LegacySecurityRealm;
|
||||
import hudson.security.Permission;
|
||||
import hudson.security.SecurityMode;
|
||||
import hudson.security.SecurityRealm;
|
||||
import hudson.tasks.BuildStep;
|
||||
import hudson.tasks.BuildWrapper;
|
||||
import hudson.tasks.BuildWrappers;
|
||||
@@ -54,10 +61,10 @@ import hudson.widgets.Widget;
|
||||
import org.apache.commons.fileupload.FileItem;
|
||||
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
||||
import org.apache.commons.fileupload.servlet.ServletFileUpload;
|
||||
import org.kohsuke.stapler.QueryParameter;
|
||||
import org.kohsuke.stapler.Stapler;
|
||||
import org.kohsuke.stapler.StaplerRequest;
|
||||
import org.kohsuke.stapler.StaplerResponse;
|
||||
import org.kohsuke.stapler.QueryParameter;
|
||||
import org.kohsuke.stapler.export.Exported;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
@@ -126,16 +133,37 @@ public final class Hudson extends View implements ItemGroup<TopLevelItem>, Node
|
||||
/**
|
||||
* False to enable anyone to do anything.
|
||||
* Left as a field so that we can still read old data that uses this flag.
|
||||
* @see #security
|
||||
*
|
||||
* @see #authorizationStrategy
|
||||
* @see #securityRealm
|
||||
*/
|
||||
private Boolean useSecurity;
|
||||
|
||||
/**
|
||||
* Hudson's security mode.
|
||||
* To supercede {@link #useSecurity} eventually.
|
||||
* @since 1.160
|
||||
* Controls how the
|
||||
* <a href="http://en.wikipedia.org/wiki/Authorization">authorization</a>
|
||||
* is handled in Hudson.
|
||||
* <p>
|
||||
* This ultimately controls who has access to what.
|
||||
*
|
||||
* Never null.
|
||||
*/
|
||||
private SecurityMode security = SecurityMode.UNSECURED;
|
||||
private volatile AuthorizationStrategy authorizationStrategy;
|
||||
|
||||
/**
|
||||
* Controls a part of the
|
||||
* <a href="http://en.wikipedia.org/wiki/Authentication">authentication</a>
|
||||
* handling in Hudson.
|
||||
* <p>
|
||||
* Intuitively, this corresponds to the user database.
|
||||
*
|
||||
* See {@link HudsonFilter} for the concrete authentication protocol.
|
||||
*
|
||||
* Never null.
|
||||
*
|
||||
* @see #getSecurity()
|
||||
*/
|
||||
private volatile SecurityRealm securityRealm;
|
||||
|
||||
/**
|
||||
* Message displayed in the top page.
|
||||
@@ -804,27 +832,50 @@ public final class Hudson extends View implements ItemGroup<TopLevelItem>, Node
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* Use {@link #getSecurity()} instead.
|
||||
* A convenience method to check if there's some security
|
||||
* restrictions in place.
|
||||
*/
|
||||
public boolean isUseSecurity() {
|
||||
return security != SecurityMode.UNSECURED;
|
||||
return securityRealm!=SecurityRealm.NO_AUTHENTICATION;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* Use {@link #setSecurity(SecurityMode)} instead.
|
||||
* Returns the constant that captures the three basic security modes
|
||||
* in Hudson.
|
||||
*/
|
||||
public void setUseSecurity(boolean useSecurity) {
|
||||
this.security = useSecurity ? SecurityMode.LEGACY : SecurityMode.UNSECURED;
|
||||
}
|
||||
|
||||
public SecurityMode getSecurity() {
|
||||
return security;
|
||||
// fix the variable so that this code works under concurrent modification to securityRealm.
|
||||
SecurityRealm realm = securityRealm;
|
||||
|
||||
if(realm==SecurityRealm.NO_AUTHENTICATION)
|
||||
return SecurityMode.UNSECURED;
|
||||
if(realm instanceof LegacySecurityRealm)
|
||||
return SecurityMode.LEGACY;
|
||||
return SecurityMode.SECURED;
|
||||
}
|
||||
|
||||
public void setSecurity(SecurityMode security) {
|
||||
this.security = security;
|
||||
/**
|
||||
* Returns the root {@link ACL}.
|
||||
*
|
||||
* @see AuthorizationStrategy#getRootACL()
|
||||
*/
|
||||
public ACL getACL() {
|
||||
return authorizationStrategy.getRootACL();
|
||||
}
|
||||
|
||||
/**
|
||||
* Short for {@code getACL().checkPermission(p)}
|
||||
*/
|
||||
public void checkPermission(Permission p) {
|
||||
getACL().checkPermission(p);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
* never null.
|
||||
*/
|
||||
public AuthorizationStrategy getAuthorizationStrategy() {
|
||||
return authorizationStrategy;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -1120,8 +1171,19 @@ public final class Hudson extends View implements ItemGroup<TopLevelItem>, Node
|
||||
}
|
||||
|
||||
// read in old data that doens't have the security field set
|
||||
if(useSecurity!=null)
|
||||
security = useSecurity ? SecurityMode.LEGACY : SecurityMode.UNSECURED;
|
||||
if(authorizationStrategy==null) {
|
||||
if(useSecurity==null || !useSecurity)
|
||||
authorizationStrategy = AuthorizationStrategy.UNSECURED;
|
||||
else
|
||||
authorizationStrategy = new LegacyAuthorizationStrategy();
|
||||
}
|
||||
if(securityRealm==null) {
|
||||
if(useSecurity==null || !useSecurity)
|
||||
securityRealm = SecurityRealm.NO_AUTHENTICATION;
|
||||
else
|
||||
securityRealm = new LegacySecurityRealm();
|
||||
}
|
||||
|
||||
|
||||
LOGGER.info(String.format("Took %s ms to load",System.currentTimeMillis()-startTime));
|
||||
}
|
||||
@@ -1180,16 +1242,18 @@ public final class Hudson extends View implements ItemGroup<TopLevelItem>, Node
|
||||
// until we get the new security implementation working
|
||||
// useSecurity = null;
|
||||
if (req.getParameter("use_security") != null) {
|
||||
security = SecurityMode.LEGACY;
|
||||
useSecurity = true;
|
||||
securityRealm = new LegacySecurityRealm();
|
||||
authorizationStrategy = new LegacyAuthorizationStrategy();
|
||||
} else {
|
||||
security = SecurityMode.UNSECURED;
|
||||
useSecurity = false;
|
||||
useSecurity = null;
|
||||
securityRealm = SecurityRealm.NO_AUTHENTICATION;
|
||||
authorizationStrategy = AuthorizationStrategy.UNSECURED;
|
||||
}
|
||||
|
||||
{
|
||||
String v = req.getParameter("slaveAgentPortType");
|
||||
if(security==SecurityMode.UNSECURED || v==null || v.equals("random"))
|
||||
if(!isUseSecurity() || v==null || v.equals("random"))
|
||||
slaveAgentPort = 0;
|
||||
else
|
||||
if(v.equals("disable"))
|
||||
@@ -1692,8 +1756,8 @@ public final class Hudson extends View implements ItemGroup<TopLevelItem>, Node
|
||||
* Run arbitrary Groovy script.
|
||||
*/
|
||||
public void doScript( StaplerRequest req, StaplerResponse rsp ) throws IOException, ServletException {
|
||||
if(!adminCheck(req,rsp))
|
||||
return; // ability to run arbitrary script is dangerous
|
||||
// ability to run arbitrary script is dangerous
|
||||
checkPermission(ADMINISTER);
|
||||
|
||||
String text = req.getParameter("script");
|
||||
if(text!=null) {
|
||||
@@ -1957,6 +2021,11 @@ public final class Hudson extends View implements ItemGroup<TopLevelItem>, Node
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Administrative access to Hudson.
|
||||
*/
|
||||
public static final Permission ADMINISTER = new Permission(Hudson.class,"Administer", Permission.WRITE);
|
||||
|
||||
/**
|
||||
* Live view of recent {@link LogRecord}s produced by Hudson.
|
||||
*/
|
||||
|
||||
@@ -2,6 +2,7 @@ package hudson.security;
|
||||
|
||||
import hudson.model.Hudson;
|
||||
import hudson.util.Scrambler;
|
||||
import org.acegisecurity.context.SecurityContextHolder;
|
||||
import org.kohsuke.stapler.StaplerRequest;
|
||||
import org.kohsuke.stapler.StaplerResponse;
|
||||
|
||||
@@ -73,9 +74,12 @@ public class BasicAuthenticationFilter implements Filter {
|
||||
String authorization = req.getHeader("Authorization");
|
||||
|
||||
String path = req.getServletPath();
|
||||
if(authorization==null || req.getUserPrincipal()!=null || path.startsWith("/secured/")
|
||||
if(authorization==null || req.getUserPrincipal() !=null || path.startsWith("/secured/")
|
||||
|| !Hudson.getInstance().isUseSecurity()) {
|
||||
// normal requests, or security not enabled
|
||||
// but before we route this request, integrate the container authentication
|
||||
// to Acegi
|
||||
SecurityContextHolder.getContext().setAuthentication(new ContainerAuthentication(req));
|
||||
chain.doFilter(request,response);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package hudson.security;
|
||||
|
||||
import org.acegisecurity.Authentication;
|
||||
import org.acegisecurity.GrantedAuthority;
|
||||
import org.acegisecurity.GrantedAuthorityImpl;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.security.Principal;
|
||||
|
||||
/**
|
||||
* {@link Authentication} implementation for {@link Principal}
|
||||
* given through {@link HttpServletRequest}.
|
||||
*
|
||||
* <p>
|
||||
* This is used to plug the container authentication to Acegi,
|
||||
* for backward compatibility with Hudson < 1.160.
|
||||
*
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public final class ContainerAuthentication implements Authentication {
|
||||
private final HttpServletRequest request;
|
||||
|
||||
public ContainerAuthentication(HttpServletRequest request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public GrantedAuthority[] getAuthorities() {
|
||||
// Servlet API doesn't provide a way to list up all roles the current user
|
||||
// has, so we are approximating the current user's capability by checking
|
||||
// the 'admin' role.
|
||||
if (request.isUserInRole("admin"))
|
||||
return ADMIN_AUTHORITY;
|
||||
else
|
||||
return NO_AUTHORITY;
|
||||
}
|
||||
|
||||
public Object getCredentials() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object getDetails() {
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object getPrincipal() {
|
||||
Principal principal = request.getUserPrincipal();
|
||||
if(principal!=null)
|
||||
return principal;
|
||||
else
|
||||
return "anonymous";
|
||||
}
|
||||
|
||||
public boolean isAuthenticated() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
|
||||
// noop
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
Principal principal = request.getUserPrincipal();
|
||||
if(principal!=null)
|
||||
return principal.getName();
|
||||
else
|
||||
return "anonymous";
|
||||
}
|
||||
|
||||
private static final GrantedAuthority[] ADMIN_AUTHORITY = {new GrantedAuthorityImpl("admin")};
|
||||
// Acegi doesn't like empty array, so we need to set something
|
||||
private static final GrantedAuthority[] NO_AUTHORITY = {new GrantedAuthorityImpl("authenticated")};
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package hudson.security;
|
||||
|
||||
import org.acegisecurity.AuthenticationManager;
|
||||
import org.acegisecurity.Authentication;
|
||||
import org.acegisecurity.AuthenticationException;
|
||||
import hudson.model.Descriptor;
|
||||
|
||||
/**
|
||||
* {@link SecurityRealm} that accepts {@link ContainerAuthentication} object
|
||||
* without any check (that is, by assuming that the such token is
|
||||
* already authenticated by the container.)
|
||||
* @author Kohsuke Kawaguchi
|
||||
*/
|
||||
public class LegacySecurityRealm extends SecurityRealm implements AuthenticationManager {
|
||||
public AuthenticationManager createAuthenticationManager() {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
|
||||
if(authentication instanceof ContainerAuthentication)
|
||||
return authentication;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
public Descriptor<SecurityRealm> getDescriptor() {
|
||||
return DESCRIPTOR;
|
||||
}
|
||||
|
||||
private static final Descriptor<SecurityRealm> DESCRIPTOR = new Descriptor<SecurityRealm>(LegacySecurityRealm.class) {
|
||||
public String getDisplayName() {
|
||||
return "Legacy mode"; // TODO
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -2,6 +2,8 @@ package hudson.security;
|
||||
|
||||
import hudson.ExtensionPoint;
|
||||
import hudson.model.Describable;
|
||||
import hudson.model.Descriptor;
|
||||
import org.acegisecurity.Authentication;
|
||||
import org.acegisecurity.AuthenticationManager;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
|
||||
@@ -46,4 +48,32 @@ public abstract class SecurityRealm implements Describable<SecurityRealm>, Exten
|
||||
throw new IllegalArgumentException("Multiple beans of "+type+" are defined: "+m);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Singleton constant that represents "no authentication."
|
||||
*/
|
||||
public static final SecurityRealm NO_AUTHENTICATION = new SecurityRealm() {
|
||||
public AuthenticationManager createAuthenticationManager() {
|
||||
return new AuthenticationManager() {
|
||||
public Authentication authenticate(Authentication authentication) {
|
||||
return authentication;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* This special instance is not configurable explicitly,
|
||||
* so it doesn't have a descriptor.
|
||||
*/
|
||||
public Descriptor<SecurityRealm> getDescriptor() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Maintain singleton semantics.
|
||||
*/
|
||||
private Object readResolve() {
|
||||
return NO_AUTHENTICATION;
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user