From f9486afebc3ea28dc91f24e361d03a415ab6fa66 Mon Sep 17 00:00:00 2001 From: kohsuke Date: Mon, 18 Aug 2008 21:02:06 +0000 Subject: [PATCH] ContainerAuthentication needs "authenticated" built-in role. git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@11597 71c3de6d-444a-0410-be80-ed276b4c234a --- .../security/ContainerAuthentication.java | 5 +- .../org/jvnet/hudson/test/HudsonTestCase.java | 60 ++++++++++++++++++- .../bugs/JnlpAccessWithSecuredHudsonTest.java | 50 ++++++++++++++++ 3 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 test/src/test/java/hudson/bugs/JnlpAccessWithSecuredHudsonTest.java diff --git a/core/src/main/java/hudson/security/ContainerAuthentication.java b/core/src/main/java/hudson/security/ContainerAuthentication.java index 18433b7a29..8ebb9ae8a2 100644 --- a/core/src/main/java/hudson/security/ContainerAuthentication.java +++ b/core/src/main/java/hudson/security/ContainerAuthentication.java @@ -38,6 +38,7 @@ public final class ContainerAuthentication implements Authentication { if(request.isUserInRole(g)) l.add(new GrantedAuthorityImpl(g)); } + l.add(AUTHENTICATED); authorities = l.toArray(new GrantedAuthority[l.size()]); } return authorities; @@ -67,7 +68,5 @@ public final class ContainerAuthentication implements Authentication { return getPrincipal(); } - 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")}; + private static final GrantedAuthorityImpl AUTHENTICATED = new GrantedAuthorityImpl("authenticated"); } diff --git a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java index 8387099486..ed3fa51329 100644 --- a/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java +++ b/test/src/main/java/org/jvnet/hudson/test/HudsonTestCase.java @@ -1,6 +1,11 @@ package org.jvnet.hudson.test; import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.gargoylesoftware.htmlunit.html.HtmlAnchor; +import com.gargoylesoftware.htmlunit.html.HtmlForm; +import com.gargoylesoftware.htmlunit.Page; +import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException; +import com.gargoylesoftware.htmlunit.ElementNotFoundException; import hudson.model.FreeStyleProject; import hudson.model.Hudson; import hudson.model.Item; @@ -23,6 +28,7 @@ import java.io.IOException; import java.lang.annotation.Annotation; import java.lang.reflect.Method; import java.net.URL; +import java.net.MalformedURLException; import java.util.ArrayList; import java.util.List; @@ -189,12 +195,64 @@ public abstract class HudsonTestCase extends TestCase { setJavaScriptEnabled(false); } + /** + * Logs in to Hudson. + */ + public WebClient login(String username, String password) throws Exception { + HtmlPage page = goTo("login"); +// page = (HtmlPage) page.getFirstAnchorByText("Login").click(); + + HtmlForm form = page.getFormByName("login"); + form.getInputByName("j_username").setValueAttribute(username); + form.getInputByName("j_password").setValueAttribute(password); + form.submit(null); + return this; + } + + /** + * Logs in to Hudson, by using the user name as the password. + * + *

+ * See {@link HudsonTestCase#configureUserRealm()} for how the container is set up with the user names + * and passwords. All the test accounts have the same user name and password. + */ + public WebClient login(String username) throws Exception { + login(username,username); + return this; + } + public HtmlPage getPage(Item item) throws IOException, SAXException { return getPage(item,""); } public HtmlPage getPage(Item item, String relative) throws IOException, SAXException { - return (HtmlPage)getPage("http://localhost:"+localPort+contextPath+item.getUrl()+relative); + return goTo(item.getUrl()+relative); + } + + /** + * @deprecated + * This method expects a full URL. This method is marked as deprecated to warn you + * that you probably should be using {@link #goTo(String)} method, which accepts + * a relative path within the Hudson being tested. (IOW, if you really need to hit + * a website on the internet, there's nothing wrong with using this method.) + */ + public Page getPage(String url) throws IOException, FailingHttpStatusCodeException { + return super.getPage(url); + } + + /** + * Requests a page within Hudson. + * + * @param relative + * Relative path within Hudson. Starts without '/'. + * For example, "job/test/" to go to a job top page. + */ + public HtmlPage goTo(String relative) throws IOException, SAXException { + return (HtmlPage)goTo(relative, "text/html"); + } + + public Page goTo(String relative, String expectedContentType) throws IOException, SAXException { + return super.getPage("http://localhost:"+localPort+contextPath+relative); } } } diff --git a/test/src/test/java/hudson/bugs/JnlpAccessWithSecuredHudsonTest.java b/test/src/test/java/hudson/bugs/JnlpAccessWithSecuredHudsonTest.java new file mode 100644 index 0000000000..9c6cccbd02 --- /dev/null +++ b/test/src/test/java/hudson/bugs/JnlpAccessWithSecuredHudsonTest.java @@ -0,0 +1,50 @@ +package hudson.bugs; + +import org.jvnet.hudson.test.HudsonTestCase; +import org.jvnet.hudson.test.recipes.PresetData; +import org.jvnet.hudson.test.recipes.PresetData.DataSet; +import org.dom4j.io.DOMReader; +import org.dom4j.Document; +import org.dom4j.Element; +import hudson.model.Slave; +import hudson.model.Node.Mode; +import hudson.slaves.JNLPLauncher; +import hudson.slaves.RetentionStrategy; +import hudson.slaves.RetentionStrategy.Always; + +import java.util.Collections; +import java.util.List; +import java.net.URL; + +import com.gargoylesoftware.htmlunit.html.HtmlPage; +import com.gargoylesoftware.htmlunit.xml.XmlPage; +import sun.management.resources.agent; + +/** + * @author Kohsuke Kawaguchi + */ +public class JnlpAccessWithSecuredHudsonTest extends HudsonTestCase { + + /** + * Creates a new slave that needs to be launched via JNLP. + */ + protected Slave createNewJnlpSlave(String name) throws Exception { + return new Slave(name,"",System.getProperty("java.io.tmpdir")+'/'+name,"2", Mode.NORMAL, "", new JNLPLauncher(), RetentionStrategy.INSTANCE); + } + + @PresetData(DataSet.NO_ANONYMOUS_READACCESS) + public void test() throws Exception { + hudson.setSlaves(Collections.singletonList(createNewJnlpSlave("test"))); + HudsonTestCase.WebClient wc = new WebClient(); + HtmlPage p = wc.login("alice").goTo("computer/test/"); + + XmlPage jnlp = (XmlPage) wc.goTo("computer/test/slave-agent.jnlp","application/x-java-jnlp-file"); + URL baseUrl = jnlp.getWebResponse().getUrl(); + + Document dom = new DOMReader().read(jnlp.getXmlDocument()); + for( Element jar : (List)dom.selectNodes("//jar") ) { + URL url = new URL(baseUrl,jar.attributeValue("href")); + System.out.println(url); + } + } +}