From 6698aca4e7d2f240259d4b112f95b76806e26fad Mon Sep 17 00:00:00 2001 From: kohsuke Date: Thu, 10 Jun 2010 02:21:28 +0000 Subject: [PATCH] [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 --- core/src/main/java/hudson/cli/CLICommand.java | 47 +++++++++++--- .../main/java/hudson/cli/CliManagerImpl.java | 31 +++------ .../main/java/hudson/cli/GroovyshCommand.java | 63 +++++-------------- .../hudson/cli/declarative/CLIRegisterer.java | 5 +- core/src/main/java/hudson/model/Hudson.java | 6 +- .../hudson/security/CliAuthenticator.java | 5 ++ .../hudson/security/LegacySecurityRealm.java | 29 +++------ .../java/hudson/security/SecurityRealm.java | 22 +------ .../main/java/hudson/remoting/Channel.java | 4 ++ .../java/hudson/remoting/ChannelProperty.java | 40 ++++++++++++ .../model/listeners/ItemListenerTest.java | 2 +- 11 files changed, 131 insertions(+), 123 deletions(-) create mode 100644 remoting/src/main/java/hudson/remoting/ChannelProperty.java diff --git a/core/src/main/java/hudson/cli/CLICommand.java b/core/src/main/java/hudson/cli/CLICommand.java index c02fae0b8e..b24d783b7a 100644 --- a/core/src/main/java/hudson/cli/CLICommand.java +++ b/core/src/main/java/hudson/cli/CLICommand.java @@ -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; * *

* 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. * *

Note for CLI command implementor

* Start with this document @@ -73,7 +75,7 @@ import java.util.logging.Logger; *
  • * Use args4j 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. * *
  • * 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 args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr, Authentication auth) { + public int main(List 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. + * + *

    + * 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. + * + *

    + * For example, imagine if the current {@link SecurityRealm} is doing Kerberos authentication, + * then this method can return a valid identity of the client. + * + *

    + * 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 TRANSPORT_AUTHENTICATION = new ChannelProperty(Authentication.class,"transportAuthentication"); +} diff --git a/core/src/main/java/hudson/cli/CliManagerImpl.java b/core/src/main/java/hudson/cli/CliManagerImpl.java index f2fd739665..17f2d73fd4 100644 --- a/core/src/main/java/hudson/cli/CliManagerImpl.java +++ b/core/src/main/java/hudson/cli/CliManagerImpl.java @@ -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 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.emptyList(), locale, stdin, out, err, auth); + new HelpCommand().main(Collections.emptyList(), locale, stdin, out, err); return -1; } @@ -110,5 +101,3 @@ public class CliManagerImpl implements CliEntryPoint, Serializable { } } } - - diff --git a/core/src/main/java/hudson/cli/GroovyshCommand.java b/core/src/main/java/hudson/cli/GroovyshCommand.java index 886f9a17bb..fab488013b 100644 --- a/core/src/main/java/hudson/cli/GroovyshCommand.java +++ b/core/src/main/java/hudson/cli/GroovyshCommand.java @@ -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 args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr, Authentication auth) { + public int main(List 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, diff --git a/core/src/main/java/hudson/cli/declarative/CLIRegisterer.java b/core/src/main/java/hudson/cli/declarative/CLIRegisterer.java index 5af1e3341b..db6f32ba99 100644 --- a/core/src/main/java/hudson/cli/declarative/CLIRegisterer.java +++ b/core/src/main/java/hudson/cli/declarative/CLIRegisterer.java @@ -106,7 +106,7 @@ public class CLIRegisterer extends ExtensionFinder { } @Override - public int main(List args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr, Authentication auth) { + public int main(List 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()); } - diff --git a/core/src/main/java/hudson/model/Hudson.java b/core/src/main/java/hudson/model/Hudson.java index 2dd8890b6d..81b90b97dd 100644 --- a/core/src/main/java/hudson/model/Hudson.java +++ b/core/src/main/java/hudson/model/Hudson.java @@ -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, 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, Stapl assert ADMINISTER!=null; } } - diff --git a/core/src/main/java/hudson/security/CliAuthenticator.java b/core/src/main/java/hudson/security/CliAuthenticator.java index 48389b4a1f..e018936b40 100644 --- a/core/src/main/java/hudson/security/CliAuthenticator.java +++ b/core/src/main/java/hudson/security/CliAuthenticator.java @@ -60,6 +60,11 @@ import java.io.IOException; * a password, you can do so by using {@link CLICommand#channel}.) * *

    + * 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. + * + *

    * 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. diff --git a/core/src/main/java/hudson/security/LegacySecurityRealm.java b/core/src/main/java/hudson/security/LegacySecurityRealm.java index 2356948919..ce4f8e75e2 100644 --- a/core/src/main/java/hudson/security/LegacySecurityRealm.java +++ b/core/src/main/java/hudson/security/LegacySecurityRealm.java @@ -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 } }; } - diff --git a/core/src/main/java/hudson/security/SecurityRealm.java b/core/src/main/java/hudson/security/SecurityRealm.java index c4c9ca36f0..e2f9ff631c 100644 --- a/core/src/main/java/hudson/security/SecurityRealm.java +++ b/core/src/main/java/hudson/security/SecurityRealm.java @@ -150,32 +150,16 @@ public abstract class SecurityRealm extends AbstractDescribableImpl T getProperty(ChannelProperty key) { + return key.type.cast(properties.get(key)); + } + /** * Works like {@link #getProperty(Object)} but wait until some value is set by someone. */ diff --git a/remoting/src/main/java/hudson/remoting/ChannelProperty.java b/remoting/src/main/java/hudson/remoting/ChannelProperty.java new file mode 100644 index 0000000000..e596ba0571 --- /dev/null +++ b/remoting/src/main/java/hudson/remoting/ChannelProperty.java @@ -0,0 +1,40 @@ +/* + * The MIT License + * + * Copyright (c) 2010, InfraDNA, Inc. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +package hudson.remoting; + +/** + * A convenient key type for {@link Channel#getProperty(Object)} and {@link Channel#setProperty(Object, Object)} + * + * @author Kohsuke Kawaguchi + */ +public class ChannelProperty { + public final Class type; + public final String displayName; + + public ChannelProperty(Class type, String displayName) { + this.type = type; + this.displayName = displayName; + } +} diff --git a/test/src/test/java/hudson/model/listeners/ItemListenerTest.java b/test/src/test/java/hudson/model/listeners/ItemListenerTest.java index 37c899bd80..768f148e9e 100644 --- a/test/src/test/java/hudson/model/listeners/ItemListenerTest.java +++ b/test/src/test/java/hudson/model/listeners/ItemListenerTest.java @@ -60,7 +60,7 @@ public class ItemListenerTest extends HudsonTestCase { cmd.main(Collections.singletonList("testJob"), Locale.ENGLISH, new ByteArrayInputStream(("" + "").getBytes()), - out, out, null); + out, out); out.flush(); assertNotNull("job should be created: " + buf, hudson.getItem("testJob")); assertEquals("onCreated event should be triggered: " + buf, "C", events.toString());