[HUDSON-6587] rolling back rev.31878. I think a better fix, given the current existing code and method signatures, is to expose the transport level authentication in a bit more implicit way.

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@31900 71c3de6d-444a-0410-be80-ed276b4c234a
This commit is contained in:
kohsuke committed 2010-06-10 02:21:28 +00:00
1 parent 3d44678e8c
commit 6698aca4e7
11 files changed
+131 -123

No files matched your search

+38 -9
View File
@@ -32,7 +32,9 @@ import hudson.ExtensionPoint.LegacyInstancesAreScopedToHudson;
import hudson.model.Hudson;
import hudson.remoting.Callable;
import hudson.remoting.Channel;
import hudson.remoting.ChannelProperty;
import hudson.security.CliAuthenticator;
import hudson.security.SecurityRealm;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
@@ -60,7 +62,7 @@ import java.util.logging.Logger;
*
* <p>
* The Hudson master then picks the right {@link CLICommand} to execute, clone it, and
* calls {@link #main(List, Locale, InputStream, PrintStream, PrintStream, Authentication)} method.
* calls {@link #main(List, Locale, InputStream, PrintStream, PrintStream)} method.
*
* <h2>Note for CLI command implementor</h2>
* Start with <a href="http://wiki.hudson-ci.org/display/HUDSON/Writing+CLI+commands">this document</a>
@@ -73,7 +75,7 @@ import java.util.logging.Logger;
* <li>
* Use <a href="http://args4j.dev.java.net/">args4j</a> annotation on your implementation to define
* options and arguments (however, if you don't like that, you could override
* the {@link #main(List, Locale, InputStream, PrintStream, PrintStream, Authentication)} method directly.
* the {@link #main(List, Locale, InputStream, PrintStream, PrintStream)} method directly.
*
* <li>
* stdin, stdout, stderr are remoted, so proper buffering is necessary for good user experience.
@@ -150,7 +152,7 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
*/
public abstract String getShortDescription();
public int main(List<String> args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr, Authentication auth) {
public int main(List<String> args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr) {
this.stdin = new BufferedInputStream(stdin);
this.stdout = stdout;
this.stderr = stderr;
@@ -162,15 +164,15 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
SecurityContext sc = SecurityContextHolder.getContext();
Authentication old = sc.getAuthentication();
CliAuthenticator authenticator = Hudson.getInstance().getSecurityRealm().createCliAuthenticator(this, auth);
CliAuthenticator authenticator = Hudson.getInstance().getSecurityRealm().createCliAuthenticator(this);
new ClassParser().parse(authenticator,p);
try {
p.parseArgument(args.toArray(new String[args.size()]));
Authentication a = authenticator.authenticate();
if (a == Hudson.ANONYMOUS)
a = loadStoredAuthentication();
sc.setAuthentication(a); // run the CLI with the right credential
Authentication auth = authenticator.authenticate();
if (auth==Hudson.ANONYMOUS)
auth = loadStoredAuthentication();
sc.setAuthentication(auth); // run the CLI with the right credential
return run();
} catch (CmdLineException e) {
stderr.println(e.getMessage());
@@ -221,6 +223,28 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
return auth==Hudson.ANONYMOUS;
}
/**
* Returns the identity of the client as determined at the CLI transport level.
*
* <p>
* When the CLI connection to the server is tunneled over HTTP, that HTTP connection
* can authenticate the client, just like any other HTTP connections to the server
* can authenticate the client. This method returns that information, if one is available.
* By generalizing it, this method returns the identity obtained at the transport-level authentication.
*
* <p>
* For example, imagine if the current {@link SecurityRealm} is doing Kerberos authentication,
* then this method can return a valid identity of the client.
*
* <p>
* If the transport doesn't do authentication, this method returns {@link Hudson#ANONYMOUS}.
*/
public Authentication getTransportAuthentication() {
Authentication a = channel.getProperty(TRANSPORT_AUTHENTICATION);
if (a==null) a = Hudson.ANONYMOUS;
return a;
}
/**
* Executes the command, and return the exit code.
*
@@ -304,5 +328,10 @@ public abstract class CLICommand implements ExtensionPoint, Cloneable {
}
private static final Logger LOGGER = Logger.getLogger(CLICommand.class.getName());
}
/**
* Key for {@link Channel#getProperty(Object)} that links to the {@link Authentication} object
* which captures the identity of the client given by the transport layer.
*/
public static final ChannelProperty<Authentication> TRANSPORT_AUTHENTICATION = new ChannelProperty<Authentication>(Authentication.class,"transportAuthentication");
}
@@ -23,25 +23,24 @@
*/
package hudson.cli;
import hudson.model.Hudson;
import hudson.remoting.Channel;
import org.acegisecurity.Authentication;
import org.apache.commons.discovery.ResourceClassIterator;
import org.apache.commons.discovery.ResourceNameIterator;
import hudson.model.Hudson;
import org.apache.commons.discovery.resource.ClassLoaders;
import org.apache.commons.discovery.resource.classes.DiscoverClasses;
import org.apache.commons.discovery.resource.names.DiscoverServiceNames;
import org.jvnet.tiger_types.Types;
import org.kohsuke.args4j.CmdLineParser;
import org.apache.commons.discovery.ResourceNameIterator;
import org.apache.commons.discovery.ResourceClassIterator;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.CmdLineParser;
import org.jvnet.tiger_types.Types;
import java.util.List;
import java.util.Locale;
import java.util.Collections;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Locale;
/**
* {@link CliEntryPoint} implementation exposed to the remote CLI.
@@ -49,15 +48,7 @@ import java.util.Locale;
* @author Kohsuke Kawaguchi
*/
public class CliManagerImpl implements CliEntryPoint, Serializable {
private final Authentication auth;
public CliManagerImpl(Authentication auth) {
this.auth = auth!=null ? auth : Hudson.ANONYMOUS;
}
public CliManagerImpl() {
this(null);
}
public int main(List<String> args, Locale locale, InputStream stdin, OutputStream stdout, OutputStream stderr) {
@@ -73,11 +64,11 @@ public class CliManagerImpl implements CliEntryPoint, Serializable {
CLICommand cmd = CLICommand.clone(subCmd);
if(cmd!=null) {
// execute the command, do so with the originator of the request as the principal
return cmd.main(args.subList(1,args.size()),locale, stdin, out, err, auth);
return cmd.main(args.subList(1,args.size()),locale, stdin, out, err);
}
err.println("No such command: "+subCmd);
new HelpCommand().main(Collections.<String>emptyList(), locale, stdin, out, err, auth);
new HelpCommand().main(Collections.<String>emptyList(), locale, stdin, out, err);
return -1;
}
@@ -110,5 +101,3 @@ public class CliManagerImpl implements CliEntryPoint, Serializable {
}
}
}
@@ -23,31 +23,26 @@
*/
package hudson.cli;
import groovy.lang.Binding;
import groovy.lang.Closure;
import hudson.AbortException;
import hudson.Extension;
import hudson.model.Hudson;
import hudson.remoting.ChannelClosedException;
import hudson.security.CliAuthenticator;
import jline.Terminal;
import jline.UnsupportedTerminal;
import groovy.lang.Binding;
import groovy.lang.Closure;
import org.acegisecurity.Authentication;
import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.codehaus.groovy.tools.shell.Groovysh;
import org.codehaus.groovy.tools.shell.IO;
import org.codehaus.groovy.tools.shell.Shell;
import org.codehaus.groovy.tools.shell.util.XmlCommandRegistrar;
import org.kohsuke.args4j.ClassParser;
import org.kohsuke.args4j.CmdLineParser;
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.List;
import java.util.Locale;
import java.io.PrintStream;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.PrintWriter;
import jline.UnsupportedTerminal;
import jline.Terminal;
/**
* Executes Groovy shell.
@@ -62,40 +57,16 @@ public class GroovyshCommand extends CLICommand {
}
@Override
public int main(List<String> args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr, Authentication auth) {
public int main(List<String> args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr) {
// this allows the caller to manipulate the JVM state, so require the admin privilege.
Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
CmdLineParser p = new CmdLineParser(this);
// add options from the authenticator
SecurityContext sc = SecurityContextHolder.getContext();
Authentication old = sc.getAuthentication();
CliAuthenticator authenticator = Hudson.getInstance().getSecurityRealm().createCliAuthenticator(this, auth);
new ClassParser().parse(authenticator, p);
try {
sc.setAuthentication(authenticator.authenticate());
// this allows the caller to manipulate the JVM state, so require the admin privilege.
Hudson.getInstance().checkPermission(Hudson.ADMINISTER);
// this being remote means no jline capability is available
System.setProperty("jline.terminal", UnsupportedTerminal.class.getName());
Terminal.resetTerminal();
Groovysh shell = createShell(stdin, stdout, stderr);
return shell.run(args.toArray(new String[args.size()]));
} catch (AbortException e) {
// signals an error without stack trace
stderr.println(e.getMessage());
return -1;
} catch (Exception e) {
e.printStackTrace(stderr);
return -1;
} finally {
sc.setAuthentication(old); // restore
}
// this being remote means no jline capability is available
System.setProperty("jline.terminal", UnsupportedTerminal.class.getName());
Terminal.resetTerminal();
Groovysh shell = createShell(stdin, stdout, stderr);
return shell.run(args.toArray(new String[args.size()]));
}
protected Groovysh createShell(InputStream stdin, PrintStream stdout,
@@ -106,7 +106,7 @@ public class CLIRegisterer extends ExtensionFinder {
}
@Override
public int main(List<String> args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr, Authentication auth) {
public int main(List<String> args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr) {
CmdLineParser parser = new CmdLineParser(null);
try {
SecurityContext sc = SecurityContextHolder.getContext();
@@ -135,7 +135,7 @@ public class CLIRegisterer extends ExtensionFinder {
binders.add(new MethodBinder(chains.pop(),parser));
// authentication
CliAuthenticator authenticator = Hudson.getInstance().getSecurityRealm().createCliAuthenticator(this, auth);
CliAuthenticator authenticator = Hudson.getInstance().getSecurityRealm().createCliAuthenticator(this);
new ClassParser().parse(authenticator,parser);
// fill up all the binders
@@ -196,4 +196,3 @@ public class CLIRegisterer extends ExtensionFinder {
private static final Logger LOGGER = Logger.getLogger(CLIRegisterer.class.getName());
}
+4 -2
View File
@@ -50,6 +50,7 @@ import static hudson.Util.fixEmpty;
import static hudson.Util.fixNull;
import hudson.WebAppMain;
import hudson.XmlFile;
import hudson.cli.CLICommand;
import hudson.cli.CliEntryPoint;
import hudson.cli.CliManagerImpl;
import hudson.cli.declarative.CLIMethod;
@@ -2864,7 +2865,9 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
if(req.getHeader("Side").equals("download")) {
duplexChannels.put(uuid,server=new FullDuplexHttpChannel(uuid, !hasPermission(ADMINISTER)) {
protected void main(Channel channel) throws IOException, InterruptedException {
channel.setProperty(CliEntryPoint.class.getName(),new CliManagerImpl(getAuthentication()));
// capture the identity given by the transport, since this can be useful for SecurityRealm.createCliAuthenticator()
channel.setProperty(CLICommand.TRANSPORT_AUTHENTICATION,getAuthentication());
channel.setProperty(CliEntryPoint.class.getName(),new CliManagerImpl());
}
});
try {
@@ -3692,4 +3695,3 @@ public final class Hudson extends Node implements ItemGroup<TopLevelItem>, Stapl
assert ADMINISTER!=null;
}
}
@@ -60,6 +60,11 @@ import java.io.IOException;
* a password, you can do so by using {@link CLICommand#channel}.)
*
* <p>
* If no explicit credential is provided, or if the {@link SecurityRealm} depends on a mode of authentication
* that doesn't involve in explicit password (such as Kerberos), it's also often useful to fall back to
* {@link CLICommand#getTransportAuthentication()}, in case the user is authenticated at the transport level.
*
* <p>
* Many commands do not require any authentication (for example, the "help" command), and still more commands
* can be run successfully with the anonymous permission. So the authenticator should normally allow unauthenticated
* CLI command invocations. For those, return {@link Hudson#ANONYMOUS} from the {@link #authenticate()} method.
@@ -23,17 +23,16 @@
*/
package hudson.security;
import groovy.lang.Binding;
import hudson.Extension;
import hudson.cli.CLICommand;
import hudson.model.Descriptor;
import hudson.util.spring.BeanBuilder;
import net.sf.json.JSONObject;
import org.acegisecurity.AuthenticationManager;
import org.acegisecurity.Authentication;
import org.acegisecurity.AuthenticationException;
import org.acegisecurity.AuthenticationManager;
import org.kohsuke.stapler.StaplerRequest;
import org.springframework.web.context.WebApplicationContext;
import org.kohsuke.stapler.StaplerRequest;
import groovy.lang.Binding;
import hudson.model.Descriptor;
import hudson.util.spring.BeanBuilder;
import hudson.Extension;
import net.sf.json.JSONObject;
import javax.servlet.Filter;
import javax.servlet.FilterConfig;
@@ -71,19 +70,6 @@ public final class LegacySecurityRealm extends SecurityRealm implements Authenti
return "loginEntry";
}
@Override
public CliAuthenticator createCliAuthenticator(CLICommand command, final Authentication auth) {
if (auth == null) {
return createCliAuthenticator(command);
} else {
return new CliAuthenticator() {
public Authentication authenticate() {
return auth;
}
};
}
}
/**
* Filter to run for the LegacySecurityRealm is the
* ChainServletFilter legacy from /WEB-INF/security/SecurityFilters.groovy.
@@ -116,4 +102,3 @@ public final class LegacySecurityRealm extends SecurityRealm implements Authenti
}
};
}
@@ -150,32 +150,16 @@ public abstract class SecurityRealm extends AbstractDescribableImpl<SecurityReal
* The command about to be executed.
* @return
* never null. By default, this method returns a no-op authenticator that always authenticates
* the session as an anonymous user.
* the session as authenticated by the transport (which is often just {@link Hudson#ANONYMOUS}.)
*/
public CliAuthenticator createCliAuthenticator(CLICommand command) {
public CliAuthenticator createCliAuthenticator(final CLICommand command) {
return new CliAuthenticator() {
public Authentication authenticate() {
return Hudson.ANONYMOUS;
return command.getTransportAuthentication();
}
};
}
/**
* Creates a {@link CliAuthenticator} object that authenticates an invocation of a CLI command.
* See {@link CliAuthenticator} for more details.
*
* @param command
* The command about to be executed.
*
* @param auth Authentication to use; not used in default implementation of this method
* @return
* never null. By default, this method returns a no-op authenticator that always authenticates
* the session as an anonymous user.
*/
public CliAuthenticator createCliAuthenticator(CLICommand command, Authentication auth) {
return createCliAuthenticator(command);
}
/**
* {@inheritDoc}
*