Skip to content

Commit

Permalink
Merge pull request #1340 from daniel-beck/JENKINS-23988
Browse files Browse the repository at this point in the history
[FIXED JENKINS-23988] Consider CLIAuthenticator for CLIMethods
  • Loading branch information
daniel-beck committed Aug 14, 2014
2 parents 1cf7354 + d8489fc commit c869cfd
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
3 changes: 1 addition & 2 deletions core/src/main/java/hudson/cli/declarative/CLIRegisterer.java
Expand Up @@ -149,8 +149,6 @@ private CmdLineParser bindMethod(List<MethodBinder> binders) {
while (!chains.isEmpty())
binders.add(new MethodBinder(chains.pop(),this,parser));

new ClassParser().parse(Jenkins.getInstance().getSecurityRealm().createCliAuthenticator(this), parser);

return parser;
}

Expand All @@ -169,6 +167,7 @@ public int main(List<String> args, Locale locale, InputStream stdin, PrintStream
try {
// authentication
CliAuthenticator authenticator = Jenkins.getInstance().getSecurityRealm().createCliAuthenticator(this);
new ClassParser().parse(authenticator,parser);

// fill up all the binders
parser.parseArgument(args);
Expand Down
36 changes: 36 additions & 0 deletions test/src/test/java/hudson/cli/CLIRegistererTest.java
@@ -0,0 +1,36 @@
package hudson.cli;

import static hudson.cli.CLICommandInvoker.Matcher.failedWith;
import static hudson.cli.CLICommandInvoker.Matcher.succeededSilently;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.CoreMatchers.containsString;
import jenkins.model.Jenkins;
import hudson.cli.CLICommandInvoker;
import hudson.cli.CLICommandInvoker.Result;

import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class CLIRegistererTest {

@Rule
public JenkinsRule j = new JenkinsRule();

@Test
public void testAuthWithSecurityRealmCLIAuthenticator() {
j.getInstance().setSecurityRealm(j.createDummySecurityRealm());

CLICommandInvoker command = new CLICommandInvoker(j, "quiet-down");

Result invocation = command.invokeWithArgs("--username", "foo", "--password", "invalid");
assertThat(invocation, failedWith(1));
assertThat(invocation.stderr(), containsString("BadCredentialsException: foo"));
Assert.assertFalse("Unauthorized command was executed", Jenkins.getInstance().isQuietingDown());

invocation = command.invokeWithArgs("--username", "foo", "--password", "foo");
assertThat(invocation, succeededSilently());
Assert.assertTrue("Authorized command was not executed", Jenkins.getInstance().isQuietingDown());
}
}

0 comments on commit c869cfd

Please sign in to comment.