Skip to content

Commit

Permalink
[FIXED JENKINS-17929] Pick up transport authentication from groovysh …
Browse files Browse the repository at this point in the history
…command, by overriding run rather than main.

(cherry picked from commit 3896aab)

Conflicts:
	changelog.html
	core/src/main/java/hudson/cli/GroovyshCommand.java
  • Loading branch information
jglick authored and olivergondza committed Jan 5, 2014
1 parent 7af2a75 commit 6d88c01
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 7 deletions.
2 changes: 2 additions & 0 deletions core/src/main/java/hudson/cli/CLICommand.java
Expand Up @@ -189,6 +189,8 @@ public String getName() {
* The default implementation uses args4j to parse command line arguments and call {@link #run()},
* but if that processing is undesirable, subtypes can directly override this method and leave {@link #run()}
* to an empty method.
* You would however then have to consider {@link CliAuthenticator} and {@link #getTransportAuthentication},
* so this is not really recommended.
*
* @param args
* Arguments to the sub command. For example, if the CLI is invoked like "java -jar cli.jar foo bar zot",
Expand Down
12 changes: 5 additions & 7 deletions core/src/main/java/hudson/cli/GroovyshCommand.java
Expand Up @@ -34,14 +34,15 @@
import org.codehaus.groovy.tools.shell.util.XmlCommandRegistrar;

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 java.util.ArrayList;

import jline.UnsupportedTerminal;
import jline.Terminal;
import org.kohsuke.args4j.Argument;

/**
* Executes Groovy shell.
Expand All @@ -55,12 +56,12 @@ public String getShortDescription() {
return Messages.GroovyshCommand_ShortDescription();
}

@Argument(metaVar="ARGS") public List<String> args = new ArrayList<String>();

@Override
public int main(List<String> args, Locale locale, InputStream stdin, PrintStream stdout, PrintStream stderr) {
protected int run() {
// this allows the caller to manipulate the JVM state, so require the admin privilege.
Jenkins.getInstance().checkPermission(Jenkins.RUN_SCRIPTS);
// TODO: ^as this class overrides main() (which has authentication stuff),
// how to get ADMIN permission for this command?

// this being remote means no jline capability is available
System.setProperty("jline.terminal", UnsupportedTerminal.class.getName());
Expand Down Expand Up @@ -123,7 +124,4 @@ public Object doCall(Object[] args) throws ChannelClosedException {
return shell;
}

protected int run() {
throw new UnsupportedOperationException();
}
}
3 changes: 3 additions & 0 deletions test/src/main/java/hudson/cli/CLICommandInvoker.java
Expand Up @@ -25,6 +25,7 @@
package hudson.cli;

import hudson.model.User;
import hudson.security.ACL;
import hudson.security.Permission;
import hudson.security.GlobalMatrixAuthorizationStrategy;

Expand Down Expand Up @@ -113,6 +114,8 @@ private void setAuth() {
rule.jenkins.setAuthorizationStrategy(auth);

command.setTransportAuth(user().impersonate());
// Otherwise it is SYSTEM, which would be relevant for a command overriding main:
ACL.impersonate(Jenkins.ANONYMOUS);
}

public User user() {
Expand Down
52 changes: 52 additions & 0 deletions test/src/test/java/hudson/cli/GroovyshCommandTest.java
@@ -0,0 +1,52 @@
/*
* The MIT License
*
* Copyright 2013 Jesse Glick.
*
* 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.cli;

import static hudson.cli.CLICommandInvoker.Matcher.*;
import jenkins.model.Jenkins;
import org.apache.tools.ant.filters.StringInputStream;
import static org.hamcrest.CoreMatchers.containsString;
import static org.junit.Assert.*;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.Bug;
import org.jvnet.hudson.test.JenkinsRule;

public class GroovyshCommandTest {

@Rule public JenkinsRule r = new JenkinsRule();

@Bug(17929)
@Test public void authentication() throws Exception {
CLICommandInvoker.Result result = new CLICommandInvoker(r, new GroovyshCommand())
.authorizedTo(Jenkins.READ, Jenkins.RUN_SCRIPTS)
.withStdin(new StringInputStream("println(jenkins.model.Jenkins.instance.getClass().name)\nquit\n"))
.invoke();
assertThat(result, succeeded());
assertThat(result, hasNoErrorOutput());
assertThat(result.stdout(), containsString("hudson.model.Hudson"));
}

}

0 comments on commit 6d88c01

Please sign in to comment.